-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_tests.pl
179 lines (142 loc) · 3.14 KB
/
run_tests.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use IPC::Run3 'run3';
use Time::HiRes qw( gettimeofday tv_interval );
my %report_test =
(
# EXIT_OK
0 => \&report_success,
# EXIT_FAILURE
10 => \&report_failure,
# EXIT_NOT_APPLICABLE
11 => \&report_not_applicable,
);
exit main(@ARGV);
sub main
{
my @tests = find_tests();
return run_tests( @tests );
}
sub find_tests
{
return grep { -x } <spec/*_spec.sh>;
}
sub run_tests
{
my $seen_failure = 0;
for my $test (@_)
{
my ($xml, $status) = run_one_test( $test );
write_xml( $test, $xml );
$seen_failure++ if $status;
}
return $seen_failure;
}
sub run_one_test
{
my $test = shift;
my $cur_time = [ gettimeofday ];
run3( "./$test", undef, \my $out, \my $err );
my $time = tv_interval( $cur_time );
my $status = $? >> 8;
return $report_test{ $status }->( $test, $out, $err, $time )
if exists $report_test{ $status };
return report_unknown_status( $test, $out, $err, $time );
}
sub report_success
{
return report_ok( @_ );
}
sub report_ok
{
my ($test, $out, $err, $time, %args) = @_;
my $xml = create_xml_header(
name => $test,
time => $time,
%args
);
my $name = filename_to_test_name( $test );
$xml .=<<"TESTCASE";
<testcase name="$name" time="$time">
</testcase>
TESTCASE
$xml .= create_xml_footer( stdout => $out, stderr => $err );
return $xml;
}
sub report_failure
{
my ($test, $out, $err, $time) = @_;
my $xml = create_xml_header(
failures => 1,
name => $test,
time => $time
);
my $name = filename_to_test_name( $test );
my ($first_line) = $out =~ /^(.+)$/m;
$xml .=<<"TESTCASE";
<testcase name="$name" time="$time">
<failure type="acceptanceFailure" message="$first_line">
$out
</failure>
</testcase>
TESTCASE
$xml .= create_xml_footer( stdout => $out, stderr => $err );
# this one reports a failure
return( $xml, 1 );
}
sub report_not_applicable
{
return report_ok( @_, skipped => 1 );
}
sub report_unknown_status
{
# this one also reports a failure
return( report_ok( @_, errors => 1 ), 1 );
}
sub create_xml_header
{
my %args =
(
errors => 0,
failures => 0,
name => 'unknown',
tests => 1,
skipped => 0,
time => '0.00',
@_,
);
return <<"XML";
<?xml version="1.0" encoding="UTF-8"?>
<testsuite errors="$args{errors}" failures="$args{failures}" name="$args{name}" tests="$args{tests}" skipped="$args{skipped}" time="$args{time}">
XML
}
sub create_xml_footer
{
my %args = @_;
return <<"XML";
<system-out>
$args{stdout}
</system-out>
<system-err>
$args{stderr}
</system-err>
</testsuite>
XML
}
sub write_xml
{
my ($test, $xml) = @_;
$test =~ s[spec/][results/];
$test =~ s/.sh$/.xml/;
open my $out, '>', $test or die "Cannot write '$test': $!\n";
print {$out} $xml;
close $out;
}
sub filename_to_test_name
{
my $filename = shift;
$filename =~ s/^spec.(\w+)_spec.sh/$1/;
$filename =~ tr/_/ /;
return $filename;
}