blob: cb807d0ca20d74341ea8ca2b33aef82d087c30dc [file] [log] [blame]
Greg Clayton86188d82018-05-21 14:14:36 +00001//===-- 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
16using namespace lldb_private;
17
18namespace {
Pavel Labathfe547e02018-05-23 10:32:05 +000019struct 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
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) {
Pavel Labath2e638402018-06-14 10:31:06 +000032 SCOPED_TRACE(fail.GetCString());
33 EXPECT_FALSE(map.RemapPath(fail, actual_remapped))
34 << "actual_remapped: " << actual_remapped.GetCString();
Pavel Labathfe547e02018-05-23 10:32:05 +000035 }
36 for (const auto &match : matches) {
Pavel Labath2e638402018-06-14 10:31:06 +000037 SCOPED_TRACE(match.original.GetPath() + " -> " + match.remapped.GetPath());
Pavel Labathfe547e02018-05-23 10:32:05 +000038 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(), false), match.remapped);
42 FileSpec unmapped_spec;
43 EXPECT_TRUE(map.ReverseRemapPath(match.remapped, unmapped_spec));
44 std::string unmapped_path = unmapped_spec.GetPath();
45 EXPECT_EQ(unmapped_path, orig_normalized);
Greg Clayton86188d82018-05-21 14:14:36 +000046 }
47}
48
49TEST(PathMappingListTest, RelativeTests) {
50 Matches matches[] = {
51 {".", "/tmp"},
52 {"./", "/tmp"},
53 {"./////", "/tmp"},
54 {"./foo.c", "/tmp/foo.c"},
55 {"foo.c", "/tmp/foo.c"},
56 {"./bar/foo.c", "/tmp/bar/foo.c"},
57 {"bar/foo.c", "/tmp/bar/foo.c"},
58 };
59 ConstString fails[] = {
Pavel Labath2e638402018-06-14 10:31:06 +000060#ifdef _WIN32
61 ConstString("C:\\"),
62 ConstString("C:\\a"),
63#else
64 ConstString("/a"),
65 ConstString("/"),
66#endif
Greg Clayton86188d82018-05-21 14:14:36 +000067 };
68 PathMappingList map;
69 map.Append(ConstString("."), ConstString("/tmp"), false);
70 TestPathMappings(map, matches, fails);
71 PathMappingList map2;
72 map2.Append(ConstString(""), ConstString("/tmp"), false);
73 TestPathMappings(map, matches, fails);
74}
75
76TEST(PathMappingListTest, AbsoluteTests) {
77 PathMappingList map;
78 map.Append(ConstString("/old"), ConstString("/new"), false);
79 Matches matches[] = {
80 {"/old", "/new"},
81 {"/old/", "/new"},
82 {"/old/foo/.", "/new/foo"},
83 {"/old/foo.c", "/new/foo.c"},
84 {"/old/foo.c/.", "/new/foo.c"},
85 {"/old/./foo.c", "/new/foo.c"},
86 };
87 ConstString fails[] = {
88 ConstString("/foo"),
89 ConstString("/"),
90 ConstString("foo.c"),
91 ConstString("./foo.c"),
92 ConstString("../foo.c"),
93 ConstString("../bar/foo.c"),
94 };
95 TestPathMappings(map, matches, fails);
96}
97
98TEST(PathMappingListTest, RemapRoot) {
99 PathMappingList map;
100 map.Append(ConstString("/"), ConstString("/new"), false);
101 Matches matches[] = {
102 {"/old", "/new/old"},
103 {"/old/", "/new/old"},
104 {"/old/foo/.", "/new/old/foo"},
105 {"/old/foo.c", "/new/old/foo.c"},
106 {"/old/foo.c/.", "/new/old/foo.c"},
107 {"/old/./foo.c", "/new/old/foo.c"},
108 };
109 ConstString fails[] = {
110 ConstString("foo.c"),
111 ConstString("./foo.c"),
112 ConstString("../foo.c"),
113 ConstString("../bar/foo.c"),
114 };
115 TestPathMappings(map, matches, fails);
116}