Merge branch 'master' of ssh://apples.lambdacomplex.org/git/bus
[bus.git] / origin-src / wvtestrun
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/perl -w
#
# WvTest:
#   Copyright (C)2007-2009 Versabanq Innovations Inc. and contributors.
#       Licensed under the GNU Library General Public License, version 2.
#       See the included file named LICENSE for license information.
#
use strict;
use Time::HiRes qw(time);

# always flush
$| = 1;

if (@ARGV < 1) {
    print STDERR "Usage: $0 <command line...>\n";
    exit 127;
}

print STDERR "Testing \"all\" in @ARGV:\n";

my $pid = open(my $fh, "-|");
if (!$pid) {
    # child
    setpgrp();
    open STDERR, '>&STDOUT' or die("Can't dup stdout: $!\n");
    exec(@ARGV);
    exit 126; # just in case
}

my $istty = -t STDOUT;
my @log = ();
my ($gpasses, $gfails) = (0,0);

sub bigkill($)
{
    my $pid = shift;
    
    if (@log) {
	print "\n" . join("\n", @log) . "\n";
    }
    
    print STDERR "\n! Killed by signal    FAILED\n";

    ($pid > 0) || die("pid is '$pid'?!\n");

    local $SIG{CHLD} = sub { }; # this will wake us from sleep() faster
    kill 15, $pid;
    sleep(2);
    
    if ($pid > 1) {
	kill 9, -$pid;
    }
    kill 9, $pid;
    
    exit(125);
}

# parent
local $SIG{INT} = sub { bigkill($pid); };
local $SIG{TERM} = sub { bigkill($pid); };
local $SIG{ALRM} = sub { 
    print STDERR "Alarm timed out!  No test results for too long.\n";
    bigkill($pid);
};

sub colourize($)
{
    my $result = shift;
    my $pass = ($result eq "ok");
    
    if ($istty) {
	my $colour = $pass ? "\e[32;1m" : "\e[31;1m";
	return "$colour$result\e[0m";
    } else {
	return $result;
    }
}

sub mstime($$$)
{
    my ($floatsec, $warntime, $badtime) = @_;
    my $ms = int($floatsec * 1000);
    my $str = sprintf("%d.%03ds", $ms/1000, $ms % 1000);
    
    if ($istty && $ms > $badtime) {
        return "\e[31;1m$str\e[0m";
    } elsif ($istty && $ms > $warntime) {
        return "\e[33;1m$str\e[0m";
    } else {
        return "$str";
    }
}

sub resultline($$)
{
    my ($name, $result) = @_;
    return sprintf("! %-65s %s", $name, colourize($result));
}

my $allstart = time();
my ($start, $stop);

sub endsect()
{
    $stop = time();
    if ($start) {
	printf " %s %s\n", mstime($stop - $start, 500, 1000), colourize("ok");
    }
}

while (<$fh>)
{
    chomp;
    s/\r//g;
    
    if (/^\s*Testing "(.*)" in (.*):\s*$/)
    {
        alarm(120);
	my ($sect, $file) = ($1, $2);
	
	endsect();
	
	printf("! %s  %s: ", $file, $sect);
	@log = ();
	$start = $stop;
    }
    elsif (/^!\s*(.*?)\s+(\S+)\s*$/)
    {
        alarm(120);
    
	my ($name, $result) = ($1, $2);
	my $pass = ($result eq "ok");
	
	if (!$start) {
	    printf("\n! Startup: ");
	    $start = time();
	}
	
	push @log, resultline($name, $result);
	
	if (!$pass) {
	    $gfails++;
	    if (@log) {
		print "\n" . join("\n", @log) . "\n";
		@log = ();
	    }
	} else {
	    $gpasses++;
	    print ".";
	}
    }
    else
    {
	push @log, $_;
    }
}

endsect();

my $newpid = waitpid($pid, 0);
if ($newpid != $pid) {
    die("waitpid returned '$newpid', expected '$pid'\n");
}

my $code = $?;
my $ret = ($code >> 8);

# return death-from-signal exits as >128.  This is what bash does if you ran
# the program directly.
if ($code && !$ret) { $ret = $code | 128; }

if ($ret && @log) {
    print "\n" . join("\n", @log) . "\n";
}

if ($code != 0) {
    print resultline("Program returned non-zero exit code ($ret)", "FAILED");
}

my $gtotal = $gpasses+$gfails;
printf("\nWvTest: %d test%s, %d failure%s, total time %s.\n",
    $gtotal, $gtotal==1 ? "" : "s",
    $gfails, $gfails==1 ? "" : "s",
    mstime(time() - $allstart, 2000, 5000));
print STDERR "\nWvTest result code: $ret\n";
exit( $ret ? $ret : ($gfails ? 125 : 0) );