Skip to content

Commit 59f1e47

Browse files
tiennouethomson
authored andcommitted
Barebones JUnit XML output
1 parent 3a9b963 commit 59f1e47

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

tests/clar.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ static struct {
138138
int report_errors_only;
139139
int exit_on_error;
140140
int report_suite_names;
141+
int write_summary;
141142

142143
struct clar_explicit *explicit;
143144
struct clar_explicit *last_explicit;
@@ -353,6 +354,7 @@ clar_usage(const char *arg)
353354
printf(" -q \tOnly report tests that had an error\n");
354355
printf(" -Q \tQuit as soon as a test fails\n");
355356
printf(" -l \tPrint suite names\n");
357+
printf(" -r \tWrite summary file\n");
356358
exit(-1);
357359
}
358360

@@ -366,7 +368,7 @@ clar_parse_args(int argc, char **argv)
366368
char *argument = argv[i];
367369

368370
if (argument[0] != '-' || argument[1] == '\0'
369-
|| strchr("sixvqQl", argument[1]) == NULL) {
371+
|| strchr("sixvqQlr", argument[1]) == NULL) {
370372
clar_usage(argv[0]);
371373
}
372374
}
@@ -462,6 +464,10 @@ clar_parse_args(int argc, char **argv)
462464
_clar.report_suite_names = 1;
463465
break;
464466

467+
case 'r':
468+
_clar.write_summary = 1;
469+
break;
470+
465471
default:
466472
assert(!"Unexpected commandline argument!");
467473
}
@@ -503,6 +509,8 @@ clar_test_run(void)
503509
return _clar.total_errors;
504510
}
505511

512+
static void clar_summary_write(void);
513+
506514
void
507515
clar_test_shutdown(void)
508516
{
@@ -517,6 +525,9 @@ clar_test_shutdown(void)
517525

518526
clar_unsandbox();
519527

528+
if (_clar.write_summary)
529+
clar_summary_write();
530+
520531
for (explicit = _clar.explicit; explicit; explicit = explicit_next) {
521532
explicit_next = explicit->next;
522533
free(explicit);
@@ -730,3 +741,4 @@ void cl_set_cleanup(void (*cleanup)(void *), void *opaque)
730741
#include "clar/fixtures.h"
731742
#include "clar/fs.h"
732743
#include "clar/print.h"
744+
#include "clar/summary.h"

tests/clar/summary.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
2+
#include <stdio.h>
3+
#include <time.h>
4+
5+
static FILE *summary;
6+
7+
int clar_summary_close_tag(const char *tag, int indent)
8+
{
9+
const char *indt;
10+
11+
if (indent == 0) indt = "";
12+
else if (indent == 1) indt = "\t";
13+
else indt = "\t\t";
14+
15+
return fprintf(summary, "%s</%s>\n", indt, tag);
16+
}
17+
18+
int clar_summary_testsuites(void)
19+
{
20+
return fprintf(summary, "<testsuites>\n");
21+
}
22+
23+
int clar_summary_testsuite(int idn, const char *name, const char *pkg, time_t timestamp, double time, int test_count, int fail_count, int error_count)
24+
{
25+
struct tm *tm = localtime(&timestamp);
26+
char iso_dt[20];
27+
28+
if (strftime(iso_dt, sizeof(iso_dt), "%FT%T", tm) == 0)
29+
return -1;
30+
31+
return fprintf(summary, "\t<testsuite "
32+
" id=\"%d\""
33+
" name=\"%s\""
34+
" package=\"%s\""
35+
" hostname=\"localhost\""
36+
" timestamp=\"%s\""
37+
" time=\"%.2f\""
38+
" tests=\"%d\""
39+
" failures=\"%d\""
40+
" errors=\"%d\">\n",
41+
idn, name, pkg, iso_dt, time, test_count, fail_count, error_count);
42+
}
43+
44+
int clar_summary_testcase(const char *name, const char *classname, double time)
45+
{
46+
return fprintf(summary, "\t\t<testcase name=\"%s\" classname=\"%s\" time=\"%.2f\">\n", name, classname, time);
47+
}
48+
49+
int clar_summary_failure(const char *type, const char *message, const char *desc)
50+
{
51+
return fprintf(summary, "\t\t\t<failure type=\"%s\"><![CDATA[%s\n%s]]></failure>\n", type, message, desc);
52+
}
53+
54+
void clar_summary_write(void)
55+
{
56+
struct clar_report *report;
57+
const char *last_suite = NULL;
58+
char wd[1024];
59+
60+
summary = fopen("summary.xml", "w");
61+
if (!summary) {
62+
printf("failed to open summary.xml for writing\n");
63+
return;
64+
}
65+
66+
clar_summary_testsuites();
67+
68+
report = _clar.reports;
69+
while (report != NULL) {
70+
struct clar_error *error = report->errors;
71+
72+
if (last_suite == NULL || strcmp(last_suite, report->suite) != 0) {
73+
clar_summary_testsuite(0, report->suite, "", time(NULL), 0, _clar.tests_ran, _clar.total_errors, 0);
74+
}
75+
76+
last_suite = report->suite;
77+
78+
clar_summary_testcase(report->test, "what", 0);
79+
80+
while (error != NULL) {
81+
clar_summary_failure("assert", error->error_msg, error->description);
82+
error = error->next;
83+
}
84+
85+
clar_summary_close_tag("testcase", 2);
86+
87+
report = report->next;
88+
89+
if (!report || strcmp(last_suite, report->suite) != 0)
90+
clar_summary_close_tag("testsuite", 1);
91+
}
92+
93+
clar_summary_close_tag("testsuites", 0);
94+
95+
fclose(summary);
96+
97+
printf("written summary file to %s\n", getcwd((char *)&wd, sizeof(wd)));
98+
}

0 commit comments

Comments
 (0)