1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
//===-- PathMappingListTest.cpp -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Target/PathMappingList.h"
#include "lldb/Utility/FileSpec.h"
#include "llvm/ADT/ArrayRef.h"
#include "gtest/gtest.h"
#include <utility>
using namespace lldb_private;
namespace {
struct Matches {
FileSpec original;
FileSpec remapped;
Matches(const char *o, const char *r) : original(o), remapped(r) {}
Matches(const char *o, llvm::sys::path::Style style, const char *r)
: original(o, style), remapped(r) {}
};
} // namespace
static void TestPathMappings(const PathMappingList &map,
llvm::ArrayRef<Matches> matches,
llvm::ArrayRef<ConstString> fails) {
ConstString actual_remapped;
for (const auto &fail : fails) {
SCOPED_TRACE(fail.GetCString());
EXPECT_FALSE(map.RemapPath(fail, actual_remapped))
<< "actual_remapped: " << actual_remapped.GetCString();
}
for (const auto &match : matches) {
SCOPED_TRACE(match.original.GetPath() + " -> " + match.remapped.GetPath());
std::string orig_normalized = match.original.GetPath();
EXPECT_TRUE(
map.RemapPath(ConstString(match.original.GetPath()), actual_remapped));
EXPECT_EQ(FileSpec(actual_remapped.GetStringRef()), match.remapped);
FileSpec unmapped_spec;
EXPECT_TRUE(
map.ReverseRemapPath(match.remapped, unmapped_spec).has_value());
std::string unmapped_path = unmapped_spec.GetPath();
EXPECT_EQ(unmapped_path, orig_normalized);
}
}
TEST(PathMappingListTest, RelativeTests) {
Matches matches[] = {
{".", "/tmp"},
{"./", "/tmp"},
{"./////", "/tmp"},
{"./foo.c", "/tmp/foo.c"},
{"foo.c", "/tmp/foo.c"},
{"./bar/foo.c", "/tmp/bar/foo.c"},
{"bar/foo.c", "/tmp/bar/foo.c"},
};
ConstString fails[] = {
#ifdef _WIN32
ConstString("C:\\"),
ConstString("C:\\a"),
#else
ConstString("/a"),
ConstString("/"),
#endif
};
PathMappingList map;
map.Append(".", "/tmp", false);
TestPathMappings(map, matches, fails);
PathMappingList map2;
map2.Append("", "/tmp", false);
TestPathMappings(map, matches, fails);
}
TEST(PathMappingListTest, AbsoluteTests) {
PathMappingList map;
map.Append("/old", "/new", false);
Matches matches[] = {
{"/old", "/new"},
{"/old/", "/new"},
{"/old/foo/.", "/new/foo"},
{"/old/foo.c", "/new/foo.c"},
{"/old/foo.c/.", "/new/foo.c"},
{"/old/./foo.c", "/new/foo.c"},
};
ConstString fails[] = {
ConstString("/foo"),
ConstString("/"),
ConstString("foo.c"),
ConstString("./foo.c"),
ConstString("../foo.c"),
ConstString("../bar/foo.c"),
};
TestPathMappings(map, matches, fails);
}
TEST(PathMappingListTest, RemapRoot) {
PathMappingList map;
map.Append("/", "/new", false);
Matches matches[] = {
{"/old", "/new/old"},
{"/old/", "/new/old"},
{"/old/foo/.", "/new/old/foo"},
{"/old/foo.c", "/new/old/foo.c"},
{"/old/foo.c/.", "/new/old/foo.c"},
{"/old/./foo.c", "/new/old/foo.c"},
};
ConstString fails[] = {
ConstString("foo.c"),
ConstString("./foo.c"),
ConstString("../foo.c"),
ConstString("../bar/foo.c"),
};
TestPathMappings(map, matches, fails);
}
#ifndef _WIN32
TEST(PathMappingListTest, CrossPlatformTests) {
PathMappingList map;
map.Append(R"(C:\old)", "/new", false);
Matches matches[] = {
{R"(C:\old)", llvm::sys::path::Style::windows, "/new"},
{R"(C:\old\)", llvm::sys::path::Style::windows, "/new"},
{R"(C:\old\foo\.)", llvm::sys::path::Style::windows, "/new/foo"},
{R"(C:\old\foo.c)", llvm::sys::path::Style::windows, "/new/foo.c"},
{R"(C:\old\foo.c\.)", llvm::sys::path::Style::windows, "/new/foo.c"},
{R"(C:\old\.\foo.c)", llvm::sys::path::Style::windows, "/new/foo.c"},
};
ConstString fails[] = {
ConstString("/foo"),
ConstString("/"),
ConstString("foo.c"),
ConstString("./foo.c"),
ConstString("../foo.c"),
ConstString("../bar/foo.c"),
};
TestPathMappings(map, matches, fails);
}
#endif
|