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     ConstString original;
21     ConstString remapped;
22     Matches(const char *o, const char *r) : original(o),
23         remapped(r) {}
24   };
25 
26   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       EXPECT_FALSE(map.RemapPath(fail, actual_remapped));
32     }
33     for (const auto &match: matches) {
34       FileSpec orig_spec(match.original.GetStringRef(), false);
35       std::string orig_normalized = orig_spec.GetPath();
36       EXPECT_TRUE(map.RemapPath(match.original, actual_remapped));
37       EXPECT_EQ(actual_remapped, match.remapped);
38       FileSpec remapped_spec(match.remapped.GetStringRef(), false);
39       FileSpec unmapped_spec;
40       EXPECT_TRUE(map.ReverseRemapPath(remapped_spec, unmapped_spec));
41       std::string unmapped_path = unmapped_spec.GetPath();
42       EXPECT_EQ(unmapped_path, orig_normalized);
43     }
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     ConstString("/a"),
59     ConstString("/"),
60   };
61   PathMappingList map;
62   map.Append(ConstString("."), ConstString("/tmp"), false);
63   TestPathMappings(map, matches, fails);
64   PathMappingList map2;
65   map2.Append(ConstString(""), ConstString("/tmp"), false);
66   TestPathMappings(map, matches, fails);
67 }
68 
69 TEST(PathMappingListTest, AbsoluteTests) {
70   PathMappingList map;
71   map.Append(ConstString("/old"), ConstString("/new"), false);
72   Matches matches[] = {
73     {"/old", "/new"},
74     {"/old/", "/new"},
75     {"/old/foo/.", "/new/foo"},
76     {"/old/foo.c", "/new/foo.c"},
77     {"/old/foo.c/.", "/new/foo.c"},
78     {"/old/./foo.c", "/new/foo.c"},
79   };
80   ConstString fails[] = {
81     ConstString("/foo"),
82     ConstString("/"),
83     ConstString("foo.c"),
84     ConstString("./foo.c"),
85     ConstString("../foo.c"),
86     ConstString("../bar/foo.c"),
87   };
88   TestPathMappings(map, matches, fails);
89 }
90 
91 TEST(PathMappingListTest, RemapRoot) {
92   PathMappingList map;
93   map.Append(ConstString("/"), ConstString("/new"), false);
94   Matches matches[] = {
95     {"/old", "/new/old"},
96     {"/old/", "/new/old"},
97     {"/old/foo/.", "/new/old/foo"},
98     {"/old/foo.c", "/new/old/foo.c"},
99     {"/old/foo.c/.", "/new/old/foo.c"},
100     {"/old/./foo.c", "/new/old/foo.c"},
101   };
102   ConstString fails[] = {
103     ConstString("foo.c"),
104     ConstString("./foo.c"),
105     ConstString("../foo.c"),
106     ConstString("../bar/foo.c"),
107   };
108   TestPathMappings(map, matches, fails);
109 }
110