blob: 09d2efdf97ff6e4d7c17e006663d1ce30acaadf3 [file] [log] [blame]
Greg Clayton86188d82018-05-21 14:14:36 +00001//===-- PathMappingListTest.cpp ---------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Greg Clayton86188d82018-05-21 14:14:36 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/ADT/ArrayRef.h"
10#include "lldb/Target/PathMappingList.h"
11#include "lldb/Utility/FileSpec.h"
12#include "gtest/gtest.h"
13#include <utility>
14
15using namespace lldb_private;
16
17namespace {
Pavel Labathfe547e02018-05-23 10:32:05 +000018struct Matches {
19 FileSpec original;
20 FileSpec remapped;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +000021 Matches(const char *o, const char *r) : original(o), remapped(r) {}
Pavel Labathfe547e02018-05-23 10:32:05 +000022};
23} // namespace
24
25static void TestPathMappings(const PathMappingList &map,
26 llvm::ArrayRef<Matches> matches,
27 llvm::ArrayRef<ConstString> fails) {
28 ConstString actual_remapped;
29 for (const auto &fail : fails) {
Pavel Labath2e638402018-06-14 10:31:06 +000030 SCOPED_TRACE(fail.GetCString());
31 EXPECT_FALSE(map.RemapPath(fail, actual_remapped))
32 << "actual_remapped: " << actual_remapped.GetCString();
Pavel Labathfe547e02018-05-23 10:32:05 +000033 }
34 for (const auto &match : matches) {
Pavel Labath2e638402018-06-14 10:31:06 +000035 SCOPED_TRACE(match.original.GetPath() + " -> " + match.remapped.GetPath());
Pavel Labathfe547e02018-05-23 10:32:05 +000036 std::string orig_normalized = match.original.GetPath();
37 EXPECT_TRUE(
38 map.RemapPath(ConstString(match.original.GetPath()), actual_remapped));
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +000039 EXPECT_EQ(FileSpec(actual_remapped.GetStringRef()), match.remapped);
Pavel Labathfe547e02018-05-23 10:32:05 +000040 FileSpec unmapped_spec;
41 EXPECT_TRUE(map.ReverseRemapPath(match.remapped, unmapped_spec));
42 std::string unmapped_path = unmapped_spec.GetPath();
43 EXPECT_EQ(unmapped_path, orig_normalized);
Greg Clayton86188d82018-05-21 14:14:36 +000044 }
45}
46
47TEST(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[] = {
Pavel Labath2e638402018-06-14 10:31:06 +000058#ifdef _WIN32
59 ConstString("C:\\"),
60 ConstString("C:\\a"),
61#else
62 ConstString("/a"),
63 ConstString("/"),
64#endif
Greg Clayton86188d82018-05-21 14:14:36 +000065 };
66 PathMappingList map;
67 map.Append(ConstString("."), ConstString("/tmp"), false);
68 TestPathMappings(map, matches, fails);
69 PathMappingList map2;
70 map2.Append(ConstString(""), ConstString("/tmp"), false);
71 TestPathMappings(map, matches, fails);
72}
73
74TEST(PathMappingListTest, AbsoluteTests) {
75 PathMappingList map;
76 map.Append(ConstString("/old"), ConstString("/new"), false);
77 Matches matches[] = {
78 {"/old", "/new"},
79 {"/old/", "/new"},
80 {"/old/foo/.", "/new/foo"},
81 {"/old/foo.c", "/new/foo.c"},
82 {"/old/foo.c/.", "/new/foo.c"},
83 {"/old/./foo.c", "/new/foo.c"},
84 };
85 ConstString fails[] = {
86 ConstString("/foo"),
87 ConstString("/"),
88 ConstString("foo.c"),
89 ConstString("./foo.c"),
90 ConstString("../foo.c"),
91 ConstString("../bar/foo.c"),
92 };
93 TestPathMappings(map, matches, fails);
94}
95
96TEST(PathMappingListTest, RemapRoot) {
97 PathMappingList map;
98 map.Append(ConstString("/"), ConstString("/new"), false);
99 Matches matches[] = {
100 {"/old", "/new/old"},
101 {"/old/", "/new/old"},
102 {"/old/foo/.", "/new/old/foo"},
103 {"/old/foo.c", "/new/old/foo.c"},
104 {"/old/foo.c/.", "/new/old/foo.c"},
105 {"/old/./foo.c", "/new/old/foo.c"},
106 };
107 ConstString fails[] = {
108 ConstString("foo.c"),
109 ConstString("./foo.c"),
110 ConstString("../foo.c"),
111 ConstString("../bar/foo.c"),
112 };
113 TestPathMappings(map, matches, fails);
114}