summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Rosdahl <joel@rosdahl.net>2019-09-30 21:28:20 +0200
committerJoel Rosdahl <joel@rosdahl.net>2019-10-05 23:16:46 +0200
commitd4d3b46a585bc952ca00603372d1dbc52b6a3166 (patch)
tree4910d2470ceb92e4758274bc6f0a637094dedca3
parentf21f65d59de2a31b42494f3414dc9e549df861fa (diff)
downloadccache-d4d3b46a585bc952ca00603372d1dbc52b6a3166.tar.gz
Add unit test of AtomicFile
-rw-r--r--Makefile.in1
-rw-r--r--unittest/test_AtomicFile.cpp46
2 files changed, 47 insertions, 0 deletions
diff --git a/Makefile.in b/Makefile.in
index f2c61294..117a0483 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -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
index 00000000..a45f2f3b
--- /dev/null
+++ b/unittest/test_AtomicFile.cpp
@@ -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"));
+}