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