Greg Clayton | 86188d8 | 2018-05-21 14:14:36 +0000 | [diff] [blame] | 1 | //===-- PathMappingListTest.cpp ---------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Greg Clayton | 86188d8 | 2018-05-21 14:14:36 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/ADT/ArrayRef.h" |
| 10 | #include "lldb/Target/PathMappingList.h" |
| 11 | #include "lldb/Utility/FileSpec.h" |
| 12 | #include "gtest/gtest.h" |
| 13 | #include <utility> |
| 14 | |
| 15 | using namespace lldb_private; |
| 16 | |
| 17 | namespace { |
Pavel Labath | fe547e0 | 2018-05-23 10:32:05 +0000 | [diff] [blame] | 18 | struct Matches { |
| 19 | FileSpec original; |
| 20 | FileSpec remapped; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 21 | Matches(const char *o, const char *r) : original(o), remapped(r) {} |
Pavel Labath | fe547e0 | 2018-05-23 10:32:05 +0000 | [diff] [blame] | 22 | }; |
| 23 | } // namespace |
| 24 | |
| 25 | static void TestPathMappings(const PathMappingList &map, |
| 26 | llvm::ArrayRef<Matches> matches, |
| 27 | llvm::ArrayRef<ConstString> fails) { |
| 28 | ConstString actual_remapped; |
| 29 | for (const auto &fail : fails) { |
Pavel Labath | 2e63840 | 2018-06-14 10:31:06 +0000 | [diff] [blame] | 30 | SCOPED_TRACE(fail.GetCString()); |
| 31 | EXPECT_FALSE(map.RemapPath(fail, actual_remapped)) |
| 32 | << "actual_remapped: " << actual_remapped.GetCString(); |
Pavel Labath | fe547e0 | 2018-05-23 10:32:05 +0000 | [diff] [blame] | 33 | } |
| 34 | for (const auto &match : matches) { |
Pavel Labath | 2e63840 | 2018-06-14 10:31:06 +0000 | [diff] [blame] | 35 | SCOPED_TRACE(match.original.GetPath() + " -> " + match.remapped.GetPath()); |
Pavel Labath | fe547e0 | 2018-05-23 10:32:05 +0000 | [diff] [blame] | 36 | std::string orig_normalized = match.original.GetPath(); |
| 37 | EXPECT_TRUE( |
| 38 | map.RemapPath(ConstString(match.original.GetPath()), actual_remapped)); |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 39 | EXPECT_EQ(FileSpec(actual_remapped.GetStringRef()), match.remapped); |
Pavel Labath | fe547e0 | 2018-05-23 10:32:05 +0000 | [diff] [blame] | 40 | FileSpec unmapped_spec; |
| 41 | EXPECT_TRUE(map.ReverseRemapPath(match.remapped, unmapped_spec)); |
| 42 | std::string unmapped_path = unmapped_spec.GetPath(); |
| 43 | EXPECT_EQ(unmapped_path, orig_normalized); |
Greg Clayton | 86188d8 | 2018-05-21 14:14:36 +0000 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
| 47 | TEST(PathMappingListTest, RelativeTests) { |
| 48 | Matches matches[] = { |
| 49 | {".", "/tmp"}, |
| 50 | {"./", "/tmp"}, |
| 51 | {"./////", "/tmp"}, |
| 52 | {"./foo.c", "/tmp/foo.c"}, |
| 53 | {"foo.c", "/tmp/foo.c"}, |
| 54 | {"./bar/foo.c", "/tmp/bar/foo.c"}, |
| 55 | {"bar/foo.c", "/tmp/bar/foo.c"}, |
| 56 | }; |
| 57 | ConstString fails[] = { |
Pavel Labath | 2e63840 | 2018-06-14 10:31:06 +0000 | [diff] [blame] | 58 | #ifdef _WIN32 |
| 59 | ConstString("C:\\"), |
| 60 | ConstString("C:\\a"), |
| 61 | #else |
| 62 | ConstString("/a"), |
| 63 | ConstString("/"), |
| 64 | #endif |
Greg Clayton | 86188d8 | 2018-05-21 14:14:36 +0000 | [diff] [blame] | 65 | }; |
| 66 | PathMappingList map; |
| 67 | map.Append(ConstString("."), ConstString("/tmp"), false); |
| 68 | TestPathMappings(map, matches, fails); |
| 69 | PathMappingList map2; |
| 70 | map2.Append(ConstString(""), ConstString("/tmp"), false); |
| 71 | TestPathMappings(map, matches, fails); |
| 72 | } |
| 73 | |
| 74 | TEST(PathMappingListTest, AbsoluteTests) { |
| 75 | PathMappingList map; |
| 76 | map.Append(ConstString("/old"), ConstString("/new"), false); |
| 77 | Matches matches[] = { |
| 78 | {"/old", "/new"}, |
| 79 | {"/old/", "/new"}, |
| 80 | {"/old/foo/.", "/new/foo"}, |
| 81 | {"/old/foo.c", "/new/foo.c"}, |
| 82 | {"/old/foo.c/.", "/new/foo.c"}, |
| 83 | {"/old/./foo.c", "/new/foo.c"}, |
| 84 | }; |
| 85 | ConstString fails[] = { |
| 86 | ConstString("/foo"), |
| 87 | ConstString("/"), |
| 88 | ConstString("foo.c"), |
| 89 | ConstString("./foo.c"), |
| 90 | ConstString("../foo.c"), |
| 91 | ConstString("../bar/foo.c"), |
| 92 | }; |
| 93 | TestPathMappings(map, matches, fails); |
| 94 | } |
| 95 | |
| 96 | TEST(PathMappingListTest, RemapRoot) { |
| 97 | PathMappingList map; |
| 98 | map.Append(ConstString("/"), ConstString("/new"), false); |
| 99 | Matches matches[] = { |
| 100 | {"/old", "/new/old"}, |
| 101 | {"/old/", "/new/old"}, |
| 102 | {"/old/foo/.", "/new/old/foo"}, |
| 103 | {"/old/foo.c", "/new/old/foo.c"}, |
| 104 | {"/old/foo.c/.", "/new/old/foo.c"}, |
| 105 | {"/old/./foo.c", "/new/old/foo.c"}, |
| 106 | }; |
| 107 | ConstString fails[] = { |
| 108 | ConstString("foo.c"), |
| 109 | ConstString("./foo.c"), |
| 110 | ConstString("../foo.c"), |
| 111 | ConstString("../bar/foo.c"), |
| 112 | }; |
| 113 | TestPathMappings(map, matches, fails); |
| 114 | } |