Skip to content

Commit 90753a9

Browse files
committed
clar: refactor explicitly run test behavior
Previously, supplying `-s` to explicitly enable some test(s) would run the tests immediately from the argument parser. This forces us to set up the entire clar environment (for example: sandboxing) before argument parsing takes place. Refactor the behavior of `-s` to add the explicitly chosen tests to a list that is executed later. This untangles the argument parsing from the setup lifecycle, allowing us to use the arguments to perform the setup.
1 parent 695067f commit 90753a9

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

tests/clar.c

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,14 @@ struct clar_error {
106106
struct clar_error *next;
107107
};
108108

109-
static struct {
110-
int argc;
111-
char **argv;
109+
struct clar_explicit {
110+
size_t suite_idx;
111+
const char *filter;
112+
113+
struct clar_explicit *next;
114+
};
112115

116+
static struct {
113117
enum cl_test_status test_status;
114118
const char *active_test;
115119
const char *active_suite;
@@ -124,6 +128,9 @@ static struct {
124128
int exit_on_error;
125129
int report_suite_names;
126130

131+
struct clar_explicit *explicit;
132+
struct clar_explicit *last_explicit;
133+
127134
struct clar_error *errors;
128135
struct clar_error *last_error;
129136

@@ -359,7 +366,24 @@ clar_parse_args(int argc, char **argv)
359366
_clar.report_suite_names = 1;
360367

361368
switch (action) {
362-
case 's': _clar_suites[j].enabled = 1; clar_run_suite(&_clar_suites[j], argument); break;
369+
case 's': {
370+
struct clar_explicit *explicit =
371+
calloc(1, sizeof(struct clar_explicit));
372+
assert(explicit);
373+
374+
explicit->suite_idx = j;
375+
explicit->filter = argument;
376+
377+
if (_clar.explicit == NULL)
378+
_clar.explicit = explicit;
379+
380+
if (_clar.last_explicit != NULL)
381+
_clar.last_explicit->next = explicit;
382+
383+
_clar_suites[j].enabled = 1;
384+
_clar.last_explicit = explicit;
385+
break;
386+
}
363387
case 'i': _clar_suites[j].enabled = 1; break;
364388
case 'x': _clar_suites[j].enabled = 0; break;
365389
}
@@ -412,23 +436,25 @@ clar_test_init(int argc, char **argv)
412436
""
413437
);
414438

439+
if (argc > 1)
440+
clar_parse_args(argc, argv);
441+
415442
if (clar_sandbox() < 0) {
416443
clar_print_onabort("Failed to sandbox the test runner.\n");
417444
exit(-1);
418445
}
419-
420-
_clar.argc = argc;
421-
_clar.argv = argv;
422446
}
423447

424448
int
425449
clar_test_run(void)
426450
{
427-
if (_clar.argc > 1)
428-
clar_parse_args(_clar.argc, _clar.argv);
451+
size_t i;
452+
struct clar_explicit *explicit;
429453

430-
if (!_clar.suites_ran) {
431-
size_t i;
454+
if (_clar.explicit) {
455+
for (explicit = _clar.explicit; explicit; explicit = explicit->next)
456+
clar_run_suite(&_clar_suites[explicit->suite_idx], explicit->filter);
457+
} else {
432458
for (i = 0; i < _clar_suite_count; ++i)
433459
clar_run_suite(&_clar_suites[i], NULL);
434460
}
@@ -439,13 +465,20 @@ clar_test_run(void)
439465
void
440466
clar_test_shutdown(void)
441467
{
468+
struct clar_explicit *explicit, *explicit_next;
469+
442470
clar_print_shutdown(
443471
_clar.tests_ran,
444472
(int)_clar_suite_count,
445473
_clar.total_errors
446474
);
447475

448476
clar_unsandbox();
477+
478+
for (explicit = _clar.explicit; explicit; explicit = explicit_next) {
479+
explicit_next = explicit->next;
480+
free(explicit);
481+
}
449482
}
450483

451484
int

0 commit comments

Comments
 (0)