diff --git a/testing/base/BUILD b/testing/base/BUILD index f19165e3f2a0..ebf326a1aa36 100644 --- a/testing/base/BUILD +++ b/testing/base/BUILD @@ -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, diff --git a/testing/base/file_helpers.cpp b/testing/base/file_helpers.cpp new file mode 100644 index 000000000000..9be7e80ff23d --- /dev/null +++ b/testing/base/file_helpers.cpp @@ -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 +#include + +namespace Carbon::Testing { + +auto ReadFile(std::filesystem::path path) -> ErrorOr { + 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 diff --git a/testing/base/file_helpers.h b/testing/base/file_helpers.h new file mode 100644 index 000000000000..db4c4098321d --- /dev/null +++ b/testing/base/file_helpers.h @@ -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 +#include + +#include "common/error.h" + +namespace Carbon::Testing { + +// Reads a file to string. +auto ReadFile(std::filesystem::path path) -> ErrorOr; + +} // namespace Carbon::Testing + +#endif // CARBON_TESTING_BASE_FILE_HELPERS_H_ diff --git a/testing/file_test/BUILD b/testing/file_test/BUILD index a82112942304..a0a0f89ab3a0 100644 --- a/testing/file_test/BUILD +++ b/testing/file_test/BUILD @@ -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", diff --git a/testing/file_test/file_test_base.cpp b/testing/file_test/file_test_base.cpp index c4d9e566ab7c..3b577206cecb 100644 --- a/testing/file_test/file_test_base.cpp +++ b/testing/file_test/file_test_base.cpp @@ -7,7 +7,6 @@ #include #include -#include #include #include #include @@ -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, 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::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 { @@ -288,7 +273,7 @@ auto FileTestBase::GetLineNumberReplacements( auto FileTestBase::ProcessTestFileAndRun(TestContext& context) -> ErrorOr { // 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)); diff --git a/toolchain/driver/BUILD b/toolchain/driver/BUILD index 72c5b7869e7d..ddcfe3534dbb 100644 --- a/toolchain/driver/BUILD +++ b/toolchain/driver/BUILD @@ -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", diff --git a/toolchain/driver/driver_test.cpp b/toolchain/driver/driver_test.cpp index 3c7188d32c75..a4fb3474ff0b 100644 --- a/toolchain/driver/driver_test.cpp +++ b/toolchain/driver/driver_test.cpp @@ -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 diff --git a/utils/tree_sitter/BUILD b/utils/tree_sitter/BUILD index 905fe4c223a0..4b6cf018bc9c 100644 --- a/utils/tree_sitter/BUILD +++ b/utils/tree_sitter/BUILD @@ -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", ], ) diff --git a/utils/tree_sitter/test_runner.cpp b/utils/tree_sitter/test_runner.cpp index 46b96f55dcec..49b25aaea39a 100644 --- a/utils/tree_sitter/test_runner.cpp +++ b/utils/tree_sitter/test_runner.cpp @@ -5,28 +5,17 @@ #include #include -#include -#include #include -#include #include #include +#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 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());