]> git.ozlabs.org Git - ccan/blob - tap/tap.c
First primitive cut of ccanlint
[ccan] / tap / tap.c
1 /*-
2  * Copyright (c) 2004 Nik Clayton
3  * All rights reserved.
4  *
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.
13  *
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.
25  */
26 #define _GNU_SOURCE
27 #include <ctype.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "tap.h"
33
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;
44
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
56
57 static void
58 _expected_tests(unsigned int tests)
59 {
60
61         printf("1..%d\n", tests);
62         e_tests = tests;
63 }
64
65 static void
66 diagv(char *fmt, va_list ap)
67 {
68         fputs("# ", stderr);
69         vfprintf(stderr, fmt, ap);
70         fputs("\n", stderr);
71 }
72
73 static void
74 _diag(char *fmt, ...)
75 {
76         va_list ap;
77         va_start(ap, fmt);
78         diagv(fmt, ap);
79         va_end(ap);
80 }
81
82 /*
83  * Generate a test result.
84  *
85  * ok -- boolean, indicates whether or not the test passed.
86  * test_name -- the name of the test, may be NULL
87  * test_comment -- a comment to print afterwards, may be NULL
88  */
89 unsigned int
90 _gen_result(int ok, const char *func, char *file, unsigned int line, 
91             char *test_name, ...)
92 {
93         va_list ap;
94         char *local_test_name = NULL;
95         char *c;
96         int name_is_digits;
97
98         LOCK;
99
100         test_count++;
101
102         /* Start by taking the test name and performing any printf()
103            expansions on it */
104         if(test_name != NULL) {
105                 va_start(ap, test_name);
106                 vasprintf(&local_test_name, test_name, ap);
107                 va_end(ap);
108
109                 /* Make sure the test name contains more than digits
110                    and spaces.  Emit an error message and exit if it
111                    does */
112                 if(local_test_name) {
113                         name_is_digits = 1;
114                         for(c = local_test_name; *c != '\0'; c++) {
115                                 if(!isdigit(*c) && !isspace(*c)) {
116                                         name_is_digits = 0;
117                                         break;
118                                 }
119                         }
120
121                         if(name_is_digits) {
122                                 _diag("    You named your test '%s'.  You shouldn't use numbers for your test names.", local_test_name);
123                                 _diag("    Very confusing.");
124                         }
125                 }
126         }
127
128         if(!ok) {
129                 printf("not ");
130                 failures++;
131         }
132
133         printf("ok %d", test_count);
134
135         if(test_name != NULL) {
136                 printf(" - ");
137
138                 /* Print the test name, escaping any '#' characters it
139                    might contain */
140                 if(local_test_name != NULL) {
141                         flockfile(stdout);
142                         for(c = local_test_name; *c != '\0'; c++) {
143                                 if(*c == '#')
144                                         fputc('\\', stdout);
145                                 fputc((int)*c, stdout);
146                         }
147                         funlockfile(stdout);
148                 } else {        /* vasprintf() failed, use a fixed message */
149                         printf("%s", todo_msg_fixed);
150                 }
151         }
152
153         /* If we're in a todo_start() block then flag the test as being
154            TODO.  todo_msg should contain the message to print at this
155            point.  If it's NULL then asprintf() failed, and we should
156            use the fixed message.
157
158            This is not counted as a failure, so decrement the counter if
159            the test failed. */
160         if(todo) {
161                 printf(" # TODO %s", todo_msg ? todo_msg : todo_msg_fixed);
162                 if(!ok)
163                         failures--;
164         }
165
166         printf("\n");
167
168         if(!ok)
169                 _diag("    Failed %stest (%s:%s() at line %d)", 
170                       todo ? "(TODO) " : "", file, func, line);
171
172         free(local_test_name);
173
174         UNLOCK;
175
176         /* We only care (when testing) that ok is positive, but here we
177            specifically only want to return 1 or 0 */
178         return ok ? 1 : 0;
179 }
180
181 /*
182  * Cleanup at the end of the run, produce any final output that might be
183  * required.
184  */
185 static void
186 _cleanup(void)
187 {
188
189         LOCK;
190
191         /* If plan_no_plan() wasn't called, and we don't have a plan,
192            and we're not skipping everything, then something happened
193            before we could produce any output */
194         if(!no_plan && !have_plan && !skip_all) {
195                 _diag("Looks like your test died before it could output anything.");
196                 UNLOCK;
197                 return;
198         }
199
200         if(test_died) {
201                 _diag("Looks like your test died just after %d.", test_count);
202                 UNLOCK;
203                 return;
204         }
205
206
207         /* No plan provided, but now we know how many tests were run, and can
208            print the header at the end */
209         if(!skip_all && (no_plan || !have_plan)) {
210                 printf("1..%d\n", test_count);
211         }
212
213         if((have_plan && !no_plan) && e_tests < test_count) {
214                 _diag("Looks like you planned %d tests but ran %d extra.",
215                       e_tests, test_count - e_tests);
216                 UNLOCK;
217                 return;
218         }
219
220         if((have_plan || !no_plan) && e_tests > test_count) {
221                 _diag("Looks like you planned %d tests but only ran %d.",
222                       e_tests, test_count);
223                 if(failures) {
224                         _diag("Looks like you failed %d tests of %d run.", 
225                               failures, test_count);
226                 }
227                 UNLOCK;
228                 return;
229         }
230
231         if(failures)
232                 _diag("Looks like you failed %d tests of %d.", 
233                       failures, test_count);
234
235         UNLOCK;
236 }
237
238 /*
239  * Initialise the TAP library.  Will only do so once, however many times it's
240  * called.
241  */
242 static void
243 _tap_init(void)
244 {
245         static int run_once = 0;
246
247         if(!run_once) {
248                 atexit(_cleanup);
249
250                 /* stdout needs to be unbuffered so that the output appears
251                    in the same place relative to stderr output as it does 
252                    with Test::Harness */
253                 setbuf(stdout, 0);
254                 run_once = 1;
255         }
256 }
257
258 /*
259  * Note that there's no plan.
260  */
261 void
262 plan_no_plan(void)
263 {
264
265         LOCK;
266
267         _tap_init();
268
269         if(have_plan != 0) {
270                 fprintf(stderr, "You tried to plan twice!\n");
271                 test_died = 1;
272                 UNLOCK;
273                 exit(255);
274         }
275
276         have_plan = 1;
277         no_plan = 1;
278
279         UNLOCK;
280 }
281
282 /*
283  * Note that the plan is to skip all tests
284  */
285 void
286 plan_skip_all(char *reason)
287 {
288
289         LOCK;
290
291         _tap_init();
292
293         skip_all = 1;
294
295         printf("1..0");
296
297         if(reason != NULL)
298                 printf(" # Skip %s", reason);
299
300         printf("\n");
301
302         UNLOCK;
303 }
304
305 /*
306  * Note the number of tests that will be run.
307  */
308 void
309 plan_tests(unsigned int tests)
310 {
311
312         LOCK;
313
314         _tap_init();
315
316         if(have_plan != 0) {
317                 fprintf(stderr, "You tried to plan twice!\n");
318                 test_died = 1;
319                 UNLOCK;
320                 exit(255);
321         }
322
323         if(tests == 0) {
324                 fprintf(stderr, "You said to run 0 tests!  You've got to run something.\n");
325                 test_died = 1;
326                 UNLOCK;
327                 exit(255);
328         }
329
330         have_plan = 1;
331
332         _expected_tests(tests);
333
334         UNLOCK;
335 }
336
337 void
338 diag(char *fmt, ...)
339 {
340         va_list ap;
341
342         LOCK;
343
344         va_start(ap, fmt);
345         diagv(fmt, ap);
346         va_end(ap);
347
348         UNLOCK;
349 }
350
351 void
352 skip(unsigned int n, char *fmt, ...)
353 {
354         va_list ap;
355         char *skip_msg;
356
357         LOCK;
358
359         va_start(ap, fmt);
360         vasprintf(&skip_msg, fmt, ap);
361         va_end(ap);
362
363         while(n-- > 0) {
364                 test_count++;
365                 printf("ok %d # skip %s\n", test_count, 
366                        skip_msg != NULL ? 
367                        skip_msg : "libtap():malloc() failed");
368         }
369
370         free(skip_msg);
371
372         UNLOCK;
373 }
374
375 void
376 todo_start(char *fmt, ...)
377 {
378         va_list ap;
379
380         LOCK;
381
382         va_start(ap, fmt);
383         vasprintf(&todo_msg, fmt, ap);
384         va_end(ap);
385
386         todo = 1;
387
388         UNLOCK;
389 }
390
391 void
392 todo_end(void)
393 {
394
395         LOCK;
396
397         todo = 0;
398         free(todo_msg);
399
400         UNLOCK;
401 }
402
403 int
404 exit_status(void)
405 {
406         int r;
407
408         LOCK;
409
410         /* If there's no plan, just return the number of failures */
411         if(no_plan || !have_plan) {
412                 UNLOCK;
413                 return failures;
414         }
415
416         /* Ran too many tests?  Return the number of tests that were run
417            that shouldn't have been */
418         if(e_tests < test_count) {
419                 r = test_count - e_tests;
420                 UNLOCK;
421                 return r;
422         }
423
424         /* Return the number of tests that failed + the number of tests 
425            that weren't run */
426         r = failures + e_tests - test_count;
427         UNLOCK;
428
429         return r;
430 }