diff --git a/include/criterion/options.h b/include/criterion/options.h index 3e2dee6..31b90d2 100644 --- a/include/criterion/options.h +++ b/include/criterion/options.h @@ -35,6 +35,7 @@ struct criterion_options { bool use_ascii; bool fail_fast; const char *pattern; + bool short_filename; }; extern struct criterion_options criterion_options; diff --git a/src/log/normal.c b/src/log/normal.c index c82bcbd..3db14d9 100644 --- a/src/log/normal.c +++ b/src/log/normal.c @@ -164,9 +164,10 @@ void normal_log_assert(struct criterion_assert_stats *stats) { char *line = strtok_r(dup, "\n", &saveptr); #endif + bool sf = criterion_options.short_filename; criterion_pimportant(CRITERION_PREFIX_DASHES, _(msg_assert_fail), - FG_BOLD, stats->file, RESET, + FG_BOLD, sf ? basename(stats->file) : stats->file, RESET, FG_RED, stats->line, RESET, line); @@ -181,9 +182,10 @@ void normal_log_assert(struct criterion_assert_stats *stats) { } void normal_log_test_crash(struct criterion_test_stats *stats) { + bool sf = criterion_options.short_filename; criterion_pimportant(CRITERION_PREFIX_DASHES, _(msg_test_crash_line), - FG_BOLD, stats->file, RESET, + FG_BOLD, sf ? basename(stats->file) : stats->file, RESET, FG_RED, stats->progress, RESET); criterion_pimportant(CRITERION_PREFIX_FAIL, _(msg_test_crash), stats->test->category, diff --git a/src/log/tap.c b/src/log/tap.c index e8a9d97..fec23e1 100644 --- a/src/log/tap.c +++ b/src/log/tap.c @@ -89,8 +89,9 @@ void tap_log_post_test(struct criterion_test_stats *stats) { char *saveptr = NULL; char *line = strtok_r(dup, "\n", &saveptr); #endif + bool sf = criterion_options.short_filename; criterion_important(" %s:%u: Assertion failed: %s\n", - asrt->file, + sf ? basename(asrt->file) : asrt->file, asrt->line, line); #ifdef VANILLA_WIN32 @@ -105,10 +106,11 @@ void tap_log_post_test(struct criterion_test_stats *stats) { } void tap_log_test_crash(struct criterion_test_stats *stats) { + bool sf = criterion_options.short_filename; criterion_important("not ok - %s::%s unexpected signal after %s:%u\n", stats->test->category, stats->test->name, - stats->file, + sf ? basename(stats->file) : stats->file, stats->progress); } diff --git a/src/main.c b/src/main.c index 0402d71..1faa4a3 100644 --- a/src/main.c +++ b/src/main.c @@ -57,6 +57,8 @@ " -f or --fail-fast: exit after the first failure\n" \ " --ascii: don't use fancy unicode symbols " \ "or colors in the output\n" \ + " -S or --short-filename: only display the base " \ + "name of the source file on a failure\n" \ PATTERN_USAGE \ " --tap: enables TAP formatting\n" \ " --always-succeed: always exit with 0\n" \ @@ -124,6 +126,7 @@ int main(int argc, char *argv[]) { {"list", no_argument, 0, 'l'}, {"ascii", no_argument, 0, 'k'}, {"fail-fast", no_argument, 0, 'f'}, + {"short-filename", no_argument, 0, 'S'}, #ifdef HAVE_PCRE {"pattern", required_argument, 0, 'p'}, #endif @@ -146,6 +149,7 @@ int main(int argc, char *argv[]) { opt->fail_fast = !strcmp("1", getenv("CRITERION_FAIL_FAST") ?: "0"); opt->use_ascii = use_ascii; opt->logging_threshold = atoi(getenv("CRITERION_VERBOSITY_LEVEL") ?: "2"); + opt->short_filename = !strcmp("1", getenv("CRITERION_SHORT_FILENAME") ?: "0"); #ifdef HAVE_PCRE opt->pattern = getenv("CRITERION_TEST_PATTERN"); #endif @@ -155,13 +159,14 @@ int main(int argc, char *argv[]) { bool do_list_tests = false; bool do_print_version = false; bool do_print_usage = false; - for (int c; (c = getopt_long(argc, argv, "hvlf", opts, NULL)) != -1;) { + for (int c; (c = getopt_long(argc, argv, "hvlfS", opts, NULL)) != -1;) { switch (c) { case 'b': criterion_options.logging_threshold = atoi(optarg ?: "1"); break; case 'y': criterion_options.always_succeed = true; break; case 'z': criterion_options.no_early_exit = true; break; case 'k': criterion_options.use_ascii = true; break; case 'f': criterion_options.fail_fast = true; break; + case 'S': criterion_options.short_filename = true; break; #ifdef HAVE_PCRE case 'p': criterion_options.pattern = optarg; break; #endif