2 * Run a set of tests, reporting results.
6 * runtests [-b <build-dir>] [-s <source-dir>] <test-list>
7 * runtests -o [-b <build-dir>] [-s <source-dir>] <test>
9 * In the first case, expects a list of executables located in the given file,
10 * one line per executable. For each one, runs it as part of a test suite,
11 * reporting results. Test output should start with a line containing the
12 * number of tests (numbered from 1 to this number), optionally preceded by
13 * "1..", although that line may be given anywhere in the output. Each
14 * additional line should be in the following format:
19 * not ok <number> # todo
21 * where <number> is the number of the test. An optional comment is permitted
22 * after the number if preceded by whitespace. ok indicates success, not ok
23 * indicates failure. "# skip" and "# todo" are a special cases of a comment,
24 * and must start with exactly that formatting. They indicate the test was
25 * skipped for some reason (maybe because it doesn't apply to this platform)
26 * or is testing something known to currently fail. The text following either
27 * "# skip" or "# todo" and whitespace is the reason.
29 * As a special case, the first line of the output may be in the form:
31 * 1..0 # skip some reason
33 * which indicates that this entire test case should be skipped and gives a
36 * Any other lines are ignored, although for compliance with the TAP protocol
37 * all lines other than the ones in the above format should be sent to
38 * standard error rather than standard output and start with #.
40 * This is a subset of TAP as documented in Test::Harness::TAP or
41 * TAP::Parser::Grammar, which comes with Perl.
43 * If the -o option is given, instead run a single test and display all of its
44 * output. This is intended for use with failing tests so that the person
45 * running the test suite can get more details about what failed.
47 * If built with the C preprocessor symbols SOURCE and BUILD defined, C TAP
48 * Harness will export those values in the environment so that tests can find
49 * the source and build directory and will look for tests under both
50 * directories. These paths can also be set with the -b and -s command-line
51 * options, which will override anything set at build time.
53 * Any bug reports, bug fixes, and improvements are very much welcome and
54 * should be sent to the e-mail address below. This program is part of C TAP
55 * Harness <http://www.eyrie.org/~eagle/software/c-tap-harness/>.
57 * Copyright 2000, 2001, 2004, 2006, 2007, 2008, 2009, 2010, 2011
58 * Russ Allbery <rra@stanford.edu>
60 * Permission is hereby granted, free of charge, to any person obtaining a
61 * copy of this software and associated documentation files (the "Software"),
62 * to deal in the Software without restriction, including without limitation
63 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
64 * and/or sell copies of the Software, and to permit persons to whom the
65 * Software is furnished to do so, subject to the following conditions:
67 * The above copyright notice and this permission notice shall be included in
68 * all copies or substantial portions of the Software.
70 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
71 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
72 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
73 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
74 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
75 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
76 * DEALINGS IN THE SOFTWARE.
79 /* Required for fdopen(), getopt(), and putenv(). */
80 #if defined(__STRICT_ANSI__) || defined(PEDANTIC)
81 # ifndef _XOPEN_SOURCE
82 # define _XOPEN_SOURCE 500
96 #include <sys/types.h>
101 /* sys/time.h must be included before sys/resource.h on some platforms. */
102 #include <sys/resource.h>
104 /* AIX doesn't have WCOREDUMP. */
106 # define WCOREDUMP(status) ((unsigned)(status) & 0x80)
110 * Used for iterating through arrays. Returns the number of elements in the
111 * array (useful for a < upper bound in a for loop).
113 #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
116 * The source and build versions of the tests directory. This is used to set
117 * the SOURCE and BUILD environment variables and find test programs, if set.
118 * Normally, this should be set as part of the build process to the test
119 * subdirectories of $(abs_top_srcdir) and $(abs_top_builddir) respectively.
128 /* Test status codes. */
136 /* Indicates the state of our plan. */
138 PLAN_INIT
, /* Nothing seen yet. */
139 PLAN_FIRST
, /* Plan seen before any tests. */
140 PLAN_PENDING
, /* Test seen and no plan yet. */
141 PLAN_FINAL
/* Plan seen after some tests. */
144 /* Error exit statuses for test processes. */
145 #define CHILDERR_DUP 100 /* Couldn't redirect stderr or stdout. */
146 #define CHILDERR_EXEC 101 /* Couldn't exec child process. */
147 #define CHILDERR_STDERR 102 /* Couldn't open stderr file. */
149 /* Structure to hold data for a set of tests. */
151 char *file
; /* The file name of the test. */
152 char *path
; /* The path to the test program. */
153 enum plan_status plan
; /* The status of our plan. */
154 unsigned long count
; /* Expected count of tests. */
155 unsigned long current
; /* The last seen test number. */
156 unsigned int length
; /* The length of the last status message. */
157 unsigned long passed
; /* Count of passing tests. */
158 unsigned long failed
; /* Count of failing lists. */
159 unsigned long skipped
; /* Count of skipped tests (passed). */
160 unsigned long allocated
; /* The size of the results table. */
161 enum test_status
*results
; /* Table of results by test number. */
162 unsigned int aborted
; /* Whether the set was aborted. */
163 int reported
; /* Whether the results were reported. */
164 int status
; /* The exit status of the test. */
165 unsigned int all_skipped
; /* Whether all tests were skipped. */
166 char *reason
; /* Why all tests were skipped. */
169 /* Structure to hold a linked list of test sets. */
172 struct testlist
*next
;
176 * Usage message. Should be used as a printf format with two arguments: the
177 * path to runtests, given twice.
179 static const char usage_message
[] = "\
180 Usage: %s [-b <build-dir>] [-s <source-dir>] <test> ...\n\
181 %s [-b <build-dir>] [-s <source-dir>] -l <test-list>\n\
182 %s -o [-b <build-dir>] [-s <source-dir>] <test>\n\
185 -b <build-dir> Set the build directory to <build-dir>\n\
186 -l <list> Take the list of tests to run from <test-list>\n\
187 -o Run a single test rather than a list of tests\n\
188 -s <source-dir> Set the source directory to <source-dir>\n\
190 runtests normally runs each test listed on the command line. With the -l\n\
191 option, it instead runs every test listed in a file. With the -o option,\n\
192 it instead runs a single test and shows its complete output.\n";
195 * Header used for test output. %s is replaced by the file name of the list
198 static const char banner
[] = "\n\
199 Running all tests listed in %s. If any tests fail, run the failing\n\
200 test program with runtests -o to see more details.\n\n";
202 /* Header for reports of failed tests. */
203 static const char header
[] = "\n\
204 Failed Set Fail/Total (%) Skip Stat Failing Tests\n\
205 -------------------------- -------------- ---- ---- ------------------------";
207 /* Include the file name and line number in malloc failures. */
208 #define xcalloc(n, size) x_calloc((n), (size), __FILE__, __LINE__)
209 #define xmalloc(size) x_malloc((size), __FILE__, __LINE__)
210 #define xrealloc(p, size) x_realloc((p), (size), __FILE__, __LINE__)
211 #define xstrdup(p) x_strdup((p), __FILE__, __LINE__)
214 * __attribute__ is available in gcc 2.5 and later, but only with gcc 2.7
215 * could you use the __format__ form of the attributes, which is what we use
216 * (to avoid confusion with other macros).
218 #ifndef __attribute__
219 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
220 # define __attribute__(spec) /* empty */
225 * We use __alloc_size__, but it was only available in fairly recent versions
226 * of GCC. Suppress warnings about the unknown attribute if GCC is too old.
227 * We know that we're GCC at this point, so we can use the GCC variadic macro
228 * extension, which will still work with versions of GCC too old to have C99
229 * variadic macro support.
231 #if !defined(__attribute__) && !defined(__alloc_size__)
232 # if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3)
233 # define __alloc_size__(spec, args...) /* empty */
238 * LLVM and Clang pretend to be GCC but don't support all of the __attribute__
239 * settings that GCC does. For them, suppress warnings about unknown
240 * attributes on declarations. This unfortunately will affect the entire
241 * compilation context, but there's no push and pop available.
243 #if !defined(__attribute__) && (defined(__llvm__) || defined(__clang__))
244 # pragma GCC diagnostic ignored "-Wattributes"
247 /* Declare internal functions that benefit from compiler attributes. */
248 static void sysdie(const char *, ...)
249 __attribute__((__nonnull__
, __noreturn__
, __format__(printf
, 1, 2)));
250 static void *x_calloc(size_t, size_t, const char *, int)
251 __attribute__((__alloc_size__(1, 2), __malloc__
, __nonnull__
));
252 static void *x_malloc(size_t, const char *, int)
253 __attribute__((__alloc_size__(1), __malloc__
, __nonnull__
));
254 static void *x_realloc(void *, size_t, const char *, int)
255 __attribute__((__alloc_size__(2), __malloc__
, __nonnull__(3)));
256 static char *x_strdup(const char *, const char *, int)
257 __attribute__((__malloc__
, __nonnull__
));
261 * Report a fatal error, including the results of strerror, and exit.
264 sysdie(const char *format
, ...)
271 fprintf(stderr
, "runtests: ");
272 va_start(args
, format
);
273 vfprintf(stderr
, format
, args
);
275 fprintf(stderr
, ": %s\n", strerror(oerrno
));
281 * Allocate zeroed memory, reporting a fatal error and exiting on failure.
284 x_calloc(size_t n
, size_t size
, const char *file
, int line
)
289 size
= (size
> 0) ? size
: 1;
292 sysdie("failed to calloc %lu bytes at %s line %d",
293 (unsigned long) size
, file
, line
);
299 * Allocate memory, reporting a fatal error and exiting on failure.
302 x_malloc(size_t size
, const char *file
, int line
)
308 sysdie("failed to malloc %lu bytes at %s line %d",
309 (unsigned long) size
, file
, line
);
315 * Reallocate memory, reporting a fatal error and exiting on failure.
318 x_realloc(void *p
, size_t size
, const char *file
, int line
)
320 p
= realloc(p
, size
);
322 sysdie("failed to realloc %lu bytes at %s line %d",
323 (unsigned long) size
, file
, line
);
329 * Copy a string, reporting a fatal error and exiting on failure.
332 x_strdup(const char *s
, const char *file
, int line
)
340 sysdie("failed to strdup %lu bytes at %s line %d",
341 (unsigned long) len
, file
, line
);
348 * Given a struct timeval, return the number of seconds it represents as a
349 * double. Use difftime() to convert a time_t to a double.
352 tv_seconds(const struct timeval
*tv
)
354 return difftime(tv
->tv_sec
, 0) + tv
->tv_usec
* 1e-6;
359 * Given two struct timevals, return the difference in seconds.
362 tv_diff(const struct timeval
*tv1
, const struct timeval
*tv0
)
364 return tv_seconds(tv1
) - tv_seconds(tv0
);
369 * Given two struct timevals, return the sum in seconds as a double.
372 tv_sum(const struct timeval
*tv1
, const struct timeval
*tv2
)
374 return tv_seconds(tv1
) + tv_seconds(tv2
);
379 * Given a pointer to a string, skip any leading whitespace and return a
380 * pointer to the first non-whitespace character.
383 skip_whitespace(const char *p
)
385 while (isspace((unsigned char)(*p
)))
392 * Start a program, connecting its stdout to a pipe on our end and its stderr
393 * to /dev/null, and storing the file descriptor to read from in the two
394 * argument. Returns the PID of the new process. Errors are fatal.
397 test_start(const char *path
, int *fd
)
402 if (pipe(fds
) == -1) {
405 sysdie("can't create pipe");
408 if (child
== (pid_t
) -1) {
411 sysdie("can't fork");
412 } else if (child
== 0) {
413 /* In child. Set up our stdout and stderr. */
414 errfd
= open("/dev/null", O_WRONLY
);
416 _exit(CHILDERR_STDERR
);
417 if (dup2(errfd
, 2) == -1)
420 if (dup2(fds
[1], 1) == -1)
423 /* Now, exec our process. */
424 if (execl(path
, path
, (char *) 0) == -1)
425 _exit(CHILDERR_EXEC
);
427 /* In parent. Close the extra file descriptor. */
436 * Back up over the output saying what test we were executing.
439 test_backspace(struct testset
*ts
)
443 if (!isatty(STDOUT_FILENO
))
445 for (i
= 0; i
< ts
->length
; i
++)
447 for (i
= 0; i
< ts
->length
; i
++)
449 for (i
= 0; i
< ts
->length
; i
++)
456 * Read the plan line of test output, which should contain the range of test
457 * numbers. We may initialize the testset structure here if we haven't yet
458 * seen a test. Return true if initialization succeeded and the test should
459 * continue, false otherwise.
462 test_plan(const char *line
, struct testset
*ts
)
468 * Accept a plan without the leading 1.. for compatibility with older
469 * versions of runtests. This will only be allowed if we've not yet seen
472 line
= skip_whitespace(line
);
473 if (strncmp(line
, "1..", 3) == 0)
477 * Get the count, check it for validity, and initialize the struct. If we
478 * have something of the form "1..0 # skip foo", the whole file was
479 * skipped; record that. If we do skip the whole file, zero out all of
480 * our statistics, since they're no longer relevant. strtol is called
481 * with a second argument to advance the line pointer past the count to
482 * make it simpler to detect the # skip case.
484 n
= strtol(line
, (char **) &line
, 10);
486 line
= skip_whitespace(line
);
488 line
= skip_whitespace(line
+ 1);
489 if (strncasecmp(line
, "skip", 4) == 0) {
490 line
= skip_whitespace(line
+ 4);
492 ts
->reason
= xstrdup(line
);
493 ts
->reason
[strlen(ts
->reason
) - 1] = '\0';
506 puts("ABORTED (invalid test count)");
511 if (ts
->plan
== PLAN_INIT
&& ts
->allocated
== 0) {
514 ts
->plan
= PLAN_FIRST
;
515 ts
->results
= xmalloc(ts
->count
* sizeof(enum test_status
));
516 for (i
= 0; i
< ts
->count
; i
++)
517 ts
->results
[i
] = TEST_INVALID
;
518 } else if (ts
->plan
== PLAN_PENDING
) {
519 if ((unsigned long) n
< ts
->count
) {
521 printf("ABORTED (invalid test number %lu)\n", ts
->count
);
527 if ((unsigned long) n
> ts
->allocated
) {
528 ts
->results
= xrealloc(ts
->results
, n
* sizeof(enum test_status
));
529 for (i
= ts
->allocated
; i
< ts
->count
; i
++)
530 ts
->results
[i
] = TEST_INVALID
;
533 ts
->plan
= PLAN_FINAL
;
540 * Given a single line of output from a test, parse it and return the success
541 * status of that test. Anything printed to stdout not matching the form
542 * /^(not )?ok \d+/ is ignored. Sets ts->current to the test number that just
546 test_checkline(const char *line
, struct testset
*ts
)
548 enum test_status status
= TEST_PASS
;
552 unsigned long i
, current
;
555 /* Before anything, check for a test abort. */
556 bail
= strstr(line
, "Bail out!");
558 bail
= skip_whitespace(bail
+ strlen("Bail out!"));
562 length
= strlen(bail
);
563 if (bail
[length
- 1] == '\n')
566 printf("ABORTED (%.*s)\n", (int) length
, bail
);
574 * If the given line isn't newline-terminated, it was too big for an
575 * fgets(), which means ignore it.
577 if (line
[strlen(line
) - 1] != '\n')
580 /* If the line begins with a hash mark, ignore it. */
584 /* If we haven't yet seen a plan, look for one. */
585 if (ts
->plan
== PLAN_INIT
&& isdigit((unsigned char)(*line
))) {
586 if (!test_plan(line
, ts
))
588 } else if (strncmp(line
, "1..", 3) == 0) {
589 if (ts
->plan
== PLAN_PENDING
) {
590 if (!test_plan(line
, ts
))
594 puts("ABORTED (multiple plans)");
601 /* Parse the line, ignoring something we can't parse. */
602 if (strncmp(line
, "not ", 4) == 0) {
606 if (strncmp(line
, "ok", 2) != 0)
608 line
= skip_whitespace(line
+ 2);
610 number
= strtol(line
, &end
, 10);
611 if (errno
!= 0 || end
== line
)
612 number
= ts
->current
+ 1;
614 if (number
<= 0 || (current
> ts
->count
&& ts
->plan
== PLAN_FIRST
)) {
616 printf("ABORTED (invalid test number %lu)\n", current
);
622 /* We have a valid test result. Tweak the results array if needed. */
623 if (ts
->plan
== PLAN_INIT
|| ts
->plan
== PLAN_PENDING
) {
624 ts
->plan
= PLAN_PENDING
;
625 if (current
> ts
->count
)
627 if (current
> ts
->allocated
) {
630 n
= (ts
->allocated
== 0) ? 32 : ts
->allocated
* 2;
633 ts
->results
= xrealloc(ts
->results
, n
* sizeof(enum test_status
));
634 for (i
= ts
->allocated
; i
< n
; i
++)
635 ts
->results
[i
] = TEST_INVALID
;
641 * Handle directives. We should probably do something more interesting
642 * with unexpected passes of todo tests.
644 while (isdigit((unsigned char)(*line
)))
646 line
= skip_whitespace(line
);
648 line
= skip_whitespace(line
+ 1);
649 if (strncasecmp(line
, "skip", 4) == 0)
651 if (strncasecmp(line
, "todo", 4) == 0)
652 status
= (status
== TEST_FAIL
) ? TEST_SKIP
: TEST_FAIL
;
655 /* Make sure that the test number is in range and not a duplicate. */
656 if (ts
->results
[current
- 1] != TEST_INVALID
) {
658 printf("ABORTED (duplicate test number %lu)\n", current
);
664 /* Good results. Increment our various counters. */
666 case TEST_PASS
: ts
->passed
++; break;
667 case TEST_FAIL
: ts
->failed
++; break;
668 case TEST_SKIP
: ts
->skipped
++; break;
669 case TEST_INVALID
: break;
671 ts
->current
= current
;
672 ts
->results
[current
- 1] = status
;
673 if (isatty(STDOUT_FILENO
)) {
675 if (ts
->plan
== PLAN_PENDING
)
676 outlen
= printf("%lu/?", current
);
678 outlen
= printf("%lu/%lu", current
, ts
->count
);
679 ts
->length
= (outlen
>= 0) ? outlen
: 0;
686 * Print out a range of test numbers, returning the number of characters it
687 * took up. Takes the first number, the last number, the number of characters
688 * already printed on the line, and the limit of number of characters the line
689 * can hold. Add a comma and a space before the range if chars indicates that
690 * something has already been printed on the line, and print ... instead if
691 * chars plus the space needed would go over the limit (use a limit of 0 to
695 test_print_range(unsigned long first
, unsigned long last
, unsigned int chars
,
698 unsigned int needed
= 0;
701 for (n
= first
; n
> 0; n
/= 10)
704 for (n
= last
; n
> 0; n
/= 10)
710 if (limit
> 0 && chars
+ needed
> limit
) {
712 if (chars
<= limit
) {
724 printf("%lu-", first
);
732 * Summarize a single test set. The second argument is 0 if the set exited
733 * cleanly, a positive integer representing the exit status if it exited
734 * with a non-zero status, and a negative integer representing the signal
735 * that terminated it if it was killed by a signal.
738 test_summarize(struct testset
*ts
, int status
)
741 unsigned long missing
= 0;
742 unsigned long failed
= 0;
743 unsigned long first
= 0;
744 unsigned long last
= 0;
747 fputs("ABORTED", stdout
);
749 printf(" (passed %lu/%lu)", ts
->passed
, ts
->count
- ts
->skipped
);
751 for (i
= 0; i
< ts
->count
; i
++) {
752 if (ts
->results
[i
] == TEST_INVALID
) {
754 fputs("MISSED ", stdout
);
755 if (first
&& i
== last
)
759 test_print_range(first
, last
, missing
- 1, 0);
767 test_print_range(first
, last
, missing
- 1, 0);
770 for (i
= 0; i
< ts
->count
; i
++) {
771 if (ts
->results
[i
] == TEST_FAIL
) {
772 if (missing
&& !failed
)
775 fputs("FAILED ", stdout
);
776 if (first
&& i
== last
)
780 test_print_range(first
, last
, failed
- 1, 0);
788 test_print_range(first
, last
, failed
- 1, 0);
789 if (!missing
&& !failed
) {
790 fputs(!status
? "ok" : "dubious", stdout
);
791 if (ts
->skipped
> 0) {
792 if (ts
->skipped
== 1)
793 printf(" (skipped %lu test)", ts
->skipped
);
795 printf(" (skipped %lu tests)", ts
->skipped
);
800 printf(" (exit status %d)", status
);
802 printf(" (killed by signal %d%s)", -status
,
803 WCOREDUMP(ts
->status
) ? ", core dumped" : "");
809 * Given a test set, analyze the results, classify the exit status, handle a
810 * few special error messages, and then pass it along to test_summarize() for
811 * the regular output. Returns true if the test set ran successfully and all
812 * tests passed or were skipped, false otherwise.
815 test_analyze(struct testset
*ts
)
819 if (ts
->all_skipped
) {
820 if (ts
->reason
== NULL
)
823 printf("skipped (%s)\n", ts
->reason
);
825 } else if (WIFEXITED(ts
->status
) && WEXITSTATUS(ts
->status
) != 0) {
826 switch (WEXITSTATUS(ts
->status
)) {
829 puts("ABORTED (can't dup file descriptors)");
833 puts("ABORTED (execution failed -- not found?)");
835 case CHILDERR_STDERR
:
837 puts("ABORTED (can't open /dev/null)");
840 test_summarize(ts
, WEXITSTATUS(ts
->status
));
844 } else if (WIFSIGNALED(ts
->status
)) {
845 test_summarize(ts
, -WTERMSIG(ts
->status
));
847 } else if (ts
->plan
!= PLAN_FIRST
&& ts
->plan
!= PLAN_FINAL
) {
848 puts("ABORTED (no valid test plan)");
852 test_summarize(ts
, 0);
853 return (ts
->failed
== 0);
859 * Runs a single test set, accumulating and then reporting the results.
860 * Returns true if the test set was successfully run and all tests passed,
864 test_run(struct testset
*ts
)
866 pid_t testpid
, child
;
872 /* Run the test program. */
873 testpid
= test_start(ts
->path
, &outfd
);
874 output
= fdopen(outfd
, "r");
878 sysdie("fdopen failed");
881 /* Pass each line of output to test_checkline(). */
882 while (!ts
->aborted
&& fgets(buffer
, sizeof(buffer
), output
))
883 test_checkline(buffer
, ts
);
884 if (ferror(output
) || ts
->plan
== PLAN_INIT
)
889 * Consume the rest of the test output, close the output descriptor,
890 * retrieve the exit status, and pass that information to test_analyze()
891 * for eventual output.
893 while (fgets(buffer
, sizeof(buffer
), output
))
896 child
= waitpid(testpid
, &ts
->status
, 0);
897 if (child
== (pid_t
) -1) {
902 sysdie("waitpid for %u failed", (unsigned int) testpid
);
906 status
= test_analyze(ts
);
908 /* Convert missing tests to failed tests. */
909 for (i
= 0; i
< ts
->count
; i
++) {
910 if (ts
->results
[i
] == TEST_INVALID
) {
912 ts
->results
[i
] = TEST_FAIL
;
920 /* Summarize a list of test failures. */
922 test_fail_summary(const struct testlist
*fails
)
926 unsigned long i
, first
, last
, total
;
930 /* Failed Set Fail/Total (%) Skip Stat Failing (25)
931 -------------------------- -------------- ---- ---- -------------- */
932 for (; fails
; fails
= fails
->next
) {
934 total
= ts
->count
- ts
->skipped
;
935 printf("%-26.26s %4lu/%-4lu %3.0f%% %4lu ", ts
->file
, ts
->failed
,
936 total
, total
? (ts
->failed
* 100.0) / total
: 0,
938 if (WIFEXITED(ts
->status
))
939 printf("%4d ", WEXITSTATUS(ts
->status
));
949 for (i
= 0; i
< ts
->count
; i
++) {
950 if (ts
->results
[i
] == TEST_FAIL
) {
951 if (first
!= 0 && i
== last
)
955 chars
+= test_print_range(first
, last
, chars
, 19);
962 test_print_range(first
, last
, chars
, 19);
969 * Check whether a given file path is a valid test. Currently, this checks
970 * whether it is executable and is a regular file. Returns true or false.
973 is_valid_test(const char *path
)
977 if (access(path
, X_OK
) < 0)
979 if (stat(path
, &st
) < 0)
981 if (!S_ISREG(st
.st_mode
))
988 * Given the name of a test, a pointer to the testset struct, and the source
989 * and build directories, find the test. We try first relative to the current
990 * directory, then in the build directory (if not NULL), then in the source
991 * directory. In each of those directories, we first try a "-t" extension and
992 * then a ".t" extension. When we find an executable program, we return the
993 * path to that program. If none of those paths are executable, just fill in
994 * the name of the test as is.
996 * The caller is responsible for freeing the path member of the testset
1000 find_test(const char *name
, const char *source
, const char *build
)
1003 const char *bases
[3], *suffix
, *base
;
1005 const char *suffixes
[3] = { "-t", ".t", "" };
1007 /* Possible base directories. */
1012 /* Try each suffix with each base. */
1013 for (i
= 0; i
< ARRAY_SIZE(suffixes
); i
++) {
1014 suffix
= suffixes
[i
];
1015 for (j
= 0; j
< ARRAY_SIZE(bases
); j
++) {
1019 path
= xmalloc(strlen(base
) + strlen(name
) + strlen(suffix
) + 2);
1020 sprintf(path
, "%s/%s%s", base
, name
, suffix
);
1021 if (is_valid_test(path
))
1028 path
= xstrdup(name
);
1034 * Read a list of tests from a file, returning the list of tests as a struct
1035 * testlist. Reports an error to standard error and exits if the list of
1036 * tests cannot be read.
1038 static struct testlist
*
1039 read_test_list(const char *filename
)
1044 char buffer
[BUFSIZ
];
1045 struct testlist
*listhead
, *current
;
1047 /* Create the initial container list that will hold our results. */
1048 listhead
= xmalloc(sizeof(struct testlist
));
1049 listhead
->ts
= NULL
;
1050 listhead
->next
= NULL
;
1054 * Open our file of tests to run and read it line by line, creating a new
1055 * struct testlist and struct testset for each line.
1057 file
= fopen(filename
, "r");
1059 sysdie("can't open %s", filename
);
1061 while (fgets(buffer
, sizeof(buffer
), file
)) {
1063 length
= strlen(buffer
) - 1;
1064 if (buffer
[length
] != '\n') {
1065 fprintf(stderr
, "%s:%u: line too long\n", filename
, line
);
1068 buffer
[length
] = '\0';
1069 if (current
== NULL
)
1072 current
->next
= xmalloc(sizeof(struct testlist
));
1073 current
= current
->next
;
1074 current
->next
= NULL
;
1076 current
->ts
= xcalloc(1, sizeof(struct testset
));
1077 current
->ts
->plan
= PLAN_INIT
;
1078 current
->ts
->file
= xstrdup(buffer
);
1079 current
->ts
->reason
= NULL
;
1083 /* Return the results. */
1089 * Build a list of tests from command line arguments. Takes the argv and argc
1090 * representing the command line arguments and returns a newly allocated test
1091 * list. The caller is responsible for freeing.
1093 static struct testlist
*
1094 build_test_list(char *argv
[], int argc
)
1097 struct testlist
*listhead
, *current
;
1099 /* Create the initial container list that will hold our results. */
1100 listhead
= xmalloc(sizeof(struct testlist
));
1101 listhead
->ts
= NULL
;
1102 listhead
->next
= NULL
;
1105 /* Walk the list of arguments and create test sets for them. */
1106 for (i
= 0; i
< argc
; i
++) {
1107 if (current
== NULL
)
1110 current
->next
= xmalloc(sizeof(struct testlist
));
1111 current
= current
->next
;
1112 current
->next
= NULL
;
1114 current
->ts
= xcalloc(1, sizeof(struct testset
));
1115 current
->ts
->plan
= PLAN_INIT
;
1116 current
->ts
->file
= xstrdup(argv
[i
]);
1117 current
->ts
->reason
= NULL
;
1120 /* Return the results. */
1125 /* Free a struct testset. */
1127 free_testset(struct testset
*ts
)
1132 if (ts
->reason
!= NULL
)
1139 * Run a batch of tests. Takes two additional parameters: the root of the
1140 * source directory and the root of the build directory. Test programs will
1141 * be first searched for in the current directory, then the build directory,
1142 * then the source directory. Returns true iff all tests passed, and always
1143 * frees the test list that's passed in.
1146 test_batch(struct testlist
*tests
, const char *source
, const char *build
)
1150 unsigned int longest
= 0;
1151 unsigned int count
= 0;
1153 struct timeval start
, end
;
1154 struct rusage stats
;
1155 struct testlist
*failhead
= NULL
;
1156 struct testlist
*failtail
= NULL
;
1157 struct testlist
*current
, *next
;
1159 unsigned long total
= 0;
1160 unsigned long passed
= 0;
1161 unsigned long skipped
= 0;
1162 unsigned long failed
= 0;
1163 unsigned long aborted
= 0;
1165 /* Walk the list of tests to find the longest name. */
1166 for (current
= tests
; current
!= NULL
; current
= current
->next
) {
1167 length
= strlen(current
->ts
->file
);
1168 if (length
> longest
)
1173 * Add two to longest and round up to the nearest tab stop. This is how
1174 * wide the column for printing the current test name will be.
1178 longest
+= 8 - (longest
% 8);
1180 /* Start the wall clock timer. */
1181 gettimeofday(&start
, NULL
);
1183 /* Now, plow through our tests again, running each one. */
1184 for (current
= tests
; current
!= NULL
; current
= current
->next
) {
1187 /* Print out the name of the test file. */
1188 fputs(ts
->file
, stdout
);
1189 for (i
= strlen(ts
->file
); i
< longest
; i
++)
1191 if (isatty(STDOUT_FILENO
))
1195 ts
->path
= find_test(ts
->file
, source
, build
);
1196 succeeded
= test_run(ts
);
1199 /* Record cumulative statistics. */
1200 aborted
+= ts
->aborted
;
1201 total
+= ts
->count
+ ts
->all_skipped
;
1202 passed
+= ts
->passed
;
1203 skipped
+= ts
->skipped
+ ts
->all_skipped
;
1204 failed
+= ts
->failed
;
1207 /* If the test fails, we shuffle it over to the fail list. */
1209 if (failhead
== NULL
) {
1210 failhead
= xmalloc(sizeof(struct testset
));
1211 failtail
= failhead
;
1213 failtail
->next
= xmalloc(sizeof(struct testset
));
1214 failtail
= failtail
->next
;
1217 failtail
->next
= NULL
;
1222 /* Stop the timer and get our child resource statistics. */
1223 gettimeofday(&end
, NULL
);
1224 getrusage(RUSAGE_CHILDREN
, &stats
);
1226 /* Summarize the failures and free the failure list. */
1227 if (failhead
!= NULL
) {
1228 test_fail_summary(failhead
);
1229 while (failhead
!= NULL
) {
1230 next
= failhead
->next
;
1236 /* Free the memory used by the test lists. */
1237 while (tests
!= NULL
) {
1239 free_testset(tests
->ts
);
1244 /* Print out the final test summary. */
1248 printf("Aborted %lu test set", aborted
);
1250 printf("Aborted %lu test sets", aborted
);
1251 printf(", passed %lu/%lu tests", passed
, total
);
1253 else if (failed
== 0)
1254 fputs("All tests successful", stdout
);
1256 printf("Failed %lu/%lu tests, %.2f%% okay", failed
, total
,
1257 (total
- failed
) * 100.0 / total
);
1260 printf(", %lu test skipped", skipped
);
1262 printf(", %lu tests skipped", skipped
);
1265 printf("Files=%u, Tests=%lu", count
, total
);
1266 printf(", %.2f seconds", tv_diff(&end
, &start
));
1267 printf(" (%.2f usr + %.2f sys = %.2f CPU)\n",
1268 tv_seconds(&stats
.ru_utime
), tv_seconds(&stats
.ru_stime
),
1269 tv_sum(&stats
.ru_utime
, &stats
.ru_stime
));
1270 return (failed
== 0 && aborted
== 0);
1275 * Run a single test case. This involves just running the test program after
1276 * having done the environment setup and finding the test program.
1279 test_single(const char *program
, const char *source
, const char *build
)
1283 path
= find_test(program
, source
, build
);
1284 if (execl(path
, path
, (char *) 0) == -1)
1285 sysdie("cannot exec %s", path
);
1290 * Main routine. Set the SOURCE and BUILD environment variables and then,
1291 * given a file listing tests, run each test listed.
1294 main(int argc
, char *argv
[])
1299 char *source_env
= NULL
;
1300 char *build_env
= NULL
;
1301 const char *shortlist
;
1302 const char *list
= NULL
;
1303 const char *source
= SOURCE
;
1304 const char *build
= BUILD
;
1305 struct testlist
*tests
;
1307 while ((option
= getopt(argc
, argv
, "b:hl:os:")) != EOF
) {
1313 printf(usage_message
, argv
[0], argv
[0], argv
[0]);
1331 if ((list
== NULL
&& argc
< 1) || (list
!= NULL
&& argc
> 0)) {
1332 fprintf(stderr
, usage_message
, argv
[0], argv
[0], argv
[0]);
1336 /* Set SOURCE and BUILD environment variables. */
1337 if (source
!= NULL
) {
1338 source_env
= xmalloc(strlen("SOURCE=") + strlen(source
) + 1);
1339 sprintf(source_env
, "SOURCE=%s", source
);
1340 if (putenv(source_env
) != 0)
1341 sysdie("cannot set SOURCE in the environment");
1343 if (build
!= NULL
) {
1344 build_env
= xmalloc(strlen("BUILD=") + strlen(build
) + 1);
1345 sprintf(build_env
, "BUILD=%s", build
);
1346 if (putenv(build_env
) != 0)
1347 sysdie("cannot set BUILD in the environment");
1350 /* Run the tests as instructed. */
1352 test_single(argv
[0], source
, build
);
1353 else if (list
!= NULL
) {
1354 shortlist
= strrchr(list
, '/');
1355 if (shortlist
== NULL
)
1359 printf(banner
, shortlist
);
1360 tests
= read_test_list(list
);
1361 status
= test_batch(tests
, source
, build
) ? 0 : 1;
1363 tests
= build_test_list(argv
, argc
);
1364 status
= test_batch(tests
, source
, build
) ? 0 : 1;
1367 /* For valgrind cleanliness, free all our memory. */
1368 if (source_env
!= NULL
) {
1369 putenv((char *) "SOURCE=");
1372 if (build_env
!= NULL
) {
1373 putenv((char *) "BUILD=");