]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Let Util::write_file default to binary mode
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 17 Jul 2020 17:37:48 +0000 (19:37 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 17 Jul 2020 17:42:18 +0000 (19:42 +0200)
That makes more sense since that’s how it’s used in practice.

src/Util.hpp
unittest/test_Util.cpp

index dd37c06303e86c1c31a0bdbb5fe58b609abedbce..62a0ffabe96b7e126ed3057f87245f4782e82b63 100644 (file)
@@ -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
index 3421fcb3732bc1abb2599148ea30bbf0f198566a..72c96c73b5112bfe3869e2ca71139bdc9d6b4bab 100644 (file)
@@ -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");