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
15using namespace lldb_private;
16
17namespace {
18struct Matches {
19 FileSpec original;
20 FileSpec remapped;
21 Matches(const char *o, const char *r) : original(o), remapped(r) {}
22 Matches(const char *o, llvm::sys::path::Style style, const char *r)
23 : original(o, style), remapped(r) {}
24};
25} // namespace
26
27static 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
50TEST(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(path: ".", replacement: "/tmp", notify: false);
71 TestPathMappings(map, matches, fails);
72 PathMappingList map2;
73 map2.Append(path: "", replacement: "/tmp", notify: false);
74 TestPathMappings(map, matches, fails);
75}
76
77TEST(PathMappingListTest, AbsoluteTests) {
78 PathMappingList map;
79 map.Append(path: "/old", replacement: "/new", notify: 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
99TEST(PathMappingListTest, RemapRoot) {
100 PathMappingList map;
101 map.Append(path: "/", replacement: "/new", notify: 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
120TEST(PathMappingListTest, CrossPlatformTests) {
121 PathMappingList map;
122 map.Append(path: R"(C:\old)", replacement: "/new", notify: 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

source code of lldb/unittests/Target/PathMappingListTest.cpp