PathMappingTests.cpp
7.87 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//===-- PathMappingTests.cpp ------------------------*- C++ -*-----------===//
//
// 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 "PathMapping.h"
#include "llvm/Support/JSON.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <string>
namespace clang {
namespace clangd {
namespace {
using ::testing::ElementsAre;
MATCHER_P2(Mapping, ClientPath, ServerPath, "") {
return arg.ClientPath == ClientPath && arg.ServerPath == ServerPath;
}
bool failedParse(llvm::StringRef RawMappings) {
llvm::Expected<PathMappings> Mappings = parsePathMappings(RawMappings);
if (!Mappings) {
consumeError(Mappings.takeError());
return true;
}
return false;
}
TEST(ParsePathMappingTests, WindowsPath) {
// Relative path to C drive
EXPECT_TRUE(failedParse(R"(C:a=/root)"));
EXPECT_TRUE(failedParse(R"(\C:a=/root)"));
// Relative path to current drive.
EXPECT_TRUE(failedParse(R"(\a=/root)"));
// Absolute paths
llvm::Expected<PathMappings> ParsedMappings =
parsePathMappings(R"(C:\a=/root)");
ASSERT_TRUE(bool(ParsedMappings));
EXPECT_THAT(*ParsedMappings, ElementsAre(Mapping("/C:/a", "/root")));
// Absolute UNC path
ParsedMappings = parsePathMappings(R"(\\Server\C$=/root)");
ASSERT_TRUE(bool(ParsedMappings));
EXPECT_THAT(*ParsedMappings, ElementsAre(Mapping("//Server/C$", "/root")));
}
TEST(ParsePathMappingTests, UnixPath) {
// Relative unix path
EXPECT_TRUE(failedParse("a/b=/root"));
// Absolute unix path
llvm::Expected<PathMappings> ParsedMappings = parsePathMappings("/A/b=/root");
ASSERT_TRUE(bool(ParsedMappings));
EXPECT_THAT(*ParsedMappings, ElementsAre(Mapping("/A/b", "/root")));
// Aboslute unix path w/ backslash
ParsedMappings = parsePathMappings(R"(/a/b\\ar=/root)");
ASSERT_TRUE(bool(ParsedMappings));
EXPECT_THAT(*ParsedMappings, ElementsAre(Mapping(R"(/a/b\\ar)", "/root")));
}
TEST(ParsePathMappingTests, ImproperFormat) {
// uneven mappings
EXPECT_TRUE(failedParse("/home/myuser1="));
// mappings need to be absolute
EXPECT_TRUE(failedParse("home/project=/workarea/project"));
// duplicate delimiter
EXPECT_TRUE(failedParse("/home==/workarea"));
// no delimiter
EXPECT_TRUE(failedParse("/home"));
// improper delimiter
EXPECT_TRUE(failedParse("/home,/workarea"));
}
TEST(ParsePathMappingTests, ParsesMultiple) {
std::string RawPathMappings =
"/home/project=/workarea/project,/home/project/.includes=/opt/include";
auto Parsed = parsePathMappings(RawPathMappings);
ASSERT_TRUE(bool(Parsed));
EXPECT_THAT(*Parsed,
ElementsAre(Mapping("/home/project", "/workarea/project"),
Mapping("/home/project/.includes", "/opt/include")));
}
bool mapsProperly(llvm::StringRef Orig, llvm::StringRef Expected,
llvm::StringRef RawMappings, PathMapping::Direction Dir) {
llvm::Expected<PathMappings> Mappings = parsePathMappings(RawMappings);
if (!Mappings)
return false;
llvm::Optional<std::string> MappedPath = doPathMapping(Orig, Dir, *Mappings);
std::string Actual = MappedPath ? *MappedPath : Orig.str();
EXPECT_STREQ(Expected.str().c_str(), Actual.c_str());
return Expected == Actual;
}
TEST(DoPathMappingTests, PreservesOriginal) {
// Preserves original path when no mapping
EXPECT_TRUE(mapsProperly("file:///home", "file:///home", "",
PathMapping::Direction::ClientToServer));
}
TEST(DoPathMappingTests, UsesFirstMatch) {
EXPECT_TRUE(mapsProperly("file:///home/foo.cpp", "file:///workarea1/foo.cpp",
"/home=/workarea1,/home=/workarea2",
PathMapping::Direction::ClientToServer));
}
TEST(DoPathMappingTests, IgnoresSubstrings) {
// Doesn't map substrings that aren't a proper path prefix
EXPECT_TRUE(mapsProperly("file://home/foo-bar.cpp", "file://home/foo-bar.cpp",
"/home/foo=/home/bar",
PathMapping::Direction::ClientToServer));
}
TEST(DoPathMappingTests, MapsOutgoingPaths) {
// When IsIncoming is false (i.e.a response), map the other way
EXPECT_TRUE(mapsProperly("file:///workarea/foo.cpp", "file:///home/foo.cpp",
"/home=/workarea",
PathMapping::Direction::ServerToClient));
}
TEST(DoPathMappingTests, OnlyMapFileUris) {
EXPECT_TRUE(mapsProperly("test:///home/foo.cpp", "test:///home/foo.cpp",
"/home=/workarea",
PathMapping::Direction::ClientToServer));
}
TEST(DoPathMappingTests, RespectsCaseSensitivity) {
EXPECT_TRUE(mapsProperly("file:///HOME/foo.cpp", "file:///HOME/foo.cpp",
"/home=/workarea",
PathMapping::Direction::ClientToServer));
}
TEST(DoPathMappingTests, MapsWindowsPaths) {
// Maps windows properly
EXPECT_TRUE(mapsProperly("file:///C:/home/foo.cpp",
"file:///C:/workarea/foo.cpp", R"(C:\home=C:\workarea)",
PathMapping::Direction::ClientToServer));
}
TEST(DoPathMappingTests, MapsWindowsUnixInterop) {
// Path mappings with a windows-style client path and unix-style server path
EXPECT_TRUE(mapsProperly(
"file:///C:/home/foo.cpp", "file:///workarea/foo.cpp",
R"(C:\home=/workarea)", PathMapping::Direction::ClientToServer));
}
TEST(ApplyPathMappingTests, PreservesOriginalParams) {
auto Params = llvm::json::parse(R"({
"textDocument": {"uri": "file:///home/foo.cpp"},
"position": {"line": 0, "character": 0}
})");
ASSERT_TRUE(bool(Params));
llvm::json::Value ExpectedParams = *Params;
PathMappings Mappings;
applyPathMappings(*Params, PathMapping::Direction::ClientToServer, Mappings);
EXPECT_EQ(*Params, ExpectedParams);
}
TEST(ApplyPathMappingTests, MapsAllMatchingPaths) {
// Handles nested objects and array values
auto Params = llvm::json::parse(R"({
"rootUri": {"uri": "file:///home/foo.cpp"},
"workspaceFolders": ["file:///home/src", "file:///tmp"]
})");
auto ExpectedParams = llvm::json::parse(R"({
"rootUri": {"uri": "file:///workarea/foo.cpp"},
"workspaceFolders": ["file:///workarea/src", "file:///tmp"]
})");
auto Mappings = parsePathMappings("/home=/workarea");
ASSERT_TRUE(bool(Params) && bool(ExpectedParams) && bool(Mappings));
applyPathMappings(*Params, PathMapping::Direction::ClientToServer, *Mappings);
EXPECT_EQ(*Params, *ExpectedParams);
}
TEST(ApplyPathMappingTests, MapsOutbound) {
auto Params = llvm::json::parse(R"({
"id": 1,
"result": [
{"uri": "file:///opt/include/foo.h"},
{"uri": "file:///workarea/src/foo.cpp"}]
})");
auto ExpectedParams = llvm::json::parse(R"({
"id": 1,
"result": [
{"uri": "file:///home/.includes/foo.h"},
{"uri": "file:///home/src/foo.cpp"}]
})");
auto Mappings =
parsePathMappings("/home=/workarea,/home/.includes=/opt/include");
ASSERT_TRUE(bool(Params) && bool(ExpectedParams) && bool(Mappings));
applyPathMappings(*Params, PathMapping::Direction::ServerToClient, *Mappings);
EXPECT_EQ(*Params, *ExpectedParams);
}
TEST(ApplyPathMappingTests, MapsKeys) {
auto Params = llvm::json::parse(R"({
"changes": {
"file:///home/foo.cpp": {"newText": "..."},
"file:///home/src/bar.cpp": {"newText": "..."}
}
})");
auto ExpectedParams = llvm::json::parse(R"({
"changes": {
"file:///workarea/foo.cpp": {"newText": "..."},
"file:///workarea/src/bar.cpp": {"newText": "..."}
}
})");
auto Mappings = parsePathMappings("/home=/workarea");
ASSERT_TRUE(bool(Params) && bool(ExpectedParams) && bool(Mappings));
applyPathMappings(*Params, PathMapping::Direction::ClientToServer, *Mappings);
EXPECT_EQ(*Params, *ExpectedParams);
}
} // namespace
} // namespace clangd
} // namespace clang