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