4 # Copyright (C)2007-2012 Versabanq Innovations Inc. and contributors.
5 # Licensed under the GNU Library General Public License, version 2.
6 # See the included file named LICENSE for license information.
7 # You can get wvtest from: http://github.com/apenwarr/wvtest
10 use Time
::HiRes
qw(time);
16 print STDERR
"Usage: $0 <command line...>\n";
20 print STDERR
"Testing \"all\" in @ARGV:\n";
22 my $pid = open(my $fh, "-|");
26 open STDERR
, '>&STDOUT' or die("Can't dup stdout: $!\n");
28 exit 126; # just in case
31 my $istty = -t STDOUT
&& $ENV{'TERM'} ne "dumb";
33 my ($gpasses, $gfails, $gxpasses, $gxfails, $gskips) = (0,0,0,0,0);
40 print "\n" . join("\n", @log) . "\n";
43 print STDERR
"\n! Killed by signal FAILED\n";
45 ($pid > 0) || die("pid is '$pid'?!\n");
47 local $SIG{CHLD
} = sub { }; # this will wake us from sleep() faster
60 local $SIG{INT
} = sub { bigkill
($pid); };
61 local $SIG{TERM
} = sub { bigkill
($pid); };
62 local $SIG{ALRM
} = sub {
63 print STDERR
"Alarm timed out! No test results for too long.\n";
69 my ($result, $text) = @_;
74 if ($result eq "ok") { $colour = "\e[32m"; } # green
75 elsif ($result eq "xpass") { $colour = "\e[34;1m"; } # *blue*
76 elsif ($result eq "xfail") { $colour = "\e[33m"; } # yellow
77 elsif ($result eq "skip") { $colour = "\e[36m"; } # cyan
78 else { $colour = "\e[31;1m"; } # *red*
80 return "$colour$text\e[0m";
89 return colourize_as
($result, $result);
95 my ($floatsec, $warntime, $badtime) = @_;
96 my $ms = int($floatsec * 1000);
97 my $str = sprintf("%d.%03ds", $ms/1000, $ms % 1000);
99 if ($istty && $ms > $badtime) {
100 return "\e[31;1m$str\e[0m";
101 } elsif ($istty && $ms > $warntime) {
102 return "\e[33;1m$str\e[0m";
110 my ($name, $result) = @_;
111 return sprintf("! %-65s %s", $name, colourize
($result));
114 my $allstart = time();
121 printf " %s %s\n", mstime
($stop - $start, 500, 1000), colourize
("ok");
130 if (/^\s*Testing "(.*)" in (.*):\s*$/)
133 my ($sect, $file) = ($1, $2);
137 printf("! %s %s: ", $file, $sect);
141 elsif (/^!\s*(.*?\s(?:(\S+)\s)?)(\S+)\s*$/)
145 my ($name, $result2, $result) = ($1, $2, $3);
149 printf("\n! Startup: ");
153 push @log, resultline
($name, $result);
155 if ($result eq "ok") {
156 if ($result2 eq "xpass") {
158 print colourize_as
("xpass", "X");
159 } elsif ($result2 eq "xfail") {
161 print colourize_as
("xfail", "f");
162 } elsif ($result2 eq "skip") {
164 print colourize_as
("skip", "s");
172 print "\n" . join("\n", @log) . "\n";
185 my $newpid = waitpid($pid, 0);
186 if ($newpid != $pid) {
187 die("waitpid returned '$newpid', expected '$pid'\n");
191 my $ret = ($code >> 8);
193 # return death-from-signal exits as >128. This is what bash does if you ran
194 # the program directly.
195 if ($code && !$ret) { $ret = $code | 128; }
198 print "\n" . join("\n", @log) . "\n";
202 print resultline
("Program returned non-zero exit code ($ret)", "FAILED");
205 my $gtotal = $gpasses+$gfails+$gxpasses+$gxfails+$gskips;
206 printf("\nWvTest: %d test%s, %s failure%s, total time %s.\n",
207 $gtotal, $gtotal==1 ?
"" : "s",
208 $gfails>0 ? colourize_as
("fail", "$gfails") : "$gfails",
209 $gfails==1 ?
"" : "s",
210 mstime
(time() - $allstart, 2000, 5000));
211 printf("WvTest: %s test%s skipped, %s known breakage%s, %s fixed breakage%s.\n",
212 $gskips>0 ? colourize_as
("skip", "$gskips") : "$gskips",
213 $gskips==1 ?
"" : "s",
214 $gxfails>0 ? colourize_as
("xfail", "$gxfails"): "$gxfails",
215 $gxfails==1 ?
"" : "s",
216 $gxpasses>0 ? colourize_as
("xpass", "$gxpasses") : "$gxpasses",
217 $gxpasses==1 ?
"" : "s");
218 print STDERR
"\nWvTest result code: $ret\n";
219 exit( $ret ?
$ret : ($gfails ?
125 : 0) );