2 * Run a set of tests, reporting results.
6 * runtests [-hv] [-b <build-dir>] [-s <source-dir>] -l <test-list>
7 * runtests [-hv] [-b <build-dir>] [-s <source-dir>] <test> [<test> ...]
8 * runtests -o [-h] [-b <build-dir>] [-s <source-dir>] <test>
10 * In the first case, expects a list of executables located in the given file,
11 * one line per executable. For each one, runs it as part of a test suite,
12 * reporting results. In the second case, use the same infrastructure, but
13 * run only the tests listed on the command line.
15 * Test output should start with a line containing the number of tests
16 * (numbered from 1 to this number), optionally preceded by "1..", although
17 * that line may be given anywhere in the output. Each additional line should
18 * be in the following format:
23 * not ok <number> # todo
25 * where <number> is the number of the test. An optional comment is permitted
26 * after the number if preceded by whitespace. ok indicates success, not ok
27 * indicates failure. "# skip" and "# todo" are a special cases of a comment,
28 * and must start with exactly that formatting. They indicate the test was
29 * skipped for some reason (maybe because it doesn't apply to this platform)
30 * or is testing something known to currently fail. The text following either
31 * "# skip" or "# todo" and whitespace is the reason.
33 * As a special case, the first line of the output may be in the form:
35 * 1..0 # skip some reason
37 * which indicates that this entire test case should be skipped and gives a
40 * Any other lines are ignored, although for compliance with the TAP protocol
41 * all lines other than the ones in the above format should be sent to
42 * standard error rather than standard output and start with #.
44 * This is a subset of TAP as documented in Test::Harness::TAP or
45 * TAP::Parser::Grammar, which comes with Perl.
47 * If the -o option is given, instead run a single test and display all of its
48 * output. This is intended for use with failing tests so that the person
49 * running the test suite can get more details about what failed.
51 * If built with the C preprocessor symbols C_TAP_SOURCE and C_TAP_BUILD
52 * defined, C TAP Harness will export those values in the environment so that
53 * tests can find the source and build directory and will look for tests under
54 * both directories. These paths can also be set with the -b and -s
55 * command-line options, which will override anything set at build time.
57 * If the -v option is given, or the C_TAP_VERBOSE environment variable is set,
58 * display the full output of each test as it runs rather than showing a
59 * summary of the results of each test.
61 * Any bug reports, bug fixes, and improvements are very much welcome and
62 * should be sent to the e-mail address below. This program is part of C TAP
63 * Harness <https://www.eyrie.org/~eagle/software/c-tap-harness/>.
65 * Copyright 2000, 2001, 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013,
66 * 2014, 2015, 2016 Russ Allbery <eagle@eyrie.org>
68 * Permission is hereby granted, free of charge, to any person obtaining a
69 * copy of this software and associated documentation files (the "Software"),
70 * to deal in the Software without restriction, including without limitation
71 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
72 * and/or sell copies of the Software, and to permit persons to whom the
73 * Software is furnished to do so, subject to the following conditions:
75 * The above copyright notice and this permission notice shall be included in
76 * all copies or substantial portions of the Software.
78 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
79 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
80 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
81 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
82 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
83 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
84 * DEALINGS IN THE SOFTWARE.
87 /* Required for fdopen(), getopt(), and putenv(). */
88 #if defined(__STRICT_ANSI__) || defined(PEDANTIC)
89 # ifndef _XOPEN_SOURCE
90 # define _XOPEN_SOURCE 500
104 #include <sys/stat.h>
105 #include <sys/time.h>
106 #include <sys/types.h>
107 #include <sys/wait.h>
111 /* sys/time.h must be included before sys/resource.h on some platforms. */
112 #include <sys/resource.h>
114 /* AIX 6.1 (and possibly later) doesn't have WCOREDUMP. */
116 # define WCOREDUMP(status) ((unsigned) (status) &0x80)
120 * POSIX requires that these be defined in <unistd.h>, but they're not always
121 * available. If one of them has been defined, all the rest almost certainly
125 # define STDIN_FILENO 0
126 # define STDOUT_FILENO 1
127 # define STDERR_FILENO 2
131 * Used for iterating through arrays. Returns the number of elements in the
132 * array (useful for a < upper bound in a for loop).
134 #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
137 * The source and build versions of the tests directory. This is used to set
138 * the C_TAP_SOURCE and C_TAP_BUILD environment variables (and the SOURCE and
139 * BUILD environment variables set for backward compatibility) and find test
140 * programs, if set. Normally, this should be set as part of the build
141 * process to the test subdirectories of $(abs_top_srcdir) and
142 * $(abs_top_builddir) respectively.
145 # define C_TAP_SOURCE NULL
148 # define C_TAP_BUILD NULL
151 /* Test status codes. */
152 enum test_status { TEST_FAIL, TEST_PASS, TEST_SKIP, TEST_INVALID };
154 /* Really, just a boolean, but this is more self-documenting. */
155 enum test_verbose { CONCISE = 0, VERBOSE = 1 };
157 /* Indicates the state of our plan. */
159 PLAN_INIT, /* Nothing seen yet. */
160 PLAN_FIRST, /* Plan seen before any tests. */
161 PLAN_PENDING, /* Test seen and no plan yet. */
162 PLAN_FINAL /* Plan seen after some tests. */
165 /* Error exit statuses for test processes. */
166 #define CHILDERR_DUP 100 /* Couldn't redirect stderr or stdout. */
167 #define CHILDERR_EXEC 101 /* Couldn't exec child process. */
168 #define CHILDERR_STDIN 102 /* Couldn't open stdin file. */
169 #define CHILDERR_STDERR 103 /* Couldn't open stderr file. */
171 /* Structure to hold data for a set of tests. */
173 char *file; /* The file name of the test. */
174 char *path; /* The path to the test program. */
175 enum plan_status plan; /* The status of our plan. */
176 unsigned long count; /* Expected count of tests. */
177 unsigned long current; /* The last seen test number. */
178 unsigned int length; /* The length of the last status message. */
179 unsigned long passed; /* Count of passing tests. */
180 unsigned long failed; /* Count of failing lists. */
181 unsigned long skipped; /* Count of skipped tests (passed). */
182 unsigned long allocated; /* The size of the results table. */
183 enum test_status *results; /* Table of results by test number. */
184 unsigned int aborted; /* Whether the set was aborted. */
185 int reported; /* Whether the results were reported. */
186 int status; /* The exit status of the test. */
187 unsigned int all_skipped; /* Whether all tests were skipped. */
188 char *reason; /* Why all tests were skipped. */
191 /* Structure to hold a linked list of test sets. */
194 struct testlist *next;
198 * Usage message. Should be used as a printf format with four arguments: the
199 * path to runtests, given three times, and the usage_description. This is
200 * split into variables to satisfy the pedantic ISO C90 limit on strings.
202 static const char usage_message[] = "\
203 Usage: %s [-hv] [-b <build-dir>] [-s <source-dir>] <test> ...\n\
204 %s [-hv] [-b <build-dir>] [-s <source-dir>] -l <test-list>\n\
205 %s -o [-h] [-b <build-dir>] [-s <source-dir>] <test>\n\
208 -b <build-dir> Set the build directory to <build-dir>\n\
210 static const char usage_extra[] = "\
211 -l <list> Take the list of tests to run from <test-list>\n\
212 -o Run a single test rather than a list of tests\n\
213 -s <source-dir> Set the source directory to <source-dir>\n\
214 -v Show the full output of each test\n\
216 runtests normally runs each test listed on the command line. With the -l\n\
217 option, it instead runs every test listed in a file. With the -o option,\n\
218 it instead runs a single test and shows its complete output.\n";
221 * Header used for test output. %s is replaced by the file name of the list
224 static const char banner[] = "\n\
225 Running all tests listed in %s. If any tests fail, run the failing\n\
226 test program with runtests -o to see more details.\n\n";
228 /* Header for reports of failed tests. */
229 static const char header[] = "\n\
230 Failed Set Fail/Total (%) Skip Stat Failing Tests\n\
231 -------------------------- -------------- ---- ---- ------------------------";
233 /* Include the file name and line number in malloc failures. */
234 #define xcalloc(n, size) x_calloc((n), (size), __FILE__, __LINE__)
235 #define xmalloc(size) x_malloc((size), __FILE__, __LINE__)
236 #define xstrdup(p) x_strdup((p), __FILE__, __LINE__)
237 #define xreallocarray(p, n, size) \
238 x_reallocarray((p), (n), (size), __FILE__, __LINE__)
241 * __attribute__ is available in gcc 2.5 and later, but only with gcc 2.7
242 * could you use the __format__ form of the attributes, which is what we use
243 * (to avoid confusion with other macros).
245 #ifndef __attribute__
246 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
247 # define __attribute__(spec) /* empty */
252 * We use __alloc_size__, but it was only available in fairly recent versions
253 * of GCC. Suppress warnings about the unknown attribute if GCC is too old.
254 * We know that we're GCC at this point, so we can use the GCC variadic macro
255 * extension, which will still work with versions of GCC too old to have C99
256 * variadic macro support.
258 #if !defined(__attribute__) && !defined(__alloc_size__)
259 # if defined(__GNUC__) && !defined(__clang__)
260 # if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3)
261 # define __alloc_size__(spec, args...) /* empty */
267 * LLVM and Clang pretend to be GCC but don't support all of the __attribute__
268 * settings that GCC does. For them, suppress warnings about unknown
269 * attributes on declarations. This unfortunately will affect the entire
270 * compilation context, but there's no push and pop available.
272 #if !defined(__attribute__) && (defined(__llvm__) || defined(__clang__))
273 # pragma GCC diagnostic ignored "-Wattributes"
276 /* Declare internal functions that benefit from compiler attributes. */
277 static void sysdie(const char *, ...)
278 __attribute__((__nonnull__, __noreturn__, __format__(printf, 1, 2)));
279 static void *x_calloc(size_t, size_t, const char *, int)
280 __attribute__((__alloc_size__(1, 2), __malloc__, __nonnull__));
281 static void *x_malloc(size_t, const char *, int)
282 __attribute__((__alloc_size__(1), __malloc__, __nonnull__));
283 static void *x_reallocarray(void *, size_t, size_t, const char *, int)
284 __attribute__((__alloc_size__(2, 3), __malloc__, __nonnull__(4)));
285 static char *x_strdup(const char *, const char *, int)
286 __attribute__((__malloc__, __nonnull__));
290 * Report a fatal error, including the results of strerror, and exit.
293 sysdie(const char *format, ...)
300 fprintf(stderr, "runtests: ");
301 va_start(args, format);
302 vfprintf(stderr, format, args);
304 fprintf(stderr, ": %s\n", strerror(oerrno));
310 * Allocate zeroed memory, reporting a fatal error and exiting on failure.
313 x_calloc(size_t n, size_t size, const char *file, int line)
318 size = (size > 0) ? size : 1;
321 sysdie("failed to calloc %lu bytes at %s line %d",
322 (unsigned long) size, file, line);
328 * Allocate memory, reporting a fatal error and exiting on failure.
331 x_malloc(size_t size, const char *file, int line)
337 sysdie("failed to malloc %lu bytes at %s line %d",
338 (unsigned long) size, file, line);
344 * Reallocate memory, reporting a fatal error and exiting on failure.
346 * We should technically use SIZE_MAX here for the overflow check, but
347 * SIZE_MAX is C99 and we're only assuming C89 + SUSv3, which does not
348 * guarantee that it exists. They do guarantee that UINT_MAX exists, and we
349 * can assume that UINT_MAX <= SIZE_MAX. And we should not be allocating
350 * anything anywhere near that large.
352 * (In theory, C89 and C99 permit size_t to be smaller than unsigned int, but
353 * I disbelieve in the existence of such systems and they will have to cope
354 * without overflow checks.)
357 x_reallocarray(void *p, size_t n, size_t size, const char *file, int line)
359 if (n > 0 && UINT_MAX / n <= size)
360 sysdie("realloc too large at %s line %d", file, line);
361 p = realloc(p, n * size);
363 sysdie("failed to realloc %lu bytes at %s line %d",
364 (unsigned long) (n * size), file, line);
370 * Copy a string, reporting a fatal error and exiting on failure.
373 x_strdup(const char *s, const char *file, int line)
381 sysdie("failed to strdup %lu bytes at %s line %d", (unsigned long) len,
389 * Form a new string by concatenating multiple strings. The arguments must be
390 * terminated by (const char *) 0.
392 * This function only exists because we can't assume asprintf. We can't
393 * simulate asprintf with snprintf because we're only assuming SUSv3, which
394 * does not require that snprintf with a NULL buffer return the required
395 * length. When those constraints are relaxed, this should be ripped out and
396 * replaced with asprintf or a more trivial replacement with snprintf.
399 concat(const char *first, ...)
408 * Find the total memory required. Ensure we don't overflow length. We
409 * aren't guaranteed to have SIZE_MAX, so use UINT_MAX as an acceptable
410 * substitute (see the x_nrealloc comments).
412 va_start(args, first);
413 for (string = first; string != NULL; string = va_arg(args, const char *)) {
414 if (length >= UINT_MAX - strlen(string)) {
416 sysdie("strings too long in concat");
418 length += strlen(string);
423 /* Create the string. */
424 result = xmalloc(length);
425 va_start(args, first);
427 for (string = first; string != NULL; string = va_arg(args, const char *)) {
428 memcpy(result + offset, string, strlen(string));
429 offset += strlen(string);
432 result[offset] = '\0';
438 * Given a struct timeval, return the number of seconds it represents as a
439 * double. Use difftime() to convert a time_t to a double.
442 tv_seconds(const struct timeval *tv)
444 return difftime(tv->tv_sec, 0) + tv->tv_usec * 1e-6;
449 * Given two struct timevals, return the difference in seconds.
452 tv_diff(const struct timeval *tv1, const struct timeval *tv0)
454 return tv_seconds(tv1) - tv_seconds(tv0);
459 * Given two struct timevals, return the sum in seconds as a double.
462 tv_sum(const struct timeval *tv1, const struct timeval *tv2)
464 return tv_seconds(tv1) + tv_seconds(tv2);
469 * Given a pointer to a string, skip any leading whitespace and return a
470 * pointer to the first non-whitespace character.
473 skip_whitespace(const char *p)
475 while (isspace((unsigned char) (*p)))
482 * Start a program, connecting its stdout to a pipe on our end and its stderr
483 * to /dev/null, and storing the file descriptor to read from in the two
484 * argument. Returns the PID of the new process. Errors are fatal.
487 test_start(const char *path, int *fd)
489 int fds[2], infd, errfd;
492 /* Create a pipe used to capture the output from the test program. */
493 if (pipe(fds) == -1) {
496 sysdie("can't create pipe");
499 /* Fork a child process, massage the file descriptors, and exec. */
505 sysdie("can't fork");
507 /* In the child. Set up our standard output. */
510 close(STDOUT_FILENO);
511 if (dup2(fds[1], STDOUT_FILENO) < 0)
515 /* Point standard input at /dev/null. */
517 infd = open("/dev/null", O_RDONLY);
519 _exit(CHILDERR_STDIN);
520 if (infd != STDIN_FILENO) {
521 if (dup2(infd, STDIN_FILENO) < 0)
526 /* Point standard error at /dev/null. */
527 close(STDERR_FILENO);
528 errfd = open("/dev/null", O_WRONLY);
530 _exit(CHILDERR_STDERR);
531 if (errfd != STDERR_FILENO) {
532 if (dup2(errfd, STDERR_FILENO) < 0)
537 /* Now, exec our process. */
538 if (execl(path, path, (char *) 0) == -1)
539 _exit(CHILDERR_EXEC);
541 /* In parent. Close the extra file descriptor. */
552 * Back up over the output saying what test we were executing.
555 test_backspace(struct testset *ts)
559 if (!isatty(STDOUT_FILENO))
561 for (i = 0; i < ts->length; i++)
563 for (i = 0; i < ts->length; i++)
565 for (i = 0; i < ts->length; i++)
572 * Allocate or resize the array of test results to be large enough to contain
573 * the test number in.
576 resize_results(struct testset *ts, unsigned long n)
581 /* If there's already enough space, return quickly. */
582 if (n <= ts->allocated)
586 * If no space has been allocated, do the initial allocation. Otherwise,
587 * resize. Start with 32 test cases and then add 1024 with each resize to
588 * try to reduce the number of reallocations.
590 if (ts->allocated == 0) {
591 s = (n > 32) ? n : 32;
592 ts->results = xcalloc(s, sizeof(enum test_status));
594 s = (n > ts->allocated + 1024) ? n : ts->allocated + 1024;
595 ts->results = xreallocarray(ts->results, s, sizeof(enum test_status));
598 /* Set the results for the newly-allocated test array. */
599 for (i = ts->allocated; i < s; i++)
600 ts->results[i] = TEST_INVALID;
606 * Report an invalid test number and set the appropriate flags. Pulled into a
607 * separate function since we do this in several places.
610 invalid_test_number(struct testset *ts, long n, enum test_verbose verbose)
614 printf("ABORTED (invalid test number %ld)\n", n);
621 * Read the plan line of test output, which should contain the range of test
622 * numbers. We may initialize the testset structure here if we haven't yet
623 * seen a test. Return true if initialization succeeded and the test should
624 * continue, false otherwise.
627 test_plan(const char *line, struct testset *ts, enum test_verbose verbose)
632 * Accept a plan without the leading 1.. for compatibility with older
633 * versions of runtests. This will only be allowed if we've not yet seen
636 line = skip_whitespace(line);
637 if (strncmp(line, "1..", 3) == 0)
641 * Get the count and check it for validity.
643 * If we have something of the form "1..0 # skip foo", the whole file was
644 * skipped; record that. If we do skip the whole file, zero out all of
645 * our statistics, since they're no longer relevant.
647 * strtol is called with a second argument to advance the line pointer
648 * past the count to make it simpler to detect the # skip case.
650 n = strtol(line, (char **) &line, 10);
652 line = skip_whitespace(line);
654 line = skip_whitespace(line + 1);
655 if (strncasecmp(line, "skip", 4) == 0) {
656 line = skip_whitespace(line + 4);
658 ts->reason = xstrdup(line);
659 ts->reason[strlen(ts->reason) - 1] = '\0';
672 puts("ABORTED (invalid test count)");
679 * If we are doing lazy planning, check the plan against the largest test
680 * number that we saw and fail now if we saw a check outside the plan
683 if (ts->plan == PLAN_PENDING && (unsigned long) n < ts->count) {
684 invalid_test_number(ts, (long) ts->count, verbose);
689 * Otherwise, allocated or resize the results if needed and update count,
690 * and then record that we've seen a plan.
692 resize_results(ts, (unsigned long) n);
693 ts->count = (unsigned long) n;
694 if (ts->plan == PLAN_INIT)
695 ts->plan = PLAN_FIRST;
696 else if (ts->plan == PLAN_PENDING)
697 ts->plan = PLAN_FINAL;
703 * Given a single line of output from a test, parse it and return the success
704 * status of that test. Anything printed to stdout not matching the form
705 * /^(not )?ok \d+/ is ignored. Sets ts->current to the test number that just
709 test_checkline(const char *line, struct testset *ts, enum test_verbose verbose)
711 enum test_status status = TEST_PASS;
715 unsigned long current;
718 /* Before anything, check for a test abort. */
719 bail = strstr(line, "Bail out!");
721 bail = skip_whitespace(bail + strlen("Bail out!"));
725 length = strlen(bail);
726 if (bail[length - 1] == '\n')
730 printf("ABORTED (%.*s)\n", (int) length, bail);
738 * If the given line isn't newline-terminated, it was too big for an
739 * fgets(), which means ignore it.
741 if (line[strlen(line) - 1] != '\n')
744 /* If the line begins with a hash mark, ignore it. */
748 /* If we haven't yet seen a plan, look for one. */
749 if (ts->plan == PLAN_INIT && isdigit((unsigned char) (*line))) {
750 if (!test_plan(line, ts, verbose))
752 } else if (strncmp(line, "1..", 3) == 0) {
753 if (ts->plan == PLAN_PENDING) {
754 if (!test_plan(line, ts, verbose))
759 puts("ABORTED (multiple plans)");
766 /* Parse the line, ignoring something we can't parse. */
767 if (strncmp(line, "not ", 4) == 0) {
771 if (strncmp(line, "ok", 2) != 0)
773 line = skip_whitespace(line + 2);
775 number = strtol(line, &end, 10);
776 if (errno != 0 || end == line)
777 current = ts->current + 1;
778 else if (number <= 0) {
779 invalid_test_number(ts, number, verbose);
782 current = (unsigned long) number;
783 if (current > ts->count && ts->plan == PLAN_FIRST) {
784 invalid_test_number(ts, (long) current, verbose);
788 /* We have a valid test result. Tweak the results array if needed. */
789 if (ts->plan == PLAN_INIT || ts->plan == PLAN_PENDING) {
790 ts->plan = PLAN_PENDING;
791 resize_results(ts, current);
792 if (current > ts->count)
797 * Handle directives. We should probably do something more interesting
798 * with unexpected passes of todo tests.
800 while (isdigit((unsigned char) (*line)))
802 line = skip_whitespace(line);
804 line = skip_whitespace(line + 1);
805 if (strncasecmp(line, "skip", 4) == 0)
807 if (strncasecmp(line, "todo", 4) == 0)
808 status = (status == TEST_FAIL) ? TEST_SKIP : TEST_FAIL;
811 /* Make sure that the test number is in range and not a duplicate. */
812 if (ts->results[current - 1] != TEST_INVALID) {
815 printf("ABORTED (duplicate test number %lu)\n", current);
821 /* Good results. Increment our various counters. */
835 ts->current = current;
836 ts->results[current - 1] = status;
837 if (!verbose && isatty(STDOUT_FILENO)) {
839 if (ts->plan == PLAN_PENDING)
840 outlen = printf("%lu/?", current);
842 outlen = printf("%lu/%lu", current, ts->count);
843 ts->length = (outlen >= 0) ? (unsigned int) outlen : 0;
850 * Print out a range of test numbers, returning the number of characters it
851 * took up. Takes the first number, the last number, the number of characters
852 * already printed on the line, and the limit of number of characters the line
853 * can hold. Add a comma and a space before the range if chars indicates that
854 * something has already been printed on the line, and print ... instead if
855 * chars plus the space needed would go over the limit (use a limit of 0 to
859 test_print_range(unsigned long first, unsigned long last, unsigned long chars,
862 unsigned int needed = 0;
865 for (n = first; n > 0; n /= 10)
868 for (n = last; n > 0; n /= 10)
874 if (limit > 0 && chars + needed > limit) {
876 if (chars <= limit) {
888 printf("%lu-", first);
896 * Summarize a single test set. The second argument is 0 if the set exited
897 * cleanly, a positive integer representing the exit status if it exited
898 * with a non-zero status, and a negative integer representing the signal
899 * that terminated it if it was killed by a signal.
902 test_summarize(struct testset *ts, int status)
905 unsigned long missing = 0;
906 unsigned long failed = 0;
907 unsigned long first = 0;
908 unsigned long last = 0;
911 fputs("ABORTED", stdout);
913 printf(" (passed %lu/%lu)", ts->passed, ts->count - ts->skipped);
915 for (i = 0; i < ts->count; i++) {
916 if (ts->results[i] == TEST_INVALID) {
918 fputs("MISSED ", stdout);
919 if (first && i == last)
923 test_print_range(first, last, missing - 1, 0);
931 test_print_range(first, last, missing - 1, 0);
934 for (i = 0; i < ts->count; i++) {
935 if (ts->results[i] == TEST_FAIL) {
936 if (missing && !failed)
939 fputs("FAILED ", stdout);
940 if (first && i == last)
944 test_print_range(first, last, failed - 1, 0);
952 test_print_range(first, last, failed - 1, 0);
953 if (!missing && !failed) {
954 fputs(!status ? "ok" : "dubious", stdout);
955 if (ts->skipped > 0) {
956 if (ts->skipped == 1)
957 printf(" (skipped %lu test)", ts->skipped);
959 printf(" (skipped %lu tests)", ts->skipped);
964 printf(" (exit status %d)", status);
966 printf(" (killed by signal %d%s)", -status,
967 WCOREDUMP(ts->status) ? ", core dumped" : "");
973 * Given a test set, analyze the results, classify the exit status, handle a
974 * few special error messages, and then pass it along to test_summarize() for
975 * the regular output. Returns true if the test set ran successfully and all
976 * tests passed or were skipped, false otherwise.
979 test_analyze(struct testset *ts)
983 if (ts->all_skipped) {
984 if (ts->reason == NULL)
987 printf("skipped (%s)\n", ts->reason);
989 } else if (WIFEXITED(ts->status) && WEXITSTATUS(ts->status) != 0) {
990 switch (WEXITSTATUS(ts->status)) {
993 puts("ABORTED (can't dup file descriptors)");
997 puts("ABORTED (execution failed -- not found?)");
1000 case CHILDERR_STDERR:
1002 puts("ABORTED (can't open /dev/null)");
1005 test_summarize(ts, WEXITSTATUS(ts->status));
1009 } else if (WIFSIGNALED(ts->status)) {
1010 test_summarize(ts, -WTERMSIG(ts->status));
1012 } else if (ts->plan != PLAN_FIRST && ts->plan != PLAN_FINAL) {
1013 puts("ABORTED (no valid test plan)");
1017 test_summarize(ts, 0);
1018 return (ts->failed == 0);
1024 * Runs a single test set, accumulating and then reporting the results.
1025 * Returns true if the test set was successfully run and all tests passed,
1029 test_run(struct testset *ts, enum test_verbose verbose)
1031 pid_t testpid, child;
1035 char buffer[BUFSIZ];
1037 /* Run the test program. */
1038 testpid = test_start(ts->path, &outfd);
1039 output = fdopen(outfd, "r");
1043 sysdie("fdopen failed");
1047 * Pass each line of output to test_checkline(), and print the line if
1048 * verbosity is requested.
1050 while (!ts->aborted && fgets(buffer, sizeof(buffer), output)) {
1052 printf("%s", buffer);
1053 test_checkline(buffer, ts, verbose);
1055 if (ferror(output) || ts->plan == PLAN_INIT)
1061 * Consume the rest of the test output, close the output descriptor,
1062 * retrieve the exit status, and pass that information to test_analyze()
1063 * for eventual output.
1065 while (fgets(buffer, sizeof(buffer), output))
1067 printf("%s", buffer);
1069 child = waitpid(testpid, &ts->status, 0);
1070 if (child == (pid_t) -1) {
1071 if (!ts->reported) {
1075 sysdie("waitpid for %u failed", (unsigned int) testpid);
1077 if (ts->all_skipped)
1079 status = test_analyze(ts);
1081 /* Convert missing tests to failed tests. */
1082 for (i = 0; i < ts->count; i++) {
1083 if (ts->results[i] == TEST_INVALID) {
1085 ts->results[i] = TEST_FAIL;
1093 /* Summarize a list of test failures. */
1095 test_fail_summary(const struct testlist *fails)
1099 unsigned long i, first, last, total;
1103 /* Failed Set Fail/Total (%) Skip Stat Failing (25)
1104 -------------------------- -------------- ---- ---- -------------- */
1105 for (; fails; fails = fails->next) {
1107 total = ts->count - ts->skipped;
1108 printf("%-26.26s %4lu/%-4lu %3.0f%% %4lu ", ts->file, ts->failed,
1109 total, total ? (ts->failed * 100.0) / total : 0, ts->skipped);
1110 if (WIFEXITED(ts->status))
1111 printf("%4d ", WEXITSTATUS(ts->status));
1121 for (i = 0; i < ts->count; i++) {
1122 if (ts->results[i] == TEST_FAIL) {
1123 if (first != 0 && i == last)
1127 chars += test_print_range(first, last, chars, 19);
1134 test_print_range(first, last, chars, 19);
1141 * Check whether a given file path is a valid test. Currently, this checks
1142 * whether it is executable and is a regular file. Returns true or false.
1145 is_valid_test(const char *path)
1149 if (access(path, X_OK) < 0)
1151 if (stat(path, &st) < 0)
1153 if (!S_ISREG(st.st_mode))
1160 * Given the name of a test, a pointer to the testset struct, and the source
1161 * and build directories, find the test. We try first relative to the current
1162 * directory, then in the build directory (if not NULL), then in the source
1163 * directory. In each of those directories, we first try a "-t" extension and
1164 * then a ".t" extension. When we find an executable program, we return the
1165 * path to that program. If none of those paths are executable, just fill in
1166 * the name of the test as is.
1168 * The caller is responsible for freeing the path member of the testset
1172 find_test(const char *name, const char *source, const char *build)
1175 const char *bases[3], *suffix, *base;
1177 const char *suffixes[3] = {"-t", ".t", ""};
1179 /* Possible base directories. */
1184 /* Try each suffix with each base. */
1185 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1186 suffix = suffixes[i];
1187 for (j = 0; j < ARRAY_SIZE(bases); j++) {
1191 path = concat(base, "/", name, suffix, (const char *) 0);
1192 if (is_valid_test(path))
1199 path = xstrdup(name);
1205 * Read a list of tests from a file, returning the list of tests as a struct
1206 * testlist, or NULL if there were no tests (such as a file containing only
1207 * comments). Reports an error to standard error and exits if the list of
1208 * tests cannot be read.
1210 static struct testlist *
1211 read_test_list(const char *filename)
1216 char buffer[BUFSIZ];
1217 const char *testname;
1218 struct testlist *listhead, *current;
1220 /* Create the initial container list that will hold our results. */
1221 listhead = xcalloc(1, sizeof(struct testlist));
1225 * Open our file of tests to run and read it line by line, creating a new
1226 * struct testlist and struct testset for each line.
1228 file = fopen(filename, "r");
1230 sysdie("can't open %s", filename);
1232 while (fgets(buffer, sizeof(buffer), file)) {
1234 length = strlen(buffer) - 1;
1235 if (buffer[length] != '\n') {
1236 fprintf(stderr, "%s:%u: line too long\n", filename, line);
1239 buffer[length] = '\0';
1241 /* Skip comments, leading spaces, and blank lines. */
1242 testname = skip_whitespace(buffer);
1243 if (strlen(testname) == 0)
1245 if (testname[0] == '#')
1248 /* Allocate the new testset structure. */
1249 if (current == NULL)
1252 current->next = xcalloc(1, sizeof(struct testlist));
1253 current = current->next;
1255 current->ts = xcalloc(1, sizeof(struct testset));
1256 current->ts->plan = PLAN_INIT;
1257 current->ts->file = xstrdup(testname);
1261 /* If there were no tests, current is still NULL. */
1262 if (current == NULL) {
1267 /* Return the results. */
1273 * Build a list of tests from command line arguments. Takes the argv and argc
1274 * representing the command line arguments and returns a newly allocated test
1275 * list, or NULL if there were no tests. The caller is responsible for
1278 static struct testlist *
1279 build_test_list(char *argv[], int argc)
1282 struct testlist *listhead, *current;
1284 /* Create the initial container list that will hold our results. */
1285 listhead = xcalloc(1, sizeof(struct testlist));
1288 /* Walk the list of arguments and create test sets for them. */
1289 for (i = 0; i < argc; i++) {
1290 if (current == NULL)
1293 current->next = xcalloc(1, sizeof(struct testlist));
1294 current = current->next;
1296 current->ts = xcalloc(1, sizeof(struct testset));
1297 current->ts->plan = PLAN_INIT;
1298 current->ts->file = xstrdup(argv[i]);
1301 /* If there were no tests, current is still NULL. */
1302 if (current == NULL) {
1307 /* Return the results. */
1312 /* Free a struct testset. */
1314 free_testset(struct testset *ts)
1325 * Run a batch of tests. Takes two additional parameters: the root of the
1326 * source directory and the root of the build directory. Test programs will
1327 * be first searched for in the current directory, then the build directory,
1328 * then the source directory. Returns true iff all tests passed, and always
1329 * frees the test list that's passed in.
1332 test_batch(struct testlist *tests, const char *source, const char *build,
1333 enum test_verbose verbose)
1337 unsigned int count = 0;
1339 struct timeval start, end;
1340 struct rusage stats;
1341 struct testlist *failhead = NULL;
1342 struct testlist *failtail = NULL;
1343 struct testlist *current, *next;
1345 unsigned long total = 0;
1346 unsigned long passed = 0;
1347 unsigned long skipped = 0;
1348 unsigned long failed = 0;
1349 unsigned long aborted = 0;
1351 /* Walk the list of tests to find the longest name. */
1352 for (current = tests; current != NULL; current = current->next) {
1353 length = strlen(current->ts->file);
1354 if (length > longest)
1359 * Add two to longest and round up to the nearest tab stop. This is how
1360 * wide the column for printing the current test name will be.
1364 longest += 8 - (longest % 8);
1366 /* Start the wall clock timer. */
1367 gettimeofday(&start, NULL);
1369 /* Now, plow through our tests again, running each one. */
1370 for (current = tests; current != NULL; current = current->next) {
1373 /* Print out the name of the test file. */
1374 fputs(ts->file, stdout);
1376 fputs("\n\n", stdout);
1378 for (i = strlen(ts->file); i < longest; i++)
1380 if (isatty(STDOUT_FILENO))
1384 ts->path = find_test(ts->file, source, build);
1385 succeeded = test_run(ts, verbose);
1390 /* Record cumulative statistics. */
1391 aborted += ts->aborted;
1392 total += ts->count + ts->all_skipped;
1393 passed += ts->passed;
1394 skipped += ts->skipped + ts->all_skipped;
1395 failed += ts->failed;
1398 /* If the test fails, we shuffle it over to the fail list. */
1400 if (failhead == NULL) {
1401 failhead = xmalloc(sizeof(struct testset));
1402 failtail = failhead;
1404 failtail->next = xmalloc(sizeof(struct testset));
1405 failtail = failtail->next;
1408 failtail->next = NULL;
1413 /* Stop the timer and get our child resource statistics. */
1414 gettimeofday(&end, NULL);
1415 getrusage(RUSAGE_CHILDREN, &stats);
1417 /* Summarize the failures and free the failure list. */
1418 if (failhead != NULL) {
1419 test_fail_summary(failhead);
1420 while (failhead != NULL) {
1421 next = failhead->next;
1427 /* Free the memory used by the test lists. */
1428 while (tests != NULL) {
1430 free_testset(tests->ts);
1435 /* Print out the final test summary. */
1439 printf("Aborted %lu test set", aborted);
1441 printf("Aborted %lu test sets", aborted);
1442 printf(", passed %lu/%lu tests", passed, total);
1443 } else if (failed == 0)
1444 fputs("All tests successful", stdout);
1446 printf("Failed %lu/%lu tests, %.2f%% okay", failed, total,
1447 (total - failed) * 100.0 / total);
1450 printf(", %lu test skipped", skipped);
1452 printf(", %lu tests skipped", skipped);
1455 printf("Files=%u, Tests=%lu", count, total);
1456 printf(", %.2f seconds", tv_diff(&end, &start));
1457 printf(" (%.2f usr + %.2f sys = %.2f CPU)\n", tv_seconds(&stats.ru_utime),
1458 tv_seconds(&stats.ru_stime),
1459 tv_sum(&stats.ru_utime, &stats.ru_stime));
1460 return (failed == 0 && aborted == 0);
1465 * Run a single test case. This involves just running the test program after
1466 * having done the environment setup and finding the test program.
1469 test_single(const char *program, const char *source, const char *build)
1473 path = find_test(program, source, build);
1474 if (execl(path, path, (char *) 0) == -1)
1475 sysdie("cannot exec %s", path);
1480 * Main routine. Set the C_TAP_SOURCE, C_TAP_BUILD, SOURCE, and BUILD
1481 * environment variables and then, given a file listing tests, run each test
1485 main(int argc, char *argv[])
1490 enum test_verbose verbose = CONCISE;
1491 char *c_tap_source_env = NULL;
1492 char *c_tap_build_env = NULL;
1493 char *source_env = NULL;
1494 char *build_env = NULL;
1495 const char *program;
1496 const char *shortlist;
1497 const char *list = NULL;
1498 const char *source = C_TAP_SOURCE;
1499 const char *build = C_TAP_BUILD;
1500 struct testlist *tests;
1503 while ((option = getopt(argc, argv, "b:hl:os:v")) != EOF) {
1509 printf(usage_message, program, program, program, usage_extra);
1529 if ((list == NULL && argc < 1) || (list != NULL && argc > 0)) {
1530 fprintf(stderr, usage_message, program, program, program, usage_extra);
1535 * If C_TAP_VERBOSE is set in the environment, that also turns on verbose
1538 if (getenv("C_TAP_VERBOSE") != NULL)
1542 * Set C_TAP_SOURCE and C_TAP_BUILD environment variables. Also set
1543 * SOURCE and BUILD for backward compatibility, although we're trying to
1544 * migrate to the ones with a C_TAP_* prefix.
1546 if (source != NULL) {
1547 c_tap_source_env = concat("C_TAP_SOURCE=", source, (const char *) 0);
1548 if (putenv(c_tap_source_env) != 0)
1549 sysdie("cannot set C_TAP_SOURCE in the environment");
1550 source_env = concat("SOURCE=", source, (const char *) 0);
1551 if (putenv(source_env) != 0)
1552 sysdie("cannot set SOURCE in the environment");
1554 if (build != NULL) {
1555 c_tap_build_env = concat("C_TAP_BUILD=", build, (const char *) 0);
1556 if (putenv(c_tap_build_env) != 0)
1557 sysdie("cannot set C_TAP_BUILD in the environment");
1558 build_env = concat("BUILD=", build, (const char *) 0);
1559 if (putenv(build_env) != 0)
1560 sysdie("cannot set BUILD in the environment");
1563 /* Run the tests as instructed. */
1565 test_single(argv[0], source, build);
1566 else if (list != NULL) {
1567 shortlist = strrchr(list, '/');
1568 if (shortlist == NULL)
1572 printf(banner, shortlist);
1573 tests = read_test_list(list);
1574 status = test_batch(tests, source, build, verbose) ? 0 : 1;
1576 tests = build_test_list(argv, argc);
1577 status = test_batch(tests, source, build, verbose) ? 0 : 1;
1580 /* For valgrind cleanliness, free all our memory. */
1581 if (source_env != NULL) {
1582 putenv((char *) "C_TAP_SOURCE=");
1583 putenv((char *) "SOURCE=");
1584 free(c_tap_source_env);
1587 if (build_env != NULL) {
1588 putenv((char *) "C_TAP_BUILD=");
1589 putenv((char *) "BUILD=");
1590 free(c_tap_build_env);