Skip to content

Commit 6bf0459

Browse files
committed
tests: update clar test runner
Update to the latest main version of clar, which includes improved xml summary output.
1 parent 6374c4d commit 6bf0459

File tree

4 files changed

+86
-42
lines changed

4 files changed

+86
-42
lines changed

tests/clar/clar.c

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <math.h>
1313
#include <stdarg.h>
1414
#include <wchar.h>
15+
#include <time.h>
16+
#include <sys/time.h>
1517

1618
/* required for sandboxing */
1719
#include <sys/types.h>
@@ -86,6 +88,8 @@
8688
typedef struct stat STAT_T;
8789
#endif
8890

91+
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
92+
8993
#include "clar.h"
9094

9195
static void fs_rm(const char *_source);
@@ -117,6 +121,8 @@ struct clar_report {
117121
const char *suite;
118122

119123
enum cl_test_status status;
124+
time_t start;
125+
double elapsed;
120126

121127
struct clar_error *errors;
122128
struct clar_error *last_error;
@@ -145,7 +151,7 @@ static struct {
145151

146152
int report_errors_only;
147153
int exit_on_error;
148-
int report_suite_names;
154+
int verbosity;
149155

150156
int write_summary;
151157
char *summary_filename;
@@ -186,7 +192,7 @@ struct clar_suite {
186192
static void clar_print_init(int test_count, int suite_count, const char *suite_names);
187193
static void clar_print_shutdown(int test_count, int suite_count, int error_count);
188194
static void clar_print_error(int num, const struct clar_report *report, const struct clar_error *error);
189-
static void clar_print_ontest(const char *test_name, int test_number, enum cl_test_status failed);
195+
static void clar_print_ontest(const char *suite_name, const char *test_name, int test_number, enum cl_test_status failed);
190196
static void clar_print_onsuite(const char *suite_name, int suite_index);
191197
static void clar_print_onabort(const char *msg, ...);
192198

@@ -247,10 +253,14 @@ clar_report_all(void)
247253

248254
static void
249255
clar_run_test(
256+
const struct clar_suite *suite,
250257
const struct clar_func *test,
251258
const struct clar_func *initialize,
252259
const struct clar_func *cleanup)
253260
{
261+
struct timeval start, end;
262+
struct timezone tz;
263+
254264
_clar.trampoline_enabled = 1;
255265

256266
CL_TRACE(CL_TRACE__TEST__BEGIN);
@@ -259,16 +269,24 @@ clar_run_test(
259269
if (initialize->ptr != NULL)
260270
initialize->ptr();
261271

272+
_clar.last_report->start = time(NULL);
273+
gettimeofday(&start, &tz);
274+
262275
CL_TRACE(CL_TRACE__TEST__RUN_BEGIN);
263276
test->ptr();
264277
CL_TRACE(CL_TRACE__TEST__RUN_END);
265278
}
266279

280+
gettimeofday(&end, &tz);
281+
267282
_clar.trampoline_enabled = 0;
268283

269284
if (_clar.last_report->status == CL_TEST_NOTRUN)
270285
_clar.last_report->status = CL_TEST_OK;
271286

287+
_clar.last_report->elapsed = ((double)end.tv_sec + (double)end.tv_usec / 1.0E6) -
288+
((double)start.tv_sec + (double)start.tv_usec / 1.0E6);
289+
272290
if (_clar.local_cleanup != NULL)
273291
_clar.local_cleanup(_clar.local_cleanup_payload);
274292

@@ -286,7 +304,7 @@ clar_run_test(
286304
if (_clar.report_errors_only) {
287305
clar_report_errors(_clar.last_report);
288306
} else {
289-
clar_print_ontest(test->name, _clar.tests_ran, _clar.last_report->status);
307+
clar_print_ontest(suite->name, test->name, _clar.tests_ran, _clar.last_report->status);
290308
}
291309
}
292310

@@ -352,7 +370,7 @@ clar_run_suite(const struct clar_suite *suite, const char *filter)
352370

353371
_clar.last_report = report;
354372

355-
clar_run_test(&test[i], &suite->initialize, &suite->cleanup);
373+
clar_run_test(suite, &test[i], &suite->initialize, &suite->cleanup);
356374

357375
if (_clar.exit_on_error && _clar.total_errors)
358376
return;
@@ -427,7 +445,7 @@ clar_parse_args(int argc, char **argv)
427445
++found;
428446

429447
if (!exact)
430-
_clar.report_suite_names = 1;
448+
_clar.verbosity = MAX(_clar.verbosity, 1);
431449

432450
switch (action) {
433451
case 's': {
@@ -486,13 +504,13 @@ clar_parse_args(int argc, char **argv)
486504
}
487505

488506
case 'v':
489-
_clar.report_suite_names = 1;
507+
_clar.verbosity++;
490508
break;
491509

492510
case 'r':
493511
_clar.write_summary = 1;
494512
free(_clar.summary_filename);
495-
_clar.summary_filename = strdup(*(argument + 2) ? (argument + 2) : "summary.xml");
513+
_clar.summary_filename = *(argument + 2) ? strdup(argument + 2) : NULL;
496514
break;
497515

498516
default:
@@ -504,6 +522,8 @@ clar_parse_args(int argc, char **argv)
504522
void
505523
clar_test_init(int argc, char **argv)
506524
{
525+
const char *summary_env;
526+
507527
if (argc > 1)
508528
clar_parse_args(argc, argv);
509529

@@ -513,11 +533,15 @@ clar_test_init(int argc, char **argv)
513533
""
514534
);
515535

516-
if ((_clar.summary_filename = getenv("CLAR_SUMMARY")) != NULL) {
536+
if (!_clar.summary_filename &&
537+
(summary_env = getenv("CLAR_SUMMARY")) != NULL) {
517538
_clar.write_summary = 1;
518-
_clar.summary_filename = strdup(_clar.summary_filename);
539+
_clar.summary_filename = strdup(summary_env);
519540
}
520541

542+
if (_clar.write_summary && !_clar.summary_filename)
543+
_clar.summary_filename = strdup("summary.xml");
544+
521545
if (_clar.write_summary &&
522546
!(_clar.summary = clar_summary_init(_clar.summary_filename))) {
523547
clar_print_onabort("Failed to open the summary file\n");

tests/clar/clar.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ enum cl_test_status {
1313
CL_TEST_OK,
1414
CL_TEST_FAILURE,
1515
CL_TEST_SKIP,
16-
CL_TEST_NOTRUN
16+
CL_TEST_NOTRUN,
1717
};
1818

1919
enum cl_output_format {
2020
CL_OUTPUT_CLAP,
21-
CL_OUTPUT_TAP
21+
CL_OUTPUT_TAP,
2222
};
2323

2424
/** Setup clar environment */
@@ -60,7 +60,7 @@ typedef enum cl_trace_event {
6060
CL_TRACE__TEST__END,
6161
CL_TRACE__TEST__RUN_BEGIN,
6262
CL_TRACE__TEST__RUN_END,
63-
CL_TRACE__TEST__LONGJMP
63+
CL_TRACE__TEST__LONGJMP,
6464
} cl_trace_event;
6565

6666
typedef void (cl_trace_cb)(

tests/clar/clar/print.h

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,35 @@ static void clar_print_clap_error(int num, const struct clar_report *report, con
3636
fflush(stdout);
3737
}
3838

39-
static void clar_print_clap_ontest(const char *test_name, int test_number, enum cl_test_status status)
39+
static void clar_print_clap_ontest(const char *suite_name, const char *test_name, int test_number, enum cl_test_status status)
4040
{
4141
(void)test_name;
4242
(void)test_number;
4343

44-
switch(status) {
45-
case CL_TEST_OK: printf("."); break;
46-
case CL_TEST_FAILURE: printf("F"); break;
47-
case CL_TEST_SKIP: printf("S"); break;
48-
case CL_TEST_NOTRUN: printf("N"); break;
44+
if (_clar.verbosity > 1) {
45+
printf("%s::%s: ", suite_name, test_name);
46+
47+
switch (status) {
48+
case CL_TEST_OK: printf("ok\n"); break;
49+
case CL_TEST_FAILURE: printf("fail\n"); break;
50+
case CL_TEST_SKIP: printf("skipped"); break;
51+
case CL_TEST_NOTRUN: printf("notrun"); break;
52+
}
53+
} else {
54+
switch (status) {
55+
case CL_TEST_OK: printf("."); break;
56+
case CL_TEST_FAILURE: printf("F"); break;
57+
case CL_TEST_SKIP: printf("S"); break;
58+
case CL_TEST_NOTRUN: printf("N"); break;
59+
}
60+
61+
fflush(stdout);
4962
}
50-
51-
fflush(stdout);
5263
}
5364

5465
static void clar_print_clap_onsuite(const char *suite_name, int suite_index)
5566
{
56-
if (_clar.report_suite_names)
67+
if (_clar.verbosity == 1)
5768
printf("\n%s", suite_name);
5869

5970
(void)suite_index;
@@ -102,7 +113,7 @@ static void print_escaped(const char *str)
102113
printf("%s", str);
103114
}
104115

105-
static void clar_print_tap_ontest(const char *test_name, int test_number, enum cl_test_status status)
116+
static void clar_print_tap_ontest(const char *suite_name, const char *test_name, int test_number, enum cl_test_status status)
106117
{
107118
const struct clar_error *error = _clar.last_report->errors;
108119

@@ -111,10 +122,10 @@ static void clar_print_tap_ontest(const char *test_name, int test_number, enum c
111122

112123
switch(status) {
113124
case CL_TEST_OK:
114-
printf("ok %d - %s::%s\n", test_number, _clar.active_suite, test_name);
125+
printf("ok %d - %s::%s\n", test_number, suite_name, test_name);
115126
break;
116127
case CL_TEST_FAILURE:
117-
printf("not ok %d - %s::%s\n", test_number, _clar.active_suite, test_name);
128+
printf("not ok %d - %s::%s\n", test_number, suite_name, test_name);
118129

119130
printf(" ---\n");
120131
printf(" reason: |\n");
@@ -132,7 +143,7 @@ static void clar_print_tap_ontest(const char *test_name, int test_number, enum c
132143
break;
133144
case CL_TEST_SKIP:
134145
case CL_TEST_NOTRUN:
135-
printf("ok %d - # SKIP %s::%s\n", test_number, _clar.active_suite, test_name);
146+
printf("ok %d - # SKIP %s::%s\n", test_number, suite_name, test_name);
136147
break;
137148
}
138149

@@ -181,9 +192,9 @@ static void clar_print_error(int num, const struct clar_report *report, const st
181192
PRINT(error, num, report, error);
182193
}
183194

184-
static void clar_print_ontest(const char *test_name, int test_number, enum cl_test_status status)
195+
static void clar_print_ontest(const char *suite_name, const char *test_name, int test_number, enum cl_test_status status)
185196
{
186-
PRINT(ontest, test_name, test_number, status);
197+
PRINT(ontest, suite_name, test_name, test_number, status);
187198
}
188199

189200
static void clar_print_onsuite(const char *suite_name, int suite_index)

tests/clar/clar/summary.h

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <stdio.h>
33
#include <time.h>
44

5-
static int clar_summary_close_tag(
5+
int clar_summary_close_tag(
66
struct clar_summary *summary, const char *tag, int indent)
77
{
88
const char *indt;
@@ -14,59 +14,65 @@ static int clar_summary_close_tag(
1414
return fprintf(summary->fp, "%s</%s>\n", indt, tag);
1515
}
1616

17-
static int clar_summary_testsuites(struct clar_summary *summary)
17+
int clar_summary_testsuites(struct clar_summary *summary)
1818
{
1919
return fprintf(summary->fp, "<testsuites>\n");
2020
}
2121

22-
static int clar_summary_testsuite(struct clar_summary *summary,
23-
int idn, const char *name, const char *pkg, time_t timestamp,
24-
double elapsed, int test_count, int fail_count, int error_count)
22+
int clar_summary_testsuite(struct clar_summary *summary,
23+
int idn, const char *name, time_t timestamp,
24+
int test_count, int fail_count, int error_count)
2525
{
2626
struct tm *tm = localtime(&timestamp);
2727
char iso_dt[20];
2828

2929
if (strftime(iso_dt, sizeof(iso_dt), "%Y-%m-%dT%H:%M:%S", tm) == 0)
3030
return -1;
3131

32-
return fprintf(summary->fp, "\t<testsuite "
32+
return fprintf(summary->fp, "\t<testsuite"
3333
" id=\"%d\""
3434
" name=\"%s\""
35-
" package=\"%s\""
3635
" hostname=\"localhost\""
3736
" timestamp=\"%s\""
38-
" time=\"%.2f\""
3937
" tests=\"%d\""
4038
" failures=\"%d\""
4139
" errors=\"%d\">\n",
42-
idn, name, pkg, iso_dt, elapsed, test_count, fail_count, error_count);
40+
idn, name, iso_dt, test_count, fail_count, error_count);
4341
}
4442

45-
static int clar_summary_testcase(struct clar_summary *summary,
43+
int clar_summary_testcase(struct clar_summary *summary,
4644
const char *name, const char *classname, double elapsed)
4745
{
4846
return fprintf(summary->fp,
4947
"\t\t<testcase name=\"%s\" classname=\"%s\" time=\"%.2f\">\n",
5048
name, classname, elapsed);
5149
}
5250

53-
static int clar_summary_failure(struct clar_summary *summary,
51+
int clar_summary_failure(struct clar_summary *summary,
5452
const char *type, const char *message, const char *desc)
5553
{
5654
return fprintf(summary->fp,
5755
"\t\t\t<failure type=\"%s\"><![CDATA[%s\n%s]]></failure>\n",
5856
type, message, desc);
5957
}
6058

59+
int clar_summary_skipped(struct clar_summary *summary)
60+
{
61+
return fprintf(summary->fp, "\t\t\t<skipped />\n");
62+
}
63+
6164
struct clar_summary *clar_summary_init(const char *filename)
6265
{
6366
struct clar_summary *summary;
6467
FILE *fp;
6568

66-
if ((fp = fopen(filename, "w")) == NULL)
69+
if ((fp = fopen(filename, "w")) == NULL) {
70+
perror("fopen");
6771
return NULL;
72+
}
6873

6974
if ((summary = malloc(sizeof(struct clar_summary))) == NULL) {
75+
perror("malloc");
7076
fclose(fp);
7177
return NULL;
7278
}
@@ -90,14 +96,14 @@ int clar_summary_shutdown(struct clar_summary *summary)
9096
struct clar_error *error = report->errors;
9197

9298
if (last_suite == NULL || strcmp(last_suite, report->suite) != 0) {
93-
if (clar_summary_testsuite(summary, 0, report->suite, "",
94-
time(NULL), 0, _clar.tests_ran, _clar.total_errors, 0) < 0)
99+
if (clar_summary_testsuite(summary, 0, report->suite,
100+
report->start, _clar.tests_ran, _clar.total_errors, 0) < 0)
95101
goto on_error;
96102
}
97103

98104
last_suite = report->suite;
99105

100-
clar_summary_testcase(summary, report->test, "what", 0);
106+
clar_summary_testcase(summary, report->test, report->suite, report->elapsed);
101107

102108
while (error != NULL) {
103109
if (clar_summary_failure(summary, "assert",
@@ -107,6 +113,9 @@ int clar_summary_shutdown(struct clar_summary *summary)
107113
error = error->next;
108114
}
109115

116+
if (report->status == CL_TEST_SKIP)
117+
clar_summary_skipped(summary);
118+
110119
if (clar_summary_close_tag(summary, "testcase", 2) < 0)
111120
goto on_error;
112121

0 commit comments

Comments
 (0)