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