Make the file_test binary work without custom environment variables. (#5442)

Instead of crashing when run outside of `bazel`, make the toolchain's
`file_test` binary work properly when no test-specific environment
variables are set. This makes it a lot easier to run `file_test` under a
debugger.

There are two main changes here:

- Don't crash if `$TEST_TMPDIR` is unset. Instead, fall back to LLVM's
temporary directory (typically `$TMPDIR`). We already did this in some
places in tests. We now do it in more places.
- Don't fall back to a target label of `<target>` in the reproduction
commands if `$TEST_TARGET` is unset, because this causes all the tests
to fail because their output doesn't match the expected output due to a
differing bazel run command. Instead explicitly specify the target from
the `FileTestBase`-derived class.

Infrastructure for this has been added generally, but only rolled out to
the toolchain `file_test` binary for now.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
Richard Smith
2025-05-07 22:28:13 +00:00
committed by GitHub
co-authored by Jon Ross-Perkins
parent f2a16d8742
commit 66caff2c26
9 changed files with 61 additions and 40 deletions
+5 -5
View File
@@ -16,6 +16,7 @@
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/FormatVariadic.h"
#include "testing/base/file_helpers.h"
namespace Carbon::Testing {
@@ -149,10 +150,9 @@ auto FileTestAutoupdater::BuildCheckLines(llvm::StringRef output,
file_to_number_map.insert({name, number});
}
// %t substitution means we may see TEST_TMPDIR in output.
char* tmpdir_env = getenv("TEST_TMPDIR");
CARBON_CHECK(tmpdir_env != nullptr);
llvm::StringRef tmpdir = tmpdir_env;
// %t substitution means we may see the temporary directory's path in output.
std::filesystem::path tmpdir_path = GetTempDirectory();
llvm::StringRef tmpdir = tmpdir_path.native();
llvm::SmallVector<llvm::StringRef> lines(llvm::split(output, '\n'));
// It's typical that output ends with a newline, but we don't want to add a
@@ -190,7 +190,7 @@ auto FileTestAutoupdater::BuildCheckLines(llvm::StringRef output,
check_line.append("{{}}");
}
// Ignore TEST_TMPDIR in output.
// Ignore mentions of the temporary directory in output.
if (auto pos = check_line.find(tmpdir); pos != std::string::npos) {
check_line.replace(pos, tmpdir.size(), "{{.+}}");
}