mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:00:13 +01:00
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>
66 lines
2.0 KiB
C++
66 lines
2.0 KiB
C++
// 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/base/file_helpers.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <cstdlib>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <system_error>
|
|
|
|
namespace Carbon::Testing {
|
|
|
|
auto GetTempDirectory() -> std::filesystem::path {
|
|
if (char* tmpdir_env = getenv("TEST_TMPDIR"); tmpdir_env != nullptr) {
|
|
return tmpdir_env;
|
|
} else {
|
|
return std::filesystem::temp_directory_path();
|
|
}
|
|
}
|
|
|
|
auto ReadFile(std::filesystem::path path) -> ErrorOr<std::string> {
|
|
std::ifstream file_stream(path);
|
|
if (file_stream.fail()) {
|
|
return Error(llvm::formatv("Error opening file: {0}", path));
|
|
}
|
|
std::stringstream buffer;
|
|
buffer << file_stream.rdbuf();
|
|
if (file_stream.fail()) {
|
|
return Error(llvm::formatv("Error reading file: {0}", path));
|
|
}
|
|
return buffer.str();
|
|
}
|
|
|
|
auto WriteTestFile(llvm::StringRef name, llvm::StringRef contents)
|
|
-> ErrorOr<std::filesystem::path> {
|
|
std::filesystem::path test_tmpdir = GetTempDirectory();
|
|
const auto* unit_test = ::testing::UnitTest::GetInstance();
|
|
const auto* test_info = unit_test->current_test_info();
|
|
std::filesystem::path test_file =
|
|
test_tmpdir / llvm::formatv("{0}_{1}_{2}", test_info->test_suite_name(),
|
|
test_info->name(), name)
|
|
.str();
|
|
// Make debugging a bit easier by cleaning up any files from previous runs.
|
|
// This is only necessary when not run in Bazel's test environment.
|
|
std::filesystem::remove(test_file);
|
|
if (std::filesystem::exists(test_file)) {
|
|
return Error(
|
|
llvm::formatv("Unable to remove an existing file: {0}", test_file));
|
|
}
|
|
|
|
std::error_code ec;
|
|
llvm::raw_fd_ostream test_file_stream(test_file.string(), ec);
|
|
if (ec) {
|
|
return Error(llvm::formatv("Test file error: {0}", ec.message()));
|
|
}
|
|
test_file_stream << contents;
|
|
|
|
return test_file;
|
|
}
|
|
|
|
} // namespace Carbon::Testing
|