]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Remove last traces of the old unittest framework
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 10 May 2020 17:43:15 +0000 (19:43 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 13 May 2020 19:26:20 +0000 (21:26 +0200)
Makefile.in
dev.mk.in
unittest/TestUtil.cpp
unittest/TestUtil.hpp
unittest/framework.cpp [deleted file]
unittest/framework.hpp [deleted file]
unittest/main.cpp

index df5324cc4e218bee6f659224ccc02b223b71e368..202159c93eec5ad49c385631bdf759e67741fc7f 100644 (file)
@@ -100,7 +100,6 @@ test_suites += unittest/test_legacy_util.cpp
 
 test_sources += unittest/TestUtil.cpp
 test_sources += unittest/catch2_tests.cpp
-test_sources += unittest/framework.cpp
 test_sources += unittest/main.cpp
 test_sources += unittest/util.cpp
 test_sources += $(test_suites)
index 96f8a1a58b7dd652bd3e638208bb586eb736cbce..aca655c6381ef7ccd0aaa5b7ebee044958abe763 100644 (file)
--- a/dev.mk.in
+++ b/dev.mk.in
@@ -51,7 +51,6 @@ non_third_party_headers_without_cpp = \
     src/exceptions.hpp \
     src/macroskip.hpp \
     src/system.hpp \
-    unittest/framework.hpp \
     unittest/util.hpp
 
 non_third_party_headers = \
index cd7f8444ceb67c93935facc273d644912b328cc2..1f8be45be8ce3daf4c7d9d6086a5c2f7516a51c6 100644 (file)
@@ -45,4 +45,13 @@ TestContext::~TestContext()
   }
 }
 
+void
+check_chdir(const std::string& dir)
+{
+  if (chdir(dir.c_str()) != 0) {
+    throw Error(fmt::format(
+      "failed to change directory to {}: {}", dir, strerror(errno)));
+  }
+}
+
 } // namespace TestUtil
index 4793417929ae69ebd793b754f3ddd786f2321a79..9e1ea003148274dbd9eb88799a9fcdb75933cc6a 100644 (file)
@@ -35,4 +35,7 @@ private:
   static size_t m_subdir_counter;
 };
 
+// Change directory to `dir`, throwing Error on failure.
+void check_chdir(const std::string& dir);
+
 } // namespace TestUtil
diff --git a/unittest/framework.cpp b/unittest/framework.cpp
deleted file mode 100644 (file)
index a1d3b2c..0000000
+++ /dev/null
@@ -1,319 +0,0 @@
-// Copyright (C) 2010-2020 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 "framework.hpp"
-
-#include "../src/Args.hpp"
-#include "../src/Config.hpp"
-#include "../src/Util.hpp"
-#include "../src/ccache.hpp"
-#include "util.hpp"
-
-#include <float.h>
-#include <math.h>
-#if defined(HAVE_TERMIOS_H)
-#  define USE_COLOR
-#  include <termios.h>
-#endif
-
-static unsigned total_asserts;
-static unsigned total_tests;
-static unsigned total_suites;
-static unsigned failed_tests;
-static const char* current_suite;
-static const char* current_test;
-static std::string dir_before_suite;
-static std::string dir_before_test;
-static int verbose;
-
-static const char COLOR_END[] = "\x1b[m";
-static const char COLOR_GREEN[] = "\x1b[1;32m";
-static const char COLOR_RED[] = "\x1b[1;31m";
-
-#define COLOR(tty, color) ((tty) ? COLOR_##color : "")
-
-static int
-is_tty(int fd)
-{
-#ifdef USE_COLOR
-  struct termios t;
-  return tcgetattr(fd, &t) == 0;
-#else
-  (void)fd;
-  return 0;
-#endif
-}
-
-static const char*
-plural_s(unsigned n)
-{
-  return n == 1 ? "" : "s";
-}
-
-int
-cct_run(const suite_fn* suites, int verbose_output)
-{
-  const suite_fn* suite;
-  int tty = is_tty(1);
-
-  x_unsetenv("GCC_COLORS"); // Avoid confusing argument processing tests.
-  verbose = verbose_output;
-
-  for (suite = suites; *suite; suite++) {
-    unsigned test_index = 0;
-    while (true) {
-      test_index = (*suite)(test_index + 1);
-      if (test_index == 0) {
-        // We have reached the end of the suite.
-        break;
-      }
-    }
-  }
-
-  if (failed_tests == 0) {
-    printf("%sPASSED%s: %u assertion%s, %u test%s, %u suite%s\n",
-           COLOR(tty, GREEN),
-           COLOR(tty, END),
-           total_asserts,
-           plural_s(total_asserts),
-           total_tests,
-           plural_s(total_tests),
-           total_suites,
-           plural_s(total_suites));
-  } else {
-    printf("%sFAILED%s: %u test%s\n",
-           COLOR(tty, RED),
-           COLOR(tty, END),
-           failed_tests,
-           plural_s(failed_tests));
-  }
-  return failed_tests > 0 ? 1 : 0;
-}
-
-void
-cct_suite_begin(const char* name)
-{
-  ++total_suites;
-  if (verbose) {
-    printf("=== SUITE: %s ===\n", name);
-  }
-  dir_before_suite = Util::get_actual_cwd();
-  Util::create_dir(name);
-  cct_chdir(name);
-  current_suite = name;
-}
-
-void
-cct_suite_end()
-{
-  cct_chdir(dir_before_suite.c_str());
-  dir_before_suite.clear();
-}
-
-void
-cct_test_begin(const char* name)
-{
-  ++total_tests;
-  if (verbose) {
-    printf("--- TEST: %s ---\n", name);
-  }
-  dir_before_test = Util::get_actual_cwd();
-  Util::create_dir(name);
-  cct_chdir(name);
-  current_test = name;
-
-  x_setenv("CCACHE_CONFIG_PATH", "/dev/null");
-}
-
-void
-cct_test_end()
-{
-  if (!dir_before_test.empty()) {
-    cct_chdir(dir_before_test.c_str());
-    dir_before_test.clear();
-  }
-}
-
-void
-cct_check_passed(const char* file, int line, const char* what)
-{
-  ++total_asserts;
-  if (verbose) {
-    printf("%s:%d: Passed assertion: %s\n", file, line, what);
-  }
-}
-
-void
-cct_check_failed(const char* file,
-                 int line,
-                 const char* what,
-                 const char* expected,
-                 const char* actual)
-{
-  ++total_asserts;
-  ++failed_tests;
-  fprintf(stderr, "%s:%d: Failed assertion:\n", file, line);
-  fprintf(stderr, "  Suite:      %s\n", current_suite);
-  fprintf(stderr, "  Test:       %s\n", current_test);
-  if (expected) {
-    fprintf(stderr, "  Expression: %s\n", what);
-    if (actual) {
-      fprintf(stderr, "  Expected:   %s\n", expected);
-      fprintf(stderr, "  Actual:     %s\n", actual);
-    } else {
-      fprintf(stderr, "  Message:    %s\n", expected);
-    }
-  } else {
-    fprintf(stderr, "  Assertion:  %s\n", what);
-  }
-  fprintf(stderr, "\n");
-}
-
-bool
-cct_check_int_eq(const char* file,
-                 int line,
-                 const char* expression,
-                 int64_t expected,
-                 int64_t actual)
-{
-  if (expected == actual) {
-    cct_check_passed(file, line, expression);
-    return true;
-  } else {
-#if defined(HAVE_LONG_LONG) && !defined(__MINGW32__)
-    char* exp_str = format("%lld", (long long)expected);
-    char* act_str = format("%lld", (long long)actual);
-#else
-    char* exp_str = format("%ld", (long)expected);
-    char* act_str = format("%ld", (long)actual);
-#endif
-    cct_check_failed(file, line, expression, exp_str, act_str);
-    free(exp_str);
-    free(act_str);
-    return false;
-  }
-}
-
-bool
-cct_check_data_eq(const char* file,
-                  int line,
-                  const char* expression,
-                  const uint8_t* expected,
-                  const uint8_t* actual,
-                  size_t size)
-{
-  if (memcmp(actual, expected, size) == 0) {
-    cct_check_passed(file, line, expression);
-    return true;
-  } else {
-    char* exp_str = static_cast<char*>(x_malloc(2 * size + 1));
-    char* act_str = static_cast<char*>(x_malloc(2 * size + 1));
-    format_hex(expected, size, exp_str);
-    format_hex(actual, size, act_str);
-    cct_check_failed(file, line, expression, exp_str, act_str);
-    free(exp_str);
-    free(act_str);
-    return false;
-  }
-}
-
-bool
-cct_check_str_eq(const char* file,
-                 int line,
-                 const char* expression,
-                 const char* expected,
-                 const char* actual,
-                 bool free1,
-                 bool free2)
-{
-  bool result;
-
-  if (expected && actual && str_eq(actual, expected)) {
-    cct_check_passed(file, line, expression);
-    result = true;
-  } else {
-    char* exp_str = expected ? format("\"%s\"", expected) : x_strdup("(null)");
-    char* act_str = actual ? format("\"%s\"", actual) : x_strdup("(null)");
-    cct_check_failed(file, line, expression, exp_str, act_str);
-    free(exp_str);
-    free(act_str);
-    result = false;
-  }
-
-  if (free1) {
-    free(const_cast<char*>(expected));
-  }
-  if (free2) {
-    free(const_cast<char*>(actual));
-  }
-  return result;
-}
-
-bool
-cct_check_args_eq(const char* file,
-                  int line,
-                  const char* expression,
-                  const Args& expected,
-                  const Args& actual)
-{
-  if (actual == expected) {
-    cct_check_passed(file, line, expression);
-    return true;
-  } else {
-    cct_check_failed(file,
-                     line,
-                     expression,
-                     expected.to_string().c_str(),
-                     actual.to_string().c_str());
-    return false;
-  }
-}
-
-void
-cct_chdir(const char* path)
-{
-  if (chdir(path) != 0) {
-    fprintf(stderr, "chdir: %s: %s", path, strerror(errno));
-    abort();
-  }
-}
-
-void
-cct_wipe(const char* path)
-{
-  // TODO: rewrite using traverse().
-#ifndef __MINGW32__
-  char* command = format("rm -rf %s", path);
-#else
-  char* command = format("rd /s /q %s", path);
-#endif
-  if (system(command) != 0) {
-    perror(command);
-  }
-  free(command);
-}
-
-void
-cct_create_fresh_dir(const char* path)
-{
-  cct_wipe(path);
-  if (mkdir(path, 0777) != 0) {
-    fprintf(stderr, "mkdir: %s: %s", path, strerror(errno));
-    abort();
-  }
-}
diff --git a/unittest/framework.hpp b/unittest/framework.hpp
deleted file mode 100644 (file)
index 41d3f9c..0000000
+++ /dev/null
@@ -1,168 +0,0 @@
-// Copyright (C) 2010-2020 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
-
-#pragma once
-
-#include "../src/system.hpp"
-
-class Args;
-
-// ============================================================================
-
-#define TEST_SUITE(name)                                                       \
-  unsigned suite_##name(unsigned _start_point);                                \
-  unsigned suite_##name(unsigned _start_point)                                 \
-  {                                                                            \
-    unsigned _test_counter = 0;                                                \
-    cct_suite_begin(#name);                                                    \
-    {                                                                          \
-      // Empty due to macro trickery.
-
-#define TEST(name)                                                             \
-  cct_test_end();                                                              \
-  }                                                                            \
-  ++_test_counter;                                                             \
-  {                                                                            \
-    static int name = 0;                                                       \
-    (void)name; /* Verify test name. */                                        \
-  }                                                                            \
-  if (_test_counter >= _start_point) {                                         \
-    cct_test_begin(#name);
-
-#define TEST_SUITE_END                                                         \
-  cct_test_end();                                                              \
-  }                                                                            \
-  cct_suite_end();                                                             \
-  return 0; /* We have reached the end. */                                     \
-  }
-
-// ============================================================================
-
-#define CHECKM(assertion, message)                                             \
-  do {                                                                         \
-    if ((assertion)) {                                                         \
-      cct_check_passed(__FILE__, __LINE__, #assertion);                        \
-    } else {                                                                   \
-      cct_check_failed(__FILE__, __LINE__, #assertion, (message), NULL);       \
-      cct_test_end();                                                          \
-      cct_suite_end();                                                         \
-      return _test_counter;                                                    \
-    }                                                                          \
-  } while (false)
-
-#define CHECK(assertion) CHECKM(assertion, NULL)
-
-#define CHECK_POINTER_EQ_BASE(t, e, a, f1, f2)                                 \
-  do {                                                                         \
-    if (!cct_check_##t##_eq(__FILE__, __LINE__, #a, (e), (a), (f1), (f2))) {   \
-      cct_test_end();                                                          \
-      cct_suite_end();                                                         \
-      return _test_counter;                                                    \
-    }                                                                          \
-  } while (false)
-
-// ============================================================================
-
-#define CHECK_INT_EQ(expected, actual)                                         \
-  do {                                                                         \
-    if (!cct_check_int_eq(                                                     \
-          __FILE__, __LINE__, #actual, (expected), (actual))) {                \
-      cct_test_end();                                                          \
-      cct_suite_end();                                                         \
-      return _test_counter;                                                    \
-    }                                                                          \
-  } while (false)
-
-// ============================================================================
-
-#define CHECK_DATA_EQ(expected, actual, size)                                  \
-  do {                                                                         \
-    if (!cct_check_data_eq(                                                    \
-          __FILE__, __LINE__, #actual, (expected), (actual), size)) {          \
-      cct_test_end();                                                          \
-      cct_suite_end();                                                         \
-      return _test_counter;                                                    \
-    }                                                                          \
-  } while (false)
-
-// ============================================================================
-
-#define CHECK_STR_EQ(expected, actual)                                         \
-  CHECK_POINTER_EQ_BASE(str, expected, actual, false, false)
-
-#define CHECK_STR_EQ_FREE1(expected, actual)                                   \
-  CHECK_POINTER_EQ_BASE(str, expected, actual, true, false)
-
-#define CHECK_STR_EQ_FREE2(expected, actual)                                   \
-  CHECK_POINTER_EQ_BASE(str, expected, actual, false, true)
-
-#define CHECK_STR_EQ_FREE12(expected, actual)                                  \
-  CHECK_POINTER_EQ_BASE(str, expected, actual, true, true)
-
-// ============================================================================
-
-#define CHECK_ARGS_EQ_FREE12(e, a)                                             \
-  do {                                                                         \
-    if (!cct_check_args_eq(__FILE__, __LINE__, #a, (e), (a))) {                \
-      cct_test_end();                                                          \
-      cct_suite_end();                                                         \
-      return _test_counter;                                                    \
-    }                                                                          \
-  } while (false)
-
-// ============================================================================
-
-typedef unsigned (*suite_fn)(unsigned);
-int cct_run(const suite_fn* suites, int verbose);
-
-void cct_suite_begin(const char* name);
-void cct_suite_end();
-void cct_test_begin(const char* name);
-void cct_test_end();
-void cct_check_passed(const char* file, int line, const char* assertion);
-void cct_check_failed(const char* file,
-                      int line,
-                      const char* assertion,
-                      const char* expected,
-                      const char* actual);
-bool cct_check_int_eq(const char* file,
-                      int line,
-                      const char* expression,
-                      int64_t expected,
-                      int64_t actual);
-bool cct_check_data_eq(const char* file,
-                       int line,
-                       const char* expression,
-                       const uint8_t* expected,
-                       const uint8_t* actual,
-                       size_t size);
-bool cct_check_str_eq(const char* file,
-                      int line,
-                      const char* expression,
-                      const char* expected,
-                      const char* actual,
-                      bool free1,
-                      bool free2);
-bool cct_check_args_eq(const char* file,
-                       int line,
-                       const char* expression,
-                       const Args& expected,
-                       const Args& actual);
-void cct_chdir(const char* path);
-void cct_wipe(const char* path);
-void cct_create_fresh_dir(const char* path);
index efa8834ce0a5d4850b70313b355a6ee94e50ffdb..3c36e43256ee9fa94fc0ab069ca2aecdc5606fdd 100644 (file)
 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 #include "../src/Util.hpp"
-#include "../src/legacy_util.hpp"
+#include "TestUtil.hpp"
 #include "catch2_tests.hpp"
-#include "framework.hpp"
 
-const suite_fn k_legacy_suites[] = {
-  nullptr,
-};
+#include "third_party/catch.hpp"
+#include "third_party/fmt/core.h"
 
 int
 main(int argc, char** argv)
@@ -31,25 +29,22 @@ main(int argc, char** argv)
 #ifdef _WIN32
   x_setenv("CCACHE_DETECT_SHEBANG", "1");
 #endif
+  x_unsetenv("GCC_COLORS"); // Don't confuse argument processing tests.
 
   std::string dir_before = Util::get_actual_cwd();
-  char* testdir = format("testdir.%d", (int)getpid());
-  cct_create_fresh_dir(testdir);
-  cct_chdir(testdir);
+  std::string testdir = fmt::format("testdir.{}", getpid());
+  Util::wipe_path(testdir);
+  Util::create_dir(testdir);
+  TestUtil::check_chdir(testdir);
 
-  // Run Catch2 tests.
   int result = run_catch2_tests(argc, argv);
 
-  // Run legacy tests.
   if (result == 0) {
-    bool verbose = false;
-    result = cct_run(k_legacy_suites, verbose);
+    TestUtil::check_chdir(dir_before);
+    Util::wipe_path(testdir);
+  } else {
+    fmt::print(stderr, "Note: Test data has been left in {}\n", testdir);
   }
 
-  if (result == 0) {
-    cct_chdir(dir_before.c_str());
-    cct_wipe(testdir);
-  }
-  free(testdir);
   return result;
 }