mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
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:
co-authored by
Richard Smith
parent
d4f15ab26e
commit
38d25cf622
@@ -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
|
||||
Reference in New Issue
Block a user