/
TestRunner.pl
executable file
·198 lines (160 loc) · 5.65 KB
/
TestRunner.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env perl
# See bottom of file for description
use strict;
use warnings;
require 5.006;
use FindBin;
use Cwd ();
my $starting_root;
sub _findRelativeTo {
my ( $startdir, $name ) = @_;
my @path = split( /\/+/, $startdir );
while ( scalar(@path) > 0 ) {
my $found = join( '/', @path ) . '/' . $name;
return $found if -e $found;
pop(@path);
}
return undef;
}
BEGIN {
if ( not defined $ENV{FOSWIKI_ASSERTS} or $ENV{FOSWIKI_ASSERTS} eq 'soft' )
{
print
"exporting FOSWIKI_ASSERTS=1 for extra checking; disable by exporting FOSWIKI_ASSERTS=0\n";
$ENV{FOSWIKI_ASSERTS} = 1;
}
$Foswiki::cfg{Engine} = 'Foswiki::Engine::CGI';
# root the tree
my $here = Cwd::abs_path;
# Look for the installation that we are testing in context
# with. This will be defined by either by finding an installation
# on the path to the current dir, or by finding the first install
# in FOSWIKI_LIBS.
my $root = _findRelativeTo( $here, 'core/bin/setlib.cfg' )
|| _findRelativeTo( $here, 'bin/setlib.cfg' );
die "Cannot locate bin/setlib.cfg" unless $root;
$root =~ s{/bin/setlib.cfg$}{};
($root) = $root =~ /^(.*)$/; # untaint
unshift @INC, "$root/test/unit";
unshift @INC, "$root/bin";
unshift @INC, "$root/lib";
unshift @INC, "$root/lib/CPAN/lib";
require 'setlib.cfg';
$starting_root = $root;
}
# Item11466: PH commented the below & added require statements where necessary,
# to avoid compiling Foswiki.pm before FOSWIKI_ASSERTS have been set
#use Foswiki; # If you take this out then TestRunner.pl will fail on IndigoPerl
use Unit::TestRunner;
my %options;
while ( scalar(@ARGV) && $ARGV[0] =~ /^-/ ) {
$options{ shift(@ARGV) } = 1;
}
my ( $stdout, $stderr, $log ); # will be destroyed at the end, if created
if ( $options{-log} and not $options{-worker} ) {
require Unit::Eavesdrop;
my @gmt = gmtime( time() );
$gmt[4]++;
$gmt[5] += 1900;
$log = sprintf( "%0.4d", $gmt[5] );
for ( my $i = 4 ; $i >= 0 ; $i-- ) {
$log .= sprintf( "%0.2d", $gmt[$i] );
}
$log .= '.log';
open( F, ">$log" ) || die $!;
print STDERR "Logging to $log\n";
$stdout = new Unit::Eavesdrop('STDOUT');
$stdout->teeTo( \*F );
# Don't need this, all the required info goes to STDOUT. STDERR is
# really just treated as a black hole (except when debugging)
# $stderr = new Unit::Eavesdrop('STDERR');
# $stderr->teeTo(\*F);
}
print STDERR "Options: ", join( ' ', keys %options ), "\n";
if ( $ENV{FOSWIKI_ASSERTS} ) {
print "Assert checking on $ENV{FOSWIKI_ASSERTS}\n";
}
else {
print "Assert checking off $ENV{FOSWIKI_ASSERTS}\n";
}
if ( $options{-clean} ) {
require File::Path;
require Foswiki;
my $rmDir = $Foswiki::cfg{DataDir};
opendir( my $dataDir, $rmDir ) or die "Can't open directory $rmDir: $!";
my @x = grep { s/^(Temp.*)/$rmDir\/$1/ } readdir($dataDir);
foreach my $x (@x) {
($x) = $x =~ /^(.*)$/;
File::Path::rmtree($x) if $x;
}
closedir $dataDir;
$rmDir = $Foswiki::cfg{PubDir};
opendir( my $pubDir, "$rmDir" ) or die "Can't open directory $rmDir: $!";
@x = grep { s/^(Temp.*)/$rmDir\/$1/ } readdir($pubDir);
foreach my $x (@x) {
($x) = $x =~ /^(.*)$/;
File::Path::rmtree($x) if $x;
}
closedir $pubDir;
my $logDir = Cwd::getcwd() . '/testlogs';
if ( -d $logDir ) {
File::Path::rmtree($1) if $logDir =~ /^(.*)$/;
}
}
if ( not $options{-worker} ) {
require Foswiki;
if ( defined $Foswiki::cfg{DataDir} && $Foswiki::cfg{DataDir} ne 'NOT SET' )
{
testForFiles( $Foswiki::cfg{DataDir}, '/Temp*' );
}
if ( defined $Foswiki::cfg{PubDir} && $Foswiki::cfg{PubDir} ne 'NOT SET' ) {
testForFiles( $Foswiki::cfg{PubDir}, '/Temp*' );
}
}
my $testrunner = Unit::TestRunner->new( { TAP => defined( $options{-tap} ) } );
my $exit;
if ( $options{-worker} ) {
$exit = $testrunner->worker(@ARGV);
}
else {
$exit = $testrunner->start(@ARGV);
}
print STDERR "Run was logged to $log\n" if $options{-log};
Cwd::chdir($starting_root) if ($starting_root);
exit $exit;
sub testForFiles {
my $testDir = shift;
my $pattrn = shift;
opendir( DIR, $testDir ) || die "Could not open $testDir";
my @list = grep { s/^($pattrn)/$testDir\/$1\n/ } readdir(DIR);
die "Please remove @list (or run with the -clean option) to run tests\n"
if ( scalar(@list) );
}
#big nasty global function hacked in to give me insta-TAP output
#TODO: move into the testrunner - I've not looked to see if the TestCase knows who is running it
sub TAP {
my ( $bool, $mess ) = @_;
$testrunner->{number_of_asserts}++;
return unless ( defined( $options{-tap} ) );
print( $bool? "ok\n" : "not ok\n" );
}
sub PRINT_TAP_TOTAL {
#return unless (defined($options{-tap}));
print "1..$testrunner->{number_of_asserts}\n";
}
1;
__DATA__
This script runs the test suites/cases defined on the command-line.
Author: Crawford Currie, http://c-dot.co.uk
Copyright (C) 2007-2020 Foswiki Contributors
Copyright (C) 2007 WikiRing, http://wikiring.com
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.