diff --git a/include/criterion/logging.h b/include/criterion/logging.h index 6b67612..c810933 100644 --- a/include/criterion/logging.h +++ b/include/criterion/logging.h @@ -32,9 +32,6 @@ enum criterion_logging_level { CRITERION_IMPORTANT, }; -extern enum criterion_logging_level logging_threshold; -extern bool enable_tap_format; - FORMAT(printf, 2, 3) void criterion_log(enum criterion_logging_level level, const char *msg, ...); diff --git a/include/criterion/options.h b/include/criterion/options.h new file mode 100644 index 0000000..21f6a1a --- /dev/null +++ b/include/criterion/options.h @@ -0,0 +1,15 @@ +#ifndef CRITERION_OPTIONS_H_ +# define CRITERION_OPTIONS_H_ + +# include "logging.h" + +struct criterion_options { + enum criterion_logging_level logging_threshold; + bool enable_tap_format; + bool no_early_exit; + bool always_succeed; +}; + +extern struct criterion_options criterion_options; + +#endif /*!CRITERION_OPTIONS_H_ */ diff --git a/src/logging.c b/src/logging.c index 293c3cc..f26d1e2 100644 --- a/src/logging.c +++ b/src/logging.c @@ -24,14 +24,12 @@ #include #include #include "criterion/logging.h" - -enum criterion_logging_level logging_threshold = CRITERION_IMPORTANT; -bool enable_tap_format = false; +#include "criterion/options.h" void criterion_log(enum criterion_logging_level level, const char *msg, ...) { va_list args; - if (level < logging_threshold) + if (level < criterion_options.logging_threshold) return; va_start(args, msg); diff --git a/src/main.c b/src/main.c index b6dcdb9..a69d24d 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,7 @@ #define _GNU_SOURCE #include #include +#include #include #include @@ -17,16 +18,27 @@ int print_usage(char *progname) { int main(int argc, char *argv[]) { static struct option opts[] = { - {"verbose", optional_argument, 0, 'v'}, - {"tap", no_argument, 0, 't'}, - {"help", no_argument, 0, 'h'}, - {0, 0, 0, 0 } + {"verbose", optional_argument, 0, 'v'}, + {"tap", no_argument, 0, 't'}, + {"help", no_argument, 0, 'h'}, + {"always-succeed", no_argument, 0, 'y'}, + {"no-early-exit", no_argument, 0, 'z'}, + {0, 0, 0, 0 } + }; + + criterion_options = (struct criterion_options) { + .always_succeed = strcmp("1", getenv("CRITERION_ALWAYS_SUCCEED") ?: "0"), + .no_early_exit = strcmp("1", getenv("CRITERION_NO_EARLY_EXIT") ?: "0"), + .enable_tap_format = strcmp("1", getenv("CRITERION_ENABLE_TAP") ?: "0"), + .logging_threshold = atoi(getenv("CRITERION_VERBOSITY_LEVEL") ?: "2"), }; for (int c; (c = getopt_long(argc, argv, "h", opts, NULL)) != -1;) { switch (c) { - case 'v': logging_threshold = atoi(optarg ?: "1"); break; - case 't': enable_tap_format = true; break; + case 'v': criterion_options.logging_threshold = atoi(optarg ?: "1"); break; + case 't': criterion_options.enable_tap_format = true; break; + case 'y': criterion_options.always_succeed = true; break; + case 'z': criterion_options.no_early_exit = true; break; case 'h': default : return print_usage(argv[0]); } diff --git a/src/options.c b/src/options.c new file mode 100644 index 0000000..d330da7 --- /dev/null +++ b/src/options.c @@ -0,0 +1,3 @@ +# include "criterion/options.h" + +struct criterion_options criterion_options = { .logging_threshold = CRITERION_IMPORTANT }; diff --git a/src/process.c b/src/process.c index 3cbab45..2b0fc42 100644 --- a/src/process.c +++ b/src/process.c @@ -6,6 +6,7 @@ #include #include "criterion/criterion.h" +#include "criterion/options.h" #include "process.h" #include "event.h" @@ -47,7 +48,7 @@ struct process *spawn_test_worker(struct criterion_test *test, void (*func)(stru func(test); close(fds[1]); - if (!strcmp("1", getenv("CRITERION_NO_EARLY_EXIT") ?: "0")) + if (!criterion_options.no_early_exit) return NULL; else _exit(0); diff --git a/src/report.c b/src/report.c index cab4418..b3c0751 100644 --- a/src/report.c +++ b/src/report.c @@ -25,6 +25,7 @@ #include "criterion/criterion.h" #include "criterion/stats.h" #include "criterion/logging.h" +#include "criterion/options.h" #include "report.h" #define IMPL_CALL_REPORT_HOOKS(Kind) \ @@ -52,13 +53,13 @@ IMPL_CALL_REPORT_HOOKS(POST_FINI); IMPL_CALL_REPORT_HOOKS(POST_EVERYTHING); ReportHook(PRE_INIT)(struct criterion_test *test) { - if (enable_tap_format) return; + if (criterion_options.enable_tap_format) return; criterion_info("%s::%s: RUNNING\n", test->category, test->name); } ReportHook(POST_TEST)(struct criterion_test_stats *stats) { - if (enable_tap_format) { + if (criterion_options.enable_tap_format) { criterion_important("%s %lu - %s::%s\n", stats->failed ? "not ok" : "ok", tap_test_index++, @@ -90,7 +91,7 @@ ReportHook(PRE_TEST)() {} ReportHook(POST_FINI)() {} ReportHook(PRE_EVERYTHING)(struct criterion_test_set *set) { - if (enable_tap_format) { + if (criterion_options.enable_tap_format) { size_t enabled_count = 0, i = 0; for (struct criterion_test **test = set->tests; i < set->nb_tests; ++i) if (!(test[i])->data->disabled) @@ -99,7 +100,7 @@ ReportHook(PRE_EVERYTHING)(struct criterion_test_set *set) { } } ReportHook(POST_EVERYTHING)(struct criterion_global_stats *stats) { - if (enable_tap_format) return; + if (criterion_options.enable_tap_format) return; criterion_important("Synthesis: %lu tests were run. %lu passed, %lu failed (with %lu crashes)\n", stats->nb_tests, @@ -109,7 +110,7 @@ ReportHook(POST_EVERYTHING)(struct criterion_global_stats *stats) { } ReportHook(ASSERT)(struct criterion_assert_stats *stats) { - if (enable_tap_format) return; + if (criterion_options.enable_tap_format) return; if (!stats->passed) { criterion_important("%s:%d: Assertion failed: %s\n", @@ -120,7 +121,7 @@ ReportHook(ASSERT)(struct criterion_assert_stats *stats) { } ReportHook(TEST_CRASH)(struct criterion_test_stats *stats) { - if (enable_tap_format) { + if (criterion_options.enable_tap_format) { criterion_important("not ok %lu - %s::%s unexpected signal after %s:%u\n", tap_test_index++, stats->test->category, diff --git a/src/runner.c b/src/runner.c index 3b15516..706adf1 100644 --- a/src/runner.c +++ b/src/runner.c @@ -27,6 +27,7 @@ #include #include #include "criterion/assert.h" +#include "criterion/options.h" #include "stats.h" #include "runner.h" #include "report.h" @@ -160,5 +161,5 @@ int criterion_run_all_tests(void) { if (res == -1) // if this is the test worker terminating exit(0); - return strcmp("1", getenv("CRITERION_ALWAYS_SUCCEED") ?: "0") && res; + return criterion_options.always_succeed && res; }