xref: /llvm-project/lldb/unittests/Target/PathMappingListTest.cpp (revision 42237d51abf00417f5a3164d843e245ad584f7c4)
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 "lldb/Target/PathMappingList.h"
10 #include "lldb/Utility/FileSpec.h"
11 #include "llvm/ADT/ArrayRef.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;
Matches__anonaf573a0d0111::Matches21   Matches(const char *o, const char *r) : original(o), remapped(r) {}
Matches__anonaf573a0d0111::Matches22   Matches(const char *o, llvm::sys::path::Style style, const char *r)
23       : original(o, style), remapped(r) {}
24 };
25 } // namespace
26 
TestPathMappings(const PathMappingList & map,llvm::ArrayRef<Matches> matches,llvm::ArrayRef<ConstString> fails)27 static void TestPathMappings(const PathMappingList &map,
28                              llvm::ArrayRef<Matches> matches,
29                              llvm::ArrayRef<ConstString> fails) {
30   ConstString actual_remapped;
31   for (const auto &fail : fails) {
32     SCOPED_TRACE(fail.GetCString());
33     EXPECT_FALSE(map.RemapPath(fail, actual_remapped))
34         << "actual_remapped: " << actual_remapped.GetCString();
35   }
36   for (const auto &match : matches) {
37     SCOPED_TRACE(match.original.GetPath() + " -> " + match.remapped.GetPath());
38     std::string orig_normalized = match.original.GetPath();
39     EXPECT_TRUE(
40         map.RemapPath(ConstString(match.original.GetPath()), actual_remapped));
41     EXPECT_EQ(FileSpec(actual_remapped.GetStringRef()), match.remapped);
42     FileSpec unmapped_spec;
43     EXPECT_TRUE(
44         map.ReverseRemapPath(match.remapped, unmapped_spec).has_value());
45     std::string unmapped_path = unmapped_spec.GetPath();
46     EXPECT_EQ(unmapped_path, orig_normalized);
47   }
48 }
49 
TEST(PathMappingListTest,RelativeTests)50 TEST(PathMappingListTest, RelativeTests) {
51   Matches matches[] = {
52     {".", "/tmp"},
53     {"./", "/tmp"},
54     {"./////", "/tmp"},
55     {"./foo.c", "/tmp/foo.c"},
56     {"foo.c", "/tmp/foo.c"},
57     {"./bar/foo.c", "/tmp/bar/foo.c"},
58     {"bar/foo.c", "/tmp/bar/foo.c"},
59   };
60   ConstString fails[] = {
61 #ifdef _WIN32
62       ConstString("C:\\"),
63       ConstString("C:\\a"),
64 #else
65       ConstString("/a"),
66       ConstString("/"),
67 #endif
68   };
69   PathMappingList map;
70   map.Append(".", "/tmp", false);
71   TestPathMappings(map, matches, fails);
72   PathMappingList map2;
73   map2.Append("", "/tmp", false);
74   TestPathMappings(map, matches, fails);
75 }
76 
TEST(PathMappingListTest,AbsoluteTests)77 TEST(PathMappingListTest, AbsoluteTests) {
78   PathMappingList map;
79   map.Append("/old", "/new", false);
80   Matches matches[] = {
81     {"/old", "/new"},
82     {"/old/", "/new"},
83     {"/old/foo/.", "/new/foo"},
84     {"/old/foo.c", "/new/foo.c"},
85     {"/old/foo.c/.", "/new/foo.c"},
86     {"/old/./foo.c", "/new/foo.c"},
87   };
88   ConstString fails[] = {
89     ConstString("/foo"),
90     ConstString("/"),
91     ConstString("foo.c"),
92     ConstString("./foo.c"),
93     ConstString("../foo.c"),
94     ConstString("../bar/foo.c"),
95   };
96   TestPathMappings(map, matches, fails);
97 }
98 
TEST(PathMappingListTest,RemapRoot)99 TEST(PathMappingListTest, RemapRoot) {
100   PathMappingList map;
101   map.Append("/", "/new", false);
102   Matches matches[] = {
103     {"/old", "/new/old"},
104     {"/old/", "/new/old"},
105     {"/old/foo/.", "/new/old/foo"},
106     {"/old/foo.c", "/new/old/foo.c"},
107     {"/old/foo.c/.", "/new/old/foo.c"},
108     {"/old/./foo.c", "/new/old/foo.c"},
109   };
110   ConstString fails[] = {
111     ConstString("foo.c"),
112     ConstString("./foo.c"),
113     ConstString("../foo.c"),
114     ConstString("../bar/foo.c"),
115   };
116   TestPathMappings(map, matches, fails);
117 }
118 
119 #ifndef _WIN32
TEST(PathMappingListTest,CrossPlatformTests)120 TEST(PathMappingListTest, CrossPlatformTests) {
121   PathMappingList map;
122   map.Append(R"(C:\old)", "/new", false);
123   Matches matches[] = {
124     {R"(C:\old)", llvm::sys::path::Style::windows, "/new"},
125     {R"(C:\old\)", llvm::sys::path::Style::windows, "/new"},
126     {R"(C:\old\foo\.)", llvm::sys::path::Style::windows, "/new/foo"},
127     {R"(C:\old\foo.c)", llvm::sys::path::Style::windows, "/new/foo.c"},
128     {R"(C:\old\foo.c\.)", llvm::sys::path::Style::windows, "/new/foo.c"},
129     {R"(C:\old\.\foo.c)", llvm::sys::path::Style::windows, "/new/foo.c"},
130   };
131   ConstString fails[] = {
132     ConstString("/foo"),
133     ConstString("/"),
134     ConstString("foo.c"),
135     ConstString("./foo.c"),
136     ConstString("../foo.c"),
137     ConstString("../bar/foo.c"),
138   };
139   TestPathMappings(map, matches, fails);
140 }
141 #endif
142