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);
46 static inline void flockfile(FILE *filehandle
)
51 static inline 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
64 unsigned int _gen_result(
65 int ok
, const char *func
, const char *file
, unsigned int line
, const char *test_name
, ...)
68 char *local_test_name
= NULL
;
76 /* Start by taking the test name and performing any printf()
78 if (test_name
!= NULL
) {
79 va_start(ap
, test_name
);
80 if (vasprintf(&local_test_name
, test_name
, ap
) == -1) {
81 local_test_name
= NULL
;
85 /* Make sure the test name contains more than digits
86 and spaces. Emit an error message and exit if it
88 if (local_test_name
) {
90 for (c
= local_test_name
; *c
!= '\0'; c
++) {
91 if (!isdigit((unsigned char) *c
) && !isspace((unsigned char) *c
)) {
98 diag(" You named your test '%s'. You shouldn't use numbers for your test names.",
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) " : "",
155 free(local_test_name
);
159 /* We only care (when testing) that ok is positive, but here we
160 specifically only want to return 1 or 0 */
165 * Initialise the TAP library. Will only do so once, however many times it's
170 static int run_once
= 0;
175 /* stdout needs to be unbuffered so that the output appears
176 in the same place relative to stderr output as it does
177 with Test::Harness */
184 * Note that there's no plan.
186 int plan_no_plan(void)
192 if (have_plan
!= 0) {
193 fprintf(stderr
, "You tried to plan twice!\n");
208 * Note that the plan is to skip all tests
210 int plan_skip_all(const char *reason
)
221 printf(" # Skip %s", reason
);
231 * Note the number of tests that will be run.
233 int plan_tests(unsigned int tests
)
239 if (have_plan
!= 0) {
240 fprintf(stderr
, "You tried to plan twice!\n");
247 fprintf(stderr
, "You said to run 0 tests! You've got to run something.\n");
255 _expected_tests(tests
);
262 unsigned int diag(const char *fmt
, ...)
269 vfprintf(stderr
, fmt
, ap
);
277 void diag_multiline(const char *val
)
279 size_t len
, i
, line_start_idx
= 0;
284 for (i
= 0; i
< len
; i
++) {
287 if (val
[i
] != '\n') {
291 assert((i
- line_start_idx
+ 1) <= INT_MAX
);
292 line_length
= i
- line_start_idx
+ 1;
293 fprintf(stderr
, "# %.*s", line_length
, &val
[line_start_idx
]);
294 line_start_idx
= i
+ 1;
298 void _expected_tests(unsigned int tests
)
300 printf("1..%d\n", tests
);
304 int skip(unsigned int n
, const char *fmt
, ...)
307 char *skip_msg
= NULL
;
312 if (vasprintf(&skip_msg
, fmt
, ap
) == -1) {
319 printf("ok %d # skip %s\n",
321 skip_msg
!= NULL
? skip_msg
: "libtap():malloc() failed");
331 void todo_start(const char *fmt
, ...)
338 if (vasprintf(&todo_msg
, fmt
, ap
) == -1) {
358 int exit_status(void)
364 /* If there's no plan, just return the number of failures */
365 if (no_plan
|| !have_plan
) {
370 /* Ran too many tests? Return the number of tests that were run
371 that shouldn't have been */
372 if (e_tests
< test_count
) {
373 r
= test_count
- e_tests
;
378 /* Return the number of tests that failed + the number of tests
380 r
= failures
+ e_tests
- test_count
;
387 * Cleanup at the end of the run, produce any final output that might be
394 /* If plan_no_plan() wasn't called, and we don't have a plan,
395 and we're not skipping everything, then something happened
396 before we could produce any output */
397 if (!no_plan
&& !have_plan
&& !skip_all
) {
398 diag("Looks like your test died before it could output anything.");
404 diag("Looks like your test died just after %d.", test_count
);
409 /* No plan provided, but now we know how many tests were run, and can
410 print the header at the end */
411 if (!skip_all
&& (no_plan
|| !have_plan
)) {
412 printf("1..%d\n", test_count
);
415 if ((have_plan
&& !no_plan
) && e_tests
< test_count
) {
416 diag("Looks like you planned %d %s but ran %d extra.",
418 e_tests
== 1 ? "test" : "tests",
419 test_count
- e_tests
);
424 if ((have_plan
|| !no_plan
) && e_tests
> test_count
) {
425 diag("Looks like you planned %d %s but only ran %d.",
427 e_tests
== 1 ? "test" : "tests",
434 diag("Looks like you failed %d %s of %d.",
436 failures
== 1 ? "test" : "tests",