Skip to content

Commit 14c1156

Browse files
authored
Merge pull request libgit2#6459 from libgit2/ethomson/clar_update
tests: update clar test runner
2 parents 6374c4d + ed26a90 commit 14c1156

File tree

4 files changed

+109
-37
lines changed

4 files changed

+109
-37
lines changed

tests/clar/clar.c

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <math.h>
1313
#include <stdarg.h>
1414
#include <wchar.h>
15+
#include <time.h>
1516

1617
/* required for sandboxing */
1718
#include <sys/types.h>
@@ -86,6 +87,8 @@
8687
typedef struct stat STAT_T;
8788
#endif
8889

90+
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
91+
8992
#include "clar.h"
9093

9194
static void fs_rm(const char *_source);
@@ -117,6 +120,8 @@ struct clar_report {
117120
const char *suite;
118121

119122
enum cl_test_status status;
123+
time_t start;
124+
double elapsed;
120125

121126
struct clar_error *errors;
122127
struct clar_error *last_error;
@@ -145,7 +150,7 @@ static struct {
145150

146151
int report_errors_only;
147152
int exit_on_error;
148-
int report_suite_names;
153+
int verbosity;
149154

150155
int write_summary;
151156
char *summary_filename;
@@ -186,7 +191,7 @@ struct clar_suite {
186191
static void clar_print_init(int test_count, int suite_count, const char *suite_names);
187192
static void clar_print_shutdown(int test_count, int suite_count, int error_count);
188193
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);
194+
static void clar_print_ontest(const char *suite_name, const char *test_name, int test_number, enum cl_test_status failed);
190195
static void clar_print_onsuite(const char *suite_name, int suite_index);
191196
static void clar_print_onabort(const char *msg, ...);
192197

@@ -245,16 +250,53 @@ clar_report_all(void)
245250
}
246251
}
247252

253+
#ifdef WIN32
254+
# define clar_time DWORD
255+
256+
static void clar_time_now(clar_time *out)
257+
{
258+
*out = GetTickCount();
259+
}
260+
261+
static double clar_time_diff(clar_time *start, clar_time *end)
262+
{
263+
return ((double)*end - (double)*start) / 1000;
264+
}
265+
#else
266+
# include <sys/time.h>
267+
268+
# define clar_time struct timeval
269+
270+
static void clar_time_now(clar_time *out)
271+
{
272+
struct timezone tz;
273+
274+
gettimeofday(out, &tz);
275+
}
276+
277+
static double clar_time_diff(clar_time *start, clar_time *end)
278+
{
279+
return ((double)end->tv_sec + (double)end->tv_usec / 1.0E6) -
280+
((double)start->tv_sec + (double)start->tv_usec / 1.0E6);
281+
}
282+
#endif
283+
248284
static void
249285
clar_run_test(
286+
const struct clar_suite *suite,
250287
const struct clar_func *test,
251288
const struct clar_func *initialize,
252289
const struct clar_func *cleanup)
253290
{
291+
clar_time start, end;
292+
254293
_clar.trampoline_enabled = 1;
255294

256295
CL_TRACE(CL_TRACE__TEST__BEGIN);
257296

297+
_clar.last_report->start = time(NULL);
298+
clar_time_now(&start);
299+
258300
if (setjmp(_clar.trampoline) == 0) {
259301
if (initialize->ptr != NULL)
260302
initialize->ptr();
@@ -264,11 +306,15 @@ clar_run_test(
264306
CL_TRACE(CL_TRACE__TEST__RUN_END);
265307
}
266308

309+
clar_time_now(&end);
310+
267311
_clar.trampoline_enabled = 0;
268312

269313
if (_clar.last_report->status == CL_TEST_NOTRUN)
270314
_clar.last_report->status = CL_TEST_OK;
271315

316+
_clar.last_report->elapsed = clar_time_diff(&start, &end);
317+
272318
if (_clar.local_cleanup != NULL)
273319
_clar.local_cleanup(_clar.local_cleanup_payload);
274320

@@ -286,7 +332,7 @@ clar_run_test(
286332
if (_clar.report_errors_only) {
287333
clar_report_errors(_clar.last_report);
288334
} else {
289-
clar_print_ontest(test->name, _clar.tests_ran, _clar.last_report->status);
335+
clar_print_ontest(suite->name, test->name, _clar.tests_ran, _clar.last_report->status);
290336
}
291337
}
292338

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

353399
_clar.last_report = report;
354400

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

357403
if (_clar.exit_on_error && _clar.total_errors)
358404
return;
@@ -427,7 +473,7 @@ clar_parse_args(int argc, char **argv)
427473
++found;
428474

429475
if (!exact)
430-
_clar.report_suite_names = 1;
476+
_clar.verbosity = MAX(_clar.verbosity, 1);
431477

432478
switch (action) {
433479
case 's': {
@@ -486,13 +532,13 @@ clar_parse_args(int argc, char **argv)
486532
}
487533

488534
case 'v':
489-
_clar.report_suite_names = 1;
535+
_clar.verbosity++;
490536
break;
491537

492538
case 'r':
493539
_clar.write_summary = 1;
494540
free(_clar.summary_filename);
495-
_clar.summary_filename = strdup(*(argument + 2) ? (argument + 2) : "summary.xml");
541+
_clar.summary_filename = *(argument + 2) ? strdup(argument + 2) : NULL;
496542
break;
497543

498544
default:
@@ -504,6 +550,8 @@ clar_parse_args(int argc, char **argv)
504550
void
505551
clar_test_init(int argc, char **argv)
506552
{
553+
const char *summary_env;
554+
507555
if (argc > 1)
508556
clar_parse_args(argc, argv);
509557

@@ -513,11 +561,15 @@ clar_test_init(int argc, char **argv)
513561
""
514562
);
515563

516-
if ((_clar.summary_filename = getenv("CLAR_SUMMARY")) != NULL) {
564+
if (!_clar.summary_filename &&
565+
(summary_env = getenv("CLAR_SUMMARY")) != NULL) {
517566
_clar.write_summary = 1;
518-
_clar.summary_filename = strdup(_clar.summary_filename);
567+
_clar.summary_filename = strdup(summary_env);
519568
}
520569

570+
if (_clar.write_summary && !_clar.summary_filename)
571+
_clar.summary_filename = strdup("summary.xml");
572+
521573
if (_clar.write_summary &&
522574
!(_clar.summary = clar_summary_init(_clar.summary_filename))) {
523575
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: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,24 @@ static int clar_summary_testsuites(struct clar_summary *summary)
2020
}
2121

2222
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)
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

4543
static int clar_summary_testcase(struct clar_summary *summary,
@@ -58,15 +56,23 @@ static int clar_summary_failure(struct clar_summary *summary,
5856
type, message, desc);
5957
}
6058

59+
static 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)