]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add unit test of AtomicFile
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 30 Sep 2019 19:28:20 +0000 (21:28 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 5 Oct 2019 21:16:46 +0000 (23:16 +0200)
Makefile.in
unittest/test_AtomicFile.cpp [new file with mode: 0644]

index f2c61294e672455245fc65e646b5300cbe4d3a18..117a0483bc3a2178103e0db7d2fe47aa4de0192f 100644 (file)
@@ -74,6 +74,7 @@ non_third_party_objs = $(patsubst %.c, %.o, $(patsubst %.cpp, %.o, $(non_third_p
 ccache_sources = src/main.cpp $(base_sources)
 ccache_objs = $(patsubst %.c, %.o, $(patsubst %.cpp, %.o, $(ccache_sources)))
 
+test_suites += unittest/test_AtomicFile.cpp
 test_suites += unittest/test_Checksum.cpp
 test_suites += unittest/test_Config.cpp
 test_suites += unittest/test_Util.cpp
diff --git a/unittest/test_AtomicFile.cpp b/unittest/test_AtomicFile.cpp
new file mode 100644 (file)
index 0000000..a45f2f3
--- /dev/null
@@ -0,0 +1,46 @@
+// Copyright (C) 2011-2019 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#include "../src/AtomicFile.hpp"
+#include "../src/Util.hpp"
+
+#include <catch.hpp>
+
+using Catch::Equals;
+
+TEST_CASE("Base case")
+{
+  unlink("test");
+  AtomicFile atomic_file("test", AtomicFile::Mode::text);
+  atomic_file.write("h");
+  atomic_file.write(std::vector<uint8_t>{0x65, 0x6c});
+  fputs("lo", atomic_file.stream());
+  atomic_file.commit();
+  CHECK(Util::read_file("test") == "hello");
+}
+
+TEST_CASE("Not committing")
+{
+  unlink("test");
+  {
+    AtomicFile atomic_file("test", AtomicFile::Mode::text);
+    atomic_file.write("hello");
+  }
+  CHECK_THROWS_WITH(Util::read_file("test"),
+                    Equals("test: No such file or directory"));
+}