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 -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());