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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
//===-- TestCompletion.cpp --------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "gtest/gtest.h"
#include "lldb/Core/StringList.h"
#include "lldb/Interpreter/CommandCompletions.h"
#include "lldb/Utility/TildeExpressionResolver.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
namespace fs = llvm::sys::fs;
namespace path = llvm::sys::path;
using namespace llvm;
using namespace lldb_private;
#define ASSERT_NO_ERROR(x) \
if (std::error_code ASSERT_NO_ERROR_ec = x) { \
SmallString<128> MessageStorage; \
raw_svector_ostream Message(MessageStorage); \
Message << #x ": did not return errc::success.\n" \
<< "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
<< "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
} else { \
}
namespace {
class MockTildeExpressionResolver : public TildeExpressionResolver {
StringRef CurrentUser;
StringMap<StringRef> UserDirectories;
public:
explicit MockTildeExpressionResolver(StringRef CurrentUser, StringRef HomeDir)
: CurrentUser(CurrentUser) {
UserDirectories.insert(std::make_pair(CurrentUser, HomeDir));
}
void AddKnownUser(StringRef User, StringRef HomeDir) {
assert(UserDirectories.find(User) == UserDirectories.end());
UserDirectories.insert(std::make_pair(User, HomeDir));
}
void Clear() {
CurrentUser = StringRef();
UserDirectories.clear();
}
void SetCurrentUser(StringRef User) {
assert(UserDirectories.find(User) != UserDirectories.end());
CurrentUser = User;
}
bool ResolveExact(StringRef Expr, SmallVectorImpl<char> &Output) override {
Output.clear();
assert(!llvm::any_of(Expr, llvm::sys::path::is_separator));
assert(Expr.empty() || Expr[0] == '~');
Expr = Expr.drop_front();
if (Expr.empty()) {
auto Dir = UserDirectories[CurrentUser];
Output.append(Dir.begin(), Dir.end());
return true;
}
for (const auto &User : UserDirectories) {
if (User.getKey() != Expr)
continue;
Output.append(User.getValue().begin(), User.getValue().end());
return true;
}
return false;
}
bool ResolvePartial(StringRef Expr, StringSet<> &Output) override {
Output.clear();
assert(!llvm::any_of(Expr, llvm::sys::path::is_separator));
assert(Expr.empty() || Expr[0] == '~');
Expr = Expr.drop_front();
SmallString<16> QualifiedName = "~";
for (const auto &User : UserDirectories) {
if (!User.getKey().startswith(Expr))
continue;
QualifiedName.resize(1);
QualifiedName.append(User.getKey().begin(), User.getKey().end());
Output.insert(QualifiedName);
}
return !Output.empty();
}
};
class CompletionTest : public testing::Test {
protected:
/// Unique temporary directory in which all created filesystem entities must
/// be placed. It is removed at the end of the test suite.
static SmallString<128> BaseDir;
static SmallString<128> DirFoo;
static SmallString<128> DirFooA;
static SmallString<128> DirFooB;
static SmallString<128> DirFooC;
static SmallString<128> DirBar;
static SmallString<128> DirBaz;
static SmallString<128> DirTestFolder;
static SmallString<128> FileAA;
static SmallString<128> FileAB;
static SmallString<128> FileAC;
static SmallString<128> FileFoo;
static SmallString<128> FileBar;
static SmallString<128> FileBaz;
static void SetUpTestCase() {
ASSERT_NO_ERROR(fs::createUniqueDirectory("FsCompletion", BaseDir));
const char *DirNames[] = {"foo", "fooa", "foob", "fooc",
"bar", "baz", "test_folder"};
const char *FileNames[] = {"aa%%%%.tmp", "ab%%%%.tmp", "ac%%%%.tmp",
"foo%%%%.tmp", "bar%%%%.tmp", "baz%%%%.tmp"};
SmallString<128> *Dirs[] = {&DirFoo, &DirFooA, &DirFooB, &DirFooC,
&DirBar, &DirBaz, &DirTestFolder};
for (auto Dir : llvm::zip(DirNames, Dirs)) {
auto &Path = *std::get<1>(Dir);
Path = BaseDir;
path::append(Path, std::get<0>(Dir));
ASSERT_NO_ERROR(fs::create_directory(Path));
}
SmallString<128> *Files[] = {&FileAA, &FileAB, &FileAC,
&FileFoo, &FileBar, &FileBaz};
for (auto File : llvm::zip(FileNames, Files)) {
auto &Path = *std::get<1>(File);
Path = BaseDir;
path::append(Path, std::get<0>(File));
int FD;
ASSERT_NO_ERROR(fs::createUniqueFile(Path, FD, Path));
::close(FD);
}
}
static void TearDownTestCase() {
ASSERT_NO_ERROR(fs::remove_directories(BaseDir));
}
static bool HasEquivalentFile(const Twine &Path, const StringList &Paths) {
for (int I = 0; I < Paths.GetSize(); ++I) {
if (fs::equivalent(Path, Paths[I]))
return true;
}
return false;
}
static bool ContainsExactString(const Twine &Str, const StringList &Paths) {
SmallString<16> Storage;
StringRef Rendered = Str.toStringRef(Storage);
for (int I = 0; I < Paths.GetSize(); ++I) {
if (Paths[I] == Rendered)
return true;
}
return false;
}
};
SmallString<128> CompletionTest::BaseDir;
SmallString<128> CompletionTest::DirFoo;
SmallString<128> CompletionTest::DirFooA;
SmallString<128> CompletionTest::DirFooB;
SmallString<128> CompletionTest::DirFooC;
SmallString<128> CompletionTest::DirBar;
SmallString<128> CompletionTest::DirBaz;
SmallString<128> CompletionTest::DirTestFolder;
SmallString<128> CompletionTest::FileAA;
SmallString<128> CompletionTest::FileAB;
SmallString<128> CompletionTest::FileAC;
SmallString<128> CompletionTest::FileFoo;
SmallString<128> CompletionTest::FileBar;
SmallString<128> CompletionTest::FileBaz;
}
TEST_F(CompletionTest, DirCompletionAbsolute) {
// All calls to DiskDirectories() return only directories, even when
// there are files which also match. The tests below all check this
// by asserting an exact result count, and verifying against known
// folders.
StringList Results;
// When a directory is specified that doesn't end in a slash, it searches
// for that directory, not items under it.
int Count = CommandCompletions::DiskDirectories2(BaseDir, Results);
ASSERT_EQ(1, Count);
ASSERT_EQ(Count, Results.GetSize());
EXPECT_TRUE(HasEquivalentFile(BaseDir, Results));
// When the same directory ends with a slash, it finds all children.
Count = CommandCompletions::DiskDirectories2(Twine(BaseDir) + "/", Results);
ASSERT_EQ(7, Count);
ASSERT_EQ(Count, Results.GetSize());
EXPECT_TRUE(HasEquivalentFile(DirFoo, Results));
EXPECT_TRUE(HasEquivalentFile(DirFooA, Results));
EXPECT_TRUE(HasEquivalentFile(DirFooB, Results));
EXPECT_TRUE(HasEquivalentFile(DirFooC, Results));
EXPECT_TRUE(HasEquivalentFile(DirBar, Results));
EXPECT_TRUE(HasEquivalentFile(DirBaz, Results));
EXPECT_TRUE(HasEquivalentFile(DirTestFolder, Results));
// When a partial name matches, it returns all matches. If it matches both
// a full name AND some partial names, it returns all of them.
Count =
CommandCompletions::DiskDirectories2(Twine(BaseDir) + "/foo", Results);
ASSERT_EQ(4, Count);
ASSERT_EQ(Count, Results.GetSize());
EXPECT_TRUE(HasEquivalentFile(DirFoo, Results));
EXPECT_TRUE(HasEquivalentFile(DirFooA, Results));
EXPECT_TRUE(HasEquivalentFile(DirFooB, Results));
EXPECT_TRUE(HasEquivalentFile(DirFooC, Results));
// If it matches only partial names, it still works as expected.
Count = CommandCompletions::DiskDirectories2(Twine(BaseDir) + "/b", Results);
ASSERT_EQ(2, Count);
ASSERT_EQ(Count, Results.GetSize());
EXPECT_TRUE(HasEquivalentFile(DirBar, Results));
EXPECT_TRUE(HasEquivalentFile(DirBaz, Results));
}
TEST_F(CompletionTest, FileCompletionAbsolute) {
// All calls to DiskFiles() return both files and directories The tests below
// all check this by asserting an exact result count, and verifying against
// known folders.
StringList Results;
// When an item is specified that doesn't end in a slash but exactly matches
// one item, it returns that item.
int Count = CommandCompletions::DiskFiles2(Twine(BaseDir) + "/fooa", Results);
ASSERT_EQ(1, Count);
ASSERT_EQ(Count, Results.GetSize());
EXPECT_TRUE(HasEquivalentFile(DirFooA, Results));
// The previous check verified a directory match. But it should work for
// files too.
Count = CommandCompletions::DiskFiles2(Twine(BaseDir) + "/aa", Results);
ASSERT_EQ(1, Count);
ASSERT_EQ(Count, Results.GetSize());
EXPECT_TRUE(HasEquivalentFile(FileAA, Results));
// When it ends with a slash, it should find all files and directories.
Count = CommandCompletions::DiskFiles2(Twine(BaseDir) + "/", Results);
ASSERT_EQ(13, Count);
ASSERT_EQ(Count, Results.GetSize());
EXPECT_TRUE(HasEquivalentFile(DirFoo, Results));
EXPECT_TRUE(HasEquivalentFile(DirFooA, Results));
EXPECT_TRUE(HasEquivalentFile(DirFooB, Results));
EXPECT_TRUE(HasEquivalentFile(DirFooC, Results));
EXPECT_TRUE(HasEquivalentFile(DirBar, Results));
EXPECT_TRUE(HasEquivalentFile(DirBaz, Results));
EXPECT_TRUE(HasEquivalentFile(DirTestFolder, Results));
EXPECT_TRUE(HasEquivalentFile(FileAA, Results));
EXPECT_TRUE(HasEquivalentFile(FileAB, Results));
EXPECT_TRUE(HasEquivalentFile(FileAC, Results));
EXPECT_TRUE(HasEquivalentFile(FileFoo, Results));
EXPECT_TRUE(HasEquivalentFile(FileBar, Results));
EXPECT_TRUE(HasEquivalentFile(FileBaz, Results));
// When a partial name matches, it returns all file & directory matches.
Count = CommandCompletions::DiskFiles2(Twine(BaseDir) + "/foo", Results);
ASSERT_EQ(5, Count);
ASSERT_EQ(Count, Results.GetSize());
EXPECT_TRUE(HasEquivalentFile(DirFoo, Results));
EXPECT_TRUE(HasEquivalentFile(DirFooA, Results));
EXPECT_TRUE(HasEquivalentFile(DirFooB, Results));
EXPECT_TRUE(HasEquivalentFile(DirFooC, Results));
EXPECT_TRUE(HasEquivalentFile(FileFoo, Results));
}
TEST_F(CompletionTest, DirCompletionUsername) {
MockTildeExpressionResolver Resolver("James", BaseDir);
Resolver.AddKnownUser("Kirk", DirFooB);
Resolver.AddKnownUser("Lars", DirFooC);
Resolver.AddKnownUser("Jason", DirFoo);
Resolver.AddKnownUser("Larry", DirFooA);
// Just resolving current user's home directory by itself should return the
// directory.
StringList Results;
int Count = CommandCompletions::DiskDirectories2("~", Results, &Resolver);
ASSERT_EQ(1, Count);
ASSERT_EQ(Count, Results.GetSize());
EXPECT_TRUE(ContainsExactString(Twine("~") + path::get_separator(), Results));
// With a slash appended, it should return all items in the directory.
Count = CommandCompletions::DiskDirectories2("~/", Results, &Resolver);
ASSERT_EQ(7, Count);
ASSERT_EQ(Count, Results.GetSize());
EXPECT_TRUE(
ContainsExactString(Twine("~/foo") + path::get_separator(), Results));
EXPECT_TRUE(
ContainsExactString(Twine("~/fooa") + path::get_separator(), Results));
EXPECT_TRUE(
ContainsExactString(Twine("~/foob") + path::get_separator(), Results));
EXPECT_TRUE(
ContainsExactString(Twine("~/fooc") + path::get_separator(), Results));
EXPECT_TRUE(
ContainsExactString(Twine("~/bar") + path::get_separator(), Results));
EXPECT_TRUE(
ContainsExactString(Twine("~/baz") + path::get_separator(), Results));
EXPECT_TRUE(ContainsExactString(
Twine("~/test_folder") + path::get_separator(), Results));
// With ~username syntax it should return one match if there is an exact
// match.
// It shouldn't translate to the actual directory, it should keep the form the
// user typed.
Count = CommandCompletions::DiskDirectories2("~Lars", Results, &Resolver);
ASSERT_EQ(1, Count);
ASSERT_EQ(Count, Results.GetSize());
EXPECT_TRUE(
ContainsExactString(Twine("~Lars") + path::get_separator(), Results));
// But with a username that is not found, no results are returned.
Count = CommandCompletions::DiskDirectories2("~Dave", Results, &Resolver);
ASSERT_EQ(0, Count);
ASSERT_EQ(Count, Results.GetSize());
// And if there are multiple matches, it should return all of them.
Count = CommandCompletions::DiskDirectories2("~La", Results, &Resolver);
ASSERT_EQ(2, Count);
ASSERT_EQ(Count, Results.GetSize());
EXPECT_TRUE(
ContainsExactString(Twine("~Lars") + path::get_separator(), Results));
EXPECT_TRUE(
ContainsExactString(Twine("~Larry") + path::get_separator(), Results));
}
|