Added better option management

This commit is contained in:
Snaipe 2015-03-09 20:15:00 +01:00
parent 4b451b1a58
commit 4a39f9daff
8 changed files with 49 additions and 21 deletions

View file

@ -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, ...);

View file

@ -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_ */

View file

@ -24,14 +24,12 @@
#include <stdio.h>
#include <stdarg.h>
#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);

View file

@ -1,6 +1,7 @@
#define _GNU_SOURCE
#include <criterion/criterion.h>
#include <criterion/logging.h>
#include <criterion/options.h>
#include <stdio.h>
#include <getopt.h>
@ -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]);
}

3
src/options.c Normal file
View file

@ -0,0 +1,3 @@
# include "criterion/options.h"
struct criterion_options criterion_options = { .logging_threshold = CRITERION_IMPORTANT };

View file

@ -6,6 +6,7 @@
#include <csptr/smart_ptr.h>
#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);

View file

@ -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,

View file

@ -27,6 +27,7 @@
#include <sys/wait.h>
#include <csptr/smart_ptr.h>
#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;
}