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
+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