From: Joel Rosdahl Date: Mon, 30 Sep 2019 19:28:20 +0000 (+0200) Subject: Add unit test of AtomicFile X-Git-Tag: v4.0~758 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d4d3b46a585bc952ca00603372d1dbc52b6a3166;p=thirdparty%2Fccache.git Add unit test of AtomicFile --- diff --git a/Makefile.in b/Makefile.in index f2c61294e..117a0483b 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 000000000..a45f2f3be --- /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 + +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{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")); +}