Everything in t/ can now run in parallel.
[p5sagit/p5-mst-13.2.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     $ENV{PERL5LIB} = '../lib';    # so children will see it too
10 }
11
12 my $torture; # torture testing?
13
14 use Test::Harness;
15 use strict;
16
17 $Test::Harness::switches = "";    # Too much noise otherwise
18 $Test::Harness::Verbose++ while @ARGV && $ARGV[0] eq '-v' && shift;
19
20 if ($ARGV[0] && $ARGV[0] eq '-torture') {
21     shift;
22     $torture = 1;
23 }
24
25 # Let tests know they're running in the perl core.  Useful for modules
26 # which live dual lives on CPAN.
27 $ENV{PERL_CORE} = 1;
28
29 #fudge DATA for now.
30 my %datahandle = qw(
31                 lib/bigint.t            1
32                 lib/bigintpm.t          1
33                 lib/bigfloat.t          1
34                 lib/bigfloatpm.t        1
35                 op/gv.t                 1
36                 lib/complex.t           1
37                 lib/ph.t                1
38                 lib/soundex.t           1
39                 op/misc.t               1
40                 op/runlevel.t           1
41                 op/tie.t                1
42                 op/lex_assign.t         1
43                 );
44
45 foreach (keys %datahandle) {
46      unlink "$_.t";
47 }
48
49 my (@tests, $rules, $re);
50
51 # [.VMS]TEST.COM calls harness with empty arguments, so clean-up @ARGV
52 @ARGV = grep $_ && length( $_ ) => @ARGV;
53
54 sub _populate_hash {
55     return map {$_, 1} split /\s+/, $_[0];
56 }
57
58 # Generate T::H schedule rules that run the contents of each directory
59 # sequentially.
60 sub _seq_dir_rules {
61     my @tests = @_;
62     my %dir;
63     for (@tests) {
64         s{[^/]+$}{\*};
65         $dir{$_}++;
66     }
67
68     return { par => [ map { { seq => $_ } } sort keys %dir ] };
69 }
70
71 sub _extract_tests;
72 sub _extract_tests {
73     # This can probably be done more tersely with a map, but I doubt that it
74     # would be as clear
75     my @results;
76     foreach (@_) {
77         my $ref = ref $_;
78         if ($ref) {
79             if ($ref eq 'ARRAY') {
80                 push @results, _extract_tests @$_;
81             } elsif ($ref eq 'HASH') {
82                 push @results, _extract_tests values %$_;
83             } else {
84                 die "Unknown reference type $ref";
85             }
86         } else {
87             push @results, glob $_;
88         }
89     }
90     @results;
91 }
92
93 if ($ARGV[0] && $ARGV[0]=~/^-re/) {
94     if ($ARGV[0]!~/=/) {
95         shift;
96         $re=join "|",@ARGV;
97         @ARGV=();
98     } else {
99         (undef,$re)=split/=/,shift;
100     }
101 }
102
103 if (@ARGV) {
104     if ($^O eq 'MSWin32') {
105         @tests = map(glob($_),@ARGV);
106     }
107     else {
108         @tests = @ARGV;
109     }
110 } else {
111     # Ideally we'd get somewhere close to Tux's Oslo rules
112     # my $rules = {
113     #     par => [
114     #         { seq => '../ext/DB_File/t/*' },
115     #         { seq => '../ext/IO_Compress_Zlib/t/*' },
116     #         { seq => '../lib/CPANPLUS/*' },
117     #         { seq => '../lib/ExtUtils/t/*' },
118     #         '*'
119     #     ]
120     # };
121
122     # but for now, run all directories in sequence. In particular, it would be
123     # nice to get the tests in t/op/*.t able to run in parallel.
124
125     unless (@tests) {
126         my @seq;
127         push @seq, <base/*.t>;
128
129         my @next = qw(comp cmd run io op uni mro lib);
130         push @next, 'japh' if $torture;
131         push @next, 'win32' if $^O eq 'MSWin32';
132         push @seq, { par => [
133                              map { glob "$_/*.t" } @next
134                             ] };
135
136         my @last;
137         use Config;
138         my %skip;
139         {
140             my %extensions = _populate_hash $Config{'extensions'};
141             my %known_extensions = _populate_hash $Config{'known_extensions'};
142             foreach (keys %known_extensions) {
143                 $skip{$_}++ unless $extensions{$_};
144             }
145         }
146         use File::Spec;
147         my $updir = File::Spec->updir;
148         my $mani  = File::Spec->catfile(File::Spec->updir, "MANIFEST");
149         if (open(MANI, $mani)) {
150             my @manitests = ();
151             my $ext_pat = $^O eq 'MSWin32' ? '(?:win32/)?ext' : 'ext';
152             while (<MANI>) { # similar code in t/TEST
153                 if (m!^($ext_pat/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
154                     my ($test, $extension) = ($1, $2);
155                     if (defined $extension) {
156                         $extension =~ s!/t$!!;
157                         # XXX Do I want to warn that I'm skipping these?
158                         next if $skip{$extension};
159                     }
160                     push @manitests, File::Spec->catfile($updir, $test);
161                 }
162             }
163             close MANI;
164             # Sort the list of test files read from MANIFEST into a sensible
165             # order instead of using the order in which they are listed there
166             push @last, sort { lc $a cmp lc $b } @manitests;
167         } else {
168             warn "$0: cannot open $mani: $!\n";
169         }
170         push @last, <Module_Pluggable/*.t>;
171         push @last, <pod/*.t>;
172         push @last, <x2p/*.t>;
173
174         @tests = (_extract_tests (@seq), @last);
175
176         push @seq, _seq_dir_rules @last;
177
178         $rules = { seq => \@seq };
179
180     }
181 }
182 if ($^O eq 'MSWin32') {
183     s,\\,/,g for @tests;
184 }
185 @tests=grep /$re/, @tests 
186     if $re;
187
188 my $jobs = $ENV{TEST_JOBS};
189 if ($jobs) {
190     eval 'use TAP::Harness 3.13; 1' or die $@;
191     my $h = TAP::Harness->new({ jobs => $jobs, rules => $rules});
192     $h->runtests(@tests);
193 } else {
194     Test::Harness::runtests @tests;
195 }
196 exit(0);