Share ReadFile in tests (#4967)

Small cleanup for code sharing. Note, `*ReadFile` will trigger a
CHECK-failure on error; the relevant implementations would previously
have failed silently (empty string).

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
Jon Ross-Perkins
2025-02-15 00:17:01 +00:00
committed by GitHub
co-authored by Richard Smith
parent d4f15ab26e
commit 38d25cf622
9 changed files with 65 additions and 41 deletions
+8
View File
@@ -91,6 +91,14 @@ cc_binary(
],
)
cc_library(
name = "file_helpers",
testonly = 1,
srcs = ["file_helpers.cpp"],
hdrs = ["file_helpers.h"],
deps = ["//common:error"],
)
cc_library(
name = "global_exe_path",
testonly = 1,
+25
View File
@@ -0,0 +1,25 @@
// 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 <fstream>
#include <sstream>
namespace Carbon::Testing {
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();
}
} // namespace Carbon::Testing
+20
View File
@@ -0,0 +1,20 @@
// 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
#ifndef CARBON_TESTING_BASE_FILE_HELPERS_H_
#define CARBON_TESTING_BASE_FILE_HELPERS_H_
#include <filesystem>
#include <string>
#include "common/error.h"
namespace Carbon::Testing {
// Reads a file to string.
auto ReadFile(std::filesystem::path path) -> ErrorOr<std::string>;
} // namespace Carbon::Testing
#endif // CARBON_TESTING_BASE_FILE_HELPERS_H_
+1
View File
@@ -39,6 +39,7 @@ cc_library(
"//common:init_llvm",
"//common:ostream",
"//common:raw_string_ostream",
"//testing/base:file_helpers",
"@abseil-cpp//absl/flags:flag",
"@abseil-cpp//absl/flags:parse",
"@googletest//:gtest",
+2 -17
View File
@@ -7,7 +7,6 @@
#include <gmock/gmock.h>
#include <filesystem>
#include <fstream>
#include <optional>
#include <string>
#include <utility>
@@ -28,6 +27,7 @@
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/ThreadPool.h"
#include "testing/base/file_helpers.h"
#include "testing/file_test/autoupdate.h"
ABSL_FLAG(std::vector<std::string>, file_tests, {},
@@ -60,21 +60,6 @@ using ::testing::StrEq;
static constexpr llvm::StringLiteral StdinFilename = "STDIN";
// Reads a file to string.
static auto ReadFile(std::string_view path) -> ErrorOr<std::string> {
std::ifstream proto_file{std::string(path)};
if (proto_file.fail()) {
return Error(llvm::formatv("Error opening file: {0}", path));
}
std::stringstream buffer;
buffer << proto_file.rdbuf();
if (proto_file.fail()) {
return Error(llvm::formatv("Error reading file: {0}", path));
}
proto_file.close();
return buffer.str();
}
// Splits outputs to string_view because gtest handles string_view by default.
static auto SplitOutput(llvm::StringRef output)
-> llvm::SmallVector<std::string_view> {
@@ -288,7 +273,7 @@ auto FileTestBase::GetLineNumberReplacements(
auto FileTestBase::ProcessTestFileAndRun(TestContext& context)
-> ErrorOr<Success> {
// Store the file so that test_files can use references to content.
CARBON_ASSIGN_OR_RETURN(context.input_content, ReadFile(test_name_));
CARBON_ASSIGN_OR_RETURN(context.input_content, ReadFile(test_name_.str()));
// Load expected output.
CARBON_RETURN_IF_ERROR(ProcessTestFile(context));
+1
View File
@@ -153,6 +153,7 @@ cc_test(
":driver",
"//common:all_llvm_targets",
"//common:raw_string_ostream",
"//testing/base:file_helpers",
"//testing/base:global_exe_path",
"//testing/base:gtest_main",
"//toolchain/diagnostics:diagnostic_emitter",
+2 -11
View File
@@ -15,6 +15,7 @@
#include "llvm/ADT/ScopeExit.h"
#include "llvm/Object/Binary.h"
#include "llvm/Support/FormatVariadic.h"
#include "testing/base/file_helpers.h"
#include "testing/base/global_exe_path.h"
#include "toolchain/testing/yaml_test_helpers.h"
@@ -28,16 +29,6 @@ using ::testing::StrEq;
namespace Yaml = ::Carbon::Testing::Yaml;
// Reads a file to string.
// TODO: Extract this to a helper and share it with other tests.
static auto ReadFile(std::filesystem::path path) -> std::string {
std::ifstream proto_file(path);
std::stringstream buffer;
buffer << proto_file.rdbuf();
proto_file.close();
return buffer.str();
}
class DriverTest : public testing::Test {
public:
DriverTest()
@@ -222,7 +213,7 @@ TEST_F(DriverTest, FileOutput) {
.success);
EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
// TODO: This may need to be tailored to other assembly formats.
EXPECT_THAT(ReadFile("test.s"), ContainsRegex("Main:"));
EXPECT_THAT(*Testing::ReadFile("test.s"), ContainsRegex("Main:"));
}
} // namespace
+4
View File
@@ -62,6 +62,7 @@ cc_binary(
tags = ["manual"],
deps = [
":parser",
"//testing/base:file_helpers",
],
)
@@ -74,6 +75,7 @@ cc_test(
tags = ["manual"],
deps = [
":parser",
"//testing/base:file_helpers",
],
)
@@ -99,6 +101,7 @@ cc_test(
tags = ["manual"],
deps = [
":parser",
"//testing/base:file_helpers",
],
)
@@ -114,5 +117,6 @@ cc_test(
tags = ["manual"],
deps = [
":parser",
"//testing/base:file_helpers",
],
)
+2 -13
View File
@@ -5,28 +5,17 @@
#include <tree_sitter/api.h>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "testing/base/file_helpers.h"
#include "utils/tree_sitter/src/tree_sitter/parser.h"
extern "C" {
auto tree_sitter_carbon() -> TSLanguage*;
}
// Reads a file to string.
static auto ReadFile(std::filesystem::path path) -> std::string {
std::ifstream file(path);
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
return buffer.str();
}
// TODO: use file_test.cpp
auto main(int argc, char** argv) -> int {
if (argc < 2) {
@@ -42,7 +31,7 @@ auto main(int argc, char** argv) -> int {
std::vector<std::string> incorrect;
for (int i = 1; i < argc; i++) {
std::string file_path = argv[i];
std::string source = ReadFile(file_path);
std::string source = std::move(*Carbon::Testing::ReadFile(file_path));
auto* tree =
ts_parser_parse_string(parser, nullptr, source.data(), source.size());