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) : original(o), remapped(r) {}
23 };
24 } // namespace
25 
26 static void TestPathMappings(const PathMappingList &map,
27                              llvm::ArrayRef<Matches> matches,
28                              llvm::ArrayRef<ConstString> fails) {
29   ConstString actual_remapped;
30   for (const auto &fail : fails) {
31     SCOPED_TRACE(fail.GetCString());
32     EXPECT_FALSE(map.RemapPath(fail, actual_remapped))
33         << "actual_remapped: " << actual_remapped.GetCString();
34   }
35   for (const auto &match : matches) {
36     SCOPED_TRACE(match.original.GetPath() + " -> " + match.remapped.GetPath());
37     std::string orig_normalized = match.original.GetPath();
38     EXPECT_TRUE(
39         map.RemapPath(ConstString(match.original.GetPath()), actual_remapped));
40     EXPECT_EQ(FileSpec(actual_remapped.GetStringRef()), match.remapped);
41     FileSpec unmapped_spec;
42     EXPECT_TRUE(map.ReverseRemapPath(match.remapped, unmapped_spec));
43     std::string unmapped_path = unmapped_spec.GetPath();
44     EXPECT_EQ(unmapped_path, orig_normalized);
45   }
46 }
47 
48 TEST(PathMappingListTest, RelativeTests) {
49   Matches matches[] = {
50     {".", "/tmp"},
51     {"./", "/tmp"},
52     {"./////", "/tmp"},
53     {"./foo.c", "/tmp/foo.c"},
54     {"foo.c", "/tmp/foo.c"},
55     {"./bar/foo.c", "/tmp/bar/foo.c"},
56     {"bar/foo.c", "/tmp/bar/foo.c"},
57   };
58   ConstString fails[] = {
59 #ifdef _WIN32
60       ConstString("C:\\"),
61       ConstString("C:\\a"),
62 #else
63       ConstString("/a"),
64       ConstString("/"),
65 #endif
66   };
67   PathMappingList map;
68   map.Append(ConstString("."), ConstString("/tmp"), false);
69   TestPathMappings(map, matches, fails);
70   PathMappingList map2;
71   map2.Append(ConstString(""), ConstString("/tmp"), false);
72   TestPathMappings(map, matches, fails);
73 }
74 
75 TEST(PathMappingListTest, AbsoluteTests) {
76   PathMappingList map;
77   map.Append(ConstString("/old"), ConstString("/new"), false);
78   Matches matches[] = {
79     {"/old", "/new"},
80     {"/old/", "/new"},
81     {"/old/foo/.", "/new/foo"},
82     {"/old/foo.c", "/new/foo.c"},
83     {"/old/foo.c/.", "/new/foo.c"},
84     {"/old/./foo.c", "/new/foo.c"},
85   };
86   ConstString fails[] = {
87     ConstString("/foo"),
88     ConstString("/"),
89     ConstString("foo.c"),
90     ConstString("./foo.c"),
91     ConstString("../foo.c"),
92     ConstString("../bar/foo.c"),
93   };
94   TestPathMappings(map, matches, fails);
95 }
96 
97 TEST(PathMappingListTest, RemapRoot) {
98   PathMappingList map;
99   map.Append(ConstString("/"), ConstString("/new"), false);
100   Matches matches[] = {
101     {"/old", "/new/old"},
102     {"/old/", "/new/old"},
103     {"/old/foo/.", "/new/old/foo"},
104     {"/old/foo.c", "/new/old/foo.c"},
105     {"/old/foo.c/.", "/new/old/foo.c"},
106     {"/old/./foo.c", "/new/old/foo.c"},
107   };
108   ConstString fails[] = {
109     ConstString("foo.c"),
110     ConstString("./foo.c"),
111     ConstString("../foo.c"),
112     ConstString("../bar/foo.c"),
113   };
114   TestPathMappings(map, matches, fails);
115 }
116