blob: 42db27fdb5046ec903c0bec07aa380b85d1e506b [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;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +000022 Matches(const char *o, const char *r) : original(o), remapped(r) {}
Pavel Labathfe547e02018-05-23 10:32:05 +000023};
24} // namespace
25
26static void TestPathMappings(const PathMappingList &map,
27 llvm::ArrayRef<Matches> matches,
28 llvm::ArrayRef<ConstString> fails) {
29 ConstString actual_remapped;
30 for (const auto &fail : fails) {
Pavel Labath2e638402018-06-14 10:31:06 +000031 SCOPED_TRACE(fail.GetCString());
32 EXPECT_FALSE(map.RemapPath(fail, actual_remapped))
33 << "actual_remapped: " << actual_remapped.GetCString();
Pavel Labathfe547e02018-05-23 10:32:05 +000034 }
35 for (const auto &match : matches) {
Pavel Labath2e638402018-06-14 10:31:06 +000036 SCOPED_TRACE(match.original.GetPath() + " -> " + match.remapped.GetPath());
Pavel Labathfe547e02018-05-23 10:32:05 +000037 std::string orig_normalized = match.original.GetPath();
38 EXPECT_TRUE(
39 map.RemapPath(ConstString(match.original.GetPath()), actual_remapped));
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +000040 EXPECT_EQ(FileSpec(actual_remapped.GetStringRef()), match.remapped);
Pavel Labathfe547e02018-05-23 10:32:05 +000041 FileSpec unmapped_spec;
42 EXPECT_TRUE(map.ReverseRemapPath(match.remapped, unmapped_spec));
43 std::string unmapped_path = unmapped_spec.GetPath();
44 EXPECT_EQ(unmapped_path, orig_normalized);
Greg Clayton86188d82018-05-21 14:14:36 +000045 }
46}
47
48TEST(PathMappingListTest, RelativeTests) {
49 Matches matches[] = {
50 {".", "/tmp"},
51 {"./", "/tmp"},
52 {"./////", "/tmp"},
53 {"./foo.c", "/tmp/foo.c"},
54 {"foo.c", "/tmp/foo.c"},
55 {"./bar/foo.c", "/tmp/bar/foo.c"},
56 {"bar/foo.c", "/tmp/bar/foo.c"},
57 };
58 ConstString fails[] = {
Pavel Labath2e638402018-06-14 10:31:06 +000059#ifdef _WIN32
60 ConstString("C:\\"),
61 ConstString("C:\\a"),
62#else
63 ConstString("/a"),
64 ConstString("/"),
65#endif
Greg Clayton86188d82018-05-21 14:14:36 +000066 };
67 PathMappingList map;
68 map.Append(ConstString("."), ConstString("/tmp"), false);
69 TestPathMappings(map, matches, fails);
70 PathMappingList map2;
71 map2.Append(ConstString(""), ConstString("/tmp"), false);
72 TestPathMappings(map, matches, fails);
73}
74
75TEST(PathMappingListTest, AbsoluteTests) {
76 PathMappingList map;
77 map.Append(ConstString("/old"), ConstString("/new"), false);
78 Matches matches[] = {
79 {"/old", "/new"},
80 {"/old/", "/new"},
81 {"/old/foo/.", "/new/foo"},
82 {"/old/foo.c", "/new/foo.c"},
83 {"/old/foo.c/.", "/new/foo.c"},
84 {"/old/./foo.c", "/new/foo.c"},
85 };
86 ConstString fails[] = {
87 ConstString("/foo"),
88 ConstString("/"),
89 ConstString("foo.c"),
90 ConstString("./foo.c"),
91 ConstString("../foo.c"),
92 ConstString("../bar/foo.c"),
93 };
94 TestPathMappings(map, matches, fails);
95}
96
97TEST(PathMappingListTest, RemapRoot) {
98 PathMappingList map;
99 map.Append(ConstString("/"), ConstString("/new"), false);
100 Matches matches[] = {
101 {"/old", "/new/old"},
102 {"/old/", "/new/old"},
103 {"/old/foo/.", "/new/old/foo"},
104 {"/old/foo.c", "/new/old/foo.c"},
105 {"/old/foo.c/.", "/new/old/foo.c"},
106 {"/old/./foo.c", "/new/old/foo.c"},
107 };
108 ConstString fails[] = {
109 ConstString("foo.c"),
110 ConstString("./foo.c"),
111 ConstString("../foo.c"),
112 ConstString("../bar/foo.c"),
113 };
114 TestPathMappings(map, matches, fails);
115}