2 # Copyright (C) 2011-2024 Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 # As a special exception to the GNU General Public License, if you
18 # distribute this file as part of a program that contains a
19 # configuration script generated by Autoconf, you may include it under
20 # the same distribution terms that you use for the rest of that program.
22 # This file is maintained in Automake, please report
23 # bugs to <bug-automake@gnu.org> or send patches to
24 # <automake-patches@gnu.org>.
26 scriptversion
=2024-06-19.01
; # UTC
28 # Make unconditional expansion of undefined variables an error. This
29 # helps a lot in preventing typo-related bugs.
36 echo "$me: fatal: $*" >&2
51 tap-driver.sh --test-name NAME --log-file PATH --trs-file PATH
52 [--expect-failure {yes|no}] [--color-tests {yes|no}]
53 [--enable-hard-errors {yes|no}] [--ignore-exit]
54 [--diagnostic-string STRING] [--merge|--no-merge]
55 [--comments|--no-comments] [--] TEST-COMMAND
56 The '--test-name', '-log-file' and '--trs-file' options are mandatory.
58 Report bugs to <bug-automake@gnu.org>.
59 GNU Automake home page: <https://www.gnu.org/software/automake/>.
60 General help using GNU software: <https://www.gnu.org/gethelp/>.
64 # TODO: better error handling in option parsing (in particular, ensure
65 # TODO: $log_file, $trs_file and $test_name are defined).
66 test_name
= # Used for reporting.
67 log_file
= # Where to save the result and output of the test script.
68 trs_file
= # Where to save the metadata of the test run.
75 while test $# -gt 0; do
77 --help) print_usage
; exit $?
;;
78 --version) echo "$me (GNU Automake) $scriptversion"; exit $?
;;
79 --test-name) test_name
=$2; shift;;
80 --log-file) log_file
=$2; shift;;
81 --trs-file) trs_file
=$2; shift;;
82 --color-tests) color_tests
=$2; shift;;
83 --expect-failure) expect_failure
=$2; shift;;
84 --enable-hard-errors) shift;; # No-op.
87 --ignore-exit) ignore_exit
=1;;
88 --comments) comments
=1;;
89 --no-comments) comments
=0;;
90 --diagnostic-string) diag_string
=$2; shift;;
92 -*) usage_error
"invalid option: '$1'";;
97 test $# -gt 0 || usage_error
"missing test command"
99 case $expect_failure in
100 yes) expect_failure
=1;;
101 *) expect_failure
=0;;
104 if test $color_tests = yes; then
106 color_map["red"]="\e[0;31m" # Red.
107 color_map["grn"]="\e[0;32m" # Green.
108 color_map["lgn"]="\e[1;32m" # Light green.
109 color_map["blu"]="\e[1;34m" # Blue.
110 color_map["mgn"]="\e[0;35m" # Magenta.
111 color_map["std"]="\e[m" # No color.
112 color_for_result["ERROR"] = "mgn"
113 color_for_result["PASS"] = "grn"
114 color_for_result["XPASS"] = "red"
115 color_for_result["FAIL"] = "red"
116 color_for_result["XFAIL"] = "lgn"
117 color_for_result["SKIP"] = "blu"'
122 # :; is there to work around a bug in bash 3.2 (and earlier) which
123 # does not always set '$?' properly on redirection failure.
124 # See the Autoconf manual for more details.
127 # Ignore common signals (in this subshell only!), to avoid potential
128 # problems with Korn shells. Some Korn shells are known to propagate
129 # to themselves signals that have killed a child process they were
130 # waiting for; this is done at least for SIGINT (and usually only for
131 # it, in truth). Without the `trap' below, such a behavior could
132 # cause a premature exit in the current subshell, e.g., in case the
133 # test command it runs gets terminated by a SIGINT. Thus, the awk
134 # script we are piping into would never seen the exit status it
135 # expects on its last input line (which is displayed below by the
136 # last `echo $?' statement), and would thus die reporting an internal
138 # For more information, see the Autoconf manual and the threads:
139 # <https://lists.gnu.org/archive/html/bug-autoconf/2011-09/msg00004.html>
140 # <http://mail.opensolaris.org/pipermail/ksh93-integration-discuss/2009-February/004121.html>
142 if test $merge -gt 0; then
149 ) | LC_ALL
=C
${AM_TAP_AWK-awk} \
151 -v test_script_name
="$test_name" \
152 -v log_file
="$log_file" \
153 -v trs_file
="$trs_file" \
154 -v expect_failure
="$expect_failure" \
156 -v ignore_exit
="$ignore_exit" \
157 -v comments
="$comments" \
158 -v diag_string
="$diag_string" \
160 # TODO: the usages of "cat >&3" below could be optimized when using
161 # GNU awk, and/on on systems that supports /dev/fd/.
163 # Implementation note: in what follows, `result_obj` will be an
164 # associative array that (partly) simulates a TAP result object
165 # from the `TAP::Parser` perl module.
173 print me ": " msg | "cat >&2"
177 function abort(where)
179 fatal("internal error " where)
182 # Convert a boolean to a "yes"/"no" string.
185 return bool ? "yes" : "no";
188 function add_test_result(result)
190 if (!test_results_index)
191 test_results_index = 0
192 test_results_list[test_results_index] = result
193 test_results_index += 1
194 test_results_seen[result] = 1;
197 # Whether the test script should be re-run by "make recheck".
198 function must_recheck()
200 for (k in test_results_seen)
201 if (k != "XFAIL" && k != "PASS" && k != "SKIP")
206 # Whether the content of the log file associated to this test should
207 # be copied into the "global" test-suite.log.
208 function copy_in_global_log()
210 for (k in test_results_seen)
216 function get_global_test_result()
218 if ("ERROR" in test_results_seen)
220 if ("FAIL" in test_results_seen || "XPASS" in test_results_seen)
223 for (k in test_results_seen)
231 function stringify_result_obj(result_obj)
233 if (result_obj["is_unplanned"] || result_obj["number"] != testno)
236 if (plan_seen == LATE_PLAN)
239 if (result_obj["directive"] == "TODO")
240 return result_obj["is_ok"] ? "XPASS" : "XFAIL"
242 if (result_obj["directive"] == "SKIP")
243 return result_obj["is_ok"] ? "SKIP" : COOKED_FAIL;
245 if (length(result_obj["directive"]))
246 abort("in function stringify_result_obj()")
248 return result_obj["is_ok"] ? COOKED_PASS : COOKED_FAIL
251 function decorate_result(result)
253 color_name = color_for_result[result]
255 return color_map[color_name] "" result "" color_map["std"]
256 # If we are not using colorized output, or if we do not know how
257 # to colorize the given result, we should return it unchanged.
261 function report(result, details)
263 if (result ~ /^(X?(PASS|FAIL)|SKIP|ERROR)/)
265 msg = ": " test_script_name
266 add_test_result(result)
268 else if (result == "#")
270 msg = " " test_script_name ":"
274 abort("in function report()")
277 msg = msg " " details
278 # Output on console might be colorized.
279 print decorate_result(result) msg
280 # Log the result in the log file too, to help debugging (this is
281 # especially true when said result is a TAP error or "Bail out!").
282 print result msg | "cat >&3";
285 function testsuite_error(error_message)
287 report("ERROR", "- " error_message)
290 function handle_tap_result()
292 details = result_obj["number"];
293 if (length(result_obj["description"]))
294 details = details " " result_obj["description"]
296 if (plan_seen == LATE_PLAN)
298 details = details " # AFTER LATE PLAN";
300 else if (result_obj["is_unplanned"])
302 details = details " # UNPLANNED";
304 else if (result_obj["number"] != testno)
306 details = sprintf("%s # OUT-OF-ORDER (expecting %d)",
309 else if (result_obj["directive"])
311 details = details " # " result_obj["directive"];
312 if (length(result_obj["explanation"]))
313 details = details " " result_obj["explanation"]
316 report(stringify_result_obj(result_obj), details)
319 # `skip_reason` should be empty whenever planned > 0.
320 function handle_tap_plan(planned, skip_reason)
322 planned += 0 # Avoid getting confused if, say, `planned` is "00"
323 if (length(skip_reason) && planned > 0)
324 abort("in function handle_tap_plan()")
327 # Error, only one plan per stream is acceptable.
328 testsuite_error("multiple test plans")
331 planned_tests = planned
332 # The TAP plan can come before or after *all* the TAP results; we speak
333 # respectively of an "early" or a "late" plan. If we see the plan line
334 # after at least one TAP result has been seen, assume we have a late
335 # plan; in this case, any further test result seen after the plan will
336 # be flagged as an error.
337 plan_seen = (testno >= 1 ? LATE_PLAN : EARLY_PLAN)
338 # If testno > 0, we have an error ("too many tests run") that will be
339 # automatically dealt with later, so do not worry about it here. If
340 # $plan_seen is true, we have an error due to a repeated plan, and that
341 # has already been dealt with above. Otherwise, we have a valid "plan
342 # with SKIP" specification, and should report it as a particular kind
344 if (planned == 0 && testno == 0)
346 if (length(skip_reason))
347 skip_reason = "- " skip_reason;
348 report("SKIP", skip_reason);
352 function extract_tap_comment(line)
354 if (index(line, diag_string) == 1)
356 # Strip leading `diag_string` from `line`.
357 line = substr(line, length(diag_string) + 1)
358 # And strip any leading and trailing whitespace left.
359 sub("^[ \t]*", "", line)
360 sub("[ \t]*$", "", line)
361 # Return what is left (if any).
367 # When this function is called, we know that line is a TAP result line,
368 # so that it matches the (perl) RE "^(not )?ok\b".
369 function setup_result_obj(line)
371 # Get the result, and remove it from the line.
372 result_obj["is_ok"] = (substr(line, 1, 2) == "ok" ? 1 : 0)
373 sub("^(not )?ok[ \t]*", "", line)
375 # If the result has an explicit number, get it and strip it; otherwise,
376 # automatically assign the next test number to it.
377 if (line ~ /^[0-9]+$/ || line ~ /^[0-9]+[^a-zA-Z0-9_]/)
379 match(line, "^[0-9]+")
380 # The final `+ 0` is to normalize numbers with leading zeros.
381 result_obj["number"] = substr(line, 1, RLENGTH) + 0
382 line = substr(line, RLENGTH + 1)
386 result_obj["number"] = testno
389 if (plan_seen == LATE_PLAN)
390 # No further test results are acceptable after a "late" TAP plan
392 result_obj["is_unplanned"] = 1
393 else if (plan_seen && testno > planned_tests)
394 result_obj["is_unplanned"] = 1
396 result_obj["is_unplanned"] = 0
398 # Strip trailing and leading whitespace.
399 sub("^[ \t]*", "", line)
400 sub("[ \t]*$", "", line)
402 # This will have to be corrected if we have a "TODO"/"SKIP" directive.
403 result_obj["description"] = line
404 result_obj["directive"] = ""
405 result_obj["explanation"] = ""
407 if (index(line, "#") == 0)
408 return # No possible directive, nothing more to do.
410 # Directives are case-insensitive.
411 rx = "[ \t]*#[ \t]*([tT][oO][dD][oO]|[sS][kK][iI][pP])[ \t]*"
413 # See whether we have the directive, and if yes, where.
414 pos = match(line, rx "$")
416 pos = match(line, rx "[^a-zA-Z0-9_]")
418 # If there was no TAP directive, we have nothing more to do.
422 # Let`s now see if the TAP directive has been escaped. For example:
423 # escaped: ok \# SKIP
424 # not escaped: ok \\# SKIP
425 # escaped: ok \\\\\# SKIP
426 # not escaped: ok \ # SKIP
427 if (substr(line, pos, 1) == "#")
430 for (i = pos; i > 1 && substr(line, i - 1, 1) == "\\"; i--)
432 if (bslash_count % 2)
433 return # Directive was escaped.
436 # Strip the directive and its explanation (if any) from the test
438 result_obj["description"] = substr(line, 1, pos - 1)
439 # Now remove the test description from the line, that has been dealt
441 line = substr(line, pos)
442 # Strip the directive, and save its value (normalized to upper case).
443 sub("^[ \t]*#[ \t]*", "", line)
444 result_obj["directive"] = toupper(substr(line, 1, 4))
445 line = substr(line, 5)
446 # Now get the explanation for the directive (if any), with leading
447 # and trailing whitespace removed.
448 sub("^[ \t]*", "", line)
449 sub("[ \t]*$", "", line)
450 result_obj["explanation"] = line
453 function get_test_exit_message(status)
457 if (status !~ /^[1-9][0-9]*$/)
458 abort("getting exit status")
461 else if (status == 127)
462 exit_details = " (command not found?)"
463 else if (status >= 128 && status <= 255)
464 exit_details = sprintf(" (terminated by signal %d?)", status - 128)
465 else if (status > 256 && status <= 384)
466 # We used to report an "abnormal termination" here, but some Korn
467 # shells, when a child process die due to signal number n, can leave
468 # in $? an exit status of 256+n instead of the more standard 128+n.
469 # Apparently, both behaviors are allowed by POSIX (2008), so be
470 # prepared to handle them both. See also Austin Group report ID
471 # 0000051 <http://www.austingroupbugs.net/view.php?id=51>
472 exit_details = sprintf(" (terminated by signal %d?)", status - 256)
474 # Never seen in practice.
475 exit_details = " (abnormal termination)"
476 return sprintf("exited with status %d%s", status, exit_details)
479 function write_test_results()
481 print ":global-test-result: " get_global_test_result() > trs_file
482 print ":recheck: " yn(must_recheck()) > trs_file
483 print ":copy-in-global-log: " yn(copy_in_global_log()) > trs_file
484 for (i = 0; i < test_results_index; i += 1)
485 print ":test-result: " test_results_list[i] > trs_file
497 # Properly initialized once the TAP plan is seen.
500 COOKED_PASS = expect_failure ? "XPASS": "PASS";
501 COOKED_FAIL = expect_failure ? "XFAIL": "FAIL";
503 # Enumeration-like constants to remember which kind of plan (if any)
504 # has been seen. It is important that NO_PLAN evaluates "false" as
510 testno = 0 # Number of test results seen so far.
511 bailed_out = 0 # Whether a "Bail out!" directive has been seen.
513 # Whether the TAP plan has been seen or not, and if yes, which kind
514 # it is ("early" is seen before any test result, "late" otherwise).
525 # Involutions required so that we are able to read the exit status
526 # from the last input line.
528 if (st < 0) # I/O error.
529 fatal("I/O error while reading from input stream")
530 else if (st == 0) # End-of-input
533 abort("in input loop: only one input line")
548 # Copy any input line verbatim into the log file.
550 # Parsing of TAP input should stop after a "Bail out!" directive.
555 if ($0 ~ /^(not )?ok$/ || $0 ~ /^(not )?ok[^a-zA-Z0-9_]/)
561 # TAP plan (normal or "SKIP" without explanation).
562 else if ($0 ~ /^1\.\.[0-9]+[ \t]*$/)
564 # The next two lines will put the number of planned tests in $0.
567 handle_tap_plan($0, "")
570 # TAP "SKIP" plan, with an explanation.
571 else if ($0 ~ /^1\.\.0+[ \t]*#/)
573 # The next lines will put the skip explanation in $0, stripping
574 # any leading and trailing whitespace. This is a little more
575 # tricky in truth, since we want to also strip a potential leading
576 # "SKIP" string from the message.
577 sub("^[^#]*#[ \t]*(SKIP[: \t][ \t]*)?", "")
579 handle_tap_plan(0, $0)
582 # Older versions of prove and TAP::Harness (e.g., 3.17) did not
583 # recognize a "Bail out!" directive when preceded by leading
584 # whitespace, but more modern versions (e.g., 3.23) do. So we
585 # emulate the latter, "more modern" behavior.
586 else if ($0 ~ /^[ \t]*Bail out!/)
589 # Get the bailout message (if any), with leading and trailing
590 # whitespace stripped. The message remains stored in `$0`.
591 sub("^[ \t]*Bail out![ \t]*", "");
593 # Format the error message for the
594 bailout_message = "Bail out!"
596 bailout_message = bailout_message " " $0
597 testsuite_error(bailout_message)
599 # Maybe we have to look for diagnostic comments too.
600 else if (comments != 0)
602 comment = extract_tap_comment($0);
604 report("#", comment);
612 # A "Bail out!" directive should cause us to ignore any following TAP
613 # error, as well as a non-zero exit status from the TAP producer.
618 testsuite_error("missing test plan")
620 else if (planned_tests != testno)
622 bad_amount = testno > planned_tests ? "many" : "few"
623 testsuite_error(sprintf("too %s tests run (expected %d, got %d)",
624 bad_amount, planned_tests, testno))
628 # Fetch exit status from the last line.
629 exit_message = get_test_exit_message(nextline)
631 testsuite_error(exit_message)
639 } # End of "BEGIN" block.
642 # TODO: document that we consume the file descriptor 3 :-(
645 test $?
-eq 0 || fatal
"I/O or internal error"
650 # eval: (add-hook 'before-save-hook 'time-stamp)
651 # time-stamp-start: "scriptversion="
652 # time-stamp-format: "%:y-%02m-%02d.%02H"
653 # time-stamp-time-zone: "UTC0"
654 # time-stamp-end: "; # UTC"