diff --git a/explorer/file_test.cpp b/explorer/file_test.cpp index a255d26b85f7..22a28f3ffd3b 100644 --- a/explorer/file_test.cpp +++ b/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 diff --git a/testing/file_test/BUILD b/testing/file_test/BUILD index db3e9d72d358..63e61ec29b93 100644 --- a/testing/file_test/BUILD +++ b/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, diff --git a/testing/file_test/file_test_base.cpp b/testing/file_test/file_test_base.cpp index a8ad338b930b..6a2c325c3fd3 100644 --- a/testing/file_test/file_test_base.cpp +++ b/testing/file_test/file_test_base.cpp @@ -51,8 +51,6 @@ ABSL_FLAG(std::vector, 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& tests) -> ErrorOr { - 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 : diff --git a/testing/file_test/file_test_base.h b/testing/file_test/file_test_base.h index 390452ab0f69..6c7433685eb5 100644 --- a/testing/file_test/file_test_base.h +++ b/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 { \ diff --git a/testing/file_test/file_test_manifest.cpp b/testing/file_test/file_test_manifest.cpp new file mode 100644 index 000000000000..9467c93e1e89 --- /dev/null +++ b/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 diff --git a/testing/file_test/rules.bzl b/testing/file_test/rules.bzl index 5bdc721bcabe..d97f9b107249 100644 --- a/testing/file_test/rules.bzl +++ b/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 )