2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (C) 2004 Nik Clayton
5 * Copyright (C) 2017 Jérémie Galarneau
18 static int no_plan
= 0;
19 static int skip_all
= 0;
20 static int have_plan
= 0;
21 static unsigned int test_count
= 0; /* Number of tests that have been run */
22 static unsigned int e_tests
= 0; /* Expected number of tests to run */
23 static unsigned int failures
= 0; /* Number of tests that failed */
24 static char *todo_msg
= NULL
;
25 static const char *todo_msg_fixed
= "libtap malloc issue";
27 static int test_died
= 0;
29 /* Encapsulate the pthread code in a conditional. In the absence of
30 libpthread the code does nothing */
31 #ifdef HAVE_LIBPTHREAD
33 static pthread_mutex_t M
= PTHREAD_MUTEX_INITIALIZER
;
34 # define LOCK pthread_mutex_lock(&M);
35 # define UNLOCK pthread_mutex_unlock(&M);
41 static void _expected_tests(unsigned int);
42 static void _tap_init(void);
43 static void _cleanup(void);
47 void flockfile (FILE * filehandle
) {
52 void funlockfile(FILE * filehandle
) {
58 * Generate a test result.
60 * ok -- boolean, indicates whether or not the test passed.
61 * test_name -- the name of the test, may be NULL
62 * test_comment -- a comment to print afterwards, may be NULL
65 _gen_result(int ok
, const char *func
, const char *file
, unsigned int line
,
66 const char *test_name
, ...)
69 char *local_test_name
= NULL
;
77 /* Start by taking the test name and performing any printf()
79 if(test_name
!= NULL
) {
80 va_start(ap
, test_name
);
81 if (vasprintf(&local_test_name
, test_name
, ap
) == -1) {
82 local_test_name
= NULL
;
86 /* Make sure the test name contains more than digits
87 and spaces. Emit an error message and exit if it
91 for(c
= local_test_name
; *c
!= '\0'; c
++) {
92 if(!isdigit((unsigned char) *c
) && !isspace((unsigned char) *c
)) {
99 diag(" You named your test '%s'. You shouldn't use numbers for your test names.", local_test_name
);
100 diag(" Very confusing.");
110 printf("ok %d", test_count
);
112 if(test_name
!= NULL
) {
115 /* Print the test name, escaping any '#' characters it
117 if(local_test_name
!= NULL
) {
119 for(c
= local_test_name
; *c
!= '\0'; c
++) {
122 fputc((int)*c
, stdout
);
125 } else { /* vasprintf() failed, use a fixed message */
126 printf("%s", todo_msg_fixed
);
130 /* If we're in a todo_start() block then flag the test as being
131 TODO. todo_msg should contain the message to print at this
132 point. If it's NULL then asprintf() failed, and we should
133 use the fixed message.
135 This is not counted as a failure, so decrement the counter if
138 printf(" # TODO %s", todo_msg
? todo_msg
: todo_msg_fixed
);
146 if(getenv("HARNESS_ACTIVE") != NULL
)
149 diag(" Failed %stest (%s:%s() at line %d)",
150 todo
? "(TODO) " : "", file
, func
, line
);
152 free(local_test_name
);
156 /* We only care (when testing) that ok is positive, but here we
157 specifically only want to return 1 or 0 */
162 * Initialise the TAP library. Will only do so once, however many times it's
168 static int run_once
= 0;
173 /* stdout needs to be unbuffered so that the output appears
174 in the same place relative to stderr output as it does
175 with Test::Harness */
182 * Note that there's no plan.
193 fprintf(stderr
, "You tried to plan twice!\n");
208 * Note that the plan is to skip all tests
211 plan_skip_all(const char *reason
)
223 printf(" # Skip %s", reason
);
233 * Note the number of tests that will be run.
236 plan_tests(unsigned int tests
)
244 fprintf(stderr
, "You tried to plan twice!\n");
251 fprintf(stderr
, "You said to run 0 tests! You've got to run something.\n");
259 _expected_tests(tests
);
267 diag(const char *fmt
, ...)
274 vfprintf(stderr
, fmt
, ap
);
283 diag_multiline(const char *val
)
285 size_t len
, i
, line_start_idx
= 0;
290 for (i
= 0; i
< len
; i
++) {
293 if (val
[i
] != '\n') {
297 assert((i
- line_start_idx
+ 1) <= INT_MAX
);
298 line_length
= i
- line_start_idx
+ 1;
299 fprintf(stderr
, "# %.*s", line_length
, &val
[line_start_idx
]);
300 line_start_idx
= i
+ 1;
305 _expected_tests(unsigned int tests
)
308 printf("1..%d\n", tests
);
313 skip(unsigned int n
, const char *fmt
, ...)
316 char *skip_msg
= NULL
;
321 if (asprintf(&skip_msg
, fmt
, ap
) == -1) {
328 printf("ok %d # skip %s\n", test_count
,
330 skip_msg
: "libtap():malloc() failed");
341 todo_start(const char *fmt
, ...)
348 if (vasprintf(&todo_msg
, fmt
, ap
) == -1) {
377 /* If there's no plan, just return the number of failures */
378 if(no_plan
|| !have_plan
) {
383 /* Ran too many tests? Return the number of tests that were run
384 that shouldn't have been */
385 if(e_tests
< test_count
) {
386 r
= test_count
- e_tests
;
391 /* Return the number of tests that failed + the number of tests
393 r
= failures
+ e_tests
- test_count
;
400 * Cleanup at the end of the run, produce any final output that might be
409 /* If plan_no_plan() wasn't called, and we don't have a plan,
410 and we're not skipping everything, then something happened
411 before we could produce any output */
412 if(!no_plan
&& !have_plan
&& !skip_all
) {
413 diag("Looks like your test died before it could output anything.");
419 diag("Looks like your test died just after %d.", test_count
);
425 /* No plan provided, but now we know how many tests were run, and can
426 print the header at the end */
427 if(!skip_all
&& (no_plan
|| !have_plan
)) {
428 printf("1..%d\n", test_count
);
431 if((have_plan
&& !no_plan
) && e_tests
< test_count
) {
432 diag("Looks like you planned %d %s but ran %d extra.",
433 e_tests
, e_tests
== 1 ? "test" : "tests", test_count
- e_tests
);
438 if((have_plan
|| !no_plan
) && e_tests
> test_count
) {
439 diag("Looks like you planned %d %s but only ran %d.",
440 e_tests
, e_tests
== 1 ? "test" : "tests", test_count
);
446 diag("Looks like you failed %d %s of %d.",
447 failures
, failures
== 1 ? "test" : "tests", test_count
);