This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Nits in perldelta template
[perl5.git] / t / harness
1 #!./perl
2
3 # We suppose that perl _mostly_ works at this moment, so may use
4 # sophisticated testing.
5
6 BEGIN {
7     chdir 't' if -d 't';
8     @INC = '../lib';              # pick up only this build's lib
9 }
10
11 my $torture; # torture testing?
12
13 use TAP::Harness 3.13;
14 use strict;
15
16 $::do_nothing = $::do_nothing = 1;
17 require './TEST';
18
19 my $Verbose = 0;
20 $Verbose++ while @ARGV && $ARGV[0] eq '-v' && shift;
21
22 if ($ARGV[0] && $ARGV[0] eq '-torture') {
23     shift;
24     $torture = 1;
25 }
26
27 # Let tests know they're running in the perl core.  Useful for modules
28 # which live dual lives on CPAN.
29 $ENV{PERL_CORE} = 1;
30
31 #fudge DATA for now.
32 my %datahandle = qw(
33                 lib/bigint.t            1
34                 lib/bigintpm.t          1
35                 lib/bigfloat.t          1
36                 lib/bigfloatpm.t        1
37                 op/gv.t                 1
38                 lib/complex.t           1
39                 lib/ph.t                1
40                 lib/soundex.t           1
41                 op/misc.t               1
42                 op/runlevel.t           1
43                 op/tie.t                1
44                 op/lex_assign.t         1
45                 );
46
47 foreach (keys %datahandle) {
48      unlink "$_.t";
49 }
50
51 my (@tests, $re);
52
53 # [.VMS]TEST.COM calls harness with empty arguments, so clean-up @ARGV
54 @ARGV = grep $_ && length( $_ ) => @ARGV;
55
56 sub _extract_tests;
57 sub _extract_tests {
58     # This can probably be done more tersely with a map, but I doubt that it
59     # would be as clear
60     my @results;
61     foreach (@_) {
62         my $ref = ref $_;
63         if ($ref) {
64             if ($ref eq 'ARRAY') {
65                 push @results, _extract_tests @$_;
66             } elsif ($ref eq 'HASH') {
67                 push @results, _extract_tests values %$_;
68             } else {
69                 die "Unknown reference type $ref";
70             }
71         } else {
72             push @results, glob $_;
73         }
74     }
75     @results;
76 }
77
78 if ($ARGV[0] && $ARGV[0]=~/^-re/) {
79     if ($ARGV[0]!~/=/) {
80         shift;
81         $re=join "|",@ARGV;
82         @ARGV=();
83     } else {
84         (undef,$re)=split/=/,shift;
85     }
86 }
87
88 my $jobs = $ENV{TEST_JOBS};
89 my ($rules, $state, $color);
90 if ($ENV{HARNESS_OPTIONS}) {
91     for my $opt ( split /:/, $ENV{HARNESS_OPTIONS} ) {
92         if ( $opt =~ /^j(\d*)$/ ) {
93             $jobs ||= $1 || 9;
94         }
95         elsif ( $opt eq 'c' ) {
96             $color = 1;
97         }
98         else {
99             die "Unknown HARNESS_OPTIONS item: $opt\n";
100         }
101     }
102 }
103
104 if (@ARGV) {
105     # If you want these run in speed order, just use prove
106     if ($^O eq 'MSWin32') {
107         @tests = map(glob($_),@ARGV);
108     }
109     else {
110         @tests = @ARGV;
111     }
112 } else {
113     # Ideally we'd get somewhere close to Tux's Oslo rules
114     # my $rules = {
115     #     par => [
116     #         { seq => '../ext/DB_File/t/*' },
117     #         { seq => '../ext/IO_Compress_Zlib/t/*' },
118     #         { seq => '../lib/CPANPLUS/*' },
119     #         { seq => '../lib/ExtUtils/t/*' },
120     #         '*'
121     #     ]
122     # };
123
124     # but for now, run all directories in sequence. In particular, it would be
125     # nice to get the tests in t/op/*.t able to run in parallel.
126
127     unless (@tests) {
128         my @seq = <base/*.t>;
129
130         my @next = qw(comp run cmd io re op uni mro lib porting);
131         push @next, 'japh' if $torture;
132         push @next, 'win32' if $^O eq 'MSWin32';
133         push @next, 'benchmark' if $ENV{PERL_BENCHMARK};
134         # Hopefully TAP::Parser::Scheduler will support this syntax soon.
135         # my $next = { par => '{' . join (',', @next) . '}/*.t' };
136         my $next = { par => [
137                              map { "$_/*.t" } @next
138                             ] };
139         @tests = _extract_tests ($next);
140
141         # This is a bit of a game, because we only want to sort these tests in
142         # speed order. base/*.t wants to run first, and ext,lib etc last and in
143         # MANIFEST order
144         if ($jobs) {
145             require App::Prove::State;
146             $state = App::Prove::State->new({ store => 'test_state' });
147             $state->apply_switch('slow', 'save');
148             # For some reason get_tests returns *all* the tests previously run,
149             # (in the right order), not simply the selection in @tests
150             # (in the right order). Not sure if this is a bug or a feature.
151             # Whatever, *we* are only interested in the ones that are in @tests
152             my %seen;
153             @seen{@tests} = ();
154             @tests = grep {exists $seen{$_} } $state->get_tests(0, @tests);
155         }
156         @tests = (@seq, @tests);
157         push @seq, $next;
158
159         my @last;
160         use Config;
161         push @last,  sort { lc $a cmp lc $b }
162             _tests_from_manifest($Config{extensions}, $Config{known_extensions});
163         push @last, <x2p/*.t>;
164
165         my %times;
166         if ($state) {
167             # Where known, collate the elapsed times by test name
168             foreach ($state->results->tests()) {
169                 $times{$_->name} = $_->elapsed();
170             }
171         }
172
173         my %dir;
174         my %total_time;
175
176         for (@last) {
177             if ($^O eq 'MSWin32') {
178                 s,\\,/,g; # canonicalize path
179             };
180             m!(.*[/])! or die "'$_'";
181             push @{$dir{$1}}, $_;
182             $total_time{$1} += $times{$_} || 0;
183         }
184
185         push @tests, @last;
186
187         # Generate T::H schedule rules that run the contents of each directory
188         # sequentially.
189         push @seq, { par => [ map { { seq => "$_*" } } sort {
190             # Directories, ordered by total time descending then name ascending
191             $total_time{$b} <=> $total_time{$a} || $a cmp $b
192         } keys %dir ] };
193
194         $rules = { seq => \@seq };
195     }
196 }
197 if ($^O eq 'MSWin32') {
198     s,\\,/,g for @tests;
199 }
200 @tests=grep /$re/, @tests 
201     if $re;
202
203 my %options;
204
205 my $type = 'perl';
206
207 # Load TAP::Parser now as otherwise it could be required in the short time span
208 # in which the harness process chdirs into ext/Dist
209 require TAP::Parser;
210
211 my $h = TAP::Harness->new({
212     rules       => $rules,
213     color       => $color,
214     jobs        => $jobs,
215     verbosity   => $Verbose,
216     exec        => sub {
217         my ($harness, $test) = @_;
218
219         my $options = $options{$test};
220         if (!defined $options) {
221             $options = $options{$test} = _scan_test($test, $type);
222         }
223
224         return [ split ' ', _cmd($options, $type) ];
225     },
226 });
227
228 if ($state) {
229     $h->callback(
230                  after_test => sub {
231                      $state->observe_test(@_);
232                  }
233                  );
234     $h->callback(
235                  after_runtests => sub {
236                      $state->commit(@_);
237                  }
238                  );
239 }
240
241 $h->callback(
242              parser_args => sub {
243                  my ($args, $job) = @_;
244                  my $test = $job->[0];
245                  _before_fork($options{$test});
246                  push @{ $args->{switches} }, "-I../../lib";
247              }
248              );
249
250 $h->callback(
251              made_parser => sub {
252                  my ($parser, $job) = @_;
253                  my $test = $job->[0];
254                  my $options = delete $options{$test};
255                  _after_fork($options);
256              }
257              );
258
259 my $agg = $h->runtests(@tests);
260 exit $agg->has_errors ? 1 : 0;