1 //===-- TestCompletion.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 "lldb/Host/FileSystem.h" 11 #include "lldb/Interpreter/CommandCompletions.h" 12 #include "lldb/Utility/StringList.h" 13 #include "lldb/Utility/TildeExpressionResolver.h" 14 15 #include "gmock/gmock.h" 16 #include "gtest/gtest.h" 17 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" 23 24 namespace fs = llvm::sys::fs; 25 namespace path = llvm::sys::path; 26 using namespace llvm; 27 using namespace lldb_private; 28 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()); \ 37 } else { \ 38 } 39 40 namespace { 41 42 class CompletionTest : public testing::Test { 43 protected: 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; 47 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; 52 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; 61 62 SmallString<128> FileAA; 63 SmallString<128> FileAB; 64 SmallString<128> FileAC; 65 SmallString<128> FileFoo; 66 SmallString<128> FileBar; 67 SmallString<128> FileBaz; 68 69 void SetUp() override { 70 FileSystem::Initialize(); 71 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)); 75 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)); 82 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); 91 Path = BaseDir; 92 path::append(Path, std::get<0>(Dir)); 93 ASSERT_NO_ERROR(fs::create_directories(Path)); 94 } 95 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); 100 Path = BaseDir; 101 path::append(Path, std::get<0>(File)); 102 int FD; 103 ASSERT_NO_ERROR(fs::createUniqueFile(Path, FD, Path)); 104 ::close(FD); 105 } 106 } 107 108 static void SetUpTestCase() { 109 ASSERT_NO_ERROR(fs::current_path(OriginalWorkingDir)); 110 } 111 112 void TearDown() override { 113 ASSERT_NO_ERROR(fs::remove_directories(BaseDir)); 114 FileSystem::Terminate(); 115 } 116 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])) 120 return true; 121 } 122 return false; 123 } 124 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. 130 uint32_t Count = 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)); 138 139 // If it matches only partial names, it still works as expected. 140 Count = CommandCompletions::DiskDirectories(Twine(Prefix) + "b", Results, 141 Resolver); 142 ASSERT_EQ(2u, Count); 143 ASSERT_EQ(Count, Results.GetSize()); 144 EXPECT_TRUE(HasEquivalentFile(DirBar, Results)); 145 EXPECT_TRUE(HasEquivalentFile(DirBaz, Results)); 146 } 147 }; 148 149 SmallString<128> CompletionTest::OriginalWorkingDir; 150 } // namespace 151 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]); 156 return Result; 157 } 158 using testing::UnorderedElementsAre; 159 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 164 // folders. 165 166 std::string Prefixes[] = {(Twine(BaseDir) + "/").str(), ""}; 167 168 StandardTildeExpressionResolver Resolver; 169 StringList Results; 170 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", 175 Results, Resolver); 176 ASSERT_EQ(1u, Count); 177 ASSERT_EQ(Count, Results.GetSize()); 178 EXPECT_TRUE(HasEquivalentFile(DirFooA, Results)); 179 180 Count = CommandCompletions::DiskDirectories(Twine(BaseDir) + "/.", Results, 181 Resolver); 182 ASSERT_EQ(0u, Count); 183 ASSERT_EQ(Count, Results.GetSize()); 184 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)); 196 197 DoDirCompletions(Twine(BaseDir) + "/", Resolver, Results); 198 llvm::sys::fs::set_current_path(BaseDir); 199 DoDirCompletions("", Resolver, Results); 200 } 201 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 205 // known folders. 206 207 StandardTildeExpressionResolver Resolver; 208 StringList Results; 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", 212 Results, Resolver); 213 ASSERT_EQ(1u, Count); 214 ASSERT_EQ(Count, Results.GetSize()); 215 EXPECT_TRUE(HasEquivalentFile(DirFooA, Results)); 216 217 // The previous check verified a directory match. But it should work for 218 // files too. 219 Count = 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)); 224 225 // When it ends with a slash, it should find all files and directories. 226 Count = 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)); 237 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)); 244 245 // When a partial name matches, it returns all file & directory matches. 246 Count = 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)); 255 } 256 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(); 264 265 // Just resolving current user's home directory by itself should return the 266 // directory. 267 StringList Results; 268 size_t Count = CommandCompletions::DiskDirectories("~", Results, Resolver); 269 EXPECT_EQ(Count, Results.GetSize()); 270 EXPECT_THAT(toVector(Results), UnorderedElementsAre("~" + sep)); 271 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()); 279 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)); 284 285 Count = CommandCompletions::DiskDirectories("~/foo/nes", Results, Resolver); 286 EXPECT_EQ(Count, Results.GetSize()); 287 EXPECT_THAT(toVector(Results), UnorderedElementsAre("~/foo/nested" + sep)); 288 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)); 295 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()); 300 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)); 306 } 307