/
TestRunner.pm
412 lines (363 loc) · 12.5 KB
/
TestRunner.pm
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# See bottom of file for license and copyright
package Unit::TestRunner;
=begin TML
---+ package Unit::TestRunner
Test run controller. Provides most of the functionality for the TestRunner.pl
script that runs testcases.
=cut
use strict;
use warnings;
use Devel::Symdump;
use Error qw(:try);
use File::Spec;
#use Devel::Leak::Object qw{ GLOBAL_bless };
#$Devel::Leak::Object::TRACKSOURCELINES = 1;
sub new {
my $class = shift;
return bless(
{
unexpected_passes => [],
expected_failures => [],
failures => [],
number_of_asserts => 0,
},
$class
);
}
sub start {
my $this = shift;
my @files = @_;
@{ $this->{failures} } = ();
@{ $this->{initialINC} } = @INC;
my $passes = 0;
my ($start_cwd) = Cwd->cwd() =~ m/^(.*)$/;
print "Starting CWD is $start_cwd \n";
# First use all the tests to get them compiled
while ( scalar(@files) ) {
my $testSuiteModule = shift @files;
$testSuiteModule =~
s/\/$//; # Trim final slash, for completion lovers like Sven
my $testToRun;
if ( $testSuiteModule =~ s/::(\w+)$// ) {
$testToRun = $1;
}
my $suite = $testSuiteModule;
if ( $testSuiteModule =~ /^(.*?)(\w+)\.pm$/ ) {
$suite = $2;
push( @INC, $1 ) if $1 && -d $1;
}
($suite) = $suite =~ /^(.*)$/;
eval "use $suite";
if ($@) {
my $useError = $@;
# Try to be clever, look for it
if ( $useError =~ /Can't locate \Q$suite\E\.pm in \@INC/ ) {
my $testToFind = $testToRun ? "::$testToRun" : '';
print "Looking for $suite$testToFind...\n";
require File::Find;
my @found;
File::Find::find(
{
wanted => sub {
/^$suite/
&& $File::Find::name =~ /^\.\/(.*\.pm)$/
&& ( print("\tFound $1\n") )
&& push( @found, $1 . $testToFind );
},
follow => 1,
untaint => 1,
untaint_pattern => qr|^([-+@\w./:]+)$|,
},
'.'
);
# Try to be even smarter: favor test suites
# unless a specific test was requested
my @suite = grep { /Suite\.pm/ } @found;
if ( $#found and @suite ) {
if ($testToFind) {
@found = grep { !/Suite.pm/ } @found;
print "$testToRun is most likely not in @suite"
. ", removing it\n";
unshift @files, @found;
}
else {
print "Found "
. scalar(@found)
. " tests,"
. " favoring @suite\n";
unshift @files, @suite;
}
}
else {
unshift @files, @found;
}
next if @found;
}
my $m = "*** Failed to use $suite: $useError";
print $m;
push( @{ $this->{failures} }, $m );
next;
}
print "Running $suite\n";
my $tester = $suite->new($suite);
if ( $tester->isa('Unit::TestSuite') ) {
# Get a list of included tests
my @set = $tester->include_tests();
unshift( @files, @set );
}
else {
my $completed;
my $action;
if ( $tester->run_in_new_process() ) {
$action =
$this->runOneInNewProcess( $testSuiteModule, $suite,
$testToRun );
}
else {
$action = runOne( $tester, $suite, $testToRun );
}
if ( Cwd->cwd() ne $start_cwd) {
print "CWD changed to " . Cwd->cwd()
. " by previous test!! \n";
chdir $start_cwd or die "Cannot change back to previous $start_cwd\n";
}
# untaint action for the case where the test is run in
# another process
$action =~ m/^(.*)$/ms;
eval $1;
die $@ if $@;
die "Test suite $suite aborted\n" unless $completed;
}
}
#marker so we can remove the above large output from the nightly emails
print "\nUnit test run Summary:\n";
my $total = $passes;
my $failed;
if ( $failed = scalar @{ $this->{unexpected_passes} } ) {
print "$failed unexpected pass" . ( $failed > 1 ? 'es' : '' ) . ":\n";
print join( "\n", @{ $this->{unexpected_passes} } );
$total += $failed;
}
if ( $failed = scalar @{ $this->{expected_failures} } ) {
print "$failed expected failure" . ( $failed > 1 ? 's' : '' ) . ":\n";
print join( "\n", @{ $this->{expected_failures} } );
$total += $failed;
}
if ( $failed = scalar @{ $this->{failures} } ) {
print "\n$failed failure" . ( $failed > 1 ? 's' : '' ) . ":\n";
print join( "\n---------------------------\n", @{ $this->{failures} } ),
"\n";
$total += $failed;
print "$passes of $total test cases passed\n";
::PRINT_TAP_TOTAL();
return $failed;
}
print "All tests passed ($passes"
. ( $passes == $total ? '' : "/$total" ) . ")\n";
::PRINT_TAP_TOTAL();
return 0;
}
sub runOneInNewProcess {
my $this = shift;
my $testSuiteModule = shift;
my $suite = shift;
my $testToRun = shift;
$testToRun ||= 'undef';
my $tempfilename = 'worker_output.' . $$ . '.' . $suite;
# Assume all new paths were either unshifted or pushed onto @INC
my @pushedOntoINC = @INC;
my @unshiftedOntoINC = ();
while ( $this->{initialINC}->[0] ne $pushedOntoINC[0] ) {
push @unshiftedOntoINC, shift @pushedOntoINC;
}
for my $oneINC ( @{ $this->{initialINC} } ) {
shift @pushedOntoINC if $pushedOntoINC[0] eq $oneINC;
}
my @paths;
push( @paths, "-I", $_ ) for ( @unshiftedOntoINC, @pushedOntoINC );
my @command = map
{
my $value = $_;
if (defined $value) {
$value =~ /(.*)/;
$value = $1; # untaint
}
$value;
}
($^X, "-wT", @paths, File::Spec->rel2abs($0), "-worker", $suite, ,$testToRun, $tempfilename);
my $command = join(' ', @command);
print "Running: $command\n";
$ENV{PATH} =~ /(.*)/;
$ENV{PATH} = $1; # untaint
system(@command);
if ( $? == -1 ) {
my $error = $!;
unlink $tempfilename;
print "*** Could not spawn new process for $suite: $error\n";
return
'push( @{ $this->{failures} }, "'
. $suite . '\n'
. quotemeta($error) . '" );';
}
else {
my $returnCode = $? >> 8;
if ($returnCode) {
print "*** Error trying to run $suite\n";
unlink $tempfilename;
return
'push( @{ $this->{failures} }, "Process for '
. $suite
. ' returned '
. $returnCode . '" );';
}
else {
open my $testoutputfile, "<", $tempfilename
or die
"Cannot open '$tempfilename' to read output from $suite: $!";
my $action = '';
while (<$testoutputfile>) {
$action .= $_;
}
close $testoutputfile or die "Error closing '$tempfilename': $!";
unlink $tempfilename;
return $action;
}
}
}
sub worker {
my $numArgs = scalar(@_);
my ( $this, $testSuiteModule, $testToRun, $tempfilename ) = @_;
if ($numArgs != 4 or
not defined $this or
not defined $testSuiteModule or
not defined $testToRun or
not defined $tempfilename ) {
my $pkg = __PACKAGE__;
die <<"DIE";
Wrong number of arguments to $pkg->worker(). Got $numArgs, expected 4.
Are you trying to use -worker from the command-line?
-worker is only intended for use by $pkg->runOneInNewProcess().
To run your test in a separate process, override run_in_new_process() in your test class so that it returns true.
DIE
}
if ( $testToRun eq 'undef' ) {
$testToRun = undef;
}
else {
$testToRun =~ /(.*)/; # untaint
$testToRun = $1;
}
$testSuiteModule =~ /(.*)/; # untaint
$testSuiteModule = $1;
$tempfilename =~ /(.*)/; # untaint
$tempfilename = $1;
my $suite = $testSuiteModule;
eval "use $suite";
die $@ if $@;
my $tester = $suite->new($suite);
my $log = "stdout.$$.log";
require Unit::Eavesdrop;
open( my $logfh, ">", $log ) || die $!;
print STDERR "Logging to $log\n";
my $stdout = new Unit::Eavesdrop('STDOUT');
$stdout->teeTo($logfh);
# Don't need this, all the required info goes to STDOUT. STDERR is
# really just treated as a black hole (except when debugging)
# my $stderr = new Unit::Eavesdrop('STDERR');
# $stderr->teeTo($logfh);
my $action = runOne( $tester, $suite, $testToRun );
{
local $SIG{__WARN__} = sub { die $_[0]; };
eval { close $logfh; };
if ($@) {
if ( $@ =~ /Bad file descriptor/ and $suite eq 'EngineTests' ) {
# This is expected - ignore it
}
else {
# propagate the error
die $@;
}
}
}
undef $logfh;
$stdout->finish();
undef $stdout;
# $stderr->finish();
# undef $stderr;
open( $logfh, "<", $log ) or die $!;
local $/; # slurp in whole file
my $logged_stdout = <$logfh>;
close $logfh or die $!;
unlink $log or die "Could not unlink $log: $!";
#escape characters so that it may be printed
$logged_stdout =~ s{\\}{\\\\}g;
$logged_stdout =~ s{'}{\\'}g;
$action .= "print '" . $logged_stdout . "';";
open my $outputfile, ">", $tempfilename
or die "Cannot open output file '$tempfilename': $!";
print $outputfile $action . "\n";
close $outputfile or die "Error closing output file '$tempfilename': $!";
exit(0);
}
sub runOne {
my $tester = shift;
my $suite = shift;
my $testToRun = shift;
my $action = '$completed = 1;';
# Get a list of the test methods in the class
my @tests = $tester->list_tests($suite);
if ($testToRun) {
@tests = grep { /^${suite}::$testToRun$/ } @tests;
if ( !@tests ) {
print "*** No test called $testToRun in $suite\n";
return $action;
}
}
unless ( scalar(@tests) ) {
print "*** No tests in $suite\n";
return $action;
}
foreach my $test (@tests) {
#Devel::Leak::Object::checkpoint();
print "\t$test\n";
$action .= "\n# $test\n ";
$tester->set_up();
try {
$tester->$test();
$action .= '$passes++;';
if ( $tester->{expect_failure} ) {
print "*** Unexpected pass\n";
$action .= 'push( @{ $this->{unexpected_passes} }, "'
. quotemeta($test) . '");';
}
}
catch Error with {
my $e = shift;
print "*** ", $e->stringify(), "\n";
if ( $tester->{expect_failure} ) {
$action .= 'push( @{ $this->{expected_failures} }, "';
}
else {
$action .= 'push( @{ $this->{failures} }, "';
}
$action .=
quotemeta($test) . '\\n' . quotemeta( $e->stringify() ) . '" );';
};
$tester->tear_down();
}
return $action;
}
1;
__DATA__
Author: Crawford Currie, http://c-dot.co.uk
Copyright (C) 2007-2010 Foswiki Contributors
All Rights Reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.