1 //===-- PathMappingListTest.cpp -------------------------------------------===// 2 // 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 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 { 18 struct Matches { 19 FileSpec original; 20 FileSpec remapped; 21 Matches(const char *o, const char *r) : original(o), remapped(r) {} 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) { 30 SCOPED_TRACE(fail.GetCString()); 31 EXPECT_FALSE(map.RemapPath(fail, actual_remapped)) 32 << "actual_remapped: " << actual_remapped.GetCString(); 33 } 34 for (const auto &match : matches) { 35 SCOPED_TRACE(match.original.GetPath() + " -> " + match.remapped.GetPath()); 36 std::string orig_normalized = match.original.GetPath(); 37 EXPECT_TRUE( 38 map.RemapPath(ConstString(match.original.GetPath()), actual_remapped)); 39 EXPECT_EQ(FileSpec(actual_remapped.GetStringRef()), match.remapped); 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); 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[] = { 58 #ifdef _WIN32 59 ConstString("C:\\"), 60 ConstString("C:\\a"), 61 #else 62 ConstString("/a"), 63 ConstString("/"), 64 #endif 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 } 115