From 31732e6f52c8e5e722d3e4cca053db793dae3b8c Mon Sep 17 00:00:00 2001 From: Nathan James Date: Mon, 11 Jan 2021 16:14:26 +0000 Subject: [PATCH] [clangd] Remove ScratchFS from tests This can lead to issues if files in the tmp directory we don't care about / control are found. This was partially addressed in D94321, but this is a more permanent fix. Fixes https://github.com/clangd/clangd/issues/354 Reviewed By: adamcz, sammccall Differential Revision: https://reviews.llvm.org/D94359 --- .../unittests/GlobalCompilationDatabaseTests.cpp | 75 +++++----------------- 1 file changed, 17 insertions(+), 58 deletions(-) diff --git a/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp b/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp index 12c986572d8..63b1b731656 100644 --- a/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp +++ b/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp @@ -169,48 +169,6 @@ TEST_F(OverlayCDBTest, Adjustments) { "-DFallback", "-DAdjust_baz.cc")); } -// Allows placement of files for tests and cleans them up after. -// FIXME: GlobalCompilationDatabase is mostly VFS-clean now, switch to MockFS? -class ScratchFS { - llvm::SmallString<128> Root; - -public: - ScratchFS() { - EXPECT_FALSE(llvm::sys::fs::createUniqueDirectory("clangd-cdb-test", Root)) - << "Failed to create unique directory"; - } - - ~ScratchFS() { - EXPECT_FALSE(llvm::sys::fs::remove_directories(Root)) - << "Failed to cleanup " << Root; - } - - llvm::StringRef root() const { return Root; } - - void write(PathRef RelativePath, llvm::StringRef Contents) { - std::string AbsPath = path(RelativePath); - EXPECT_FALSE(llvm::sys::fs::create_directories( - llvm::sys::path::parent_path(AbsPath))) - << "Failed to create directories for: " << AbsPath; - - std::error_code EC; - llvm::raw_fd_ostream OS(AbsPath, EC); - EXPECT_FALSE(EC) << "Failed to open " << AbsPath << " for writing"; - OS << llvm::formatv(Contents.data(), - llvm::sys::path::convert_to_slash(Root)); - OS.close(); - - EXPECT_FALSE(OS.has_error()); - } - - std::string path(PathRef RelativePath) const { - llvm::SmallString<128> AbsPath(Root); - llvm::sys::path::append(AbsPath, RelativePath); - llvm::sys::path::native(AbsPath); - return AbsPath.str().str(); - } -}; - TEST(GlobalCompilationDatabaseTest, DiscoveryWithNestedCDBs) { const char *const CDBOuter = R"cdb( @@ -242,34 +200,35 @@ TEST(GlobalCompilationDatabaseTest, DiscoveryWithNestedCDBs) { } ] )cdb"; - ScratchFS FS; - RealThreadsafeFS TFS; - FS.write("compile_commands.json", CDBOuter); - FS.write("build/compile_commands.json", CDBInner); + MockFS FS; + FS.Files[testPath("compile_commands.json")] = + llvm::formatv(CDBOuter, llvm::sys::path::convert_to_slash(testRoot())); + FS.Files[testPath("build/compile_commands.json")] = + llvm::formatv(CDBInner, llvm::sys::path::convert_to_slash(testRoot())); // Note that gen2.cc goes missing with our following model, not sure this // happens in practice though. { - DirectoryBasedGlobalCompilationDatabase DB(TFS); + DirectoryBasedGlobalCompilationDatabase DB(FS); std::vector DiscoveredFiles; auto Sub = DB.watch([&DiscoveredFiles](const std::vector Changes) { DiscoveredFiles = Changes; }); - DB.getCompileCommand(FS.path("build/../a.cc")); + DB.getCompileCommand(testPath("build/../a.cc")); EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(AllOf( EndsWith("a.cc"), Not(HasSubstr(".."))))); DiscoveredFiles.clear(); - DB.getCompileCommand(FS.path("build/gen.cc")); + DB.getCompileCommand(testPath("build/gen.cc")); EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(EndsWith("gen.cc"))); } // With a custom compile commands dir. { - DirectoryBasedGlobalCompilationDatabase::Options Opts(TFS); - Opts.CompileCommandsDir = FS.root().str(); + DirectoryBasedGlobalCompilationDatabase::Options Opts(FS); + Opts.CompileCommandsDir = testRoot(); DirectoryBasedGlobalCompilationDatabase DB(Opts); std::vector DiscoveredFiles; auto Sub = @@ -277,24 +236,23 @@ TEST(GlobalCompilationDatabaseTest, DiscoveryWithNestedCDBs) { DiscoveredFiles = Changes; }); - DB.getCompileCommand(FS.path("a.cc")); + DB.getCompileCommand(testPath("a.cc")); EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(EndsWith("a.cc"), EndsWith("gen.cc"), EndsWith("gen2.cc"))); DiscoveredFiles.clear(); - DB.getCompileCommand(FS.path("build/gen.cc")); + DB.getCompileCommand(testPath("build/gen.cc")); EXPECT_THAT(DiscoveredFiles, IsEmpty()); } } TEST(GlobalCompilationDatabaseTest, BuildDir) { - ScratchFS FS; - RealThreadsafeFS TFS; + MockFS FS; auto Command = [&](llvm::StringRef Relative) { - DirectoryBasedGlobalCompilationDatabase::Options Opts(TFS); + DirectoryBasedGlobalCompilationDatabase::Options Opts(FS); return DirectoryBasedGlobalCompilationDatabase(Opts) - .getCompileCommand(FS.path(Relative)) + .getCompileCommand(testPath(Relative)) .getValueOr(tooling::CompileCommand()) .CommandLine; }; @@ -314,7 +272,8 @@ TEST(GlobalCompilationDatabaseTest, BuildDir) { } ] )cdb"; - FS.write("x/build/compile_commands.json", CDB); + FS.Files[testPath("x/build/compile_commands.json")] = + llvm::formatv(CDB, llvm::sys::path::convert_to_slash(testRoot())); EXPECT_THAT(Command("x/foo.cc"), Contains("-DXYZZY")); EXPECT_THAT(Command("bar.cc"), IsEmpty()) << "x/build/compile_flags.json only applicable to x/"; -- 2.11.0