1 //===-- PathMappingListTest.cpp -------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Target/PathMappingList.h"
10 #include "lldb/Utility/FileSpec.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "gtest/gtest.h"
15 using namespace lldb_private
;
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
) {}
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();
36 for (const auto &match
: matches
) {
37 SCOPED_TRACE(match
.original
.GetPath() + " -> " + match
.remapped
.GetPath());
38 std::string orig_normalized
= match
.original
.GetPath();
40 map
.RemapPath(ConstString(match
.original
.GetPath()), actual_remapped
));
41 EXPECT_EQ(FileSpec(actual_remapped
.GetStringRef()), match
.remapped
);
42 FileSpec unmapped_spec
;
44 map
.ReverseRemapPath(match
.remapped
, unmapped_spec
).has_value());
45 std::string unmapped_path
= unmapped_spec
.GetPath();
46 EXPECT_EQ(unmapped_path
, orig_normalized
);
50 TEST(PathMappingListTest
, RelativeTests
) {
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"},
60 ConstString fails
[] = {
70 map
.Append(".", "/tmp", false);
71 TestPathMappings(map
, matches
, fails
);
73 map2
.Append("", "/tmp", false);
74 TestPathMappings(map
, matches
, fails
);
77 TEST(PathMappingListTest
, AbsoluteTests
) {
79 map
.Append("/old", "/new", false);
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"},
88 ConstString fails
[] = {
92 ConstString("./foo.c"),
93 ConstString("../foo.c"),
94 ConstString("../bar/foo.c"),
96 TestPathMappings(map
, matches
, fails
);
99 TEST(PathMappingListTest
, RemapRoot
) {
101 map
.Append("/", "/new", 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"},
110 ConstString fails
[] = {
111 ConstString("foo.c"),
112 ConstString("./foo.c"),
113 ConstString("../foo.c"),
114 ConstString("../bar/foo.c"),
116 TestPathMappings(map
, matches
, fails
);
120 TEST(PathMappingListTest
, CrossPlatformTests
) {
122 map
.Append(R
"(C:\old)", "/new", 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"},
131 ConstString fails
[] = {
134 ConstString("foo.c"),
135 ConstString("./foo.c"),
136 ConstString("../foo.c"),
137 ConstString("../bar/foo.c"),
139 TestPathMappings(map
, matches
, fails
);