2 # Copyright (C) 2011-2012 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 # ---------------------------------- #
27 # Imports, static data, and setup. #
28 # ---------------------------------- #
30 use warnings FATAL
=> 'all';
35 my $VERSION = '2012-02-01.19'; # UTC
37 my $ME = "tap-driver.pl";
41 tap-driver --test-name=NAME --log-file=PATH --trs-file=PATH
42 [--expect-failure={yes|no}] [--color-tests={yes|no}]
43 [--enable-hard-errors={yes|no}] [--ignore-exit]
44 [--diagnostic-string=STRING] [--merge|--no-merge]
45 [--comments|--no-comments] [--] TEST-COMMAND
46 The `--test-name', `--log-file' and `--trs-file' options are mandatory.
49 my $HELP = "$ME: TAP-aware test driver for Automake testsuite harness." .
52 # Keep this in sync with `lib/am/check.am:$(am__tty_colors)'.
63 # It's important that NO_PLAN evaluates "false" as a boolean.
64 use constant NO_PLAN
=> 0;
65 use constant EARLY_PLAN
=> 1;
66 use constant LATE_PLAN
=> 2;
68 # ------------------- #
70 # ------------------- #
72 my $testno = 0; # Number of test results seen so far.
73 my $bailed_out = 0; # Whether a "Bail out!" directive has been seen.
74 my $parser; # TAP parser object (will be initialized later).
76 # Whether the TAP plan has been seen or not, and if yes, which kind
77 # it is ("early" is seen before any test result, "late" otherwise).
78 my $plan_seen = NO_PLAN
;
86 "expect-failure" => 0,
92 my $test_script_name = undef;
95 my $diag_string = "#";
97 Getopt
::Long
::GetOptions
99 'help' => sub { print $HELP; exit 0; },
100 'version' => sub { print "$ME $VERSION\n"; exit 0; },
101 'test-name=s' => \
$test_script_name,
102 'log-file=s' => \
$log_file,
103 'trs-file=s' => \
$trs_file,
104 'color-tests=s' => \
&bool_opt
,
105 'expect-failure=s' => \
&bool_opt
,
106 'enable-hard-errors=s' => sub {}, # No-op.
107 'diagnostic-string=s' => \
$diag_string,
108 'comments' => sub { $cfg{"comments"} = 1; },
109 'no-comments' => sub { $cfg{"comments"} = 0; },
110 'merge' => sub { $cfg{"merge"} = 1; },
111 'no-merge' => sub { $cfg{"merge"} = 0; },
112 'ignore-exit' => sub { $cfg{"ignore-exit"} = 1; },
119 sub add_test_result
($);
122 sub copy_in_global_log
();
123 sub decorate_result
($);
124 sub extract_tap_comment
($);
126 sub get_global_test_result
();
127 sub get_test_exit_message
();
128 sub get_test_results
();
129 sub handle_tap_bailout
($);
130 sub handle_tap_plan
($);
131 sub handle_tap_result
($);
132 sub is_null_string
($);
137 sub setup_parser
(@
);
138 sub stringify_result_obj
($);
139 sub testsuite_error
($);
140 sub trap_perl_warnings_and_errors
();
141 sub write_test_results
();
150 my ($opt, $val) = @_;
151 if ($val =~ /^(?:y|yes)\z/i)
155 elsif ($val =~ /^(?:n|no)\z/i)
161 die "$ME: invalid argument '$val' for option '$opt'\n";
165 # If the given string is undefined or empty, return true, otherwise
166 # return false. This function is useful to avoid pitfalls like:
167 # if ($message) { print "$message\n"; }
168 # which wouldn't print anything if $message is the literal "0".
169 sub is_null_string
($)
172 return ! (defined $str and length $str);
175 # Convert a boolean to a "yes"/"no" string.
179 return $bool ?
"yes" : "no";
184 my (@test_results_list, %test_results_seen);
186 sub add_test_result
($)
189 push @test_results_list, $res;
190 $test_results_seen{$res} = 1;
193 sub get_test_results
()
195 return @test_results_list;
198 # Whether the test script should be re-run by "make recheck".
201 return grep { !/^(?:XFAIL|PASS|SKIP)$/ } (keys %test_results_seen);
204 # Whether the content of the log file associated to this test should
205 # be copied into the "global" test-suite.log.
206 sub copy_in_global_log
()
208 return grep { not $_ eq "PASS" } (keys %test_results_seen);
211 # FIXME: this can certainly be improved ...
212 sub get_global_test_result
()
215 if $test_results_seen{"ERROR"};
217 if $test_results_seen{"FAIL"} || $test_results_seen{"XPASS"};
219 if scalar keys %test_results_seen == 1 && $test_results_seen{"SKIP"};
225 sub write_test_results
()
227 open RES
, ">", $trs_file or die "$ME: opening $trs_file: $!\n";
228 print RES
":global-test-result: " . get_global_test_result
. "\n";
229 print RES
":recheck: " . yn
(must_recheck
) . "\n";
230 print RES
":copy-in-global-log: " . yn
(copy_in_global_log
) . "\n";
231 foreach my $result (get_test_results
)
233 print RES
":test-result: $result\n";
235 close RES
or die "$ME: closing $trs_file: $!\n";
238 sub trap_perl_warnings_and_errors
()
240 $SIG{__WARN__
} = $SIG{__DIE__
} = sub
242 # Be sure to send the warning/error message to the original stderr
243 # (presumably the console), not into the log file.
244 open STDERR
, ">&OLDERR";
251 # Redirect stderr and stdout to a temporary log file. Save the
252 # original stdout stream, since we need it to print testsuite
253 # progress output. Save original stderr stream, so that we can
254 # redirect warning and error messages from perl there.
255 open LOG
, ">", $log_file or die "$ME: opening $log_file: $!\n";
256 open OLDOUT
, ">&STDOUT" or die "$ME: duplicating stdout: $!\n";
257 open OLDERR
, ">&STDERR" or die "$ME: duplicating stdout: $!\n";
258 *OLDERR
= *OLDERR
; # To pacify a "used only once" warning.
259 trap_perl_warnings_and_errors
;
260 open STDOUT
, ">&LOG" or die "$ME: redirecting stdout: $!\n";
261 open STDERR
, ">&LOG" or die "$ME: redirecting stderr: $!\n";
267 eval { $parser = TAP
::Parser
->new ({exec => \
@_, merge
=> $cfg{merge
}}) };
270 # Don't use the error message in $@ as set by TAP::Parser, since
271 # currently it's both too generic (at the point of being basically
272 # useless) and quite long.
273 report
"ERROR", "- couldn't execute test script";
278 sub get_test_exit_message
()
280 my $wstatus = $parser->wait;
281 # Watch out for possible internal errors.
282 die "$ME: couldn't get the exit status of the TAP producer"
283 unless defined $wstatus;
284 # Return an undefined value if the producer exited with success.
285 return unless $wstatus;
286 # Otherwise, determine whether it exited with error or was terminated
288 use POSIX qw
(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG
);
289 if (WIFEXITED
($wstatus))
291 return sprintf "exited with status %d", WEXITSTATUS
($wstatus);
293 elsif (WIFSIGNALED
($wstatus))
295 return sprintf "terminated by signal %d", WTERMSIG
($wstatus);
299 return "terminated abnormally";
303 sub stringify_result_obj
($)
305 my $result_obj = shift;
306 my $COOKED_PASS = $cfg{"expect-failure"} ?
"XPASS": "PASS";
307 my $COOKED_FAIL = $cfg{"expect-failure"} ?
"XFAIL": "FAIL";
308 if ($result_obj->is_unplanned || $result_obj->number != $testno)
312 elsif ($plan_seen == LATE_PLAN
)
316 elsif (!$result_obj->directive)
318 return $result_obj->is_ok ?
$COOKED_PASS: $COOKED_FAIL;
320 elsif ($result_obj->has_todo)
322 return $result_obj->is_actual_ok ?
"XPASS" : "XFAIL";
324 elsif ($result_obj->has_skip)
326 return $result_obj->is_ok ?
"SKIP" : $COOKED_FAIL;
328 die "$ME: INTERNAL ERROR"; # NOTREACHED
333 my ($color_name, $text) = @_;
334 return $COLOR{$color_name} . $text . $COLOR{'std'};
337 sub decorate_result
($)
340 return $result unless $cfg{"color-tests"};
341 my %color_for_result =
350 if (my $color = $color_for_result{$result})
352 return colored
($color, $result);
356 return $result; # Don't colorize unknown stuff.
362 my ($msg, $result, $explanation) = (undef, @_);
363 if ($result =~ /^(?:X?(?:PASS|FAIL)|SKIP|ERROR)/)
365 $msg = ": $test_script_name";
366 add_test_result
$result;
368 elsif ($result eq "#")
370 $msg = " $test_script_name:";
374 die "$ME: INTERNAL ERROR"; # NOTREACHED
376 $msg .= " $explanation" if defined $explanation;
378 # Output on console might be colorized.
379 print OLDOUT decorate_result
($result) . $msg;
380 # Log the result in the log file too, to help debugging (this is
381 # especially true when said result is a TAP error or "Bail out!").
382 print $result . $msg;
385 sub testsuite_error
($)
387 report
"ERROR", "- $_[0]";
390 sub handle_tap_result
($)
393 my $result_obj = shift;
395 my $test_result = stringify_result_obj
$result_obj;
396 my $string = $result_obj->number;
398 my $description = $result_obj->description;
399 $string .= " $description"
400 unless is_null_string
$description;
402 if ($plan_seen == LATE_PLAN
)
404 $string .= " # AFTER LATE PLAN";
406 elsif ($result_obj->is_unplanned)
408 $string .= " # UNPLANNED";
410 elsif ($result_obj->number != $testno)
412 $string .= " # OUT-OF-ORDER (expecting $testno)";
414 elsif (my $directive = $result_obj->directive)
416 $string .= " # $directive";
417 my $explanation = $result_obj->explanation;
418 $string .= " $explanation"
419 unless is_null_string
$explanation;
422 report
$test_result, $string;
425 sub handle_tap_plan
($)
430 # Error, only one plan per stream is acceptable.
431 testsuite_error
"multiple test plans";
434 # The TAP plan can come before or after *all* the TAP results; we speak
435 # respectively of an "early" or a "late" plan. If we see the plan line
436 # after at least one TAP result has been seen, assume we have a late
437 # plan; in this case, any further test result seen after the plan will
438 # be flagged as an error.
439 $plan_seen = ($testno >= 1 ? LATE_PLAN
: EARLY_PLAN
);
440 # If $testno > 0, we have an error ("too many tests run") that will be
441 # automatically dealt with later, so don't worry about it here. If
442 # $plan_seen is true, we have an error due to a repeated plan, and that
443 # has already been dealt with above. Otherwise, we have a valid "plan
444 # with SKIP" specification, and should report it as a particular kind
446 if ($plan->directive && $testno == 0)
448 my $explanation = is_null_string
($plan->explanation) ?
449 undef : "- " . $plan->explanation;
450 report
"SKIP", $explanation;
454 sub handle_tap_bailout
($)
456 my ($bailout, $msg) = ($_[0], "Bail out!");
458 $msg .= " " . $bailout->explanation
459 unless is_null_string
$bailout->explanation;
460 testsuite_error
$msg;
463 sub extract_tap_comment
($)
466 if (index ($line, $diag_string) == 0)
468 # Strip leading `$diag_string' from `$line'.
469 $line = substr ($line, length ($diag_string));
470 # And strip any leading and trailing whitespace left.
471 $line =~ s/(?:^\s*|\s*$)//g;
472 # Return what is left (if any).
481 close LOG
or die "$ME: closing $log_file: $!\n";
490 while (defined (my $cur = $parser->next))
492 # Verbatim copy any input line into the log file.
493 print $cur->raw . "\n";
494 # Parsing of TAP input should stop after a "Bail out!" directive.
499 handle_tap_plan
($cur);
501 elsif ($cur->is_test)
503 handle_tap_result
($cur);
505 elsif ($cur->is_bailout)
507 handle_tap_bailout
($cur);
509 elsif ($cfg{comments
})
511 my $comment = extract_tap_comment
($cur->raw);
512 report
"#", "$comment" if length $comment;
515 # A "Bail out!" directive should cause us to ignore any following TAP
516 # error, as well as a non-zero exit status from the TAP producer.
521 testsuite_error
"missing test plan";
523 elsif ($parser->tests_planned != $parser->tests_run)
525 my ($planned, $run) = ($parser->tests_planned, $parser->tests_run);
526 my $bad_amount = $run > $planned ?
"many" : "few";
527 testsuite_error
(sprintf "too %s tests run (expected %d, got %d)",
528 $bad_amount, $planned, $run);
530 if (!$cfg{"ignore-exit"})
532 my $msg = get_test_exit_message
();
533 testsuite_error
$msg if $msg;
546 # perl-indent-level: 2
547 # perl-continued-statement-offset: 2
548 # perl-continued-brace-offset: 0
549 # perl-brace-offset: 0
550 # perl-brace-imaginary-offset: 0
551 # perl-label-offset: -2
552 # cperl-indent-level: 2
553 # cperl-brace-offset: 0
554 # cperl-continued-brace-offset: 0
555 # cperl-label-offset: -2
556 # cperl-extra-newline-before-brace: t
557 # cperl-merge-trailing-else: nil
558 # cperl-continued-statement-offset: 2
559 # eval: (add-hook 'write-file-hooks 'time-stamp)
560 # time-stamp-start: "my $VERSION = "
561 # time-stamp-format: "'%:y-%02m-%02d.%02H'"
562 # time-stamp-time-zone: "UTC"
563 # time-stamp-end: "; # UTC"