1 //===-- TestCompletion.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/FileSystem.h"
11 #include "lldb/Interpreter/CommandCompletions.h"
12 #include "lldb/Utility/StringList.h"
13 #include "lldb/Utility/TildeExpressionResolver.h"
15 #include "gmock/gmock.h"
16 #include "gtest/gtest.h"
18 #include "TestingSupport/MockTildeExpressionResolver.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/raw_ostream.h"
24 namespace fs = llvm::sys::fs;
25 namespace path = llvm::sys::path;
27 using namespace lldb_private;
29 #define ASSERT_NO_ERROR(x) \
30 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
31 SmallString<128> MessageStorage; \
32 raw_svector_ostream Message(MessageStorage); \
33 Message << #x ": did not return errc::success.\n" \
34 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
35 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
36 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
42 class CompletionTest : public testing::Test {
44 /// Unique temporary directory in which all created filesystem entities must
45 /// be placed. It is removed at the end of the test suite.
46 SmallString<128> BaseDir;
48 /// The working directory that we got when starting the test. Every test
49 /// should chdir into this directory first because some tests maybe chdir
50 /// into another one during their run.
51 static SmallString<128> OriginalWorkingDir;
53 SmallString<128> DirFoo;
54 SmallString<128> DirFooA;
55 SmallString<128> DirFooB;
56 SmallString<128> DirFooC;
57 SmallString<128> DirBar;
58 SmallString<128> DirBaz;
59 SmallString<128> DirTestFolder;
60 SmallString<128> DirNested;
62 SmallString<128> FileAA;
63 SmallString<128> FileAB;
64 SmallString<128> FileAC;
65 SmallString<128> FileFoo;
66 SmallString<128> FileBar;
67 SmallString<128> FileBaz;
69 void SetUp() override {
70 FileSystem::Initialize();
72 // chdir back into the original working dir this test binary started with.
73 // A previous test may have have changed the working dir.
74 ASSERT_NO_ERROR(fs::set_current_path(OriginalWorkingDir));
76 // Get the name of the current test. To prevent that by chance two tests
77 // get the same temporary directory if createUniqueDirectory fails.
78 auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
79 ASSERT_TRUE(test_info != nullptr);
80 std::string name = test_info->name();
81 ASSERT_NO_ERROR(fs::createUniqueDirectory("FsCompletion-" + name, BaseDir));
83 const char *DirNames[] = {"foo", "fooa", "foob", "fooc",
84 "bar", "baz", "test_folder", "foo/nested"};
85 const char *FileNames[] = {"aa1234.tmp", "ab1234.tmp", "ac1234.tmp",
86 "foo1234.tmp", "bar1234.tmp", "baz1234.tmp"};
87 SmallString<128> *Dirs[] = {&DirFoo, &DirFooA, &DirFooB, &DirFooC,
88 &DirBar, &DirBaz, &DirTestFolder, &DirNested};
89 for (auto Dir : llvm::zip(DirNames, Dirs)) {
90 auto &Path = *std::get<1>(Dir);
92 path::append(Path, std::get<0>(Dir));
93 ASSERT_NO_ERROR(fs::create_directories(Path));
96 SmallString<128> *Files[] = {&FileAA, &FileAB, &FileAC,
97 &FileFoo, &FileBar, &FileBaz};
98 for (auto File : llvm::zip(FileNames, Files)) {
99 auto &Path = *std::get<1>(File);
101 path::append(Path, std::get<0>(File));
103 ASSERT_NO_ERROR(fs::createUniqueFile(Path, FD, Path));
108 static void SetUpTestCase() {
109 ASSERT_NO_ERROR(fs::current_path(OriginalWorkingDir));
112 void TearDown() override {
113 ASSERT_NO_ERROR(fs::remove_directories(BaseDir));
114 FileSystem::Terminate();
117 static bool HasEquivalentFile(const Twine &Path, const StringList &Paths) {
118 for (size_t I = 0; I < Paths.GetSize(); ++I) {
119 if (fs::equivalent(Path, Paths[I]))
125 void DoDirCompletions(const Twine &Prefix,
126 StandardTildeExpressionResolver &Resolver,
127 StringList &Results) {
128 // When a partial name matches, it returns all matches. If it matches both
129 // a full name AND some partial names, it returns all of them.
131 CommandCompletions::DiskDirectories(Prefix + "foo", Results, Resolver);
132 ASSERT_EQ(4u, Count);
133 ASSERT_EQ(Count, Results.GetSize());
134 EXPECT_TRUE(HasEquivalentFile(DirFoo, Results));
135 EXPECT_TRUE(HasEquivalentFile(DirFooA, Results));
136 EXPECT_TRUE(HasEquivalentFile(DirFooB, Results));
137 EXPECT_TRUE(HasEquivalentFile(DirFooC, Results));
139 // If it matches only partial names, it still works as expected.
140 Count = CommandCompletions::DiskDirectories(Twine(Prefix) + "b", Results,
142 ASSERT_EQ(2u, Count);
143 ASSERT_EQ(Count, Results.GetSize());
144 EXPECT_TRUE(HasEquivalentFile(DirBar, Results));
145 EXPECT_TRUE(HasEquivalentFile(DirBaz, Results));
149 SmallString<128> CompletionTest::OriginalWorkingDir;
152 static std::vector<std::string> toVector(const StringList &SL) {
153 std::vector<std::string> Result;
154 for (size_t Idx = 0; Idx < SL.GetSize(); ++Idx)
155 Result.push_back(SL[Idx]);
158 using testing::UnorderedElementsAre;
160 TEST_F(CompletionTest, DirCompletionAbsolute) {
161 // All calls to DiskDirectories() return only directories, even when
162 // there are files which also match. The tests below all check this
163 // by asserting an exact result count, and verifying against known
166 std::string Prefixes[] = {(Twine(BaseDir) + "/").str(), ""};
168 StandardTildeExpressionResolver Resolver;
171 // When a directory is specified that doesn't end in a slash, it searches
172 // for that directory, not items under it.
173 // Sanity check that the path we complete on exists and isn't too long.
174 size_t Count = CommandCompletions::DiskDirectories(Twine(BaseDir) + "/fooa",
176 ASSERT_EQ(1u, Count);
177 ASSERT_EQ(Count, Results.GetSize());
178 EXPECT_TRUE(HasEquivalentFile(DirFooA, Results));
180 Count = CommandCompletions::DiskDirectories(Twine(BaseDir) + "/.", Results,
182 ASSERT_EQ(0u, Count);
183 ASSERT_EQ(Count, Results.GetSize());
185 // When the same directory ends with a slash, it finds all children.
186 Count = CommandCompletions::DiskDirectories(Prefixes[0], Results, Resolver);
187 ASSERT_EQ(7u, Count);
188 ASSERT_EQ(Count, Results.GetSize());
189 EXPECT_TRUE(HasEquivalentFile(DirFoo, Results));
190 EXPECT_TRUE(HasEquivalentFile(DirFooA, Results));
191 EXPECT_TRUE(HasEquivalentFile(DirFooB, Results));
192 EXPECT_TRUE(HasEquivalentFile(DirFooC, Results));
193 EXPECT_TRUE(HasEquivalentFile(DirBar, Results));
194 EXPECT_TRUE(HasEquivalentFile(DirBaz, Results));
195 EXPECT_TRUE(HasEquivalentFile(DirTestFolder, Results));
197 DoDirCompletions(Twine(BaseDir) + "/", Resolver, Results);
198 llvm::sys::fs::set_current_path(BaseDir);
199 DoDirCompletions("", Resolver, Results);
202 TEST_F(CompletionTest, FileCompletionAbsolute) {
203 // All calls to DiskFiles() return both files and directories The tests below
204 // all check this by asserting an exact result count, and verifying against
207 StandardTildeExpressionResolver Resolver;
209 // When an item is specified that doesn't end in a slash but exactly matches
210 // one item, it returns that item.
211 size_t Count = CommandCompletions::DiskFiles(Twine(BaseDir) + "/fooa",
213 ASSERT_EQ(1u, Count);
214 ASSERT_EQ(Count, Results.GetSize());
215 EXPECT_TRUE(HasEquivalentFile(DirFooA, Results));
217 // The previous check verified a directory match. But it should work for
220 CommandCompletions::DiskFiles(Twine(BaseDir) + "/aa", Results, Resolver);
221 ASSERT_EQ(1u, Count);
222 ASSERT_EQ(Count, Results.GetSize());
223 EXPECT_TRUE(HasEquivalentFile(FileAA, Results));
225 // When it ends with a slash, it should find all files and directories.
227 CommandCompletions::DiskFiles(Twine(BaseDir) + "/", Results, Resolver);
228 ASSERT_EQ(13u, Count);
229 ASSERT_EQ(Count, Results.GetSize());
230 EXPECT_TRUE(HasEquivalentFile(DirFoo, Results));
231 EXPECT_TRUE(HasEquivalentFile(DirFooA, Results));
232 EXPECT_TRUE(HasEquivalentFile(DirFooB, Results));
233 EXPECT_TRUE(HasEquivalentFile(DirFooC, Results));
234 EXPECT_TRUE(HasEquivalentFile(DirBar, Results));
235 EXPECT_TRUE(HasEquivalentFile(DirBaz, Results));
236 EXPECT_TRUE(HasEquivalentFile(DirTestFolder, Results));
238 EXPECT_TRUE(HasEquivalentFile(FileAA, Results));
239 EXPECT_TRUE(HasEquivalentFile(FileAB, Results));
240 EXPECT_TRUE(HasEquivalentFile(FileAC, Results));
241 EXPECT_TRUE(HasEquivalentFile(FileFoo, Results));
242 EXPECT_TRUE(HasEquivalentFile(FileBar, Results));
243 EXPECT_TRUE(HasEquivalentFile(FileBaz, Results));
245 // When a partial name matches, it returns all file & directory matches.
247 CommandCompletions::DiskFiles(Twine(BaseDir) + "/foo", Results, Resolver);
248 ASSERT_EQ(5u, Count);
249 ASSERT_EQ(Count, Results.GetSize());
250 EXPECT_TRUE(HasEquivalentFile(DirFoo, Results));
251 EXPECT_TRUE(HasEquivalentFile(DirFooA, Results));
252 EXPECT_TRUE(HasEquivalentFile(DirFooB, Results));
253 EXPECT_TRUE(HasEquivalentFile(DirFooC, Results));
254 EXPECT_TRUE(HasEquivalentFile(FileFoo, Results));
257 TEST_F(CompletionTest, DirCompletionUsername) {
258 MockTildeExpressionResolver Resolver("James", BaseDir);
259 Resolver.AddKnownUser("Kirk", DirFooB);
260 Resolver.AddKnownUser("Lars", DirFooC);
261 Resolver.AddKnownUser("Jason", DirFoo);
262 Resolver.AddKnownUser("Larry", DirFooA);
263 std::string sep = path::get_separator();
265 // Just resolving current user's home directory by itself should return the
268 size_t Count = CommandCompletions::DiskDirectories("~", Results, Resolver);
269 EXPECT_EQ(Count, Results.GetSize());
270 EXPECT_THAT(toVector(Results), UnorderedElementsAre("~" + sep));
272 // With a slash appended, it should return all items in the directory.
273 Count = CommandCompletions::DiskDirectories("~/", Results, Resolver);
274 EXPECT_THAT(toVector(Results),
275 UnorderedElementsAre(
276 "~/foo" + sep, "~/fooa" + sep, "~/foob" + sep, "~/fooc" + sep,
277 "~/bar" + sep, "~/baz" + sep, "~/test_folder" + sep));
278 EXPECT_EQ(Count, Results.GetSize());
280 // Check that we can complete directories in nested paths
281 Count = CommandCompletions::DiskDirectories("~/foo/", Results, Resolver);
282 EXPECT_EQ(Count, Results.GetSize());
283 EXPECT_THAT(toVector(Results), UnorderedElementsAre("~/foo/nested" + sep));
285 Count = CommandCompletions::DiskDirectories("~/foo/nes", Results, Resolver);
286 EXPECT_EQ(Count, Results.GetSize());
287 EXPECT_THAT(toVector(Results), UnorderedElementsAre("~/foo/nested" + sep));
289 // With ~username syntax it should return one match if there is an exact
290 // match. It shouldn't translate to the actual directory, it should keep the
291 // form the user typed.
292 Count = CommandCompletions::DiskDirectories("~Lars", Results, Resolver);
293 EXPECT_EQ(Count, Results.GetSize());
294 EXPECT_THAT(toVector(Results), UnorderedElementsAre("~Lars" + sep));
296 // But with a username that is not found, no results are returned.
297 Count = CommandCompletions::DiskDirectories("~Dave", Results, Resolver);
298 EXPECT_EQ(Count, Results.GetSize());
299 EXPECT_THAT(toVector(Results), UnorderedElementsAre());
301 // And if there are multiple matches, it should return all of them.
302 Count = CommandCompletions::DiskDirectories("~La", Results, Resolver);
303 EXPECT_EQ(Count, Results.GetSize());
304 EXPECT_THAT(toVector(Results),
305 UnorderedElementsAre("~Lars" + sep, "~Larry" + sep));