Merge pull request #2044 from RincewindsHat/fix/fedora-rpm-build
[monitoring-plugins.git] / tap / tap.c
blob00ceab279d594c3a42c54bf032633c667775ce6a
1 /*-
2 * Copyright (c) 2004 Nik Clayton
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
27 #include <ctype.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
32 #include "tap.h"
34 static int no_plan = 0;
35 static int skip_all = 0;
36 static int have_plan = 0;
37 static unsigned int test_count = 0; /* Number of tests that have been run */
38 static unsigned int e_tests = 0; /* Expected number of tests to run */
39 static unsigned int failures = 0; /* Number of tests that failed */
40 static char *todo_msg = NULL;
41 static char *todo_msg_fixed = "libtap malloc issue";
42 static int todo = 0;
43 static int test_died = 0;
45 /* Encapsulate the pthread code in a conditional. In the absence of
46 libpthread the code does nothing */
47 #ifdef HAVE_LIBPTHREAD
48 # include <pthread.h>
49 static pthread_mutex_t M = PTHREAD_MUTEX_INITIALIZER;
50 # define LOCK pthread_mutex_lock(&M);
51 # define UNLOCK pthread_mutex_unlock(&M);
52 #else
53 # define LOCK
54 # define UNLOCK
55 #endif
57 static void _expected_tests(unsigned int);
58 static void _tap_init(void);
59 static void _cleanup(void);
62 * Generate a test result.
64 * ok -- boolean, indicates whether or not the test passed.
65 * test_name -- the name of the test, may be NULL
66 * test_comment -- a comment to print afterwards, may be NULL
68 unsigned int _gen_result(int ok, const char *func, char *file, unsigned int line, char *test_name, ...) {
69 va_list ap;
70 char *local_test_name = NULL;
71 char *c;
72 int name_is_digits;
74 LOCK;
76 test_count++;
78 /* Start by taking the test name and performing any printf()
79 expansions on it */
80 if (test_name != NULL) {
81 va_start(ap, test_name);
82 vasprintf(&local_test_name, test_name, ap);
83 va_end(ap);
85 /* Make sure the test name contains more than digits
86 and spaces. Emit an error message and exit if it
87 does */
88 if (local_test_name) {
89 name_is_digits = 1;
90 for (c = local_test_name; *c != '\0'; c++) {
91 if (!isdigit(*c) && !isspace(*c)) {
92 name_is_digits = 0;
93 break;
97 if (name_is_digits) {
98 diag(" You named your test '%s'. You shouldn't use numbers for your test names.", local_test_name);
99 diag(" Very confusing.");
104 if (!ok) {
105 printf("not ");
106 failures++;
109 printf("ok %d", test_count);
111 if (test_name != NULL) {
112 printf(" - ");
114 /* Print the test name, escaping any '#' characters it
115 might contain */
116 if (local_test_name != NULL) {
117 flockfile(stdout);
118 for (c = local_test_name; *c != '\0'; c++) {
119 if (*c == '#')
120 fputc('\\', stdout);
121 fputc((int)*c, stdout);
123 funlockfile(stdout);
124 } else { /* vasprintf() failed, use a fixed message */
125 printf("%s", todo_msg_fixed);
129 /* If we're in a todo_start() block then flag the test as being
130 TODO. todo_msg should contain the message to print at this
131 point. If it's NULL then asprintf() failed, and we should
132 use the fixed message.
134 This is not counted as a failure, so decrement the counter if
135 the test failed. */
136 if (todo) {
137 printf(" # TODO %s", todo_msg ? todo_msg : todo_msg_fixed);
138 if (!ok)
139 failures--;
142 printf("\n");
144 if (!ok)
145 diag(" Failed %stest (%s:%s() at line %d)", todo ? "(TODO) " : "", file, func, line);
147 free(local_test_name);
149 UNLOCK;
151 /* We only care (when testing) that ok is positive, but here we
152 specifically only want to return 1 or 0 */
153 return ok ? 1 : 0;
157 * Initialise the TAP library. Will only do so once, however many times it's
158 * called.
160 void _tap_init(void) {
161 static int run_once = 0;
163 LOCK;
165 if (!run_once) {
166 atexit(_cleanup);
168 /* stdout needs to be unbuffered so that the output appears
169 in the same place relative to stderr output as it does
170 with Test::Harness */
171 setbuf(stdout, 0);
172 run_once = 1;
175 UNLOCK;
179 * Note that there's no plan.
181 int plan_no_plan(void) {
183 LOCK;
185 _tap_init();
187 if (have_plan != 0) {
188 fprintf(stderr, "You tried to plan twice!\n");
189 test_died = 1;
190 UNLOCK;
191 exit(255);
194 have_plan = 1;
195 no_plan = 1;
197 UNLOCK;
199 return 0;
203 * Note that the plan is to skip all tests
205 int plan_skip_all(char *reason) {
207 LOCK;
209 _tap_init();
211 skip_all = 1;
213 printf("1..0");
215 if (reason != NULL)
216 printf(" # Skip %s", reason);
218 printf("\n");
220 UNLOCK;
222 exit(0);
226 * Note the number of tests that will be run.
228 int plan_tests(unsigned int tests) {
230 LOCK;
232 _tap_init();
234 if (have_plan != 0) {
235 fprintf(stderr, "You tried to plan twice!\n");
236 test_died = 1;
237 UNLOCK;
238 exit(255);
241 if (tests == 0) {
242 fprintf(stderr, "You said to run 0 tests! You've got to run something.\n");
243 test_died = 1;
244 UNLOCK;
245 exit(255);
248 have_plan = 1;
250 _expected_tests(tests);
252 UNLOCK;
254 return 0;
257 unsigned int diag(char *fmt, ...) {
258 va_list ap;
260 LOCK;
262 fputs("# ", stderr);
264 va_start(ap, fmt);
265 vfprintf(stderr, fmt, ap);
266 va_end(ap);
268 fputs("\n", stderr);
270 UNLOCK;
272 return 0;
275 void _expected_tests(unsigned int tests) {
277 LOCK;
279 printf("1..%d\n", tests);
280 e_tests = tests;
282 UNLOCK;
285 int skip(unsigned int n, char *fmt, ...) {
286 va_list ap;
287 char *skip_msg;
289 LOCK;
291 va_start(ap, fmt);
292 asprintf(&skip_msg, fmt, ap);
293 va_end(ap);
295 while (n-- > 0) {
296 test_count++;
297 printf("ok %d # skip %s\n", test_count, skip_msg != NULL ? skip_msg : "libtap():malloc() failed");
300 free(skip_msg);
302 UNLOCK;
304 return 1;
307 void todo_start(char *fmt, ...) {
308 va_list ap;
310 LOCK;
312 va_start(ap, fmt);
313 vasprintf(&todo_msg, fmt, ap);
314 va_end(ap);
316 todo = 1;
318 UNLOCK;
321 void todo_end(void) {
323 LOCK;
325 todo = 0;
326 free(todo_msg);
328 UNLOCK;
331 int exit_status(void) {
332 int r;
334 LOCK;
336 /* If there's no plan, just return the number of failures */
337 if (no_plan || !have_plan) {
338 UNLOCK;
339 return failures;
342 /* Ran too many tests? Return the number of tests that were run
343 that shouldn't have been */
344 if (e_tests < test_count) {
345 r = test_count - e_tests;
346 UNLOCK;
347 return r;
350 /* Return the number of tests that failed + the number of tests
351 that weren't run */
352 r = failures + e_tests - test_count;
353 UNLOCK;
355 return r;
359 * Cleanup at the end of the run, produce any final output that might be
360 * required.
362 void _cleanup(void) {
364 LOCK;
366 /* If plan_no_plan() wasn't called, and we don't have a plan,
367 and we're not skipping everything, then something happened
368 before we could produce any output */
369 if (!no_plan && !have_plan && !skip_all) {
370 diag("Looks like your test died before it could output anything.");
371 UNLOCK;
372 return;
375 if (test_died) {
376 diag("Looks like your test died just after %d.", test_count);
377 UNLOCK;
378 return;
381 /* No plan provided, but now we know how many tests were run, and can
382 print the header at the end */
383 if (!skip_all && (no_plan || !have_plan)) {
384 printf("1..%d\n", test_count);
387 if ((have_plan && !no_plan) && e_tests < test_count) {
388 diag("Looks like you planned %d tests but ran %d extra.", e_tests, test_count - e_tests);
389 UNLOCK;
390 return;
393 if ((have_plan || !no_plan) && e_tests > test_count) {
394 diag("Looks like you planned %d tests but only ran %d.", e_tests, test_count);
395 UNLOCK;
396 return;
399 if (failures)
400 diag("Looks like you failed %d tests of %d.", failures, test_count);
402 UNLOCK;