1 //===-- PathMappingListTest.cpp ---------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/ADT/ArrayRef.h"
11 #include "lldb/Target/PathMappingList.h"
12 #include "lldb/Utility/FileSpec.h"
13 #include "gtest/gtest.h"
14 #include <utility>
15 
16 using namespace lldb_private;
17 
18 namespace {
19 struct Matches {
20   FileSpec original;
21   FileSpec remapped;
22   Matches(const char *o, const char *r)
23       : original(o, false), remapped(r, false) {}
24 };
25 } // namespace
26 
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(), false), match.remapped);
42     FileSpec unmapped_spec;
43     EXPECT_TRUE(map.ReverseRemapPath(match.remapped, unmapped_spec));
44     std::string unmapped_path = unmapped_spec.GetPath();
45     EXPECT_EQ(unmapped_path, orig_normalized);
46   }
47 }
48 
49 TEST(PathMappingListTest, RelativeTests) {
50   Matches matches[] = {
51     {".", "/tmp"},
52     {"./", "/tmp"},
53     {"./////", "/tmp"},
54     {"./foo.c", "/tmp/foo.c"},
55     {"foo.c", "/tmp/foo.c"},
56     {"./bar/foo.c", "/tmp/bar/foo.c"},
57     {"bar/foo.c", "/tmp/bar/foo.c"},
58   };
59   ConstString fails[] = {
60 #ifdef _WIN32
61       ConstString("C:\\"),
62       ConstString("C:\\a"),
63 #else
64       ConstString("/a"),
65       ConstString("/"),
66 #endif
67   };
68   PathMappingList map;
69   map.Append(ConstString("."), ConstString("/tmp"), false);
70   TestPathMappings(map, matches, fails);
71   PathMappingList map2;
72   map2.Append(ConstString(""), ConstString("/tmp"), false);
73   TestPathMappings(map, matches, fails);
74 }
75 
76 TEST(PathMappingListTest, AbsoluteTests) {
77   PathMappingList map;
78   map.Append(ConstString("/old"), ConstString("/new"), false);
79   Matches matches[] = {
80     {"/old", "/new"},
81     {"/old/", "/new"},
82     {"/old/foo/.", "/new/foo"},
83     {"/old/foo.c", "/new/foo.c"},
84     {"/old/foo.c/.", "/new/foo.c"},
85     {"/old/./foo.c", "/new/foo.c"},
86   };
87   ConstString fails[] = {
88     ConstString("/foo"),
89     ConstString("/"),
90     ConstString("foo.c"),
91     ConstString("./foo.c"),
92     ConstString("../foo.c"),
93     ConstString("../bar/foo.c"),
94   };
95   TestPathMappings(map, matches, fails);
96 }
97 
98 TEST(PathMappingListTest, RemapRoot) {
99   PathMappingList map;
100   map.Append(ConstString("/"), ConstString("/new"), false);
101   Matches matches[] = {
102     {"/old", "/new/old"},
103     {"/old/", "/new/old"},
104     {"/old/foo/.", "/new/old/foo"},
105     {"/old/foo.c", "/new/old/foo.c"},
106     {"/old/foo.c/.", "/new/old/foo.c"},
107     {"/old/./foo.c", "/new/old/foo.c"},
108   };
109   ConstString fails[] = {
110     ConstString("foo.c"),
111     ConstString("./foo.c"),
112     ConstString("../foo.c"),
113     ConstString("../bar/foo.c"),
114   };
115   TestPathMappings(map, matches, fails);
116 }
117