[PATCH] D94321: [clangd] Prevent tests from failing when /tmp/compile_flags.txt exists

Adam Czachorowski via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 8 10:16:53 PST 2021


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) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94321.315437.patch
Type: text/x-patch
Size: 1443 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210108/e2561d47/attachment.bin>


More information about the cfe-commits mailing list