diff --git a/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp b/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp --- a/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp +++ b/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp @@ -298,7 +298,14 @@ .getValueOr(tooling::CompileCommand()) .CommandLine; }; - EXPECT_THAT(Command("x/foo.cc"), IsEmpty()); + + // Since we use real FS and place our files somewhere in /tmp/, an existing + // /tmp/compile_flags.txt or /tmp/compile_commands.json could affect the test. + // By creating this file we prevent CDB from looking outside of the test + // directory. + FS.write("compile_flags.txt", "-DFOO"); + + EXPECT_THAT(Command("x/foo.cc"), Contains("-DFOO")); const char *const CDB = R"cdb( [ @@ -315,9 +322,11 @@ ] )cdb"; FS.write("x/build/compile_commands.json", CDB); - EXPECT_THAT(Command("x/foo.cc"), Contains("-DXYZZY")); - EXPECT_THAT(Command("bar.cc"), IsEmpty()) - << "x/build/compile_flags.json only applicable to x/"; + EXPECT_THAT(Command("x/foo.cc"), + AllOf(Contains("-DXYZZY"), Not(Contains("-DFOO")))); + EXPECT_THAT(Command("bar.cc"), + AllOf(Contains("-DFOO"), Not(Contains("-DXYZZY")))) + << "x/build/compile_commands.json only applicable to x/"; } TEST(GlobalCompilationDatabaseTest, NonCanonicalFilenames) {