From: Joel Rosdahl Date: Fri, 17 Jul 2020 17:37:48 +0000 (+0200) Subject: Let Util::write_file default to binary mode X-Git-Tag: v4.0~318 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=46781e6f7ecea09184cb9f2e54371faa172c51de;p=thirdparty%2Fccache.git Let Util::write_file default to binary mode That makes more sense since that’s how it’s used in practice. --- diff --git a/src/Util.hpp b/src/Util.hpp index dd37c0630..62a0ffabe 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -352,14 +352,14 @@ bool unlink_tmp(const std::string& path, // Throws Error on error. void wipe_path(const std::string& path); -// Write file data from a string. The file will be opened according to -// `open_mode`, which always will include `std::ios::out` even if not specified -// at the call site. +// Write `data` to `path`. The file will be opened according to `open_mode`, +// which always will include `std::ios::out` even if not specified at the call +// site. // // Throws `Error` on error. The description contains the error message without // the path. void write_file(const std::string& path, const std::string& data, - std::ios_base::openmode open_mode = std::ios::out); + std::ios_base::openmode open_mode = std::ios::binary); } // namespace Util diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index 3421fcb37..72c96c73b 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -532,6 +532,17 @@ TEST_CASE("Util::read_file and Util::write_file") data = Util::read_file("test"); CHECK(data == "carpet"); + Util::write_file("test", "\n", std::ios::app | std::ios::binary); + data = Util::read_file("test"); + CHECK(data == "carpet\n"); + + Util::write_file("test", "\n", std::ios::app); // text mode + data = Util::read_file("test"); +#ifdef _WIN32 + CHECK(data == "carpet\n\r\n"); +#else + CHECK(data == "carpet\n\n"); +#endif CHECK_THROWS_WITH(Util::read_file("does/not/exist"), "No such file or directory");