adamcz created this revision. Herald added subscribers: usaxena95, kadircet, arphaman. adamcz requested review of this revision. Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov. Herald added a project: clang.
The BuildDir test uses RealFS and would fail when /tmp/compile_flags.txt or /tmp/compile_commands.json would exist, since it would read it for files not covered by the CDB it creates and then fail an IsEmpty() test. With this change, we create a compile_flags.txt file simply to prevent CDB from looking for it in parent directory. Long-term, all CDB implementations should use VFS and the test should use MemoryFS instead, and be fully hermetic, but for now this will do. Fixes https://github.com/clangd/clangd/issues/354 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D94321 Files: clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp Index: clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp +++ 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) {
Index: clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp +++ 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) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits