]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Run each test case in a separate directory for isolation
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 17 Jul 2010 17:10:42 +0000 (19:10 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 17 Jul 2010 17:10:42 +0000 (19:10 +0200)
test/framework.c
test/framework.h
test/main.c

index 145872bf758433533f9a7ea6df5f330c57ef2700..b85cfb271356bab2487e1e9f78e6139fba48747d 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "ccache.h"
 #include "framework.h"
+#include <errno.h>
 #include <stdio.h>
 #if defined(HAVE_TERMIOS_H)
 #define USE_COLOR
@@ -34,6 +35,8 @@ static unsigned failed_asserts_before_suite;
 static unsigned failed_asserts_before_test;
 static const char *current_suite;
 static const char *current_test;
+static char *dir_before_suite;
+static char *dir_before_test;
 static int verbose;
 
 static const char COLOR_END[] = "\x1b[m";
@@ -55,7 +58,7 @@ is_tty(int fd)
 }
 
 static const char *
-numerus(unsigned n)
+plural_s(unsigned n)
 {
        return n == 1 ? "" : "s";
 }
@@ -75,7 +78,6 @@ cct_run(suite_fn *suites, int verbose_output)
                        if (test_index == 0) {
                                break;
                        }
-                       ++failed_suites;
                }
                --passed_tests; /* Fix false increase in first TEST expansion. */
        }
@@ -83,21 +85,15 @@ cct_run(suite_fn *suites, int verbose_output)
        if (failed_asserts == 0) {
                printf("%sPASSED%s: %u assertion%s, %u test%s, %u suite%s\n",
                       COLOR(tty, GREEN), COLOR(tty, END),
-                      passed_asserts,
-                      numerus(passed_asserts),
-                      passed_tests,
-                      numerus(passed_tests),
-                      passed_suites,
-                      numerus(passed_suites));
+                      passed_asserts, plural_s(passed_asserts),
+                      passed_tests, plural_s(passed_tests),
+                      passed_suites, plural_s(passed_suites));
        } else {
                printf("%sFAILED%s: %u assertion%s, %u test%s, %u suite%s\n",
                       COLOR(tty, RED), COLOR(tty, END),
-                      failed_asserts,
-                      numerus(failed_asserts),
-                      failed_tests,
-                      numerus(failed_tests),
-                      failed_suites,
-                      numerus(failed_suites));
+                      failed_asserts, plural_s(failed_asserts),
+                      failed_tests, plural_s(failed_tests),
+                      failed_suites, plural_s(failed_suites));
        }
        return failed_asserts > 0 ? 1 : 0;
 }
@@ -107,6 +103,9 @@ void cct_suite_begin(const char *name)
        if (verbose) {
                printf("=== SUITE: %s ===\n", name);
        }
+       dir_before_suite = gnu_getcwd();
+       create_dir(name);
+       cct_chdir(name);
        current_suite = name;
        failed_asserts_before_suite = failed_asserts;
        failed_asserts_before_test = failed_tests; /* For first cct_test_end(). */
@@ -114,6 +113,9 @@ void cct_suite_begin(const char *name)
 
 void cct_suite_end()
 {
+       cct_chdir(dir_before_suite);
+       free(dir_before_suite);
+       dir_before_suite = NULL;
        if (failed_asserts > failed_asserts_before_suite) {
                ++failed_suites;
        } else {
@@ -126,12 +128,20 @@ void cct_test_begin(const char *name)
        if (verbose) {
                printf("--- TEST: %s ---\n", name);
        }
+       dir_before_test = gnu_getcwd();
+       create_dir(name);
+       cct_chdir(name);
        current_test = name;
        failed_asserts_before_test = failed_asserts;
 }
 
 void cct_test_end()
 {
+       if (dir_before_test) {
+               cct_chdir(dir_before_test);
+               free(dir_before_test);
+               dir_before_test = NULL;
+       }
        if (failed_asserts > failed_asserts_before_test) {
                ++failed_tests;
        } else {
@@ -263,3 +273,32 @@ cct_check_args_eq(const char *file, int line, const char *expression,
        }
        return result;
 }
+
+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(). */
+       char *command;
+       x_asprintf(&command, "rm -rf %s", path);
+       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();
+       }
+}
index d12c031cf59292306bfdb80979e7bbcdbc5c3c1d..5685705544853a526eb6f75da729c824f20aa9f0 100644 (file)
@@ -56,6 +56,8 @@
                        cct_check_passed(); \
                } else { \
                        cct_check_failed(__FILE__, __LINE__, #assertion, NULL, NULL); \
+                       cct_test_end(); \
+                       cct_suite_end(); \
                        return _test_counter; \
                } \
        } while (0)
@@ -63,6 +65,8 @@
 #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 (0)
@@ -72,6 +76,8 @@
 #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 (0)
@@ -79,6 +85,8 @@
 #define CHECK_UNS_EQ(expected, actual) \
        do { \
                if (!cct_check_int_eq(__FILE__, __LINE__, #actual, (expected), (actual))) { \
+                       cct_test_end(); \
+                       cct_suite_end(); \
                        return _test_counter; \
                } \
        } while (0)
@@ -132,5 +140,8 @@ int cct_check_str_eq(const char *file, int line, const char *expression,
 int cct_check_args_eq(const char *file, int line, const char *expression,
                       struct args *expected, struct args *actual,
                       int free1, int free2);
+void cct_chdir(const char *path);
+void cct_wipe(const char *path);
+void cct_create_fresh_dir(const char *path);
 
 #endif
index 7924d5df85ce85f37dc1fbd73eedc5410e523dea..a54bb896d15ac16150b25a9299e526bd5aaf3854 100644 (file)
@@ -45,6 +45,8 @@ int main(int argc, char **argv)
        };
        int verbose = 0;
        int c;
+       char *testdir;
+       int result;
 
        while ((c = getopt_long(argc, argv, "hv", options, NULL)) != -1) {
                switch (c) {
@@ -74,5 +76,13 @@ int main(int argc, char **argv)
        }
        cache_logfile = getenv("CCACHE_LOGFILE");
 
-       return cct_run(suites, verbose);
+       x_asprintf(&testdir, "testdir.%d", (int)getpid());
+       cct_create_fresh_dir(testdir);
+       cct_chdir(testdir);
+       result = cct_run(suites, verbose);
+       if (result == 0) {
+               cct_wipe(testdir);
+       }
+       free(testdir);
+       return result;
 }