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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
diff --git a/tests/clar.c b/tests/clar.c
index d5212d1..bef7c00 100644
--- a/tests/clar.c
+++ b/tests/clar.c
@@ -106,10 +106,14 @@ struct clar_error {
struct clar_error *next;
};
-static struct {
- int argc;
- char **argv;
+struct clar_explicit {
+ size_t suite_idx;
+ const char *filter;
+
+ struct clar_explicit *next;
+};
+static struct {
enum cl_test_status test_status;
const char *active_test;
const char *active_suite;
@@ -124,6 +128,9 @@ static struct {
int exit_on_error;
int report_suite_names;
+ struct clar_explicit *explicit;
+ struct clar_explicit *last_explicit;
+
struct clar_error *errors;
struct clar_error *last_error;
@@ -359,7 +366,24 @@ clar_parse_args(int argc, char **argv)
_clar.report_suite_names = 1;
switch (action) {
- case 's': _clar_suites[j].enabled = 1; clar_run_suite(&_clar_suites[j], argument); break;
+ case 's': {
+ struct clar_explicit *explicit =
+ calloc(1, sizeof(struct clar_explicit));
+ assert(explicit);
+
+ explicit->suite_idx = j;
+ explicit->filter = argument;
+
+ if (_clar.explicit == NULL)
+ _clar.explicit = explicit;
+
+ if (_clar.last_explicit != NULL)
+ _clar.last_explicit->next = explicit;
+
+ _clar_suites[j].enabled = 1;
+ _clar.last_explicit = explicit;
+ break;
+ }
case 'i': _clar_suites[j].enabled = 1; break;
case 'x': _clar_suites[j].enabled = 0; break;
}
@@ -412,23 +436,25 @@ clar_test_init(int argc, char **argv)
""
);
+ if (argc > 1)
+ clar_parse_args(argc, argv);
+
if (clar_sandbox() < 0) {
clar_print_onabort("Failed to sandbox the test runner.\n");
exit(-1);
}
-
- _clar.argc = argc;
- _clar.argv = argv;
}
int
clar_test_run(void)
{
- if (_clar.argc > 1)
- clar_parse_args(_clar.argc, _clar.argv);
+ size_t i;
+ struct clar_explicit *explicit;
- if (!_clar.suites_ran) {
- size_t i;
+ if (_clar.explicit) {
+ for (explicit = _clar.explicit; explicit; explicit = explicit->next)
+ clar_run_suite(&_clar_suites[explicit->suite_idx], explicit->filter);
+ } else {
for (i = 0; i < _clar_suite_count; ++i)
clar_run_suite(&_clar_suites[i], NULL);
}
@@ -439,6 +465,8 @@ clar_test_run(void)
void
clar_test_shutdown(void)
{
+ struct clar_explicit *explicit, *explicit_next;
+
clar_print_shutdown(
_clar.tests_ran,
(int)_clar_suite_count,
@@ -446,6 +474,11 @@ clar_test_shutdown(void)
);
clar_unsandbox();
+
+ for (explicit = _clar.explicit; explicit; explicit = explicit_next) {
+ explicit_next = explicit->next;
+ free(explicit);
+ }
}
int