2 # Copyright (C) 2011 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 <http://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=2011-08-21.21; # 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.
60 # TODO: better error handling in option parsing (in particular, ensure
61 # TODO: $log_file, $trs_file and $test_name are defined).
62 test_name= # Used for reporting.
63 log_file= # Where to save the result and output of the test script.
64 trs_file= # Where to save the metadata of the test run.
71 while test $# -gt 0; do
73 --help) print_usage; exit $?;;
74 --version) echo "$me $scriptversion"; exit $?;;
75 --test-name) test_name=$2; shift;;
76 --log-file) log_file=$2; shift;;
77 --trs-file) trs_file=$2; shift;;
78 --color-tests) color_tests=$2; shift;;
79 --expect-failure) expect_failure=$2; shift;;
80 --enable-hard-errors) shift;; # No-op.
83 --ignore-exit) ignore_exit=1;;
84 --comments) comments=1;;
85 --no-comments) comments=0;;
86 --diagnostic-string) diag_string=$2; shift;;
88 -*) usage_error "invalid option: '$1'";;
93 test $# -gt 0 || usage_error "missing test command"
95 case $expect_failure in
96 yes) expect_failure=1;;
100 if test $color_tests = yes; then
102 color_map["red"]="
\e[0;31m" # Red.
103 color_map["grn"]="
\e[0;32m" # Green.
104 color_map["lgn"]="
\e[1;32m" # Light green.
105 color_map["blu"]="
\e[1;34m" # Blue.
106 color_map["mgn"]="
\e[0;35m" # Magenta.
107 color_map["std"]="
\e[m" # No color.
108 color_for_result["ERROR"] = "mgn"
109 color_for_result["PASS"] = "grn"
110 color_for_result["XPASS"] = "red"
111 color_for_result["FAIL"] = "red"
112 color_for_result["XFAIL"] = "lgn"
113 color_for_result["SKIP"] = "blu"'
119 # FIXME: this usage loses the test program exit status. We should
120 # probably rewrite the awk script to use the
121 # expression | getline [var]
122 # idiom, which should allow us to obtain the final exit status from
123 # <expression> when closing it.
124 { test $merge -eq 0 || exec 2>&1; "$@"; } \
125 | LC_ALL=C ${AM_TAP_AWK-awk} \
127 -v test_script_name="$test_name" \
128 -v log_file="$log_file" \
129 -v trs_file="$trs_file" \
130 -v expect_failure="$expect_failure" \
132 -v ignore_exit="$ignore_exit" \
133 -v comments="$comments" \
134 -v diag_string="$diag_string" \
136 # FIXME: the usages of "cat >&3" below could be optimized whne using
137 # FIXME: GNU awk, and/on on systems that supports /dev/fd/.
139 # Implementation note: in what follows, `result_obj` will be an
140 # associative array that (partly) simulates a TAP result object
141 # from the `TAP::Parser` perl module.
149 print me ": " msg | "cat >&3"
153 function abort(where)
155 fatal("internal error " where)
158 # Convert a boolean to a "yes"/"no" string.
161 return bool ? "yes" : "no";
164 function add_test_result(result)
166 if (!test_results_index)
167 test_results_index = 0
168 test_results_list[test_results_index] = result
169 test_results_index += 1
170 test_results_seen[result] = 1;
173 # Whether the test script should be re-run by "make recheck".
174 function must_recheck()
176 for (k in test_results_seen)
177 if (k != "XFAIL" && k != "PASS" && k != "SKIP")
182 # Whether the content of the log file associated to this test should
183 # be copied into the "global" test-suite.log.
184 function copy_in_global_log()
186 for (k in test_results_seen)
192 # FIXME: this can certainly be improved ...
193 function get_global_test_result()
195 if ("ERROR" in test_results_seen)
198 for (k in test_results_seen)
203 if ("FAIL" in test_results_seen || "XPASS" in test_results_seen)
208 function stringify_result_obj(obj)
210 if (obj["is_unplanned"] || obj["number"] != testno)
213 if (plan_seen == LATE_PLAN)
216 if (result_obj["directive"] == "TODO")
217 return obj["is_ok"] ? "XPASS" : "XFAIL"
219 if (result_obj["directive"] == "SKIP")
220 return obj["is_ok"] ? "SKIP" : COOKED_FAIL;
222 if (length(result_obj["directive"]))
223 abort("in function stringify_result_obj()")
225 return obj["is_ok"] ? COOKED_PASS : COOKED_FAIL
228 function decorate_result(result)
230 color_name = color_for_result[result]
232 return color_map[color_name] "" result "" color_map["std"]
233 # If we are not using colorized output, or if we do not know how
234 # to colorize the given result, we should return it unchanged.
238 function report(result, details)
240 if (result ~ /^(X?(PASS|FAIL)|SKIP|ERROR)/)
242 msg = ": " test_script_name
243 add_test_result(result)
245 else if (result == "#")
247 msg = " " test_script_name ":"
251 abort("in function report()")
254 msg = msg " " details
255 # Output on console might be colorized.
256 print decorate_result(result) msg | "cat >&3";
257 # Log the result in the log file too, to help debugging (this is
258 # especially true when said result is a TAP error or "Bail out!").
262 function testsuite_error(error_message)
264 report("ERROR", "- " error_message)
267 function handle_tap_result()
269 details = result_obj["number"];
270 if (length(result_obj["description"]))
271 details = details " " result_obj["description"]
273 if (plan_seen == LATE_PLAN)
275 details = details " # AFTER LATE PLAN";
277 else if (result_obj["is_unplanned"])
279 details = details " # UNPLANNED";
281 else if (result_obj["number"] != testno)
283 details = sprintf("%s # OUT-OF-ORDER (expecting %d)",
286 else if (result_obj["directive"])
288 details = details " # " result_obj["directive"];
289 if (length(result_obj["explanation"]))
290 details = details " " result_obj["explanation"]
293 report(stringify_result_obj(result_obj), details)
296 # `skip_reason` should be emprty whenever planned > 0.
297 function handle_tap_plan(planned, skip_reason)
299 planned += 0 # Avoid getting confused if, say, `planned` is "00"
300 if (length(skip_reason) && planned > 0)
301 abort("in function handle_tap_plan()")
304 # Error, only one plan per stream is acceptable.
305 testsuite_error("multiple test plans")
308 planned_tests = planned
309 # The TAP plan can come before or after *all* the TAP results; we speak
310 # respectively of an "early" or a "late" plan. If we see the plan line
311 # after at least one TAP result has been seen, assume we have a late
312 # plan; in this case, any further test result seen after the plan will
313 # be flagged as an error.
314 plan_seen = (testno >= 1 ? LATE_PLAN : EARLY_PLAN)
315 # If testno > 0, we have an error ("too many tests run") that will be
316 # automatically dealt with later, so do not worry about it here. If
317 # $plan_seen is true, we have an error due to a repeated plan, and that
318 # has already been dealt with above. Otherwise, we have a valid "plan
319 # with SKIP" specification, and should report it as a particular kind
321 if (planned == 0 && testno == 0)
323 if (length(skip_reason))
324 skip_reason = "- " skip_reason;
325 report("SKIP", skip_reason);
329 function extract_tap_comment(line)
331 # FIXME: verify there is not an off-by-one bug here.
332 if (index(line, diag_string) == 1)
334 # Strip leading `diag_string` from `line`.
335 # FIXME: verify there is not an off-by-one bug here.
336 line = substr(line, length(diag_string) + 1)
337 # And strip any leading and trailing whitespace left.
338 sub("^[ \\t]*", "", line)
339 sub("[ \\t]*$", "", line)
340 # Return what is left (if any).
346 # When this function is called, we know that line is a TAP result line,
347 # so that it matches the (perl) RE "^(not )?ok\b".
348 function setup_result_obj(line)
350 # Get the result, and remove it from the line.
351 result_obj["is_ok"] = (substr(line, 1, 2) == "ok" ? 1 : 0)
352 sub("^(not )?ok[ \\t]*", "", line)
354 # If the result has an explicit number, get it and strip it; otherwise,
355 # automatically assing the next progresive number to it.
356 if (line ~ /^[0-9]+$/ || line ~ /^[0-9]+[^a-zA-Z0-9_]/)
358 match(line, "^[0-9]+")
359 # The final `+ 0` is to normalize numbers with leading zeros.
360 result_obj["number"] = substr(line, 1, RLENGTH) + 0
361 line = substr(line, RLENGTH + 1)
365 result_obj["number"] = testno
368 if (plan_seen == LATE_PLAN)
369 # No further test results are acceptable after a "late" TAP plan
371 result_obj["is_unplanned"] = 1
372 else if (plan_seen && testno > planned_tests)
373 result_obj["is_unplanned"] = 1
375 result_obj["is_unplanned"] = 0
377 # Strip trailing and leading whitespace.
378 sub("^[ \\t]*", "", line)
379 sub("[ \\t]*$", "", line)
381 # This will have to be corrected if we have a "TODO"/"SKIP" directive.
382 result_obj["description"] = line
383 result_obj["directive"] = ""
384 result_obj["explanation"] = ""
386 # TODO: maybe we should allow a way to escape "#"?
387 if (index(line, "#") == 0)
388 return # No possible directive, nothing more to do.
390 # Directives are case-insensitive.
391 rx = "[ \\t]*#[ \\t]*([tT][oO][dD][oO]|[sS][kK][iI][pP])[ \\t]*"
393 # See whether we have the directive, and if yes, where.
394 pos = match(line, rx "$")
396 pos = match(line, rx "[^a-zA-Z0-9_]")
398 # If there was no TAP directive, we have nothing more to do.
402 # Strip the directive and its explanation (if any) from the test
404 result_obj["description"] = substr(line, 1, pos - 1)
405 # Now remove the test description from the line, that has been dealt
407 line = substr(line, pos)
408 # Strip the directive, and save its value (normalized to upper case).
409 sub("^[ \\t]*#[ \\t]*", "", line)
410 result_obj["directive"] = toupper(substr(line, 1, 4))
411 line = substr(line, 5)
412 # Now get the explanation for the directive (if any), with leading
413 # and trailing whitespace removed.
414 sub("^[ \\t]*", "", line)
415 sub("[ \\t]*$", "", line)
416 result_obj["explanation"] = line
419 function write_test_results()
421 print ":global-test-result: " get_global_test_result() > trs_file
422 print ":recheck: " yn(must_recheck()) > trs_file
423 print ":copy-in-global-log: " yn(copy_in_global_log()) > trs_file
424 for (i = 0; i < test_results_index; i += 1)
425 print ":test-result: " test_results_list[i] > trs_file
437 # Properly initialized once the TAP plan is seen.
440 COOKED_PASS = expect_failure ? "XPASS": "PASS";
441 COOKED_FAIL = expect_failure ? "XFAIL": "FAIL";
443 # Enumeration-like constants to remember which kind of plan (if any)
444 # has been seen. It is important that NO_PLAN evaluates "false" as
450 testno = 0 # Number of test results seen so far.
451 bailed_out = 0 # Whether a "Bail out!" directive has been seen.
453 # Whether the TAP plan has been seen or not, and if yes, which kind
454 # it is ("early" is seen before any test result, "late" otherwise).
464 # Copy any input line verbatim into the log file.
466 # Parsing of TAP input should stop after a "Bail out!" directive.
472 ($0 ~ /^(not )?ok$/ || $0 ~ /^(not )?ok[^a-zA-Z0-9_]/) {
481 # TAP plan (normal or "SKIP" without explanation).
482 /^1\.\.[0-9]+[ \t]*$/ {
484 # The next two lines will put the number of planned tests in $0.
487 handle_tap_plan($0, "")
492 # TAP "SKIP" plan, with an explanation.
495 # The next lines will put the skip explanation in $0, stripping any
496 # leading and trailing whitespace. This is a little more tricky in
497 # thruth, since we want to also strip a potential leading "SKIP"
498 # string from the message.
499 sub("^[^#]*#[ \\t]*(SKIP[: \\t][ \\t]*)?", "")
501 handle_tap_plan(0, $0)
510 # Get the bailout message (if any), with leading and trailing
511 # whitespace stripped. The message remains stored in `$0`.
512 sub("^Bail out![ \\t]*", "");
514 # Format the error message for the
515 bailout_message = "Bail out!"
517 bailout_message = bailout_message " " $0
518 testsuite_error(bailout_message)
525 comment = extract_tap_comment($0);
527 report("#", comment);
537 # A "Bail out!" directive should cause us to ignore any following TAP
538 # error, as well as a non-zero exit status from the TAP producer.
542 testsuite_error("missing test plan")
543 else if (planned_tests != testno)
545 bad_amount = testno > planned_tests ? "many" : "few"
546 testsuite_error(sprintf("too %s tests run (expected %d, got %d)",
547 bad_amount, planned_tests, testno))
556 # TODO: document that we consume the file descriptor 3 :-(
557 } 3>&1 >"$log_file" 2>&1
559 test $? -eq 0 || fatal "I/O or internal error"
564 # eval: (add-hook 'write-file-hooks 'time-stamp)
565 # time-stamp-start: "scriptversion="
566 # time-stamp-format: "%:y-%02m-%02d.%02H"
567 # time-stamp-time-zone: "UTC"
568 # time-stamp-end: "; # UTC"