From d306a41453a20e6bf3fdda879351a4a5bf67b79c Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Thu, 5 Oct 2023 11:50:41 -0700 Subject: [PATCH] Allow our test runners to be more easily run outside bazel. (#3269) Don't assume that the bazel-supplied environment variable TEST_TARGET is present. We don't actually need it for anything other than providing feedback to the developer if the test fails. This makes it a bit easier to reproduce test failures under gdb, particularly for multi-file tests. --- testing/file_test/file_test_base.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/testing/file_test/file_test_base.cpp b/testing/file_test/file_test_base.cpp index c5eb7f3783ab..14113a256ea7 100644 --- a/testing/file_test/file_test_base.cpp +++ b/testing/file_test/file_test_base.cpp @@ -60,15 +60,21 @@ static auto SplitOutput(llvm::StringRef output) // Runs a test and compares output. This keeps output split by line so that // issues are a little easier to identify by the different line. auto FileTestBase::TestBody() -> void { - const char* target = getenv("TEST_TARGET"); - CARBON_CHECK(target); - // This advice overrides the --file_tests flag provided by the file_test rule. - llvm::errs() << "\nTo test this file alone, run:\n bazel test " << target - << " --test_arg=--file_tests=" << test_name_ << "\n\n"; + std::optional stack_trace_entry; - // Add a crash trace entry with a command that runs this test in isolation. - llvm::PrettyStackTraceFormat stack_trace_entry( - "bazel test %s --test_arg=--file_tests=%s", target, test_name_); + // If we're being run from bazel, provide some assistance for understanding + // and reproducing failures. + const char* target = getenv("TEST_TARGET"); + if (target) { + // This advice overrides the --file_tests flag provided by the file_test + // rule. + llvm::errs() << "\nTo test this file alone, run:\n bazel test " << target + << " --test_arg=--file_tests=" << test_name_ << "\n\n"; + + // Add a crash trace entry with a command that runs this test in isolation. + stack_trace_entry.emplace("bazel test %s --test_arg=--file_tests=%s", + target, test_name_); + } TestContext context; auto run_result = ProcessTestFileAndRun(context); @@ -83,7 +89,7 @@ auto FileTestBase::TestBody() -> void { // Check results. Include a reminder of the autoupdate command for any // stdout/stderr differences. std::string update_message; - if (context.autoupdate_line_number) { + if (target && context.autoupdate_line_number) { update_message = llvm::formatv( "If these differences are expected, try the autoupdater:\n" "\tbazel run {0} -- --autoupdate --file_tests={1}",