{
std::ifstream file(path);
if (!file) {
- throw Error(fmt::format("{}: {}", path, strerror(errno)));
+ throw Error(strerror(errno));
}
return std::string(std::istreambuf_iterator<char>(file),
std::istreambuf_iterator<char>());
std::ofstream file(path,
binary ? std::ios::out | std::ios::binary : std::ios::out);
if (!file) {
- throw Error(fmt::format("{}: {}", path, strerror(errno)));
+ throw Error(strerror(errno));
}
file << data;
}
// Read file data as a string.
//
-// Throws Error on error.
+// Throws `Error` on error. The description contains the error message without
+// the path.
std::string read_file(const std::string& path);
#ifndef _WIN32
// Write file data from a string.
//
-// Throws Error on error.
+// Throws `Error` on error. The description contains the error message without
+// the path.
void write_file(const std::string& path,
const std::string& data,
bool binary = false);
atomic_file.write("hello");
}
CHECK_THROWS_WITH(Util::read_file("test"),
- Equals("test: No such file or directory"));
+ Equals("No such file or directory"));
}
Util::write_file("test", "foo\nbar\n");
std::string data = Util::read_file("test");
CHECK(data == "foo\nbar\n");
+
+ CHECK_THROWS_WITH(Util::read_file("does/not/exist"),
+ Equals("No such file or directory"));
+
+ CHECK_THROWS_WITH(Util::write_file("", "does/not/exist"),
+ Equals("No such file or directory"));
}
TEST_CASE("Util::remove_extension")