Просмотр исходного кода

Change manifest passing to drop the flag outside explorer (#5025)

Jon Ross-Perkins 1 год назад
Родитель
Сommit
46752eeed6

+ 7 - 0
explorer/file_test.cpp

@@ -12,6 +12,8 @@ ABSL_FLAG(bool, trace, false,
           "Set to true to run tests with tracing enabled, even if they don't "
           "otherwise specify it. This does not result in checking trace output "
           "contents; it essentially only verifies there's not a crash bug.");
+ABSL_FLAG(std::string, explorer_test_targets_file, "",
+          "A path to a file containing repo-relative names of test files.");
 
 namespace Carbon::Testing {
 namespace {
@@ -112,6 +114,11 @@ class ExplorerFileTest : public FileTestBase {
 
 }  // namespace
 
+// Explorer uses a non-standard approach to getting the manifest path.
+auto GetFileTestManifestPath() -> std::filesystem::path {
+  return absl::GetFlag(FLAGS_explorer_test_targets_file);
+}
+
 CARBON_FILE_TEST_FACTORY(ExplorerFileTest)
 
 }  // namespace Carbon::Testing

+ 3 - 0
testing/file_test/BUILD

@@ -7,6 +7,9 @@ load("rules.bzl", "file_test")
 
 package(default_visibility = ["//visibility:public"])
 
+# Used to access a `#define` with the manifest file, produced by rules.bzl.
+exports_files(["file_test_manifest.cpp"])
+
 cc_library(
     name = "autoupdate",
     testonly = 1,

+ 2 - 6
testing/file_test/file_test_base.cpp

@@ -51,8 +51,6 @@ ABSL_FLAG(std::vector<std::string>, file_tests, {},
           "A comma-separated list of repo-relative names of test files. "
           "Similar to and overrides `--gtest_filter`, but doesn't require the "
           "test class name to be known.");
-ABSL_FLAG(std::string, test_targets_file, "",
-          "A path to a file containing repo-relative names of test files.");
 ABSL_FLAG(bool, autoupdate, false,
           "Instead of verifying files match test output, autoupdate files "
           "based on test output.");
@@ -300,11 +298,9 @@ static auto RegisterTests(FileTestFactory* test_factory,
                           llvm::StringRef exe_path,
                           llvm::SmallVectorImpl<FileTestInfo>& tests)
     -> ErrorOr<Success> {
-  if (absl::GetFlag(FLAGS_test_targets_file).empty()) {
-    return Error("Missing --test_targets_file.");
-  }
+  GetFileTestManifestPath();
   CARBON_ASSIGN_OR_RETURN(auto test_manifest,
-                          ReadFile(absl::GetFlag(FLAGS_test_targets_file)));
+                          ReadFile(GetFileTestManifestPath()));
 
   // Prepare the vector first, so that the location of entries won't change.
   for (const auto& test_name :

+ 5 - 0
testing/file_test/file_test_base.h

@@ -133,6 +133,11 @@ struct FileTestFactory {
 // to implement this function.
 extern auto GetFileTestFactory() -> FileTestFactory;
 
+// Returns the manifest path, which is provided by rules.bzl and
+// file_test_manifest.cpp. This is exposed so that the explorer sharding
+// approach can use a different implementation.
+auto GetFileTestManifestPath() -> std::filesystem::path;
+
 // Provides a standard GetFileTestFactory implementation.
 #define CARBON_FILE_TEST_FACTORY(Name)                                       \
   auto GetFileTestFactory() -> FileTestFactory {                             \

+ 13 - 0
testing/file_test/file_test_manifest.cpp

@@ -0,0 +1,13 @@
+// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+// Exceptions. See /LICENSE for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+#include "testing/file_test/file_test_base.h"
+
+namespace Carbon::Testing {
+
+auto GetFileTestManifestPath() -> std::filesystem::path {
+  return CARBON_FILE_TEST_MANIFEST;
+}
+
+}  // namespace Carbon::Testing

+ 9 - 2
testing/file_test/rules.bzl

@@ -15,6 +15,7 @@ load("//bazel/manifest:defs.bzl", "manifest")
 def file_test(
         name,
         tests,
+        srcs = [],
         data = [],
         args = [],
         prebuilt_binary = None,
@@ -28,6 +29,7 @@ def file_test(
     Args:
       name: The base name of the tests.
       tests: The list of test files to use as data, typically a glob.
+      srcs: Passed to cc_test.
       data: Passed to cc_test.
       args: Passed to cc_test.
       prebuilt_binary: If set, specifies a prebuilt test binary to use instead
@@ -42,13 +44,16 @@ def file_test(
         srcs = tests,
         testonly = 1,
     )
-    args = ["--test_targets_file=$(rootpath :{0})".format(tests_file)] + args
     data = [":" + tests_file] + tests + data
 
     if prebuilt_binary:
+        # TODO: The prebuilt_binary support is only used by explorer. We should
+        # remove this once explorer is removed, and think about better factoring
+        # approaches if we need it later for toolchain.
+        args = ["--explorer_test_targets_file=$(rootpath :{0})".format(tests_file)] + args
         native.sh_test(
             name = name,
-            srcs = [prebuilt_binary],
+            srcs = srcs + [prebuilt_binary],
             data = data,
             args = args,
             env = cc_env(),
@@ -57,8 +62,10 @@ def file_test(
     else:
         cc_test(
             name = name,
+            srcs = srcs + ["//testing/file_test:file_test_manifest.cpp"],
             data = data,
             args = args,
             env = cc_env(),
+            local_defines = ["CARBON_FILE_TEST_MANIFEST='\"$(rootpath :{0})\"'".format(tests_file)],
             **kwargs
         )