Skip to content

Commit 31732e6

Browse files
committed
[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 clangd/clangd#354 Reviewed By: adamcz, sammccall Differential Revision: https://reviews.llvm.org/D94359
1 parent 9751705 commit 31732e6

File tree

1 file changed

+17
-58
lines changed

1 file changed

+17
-58
lines changed

clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp

Lines changed: 17 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -169,48 +169,6 @@ TEST_F(OverlayCDBTest, Adjustments) {
169169
"-DFallback", "-DAdjust_baz.cc"));
170170
}
171171

172-
// Allows placement of files for tests and cleans them up after.
173-
// FIXME: GlobalCompilationDatabase is mostly VFS-clean now, switch to MockFS?
174-
class ScratchFS {
175-
llvm::SmallString<128> Root;
176-
177-
public:
178-
ScratchFS() {
179-
EXPECT_FALSE(llvm::sys::fs::createUniqueDirectory("clangd-cdb-test", Root))
180-
<< "Failed to create unique directory";
181-
}
182-
183-
~ScratchFS() {
184-
EXPECT_FALSE(llvm::sys::fs::remove_directories(Root))
185-
<< "Failed to cleanup " << Root;
186-
}
187-
188-
llvm::StringRef root() const { return Root; }
189-
190-
void write(PathRef RelativePath, llvm::StringRef Contents) {
191-
std::string AbsPath = path(RelativePath);
192-
EXPECT_FALSE(llvm::sys::fs::create_directories(
193-
llvm::sys::path::parent_path(AbsPath)))
194-
<< "Failed to create directories for: " << AbsPath;
195-
196-
std::error_code EC;
197-
llvm::raw_fd_ostream OS(AbsPath, EC);
198-
EXPECT_FALSE(EC) << "Failed to open " << AbsPath << " for writing";
199-
OS << llvm::formatv(Contents.data(),
200-
llvm::sys::path::convert_to_slash(Root));
201-
OS.close();
202-
203-
EXPECT_FALSE(OS.has_error());
204-
}
205-
206-
std::string path(PathRef RelativePath) const {
207-
llvm::SmallString<128> AbsPath(Root);
208-
llvm::sys::path::append(AbsPath, RelativePath);
209-
llvm::sys::path::native(AbsPath);
210-
return AbsPath.str().str();
211-
}
212-
};
213-
214172
TEST(GlobalCompilationDatabaseTest, DiscoveryWithNestedCDBs) {
215173
const char *const CDBOuter =
216174
R"cdb(
@@ -242,59 +200,59 @@ TEST(GlobalCompilationDatabaseTest, DiscoveryWithNestedCDBs) {
242200
}
243201
]
244202
)cdb";
245-
ScratchFS FS;
246-
RealThreadsafeFS TFS;
247-
FS.write("compile_commands.json", CDBOuter);
248-
FS.write("build/compile_commands.json", CDBInner);
203+
MockFS FS;
204+
FS.Files[testPath("compile_commands.json")] =
205+
llvm::formatv(CDBOuter, llvm::sys::path::convert_to_slash(testRoot()));
206+
FS.Files[testPath("build/compile_commands.json")] =
207+
llvm::formatv(CDBInner, llvm::sys::path::convert_to_slash(testRoot()));
249208

250209
// Note that gen2.cc goes missing with our following model, not sure this
251210
// happens in practice though.
252211
{
253-
DirectoryBasedGlobalCompilationDatabase DB(TFS);
212+
DirectoryBasedGlobalCompilationDatabase DB(FS);
254213
std::vector<std::string> DiscoveredFiles;
255214
auto Sub =
256215
DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
257216
DiscoveredFiles = Changes;
258217
});
259218

260-
DB.getCompileCommand(FS.path("build/../a.cc"));
219+
DB.getCompileCommand(testPath("build/../a.cc"));
261220
EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(AllOf(
262221
EndsWith("a.cc"), Not(HasSubstr("..")))));
263222
DiscoveredFiles.clear();
264223

265-
DB.getCompileCommand(FS.path("build/gen.cc"));
224+
DB.getCompileCommand(testPath("build/gen.cc"));
266225
EXPECT_THAT(DiscoveredFiles, UnorderedElementsAre(EndsWith("gen.cc")));
267226
}
268227

269228
// With a custom compile commands dir.
270229
{
271-
DirectoryBasedGlobalCompilationDatabase::Options Opts(TFS);
272-
Opts.CompileCommandsDir = FS.root().str();
230+
DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
231+
Opts.CompileCommandsDir = testRoot();
273232
DirectoryBasedGlobalCompilationDatabase DB(Opts);
274233
std::vector<std::string> DiscoveredFiles;
275234
auto Sub =
276235
DB.watch([&DiscoveredFiles](const std::vector<std::string> Changes) {
277236
DiscoveredFiles = Changes;
278237
});
279238

280-
DB.getCompileCommand(FS.path("a.cc"));
239+
DB.getCompileCommand(testPath("a.cc"));
281240
EXPECT_THAT(DiscoveredFiles,
282241
UnorderedElementsAre(EndsWith("a.cc"), EndsWith("gen.cc"),
283242
EndsWith("gen2.cc")));
284243
DiscoveredFiles.clear();
285244

286-
DB.getCompileCommand(FS.path("build/gen.cc"));
245+
DB.getCompileCommand(testPath("build/gen.cc"));
287246
EXPECT_THAT(DiscoveredFiles, IsEmpty());
288247
}
289248
}
290249

291250
TEST(GlobalCompilationDatabaseTest, BuildDir) {
292-
ScratchFS FS;
293-
RealThreadsafeFS TFS;
251+
MockFS FS;
294252
auto Command = [&](llvm::StringRef Relative) {
295-
DirectoryBasedGlobalCompilationDatabase::Options Opts(TFS);
253+
DirectoryBasedGlobalCompilationDatabase::Options Opts(FS);
296254
return DirectoryBasedGlobalCompilationDatabase(Opts)
297-
.getCompileCommand(FS.path(Relative))
255+
.getCompileCommand(testPath(Relative))
298256
.getValueOr(tooling::CompileCommand())
299257
.CommandLine;
300258
};
@@ -314,7 +272,8 @@ TEST(GlobalCompilationDatabaseTest, BuildDir) {
314272
}
315273
]
316274
)cdb";
317-
FS.write("x/build/compile_commands.json", CDB);
275+
FS.Files[testPath("x/build/compile_commands.json")] =
276+
llvm::formatv(CDB, llvm::sys::path::convert_to_slash(testRoot()));
318277
EXPECT_THAT(Command("x/foo.cc"), Contains("-DXYZZY"));
319278
EXPECT_THAT(Command("bar.cc"), IsEmpty())
320279
<< "x/build/compile_flags.json only applicable to x/";

0 commit comments

Comments
 (0)