]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add TestContext to test cases that create local files
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 10 May 2020 17:28:00 +0000 (19:28 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 13 May 2020 19:26:20 +0000 (21:26 +0200)
unittest/test_Args.cpp
unittest/test_AtomicFile.cpp
unittest/test_Config.cpp
unittest/test_Lockfile.cpp
unittest/test_NullCompression.cpp
unittest/test_Stat.cpp
unittest/test_Util.cpp
unittest/test_ZstdCompression.cpp
unittest/test_argprocessing.cpp
unittest/test_hashutil.cpp

index 49f2a28b60406966c623f5e37345d096c5e7a9d6..55fe72280871f7af315bf35e69d5031166627329 100644 (file)
 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 #include "../src/Args.hpp"
+#include "TestUtil.hpp"
 
 #include "third_party/catch.hpp"
 
+using TestUtil::TestContext;
+
 TEST_CASE("Args default constructor")
 {
   Args args;
@@ -73,6 +76,8 @@ TEST_CASE("Args::from_string")
 
 TEST_CASE("Args::from_gcc_atfile")
 {
+  TestContext testContext;
+
   Args args;
 
   SECTION("Non-existing file")
index 71b19234fb399511822756a0942e02a3f86bae14..22a35d7f33ea7e8aecc08ae51ce9fe194b7fc488 100644 (file)
 
 #include "../src/AtomicFile.hpp"
 #include "../src/Util.hpp"
+#include "TestUtil.hpp"
 
 #include "third_party/catch.hpp"
 
 using Catch::Equals;
+using TestUtil::TestContext;
 
 TEST_CASE("Base case")
 {
-  unlink("test");
+  TestContext test_context;
+
   AtomicFile atomic_file("test", AtomicFile::Mode::text);
   atomic_file.write("h");
   atomic_file.write(std::vector<uint8_t>{0x65, 0x6c});
@@ -36,7 +39,8 @@ TEST_CASE("Base case")
 
 TEST_CASE("Not committing")
 {
-  unlink("test");
+  TestContext test_context;
+
   {
     AtomicFile atomic_file("test", AtomicFile::Mode::text);
     atomic_file.write("hello");
index 014b28a310e308e5748059b8c475a03bef5d490e..255d39a6eb18f736952a0d20e7cb24a423bbb508 100644 (file)
@@ -20,6 +20,7 @@
 #include "../src/Util.hpp"
 #include "../src/ccache.hpp"
 #include "../src/exceptions.hpp"
+#include "TestUtil.hpp"
 
 #include "third_party/catch.hpp"
 #include "third_party/fmt/core.h"
@@ -29,6 +30,7 @@
 #include <vector>
 
 using Catch::Equals;
+using TestUtil::TestContext;
 
 TEST_CASE("Config: default values")
 {
@@ -71,6 +73,8 @@ TEST_CASE("Config: default values")
 
 TEST_CASE("Config::update_from_file")
 {
+  TestContext test_context;
+
   const char user[] = "rabbit";
   x_setenv("USER", user);
 
@@ -165,8 +169,9 @@ TEST_CASE("Config::update_from_file")
 
 TEST_CASE("Config::update_from_file, error handling")
 {
+  TestContext test_context;
+
   Config config;
-  unlink("ccache.conf"); // Make sure it doesn't exist.
 
   SECTION("missing equal sign")
   {
@@ -296,6 +301,8 @@ TEST_CASE("Config::update_from_environment")
 
 TEST_CASE("Config::set_value_in_file")
 {
+  TestContext test_context;
+
   SECTION("set new value")
   {
     Util::write_file("ccache.conf", "path = vanilla\n");
@@ -358,6 +365,8 @@ TEST_CASE("Config::get_string_value")
 
 TEST_CASE("Config::visit_items")
 {
+  TestContext test_context;
+
   Util::write_file(
     "test.conf",
 #ifndef _WIN32
index cbd61b789327d244f0e7be2b79ce9dfa211bc9b1..0c132eb0663025c284f933801a1b06174997f388 100644 (file)
 
 #include "../src/Lockfile.hpp"
 #include "../src/Stat.hpp"
+#include "TestUtil.hpp"
 
 #include "third_party/catch.hpp"
 
+using TestUtil::TestContext;
+
 TEST_CASE("Lockfile acquire and release")
 {
+  TestContext test_context;
+
   {
     Lockfile lock("test", 1000);
     CHECK(lock.acquired());
@@ -43,6 +48,8 @@ TEST_CASE("Lockfile acquire and release")
 #ifndef _WIN32
 TEST_CASE("Lockfile breaking")
 {
+  TestContext test_context;
+
   CHECK(symlink("foo", "test.lock") == 0);
 
   Lockfile lock("test", 1000);
index 0d767545aab3ca452787417d599aa696f7714a9f..cdcd1cbc01cfc32aad3a7ffa91964806ae9011a2 100644 (file)
 #include "../src/Compressor.hpp"
 #include "../src/Decompressor.hpp"
 #include "../src/File.hpp"
+#include "TestUtil.hpp"
 
 #include "third_party/catch.hpp"
 
 using Catch::Equals;
+using TestUtil::TestContext;
 
 TEST_CASE("Compression::Type::none roundtrip")
 {
+  TestContext test_context;
+
   File f("data.uncompressed", "w");
   auto compressor =
     Compressor::create_from_type(Compression::Type::none, f.get(), 1);
index ba140bd8a25464241c6ea8d18029831e8adbafb4..8054e62d3ada7a1b9d89cbcdb6ec7f76091f8168 100644 (file)
 
 #include "../src/Stat.hpp"
 #include "../src/Util.hpp"
+#include "TestUtil.hpp"
 
 #include "third_party/catch.hpp"
 
 #include <unistd.h>
 
 using Catch::Equals;
+using TestUtil::TestContext;
 
 TEST_CASE("Default constructor")
 {
@@ -54,6 +56,8 @@ TEST_CASE("Named constructors")
 
 TEST_CASE("Same i-node as")
 {
+  TestContext test_context;
+
   Util::write_file("a", "");
   Util::write_file("b", "");
   auto a_stat = Stat::stat("a");
@@ -90,6 +94,8 @@ TEST_CASE("Return values when file is missing")
 
 TEST_CASE("Return values when file exists")
 {
+  TestContext test_context;
+
   Util::write_file("file", "1234567");
 
   auto stat = Stat::stat("file");
@@ -116,7 +122,8 @@ TEST_CASE("Return values when file exists")
 
 TEST_CASE("Directory")
 {
-  rmdir("directory");
+  TestContext test_context;
+
   REQUIRE(mkdir("directory", 0456) == 0);
   auto stat = Stat::stat("directory");
 
@@ -130,6 +137,8 @@ TEST_CASE("Directory")
 #ifndef _WIN32
 TEST_CASE("Symlinks")
 {
+  TestContext test_context;
+
   Util::write_file("file", "1234567");
 
   SECTION("file lstat")
@@ -154,7 +163,6 @@ TEST_CASE("Symlinks")
 
   SECTION("symlink lstat")
   {
-    unlink("symlink");
     REQUIRE(symlink("file", "symlink") == 0);
     auto stat = Stat::lstat("symlink", Stat::OnError::ignore);
     CHECK(stat);
@@ -166,7 +174,6 @@ TEST_CASE("Symlinks")
 
   SECTION("symlink stat")
   {
-    unlink("symlink");
     REQUIRE(symlink("file", "symlink") == 0);
     auto stat = Stat::stat("symlink", Stat::OnError::ignore);
     CHECK(stat);
index 28fed2133cf4250c333b864da0d9159402c130ef..ade6875bce7fa47cb493494dc79e2f369749de94 100644 (file)
 
 #include "../src/Config.hpp"
 #include "../src/Util.hpp"
+#include "TestUtil.hpp"
 
 #include "third_party/catch.hpp"
 
 using Catch::EndsWith;
 using Catch::Equals;
+using TestUtil::TestContext;
 
 TEST_CASE("Util::base_name")
 {
@@ -106,6 +108,8 @@ TEST_CASE("Util::common_dir_prefix_length")
 
 TEST_CASE("Util::create_dir")
 {
+  TestContext test_context;
+
   CHECK(Util::create_dir("/"));
 
   CHECK(Util::create_dir("create/dir"));
@@ -191,6 +195,8 @@ TEST_CASE("Util::get_extension")
 
 TEST_CASE("Util::get_level_1_files")
 {
+  TestContext test_context;
+
   Util::create_dir("e/m/p/t/y");
 
   Util::create_dir("0/1");
@@ -470,6 +476,8 @@ TEST_CASE("Util::parse_int")
 
 TEST_CASE("Util::read_file and Util::write_file")
 {
+  TestContext test_context;
+
   Util::write_file("test", "foo\nbar\n");
   std::string data = Util::read_file("test");
   CHECK(data == "foo\nbar\n");
@@ -617,12 +625,14 @@ TEST_CASE("Util::to_lowercase")
 
 TEST_CASE("Util::traverse")
 {
-  REQUIRE(Util::create_dir("traverse/dir-with-subdir-and-file/subdir"));
-  Util::write_file("traverse/dir-with-subdir-and-file/subdir/f", "");
-  REQUIRE(Util::create_dir("traverse/dir-with-files"));
-  Util::write_file("traverse/dir-with-files/f1", "");
-  Util::write_file("traverse/dir-with-files/f2", "");
-  REQUIRE(Util::create_dir("traverse/empty-dir"));
+  TestContext test_context;
+
+  REQUIRE(Util::create_dir("dir-with-subdir-and-file/subdir"));
+  Util::write_file("dir-with-subdir-and-file/subdir/f", "");
+  REQUIRE(Util::create_dir("dir-with-files"));
+  Util::write_file("dir-with-files/f1", "");
+  Util::write_file("dir-with-files/f2", "");
+  REQUIRE(Util::create_dir("empty-dir"));
 
   std::vector<std::string> visited;
   auto visitor = [&visited](const std::string& path, bool is_dir) {
@@ -638,42 +648,43 @@ TEST_CASE("Util::traverse")
 
   SECTION("traverse file")
   {
-    CHECK_NOTHROW(
-      Util::traverse("traverse/dir-with-subdir-and-file/subdir/f", visitor));
+    CHECK_NOTHROW(Util::traverse("dir-with-subdir-and-file/subdir/f", visitor));
     REQUIRE(visited.size() == 1);
-    CHECK(visited[0] == "[f] traverse/dir-with-subdir-and-file/subdir/f");
+    CHECK(visited[0] == "[f] dir-with-subdir-and-file/subdir/f");
   }
 
   SECTION("traverse empty directory")
   {
-    CHECK_NOTHROW(Util::traverse("traverse/empty-dir", visitor));
+    CHECK_NOTHROW(Util::traverse("empty-dir", visitor));
     REQUIRE(visited.size() == 1);
-    CHECK(visited[0] == "[d] traverse/empty-dir");
+    CHECK(visited[0] == "[d] empty-dir");
   }
 
   SECTION("traverse directory with files")
   {
-    CHECK_NOTHROW(Util::traverse("traverse/dir-with-files", visitor));
+    CHECK_NOTHROW(Util::traverse("dir-with-files", visitor));
     REQUIRE(visited.size() == 3);
-    std::string f1 = "[f] traverse/dir-with-files/f1";
-    std::string f2 = "[f] traverse/dir-with-files/f2";
+    std::string f1 = "[f] dir-with-files/f1";
+    std::string f2 = "[f] dir-with-files/f2";
     CHECK(((visited[0] == f1 && visited[1] == f2)
            || (visited[0] == f2 && visited[1] == f1)));
-    CHECK(visited[2] == "[d] traverse/dir-with-files");
+    CHECK(visited[2] == "[d] dir-with-files");
   }
 
   SECTION("traverse directory hierarchy")
   {
-    CHECK_NOTHROW(Util::traverse("traverse/dir-with-subdir-and-file", visitor));
+    CHECK_NOTHROW(Util::traverse("dir-with-subdir-and-file", visitor));
     REQUIRE(visited.size() == 3);
-    CHECK(visited[0] == "[f] traverse/dir-with-subdir-and-file/subdir/f");
-    CHECK(visited[1] == "[d] traverse/dir-with-subdir-and-file/subdir");
-    CHECK(visited[2] == "[d] traverse/dir-with-subdir-and-file");
+    CHECK(visited[0] == "[f] dir-with-subdir-and-file/subdir/f");
+    CHECK(visited[1] == "[d] dir-with-subdir-and-file/subdir");
+    CHECK(visited[2] == "[d] dir-with-subdir-and-file");
   }
 }
 
 TEST_CASE("Util::wipe_path")
 {
+  TestContext test_context;
+
   SECTION("Wipe non-existing path")
   {
     CHECK_NOTHROW(Util::wipe_path("a"));
index c6fe00ca176e240ba59620dafadc204428d0d654..8f6ab64ca470be7ae6dda658c620611b869dbbe7 100644 (file)
 #include "../src/Compressor.hpp"
 #include "../src/Decompressor.hpp"
 #include "../src/File.hpp"
+#include "TestUtil.hpp"
 
 #include "third_party/catch.hpp"
 
 using Catch::Equals;
+using TestUtil::TestContext;
 
 TEST_CASE("Small Compression::Type::zstd roundtrip")
 {
+  TestContext test_context;
+
   File f("data.zstd", "wb");
   auto compressor =
     Compressor::create_from_type(Compression::Type::zstd, f.get(), 1);
@@ -59,6 +63,8 @@ TEST_CASE("Small Compression::Type::zstd roundtrip")
 
 TEST_CASE("Large compressible Compression::Type::zstd roundtrip")
 {
+  TestContext test_context;
+
   char data[] = "The quick brown fox jumps over the lazy dog";
 
   File f("data.zstd", "wb");
@@ -89,6 +95,8 @@ TEST_CASE("Large compressible Compression::Type::zstd roundtrip")
 
 TEST_CASE("Large uncompressible Compression::Type::zstd roundtrip")
 {
+  TestContext test_context;
+
   char data[100000];
   for (char& c : data) {
     c = rand() % 256;
index ac95d4d151fe16fd3cd2e733c2c07ebe6305399d..8ddc125d90d4ea3b76eeca3231057437ce84f2e4 100644 (file)
 #include "../src/Util.hpp"
 #include "../src/ccache.hpp"
 #include "../src/stats.hpp"
+#include "TestUtil.hpp"
 #include "argprocessing.hpp"
 #include "util.hpp"
 
 #include "third_party/catch.hpp"
 
+using TestUtil::TestContext;
+
 namespace {
 
 std::string
@@ -73,6 +76,8 @@ get_posix_path(const char* path)
 
 TEST_CASE("dash_E_should_result_in_called_for_preprocessing")
 {
+  TestContext test_context;
+
   Context ctx;
 
   ctx.orig_args = Args::from_string("cc -c foo.c -E");
@@ -87,6 +92,8 @@ TEST_CASE("dash_E_should_result_in_called_for_preprocessing")
 
 TEST_CASE("dash_M_should_be_unsupported")
 {
+  TestContext test_context;
+
   Context ctx;
 
   ctx.orig_args = Args::from_string("cc -c foo.c -M");
@@ -101,6 +108,8 @@ TEST_CASE("dash_M_should_be_unsupported")
 
 TEST_CASE("dependency_args_to_preprocessor_if_run_second_cpp_is_false")
 {
+  TestContext test_context;
+
   Context ctx;
 
 #define DEP_ARGS                                                               \
@@ -125,6 +134,8 @@ TEST_CASE("dependency_args_to_preprocessor_if_run_second_cpp_is_false")
 
 TEST_CASE("dependency_args_to_compiler_if_run_second_cpp_is_true")
 {
+  TestContext test_context;
+
   Context ctx;
 
 #define DEP_ARGS                                                               \
@@ -148,6 +159,8 @@ TEST_CASE("dependency_args_to_compiler_if_run_second_cpp_is_true")
 
 TEST_CASE("cpp_only_args_to_preprocessor_if_run_second_cpp_is_false")
 {
+  TestContext test_context;
+
   Context ctx;
 
 #define CPP_ARGS                                                               \
@@ -180,6 +193,8 @@ TEST_CASE("cpp_only_args_to_preprocessor_if_run_second_cpp_is_false")
 TEST_CASE(
   "cpp_only_args_to_preprocessor_and_compiler_if_run_second_cpp_is_true")
 {
+  TestContext test_context;
+
   Context ctx;
 
 #define CPP_ARGS                                                               \
@@ -211,6 +226,8 @@ TEST_CASE(
 TEST_CASE(
   "dependency_args_that_take_an_argument_should_not_require_space_delimiter")
 {
+  TestContext test_context;
+
   Context ctx;
 
 #define DEP_ARGS "-MMD -MFfoo.d -MT mt -MTmt -MQmq"
@@ -232,6 +249,8 @@ TEST_CASE(
 
 TEST_CASE("MQ_flag_should_not_be_added_if_run_second_cpp_is_true")
 {
+  TestContext test_context;
+
   Context ctx;
 
   ctx.orig_args = Args::from_string("cc -c -MD foo.c -MF foo.d -o foo.o");
@@ -251,6 +270,8 @@ TEST_CASE("MQ_flag_should_not_be_added_if_run_second_cpp_is_true")
 
 TEST_CASE("MQ_flag_should_be_added_if_run_second_cpp_is_false")
 {
+  TestContext test_context;
+
   Context ctx;
 
   ctx.orig_args = Args::from_string("cc -c -MD foo.c -MF foo.d -o foo.o");
@@ -271,6 +292,8 @@ TEST_CASE("MQ_flag_should_be_added_if_run_second_cpp_is_false")
 
 TEST_CASE("MF_should_be_added_if_run_second_cpp_is_false")
 {
+  TestContext test_context;
+
   Context ctx;
 
   ctx.orig_args = Args::from_string("cc -c -MD foo.c -o foo.o");
@@ -292,6 +315,8 @@ TEST_CASE("MF_should_be_added_if_run_second_cpp_is_false")
 
 TEST_CASE("MF_should_not_be_added_if_run_second_cpp_is_true")
 {
+  TestContext test_context;
+
   Context ctx;
 
   ctx.orig_args = Args::from_string("cc -c -MD foo.c -o foo.o");
@@ -312,6 +337,8 @@ TEST_CASE("MF_should_not_be_added_if_run_second_cpp_is_true")
 
 TEST_CASE("equal_sign_after_MF_should_be_removed")
 {
+  TestContext test_context;
+
   Context ctx;
 
   ctx.orig_args = Args::from_string("cc -c -MF=path foo.c -o foo.o");
@@ -332,6 +359,8 @@ TEST_CASE("equal_sign_after_MF_should_be_removed")
 
 TEST_CASE("sysroot_should_be_rewritten_if_basedir_is_used")
 {
+  TestContext test_context;
+
   Context ctx;
 
   char* arg_string;
@@ -353,6 +382,8 @@ TEST_CASE("sysroot_should_be_rewritten_if_basedir_is_used")
 TEST_CASE(
   "sysroot_with_separate_argument_should_be_rewritten_if_basedir_is_used")
 {
+  TestContext test_context;
+
   Context ctx;
 
   char* arg_string;
@@ -373,6 +404,8 @@ TEST_CASE(
 
 TEST_CASE("MF_flag_with_immediate_argument_should_work_as_last_argument")
 {
+  TestContext test_context;
+
   Context ctx;
 
   ctx.orig_args =
@@ -394,6 +427,8 @@ TEST_CASE("MF_flag_with_immediate_argument_should_work_as_last_argument")
 
 TEST_CASE("MT_flag_with_immediate_argument_should_work_as_last_argument")
 {
+  TestContext test_context;
+
   Context ctx;
 
   ctx.orig_args =
@@ -415,6 +450,8 @@ TEST_CASE("MT_flag_with_immediate_argument_should_work_as_last_argument")
 
 TEST_CASE("MQ_flag_with_immediate_argument_should_work_as_last_argument")
 {
+  TestContext test_context;
+
   Context ctx;
 
   ctx.orig_args =
@@ -436,6 +473,8 @@ TEST_CASE("MQ_flag_with_immediate_argument_should_work_as_last_argument")
 
 TEST_CASE("MQ_flag_without_immediate_argument_should_not_add_MQobj")
 {
+  TestContext test_context;
+
   Context ctx;
 
   ctx.orig_args = Args::from_string("gcc -c -MD -MP -MFfoo.d -MQ foo.d foo.c");
@@ -456,6 +495,8 @@ TEST_CASE("MQ_flag_without_immediate_argument_should_not_add_MQobj")
 
 TEST_CASE("MT_flag_without_immediate_argument_should_not_add_MTobj")
 {
+  TestContext test_context;
+
   Context ctx;
 
   ctx.orig_args = Args::from_string("gcc -c -MD -MP -MFfoo.d -MT foo.d foo.c");
@@ -476,6 +517,8 @@ TEST_CASE("MT_flag_without_immediate_argument_should_not_add_MTobj")
 
 TEST_CASE("MQ_flag_with_immediate_argument_should_not_add_MQobj")
 {
+  TestContext test_context;
+
   Context ctx;
 
   ctx.orig_args = Args::from_string("gcc -c -MD -MP -MFfoo.d -MQfoo.d foo.c");
@@ -496,6 +539,8 @@ TEST_CASE("MQ_flag_with_immediate_argument_should_not_add_MQobj")
 
 TEST_CASE("MT_flag_with_immediate_argument_should_not_add_MQobj")
 {
+  TestContext test_context;
+
   Context ctx;
 
   ctx.orig_args = Args::from_string("gcc -c -MD -MP -MFfoo.d -MTfoo.d foo.c");
@@ -517,6 +562,8 @@ TEST_CASE("MT_flag_with_immediate_argument_should_not_add_MQobj")
 TEST_CASE(
   "isystem_flag_with_separate_arg_should_be_rewritten_if_basedir_is_used")
 {
+  TestContext test_context;
+
   Context ctx;
 
   char* arg_string;
@@ -536,6 +583,8 @@ TEST_CASE(
 
 TEST_CASE("isystem_flag_with_concat_arg_should_be_rewritten_if_basedir_is_used")
 {
+  TestContext test_context;
+
   Context ctx;
 
   char* cwd;
@@ -560,6 +609,8 @@ TEST_CASE("isystem_flag_with_concat_arg_should_be_rewritten_if_basedir_is_used")
 
 TEST_CASE("I_flag_with_concat_arg_should_be_rewritten_if_basedir_is_used")
 {
+  TestContext test_context;
+
   Context ctx;
 
   char* cwd;
@@ -584,6 +635,8 @@ TEST_CASE("I_flag_with_concat_arg_should_be_rewritten_if_basedir_is_used")
 
 TEST_CASE("debug_flag_order_with_known_option_first")
 {
+  TestContext test_context;
+
   Context ctx;
 
   ctx.orig_args = Args::from_string("cc -g1 -gsplit-dwarf foo.c -c");
@@ -603,6 +656,8 @@ TEST_CASE("debug_flag_order_with_known_option_first")
 
 TEST_CASE("debug_flag_order_with_known_option_last")
 {
+  TestContext test_context;
+
   Context ctx;
 
   ctx.orig_args = Args::from_string("cc -gsplit-dwarf -g1 foo.c -c");
@@ -622,6 +677,8 @@ TEST_CASE("debug_flag_order_with_known_option_last")
 
 TEST_CASE("options_not_to_be_passed_to_the_preprocesor")
 {
+  TestContext test_context;
+
   Context ctx;
 
   ctx.orig_args = Args::from_string(
@@ -644,6 +701,8 @@ TEST_CASE("options_not_to_be_passed_to_the_preprocesor")
 
 TEST_CASE("cuda_option_file")
 {
+  TestContext test_context;
+
   Context ctx;
   ctx.guessed_compiler = GuessedCompiler::nvcc;
 
index d3f0e7442032dcba2874df9b979b78193a5ef848..9b4f7845d07ec964d8c743a06a7409d029931154 100644 (file)
 
 #include "../src/Context.hpp"
 #include "../src/hashutil.hpp"
+#include "TestUtil.hpp"
 #include "util.hpp"
 
 #include "third_party/catch.hpp"
 
+using TestUtil::TestContext;
+
 TEST_CASE("hash_command_output_simple")
 {
   char d1[DIGEST_STRING_BUFFER_SIZE];
@@ -104,6 +107,8 @@ TEST_CASE("hash_command_output_compiler_substitution")
 
 TEST_CASE("hash_command_output_stdout_versus_stderr")
 {
+  TestContext test_context;
+
   Context ctx;
 
   char d1[DIGEST_STRING_BUFFER_SIZE];