make lib/warnings.t use t/test.pl
[p5sagit/p5-mst-13.2.git] / lib / warnings.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     $ENV{PERL5LIB} = '../lib';
7     require Config; import Config;
8     require './test.pl';
9 }
10
11 use File::Path;
12 use File::Spec::Functions;
13
14 $| = 1;
15
16 my $Is_MacOS   = $^O eq 'MacOS';
17 my $tmpfile = "tmp0000";
18 1 while -e ++$tmpfile;
19 END {  if ($tmpfile) { 1 while unlink $tmpfile} }
20
21 my @prgs = () ;
22 my @w_files = () ;
23
24 if (@ARGV)
25   { print "ARGV = [@ARGV]\n" ;
26     if ($^O eq 'MacOS') {
27       @w_files = map { s#^#:lib:warnings:#; $_ } @ARGV
28     } else {
29       @w_files = map { s#^#./lib/warnings/#; $_ } @ARGV
30     }
31   }
32 else
33   { @w_files = sort glob(catfile(curdir(), "lib", "warnings", "*")) }
34
35 my $files = 0;
36 foreach my $file (@w_files) {
37
38     next if $file =~ /(~|\.orig|,v)$/;
39     next if $file =~ /perlio$/ && !(find PerlIO::Layer 'perlio');
40     next if -d $file;
41
42     open F, "<$file" or die "Cannot open $file: $!\n" ;
43     my $line = 0;
44     while (<F>) {
45         $line++;
46         last if /^__END__/ ;
47     }
48
49     {
50         local $/ = undef;
51         $files++;
52         @prgs = (@prgs, $file, split "\n########\n", <F>) ;
53     }
54     close F ;
55 }
56
57 undef $/;
58
59 plan tests => (scalar(@prgs)-$files);
60
61
62
63 for (@prgs){
64     unless (/\n/)
65      {
66       print "# From $_\n";
67       next;
68      }
69     my $switch = "";
70     my @temps = () ;
71     my @temp_path = () ;
72     if (s/^\s*-\w+//){
73         $switch = $&;
74     }
75     my($prog,$expected) = split(/\nEXPECT\n/, $_);
76     my ($todo, $todo_reason);
77     $todo = $prog =~ s/^#\s*TODO(.*)\n//m and $todo_reason = $1;
78     if ( $prog =~ /--FILE--/) {
79         my(@files) = split(/\n--FILE--\s*([^\s\n]*)\s*\n/, $prog) ;
80         shift @files ;
81         die "Internal error test $test didn't split into pairs, got " .
82                 scalar(@files) . "[" . join("%%%%", @files) ."]\n"
83             if @files % 2 ;
84         while (@files > 2) {
85             my $filename = shift @files ;
86             my $code = shift @files ;
87             push @temps, $filename ;
88             if ($filename =~ m#(.*)/#) {
89                 mkpath($1);
90                 push(@temp_path, $1);
91             }
92             open F, ">$filename" or die "Cannot open $filename: $!\n" ;
93             print F $code ;
94             close F or die "Cannot close $filename: $!\n";
95         }
96         shift @files ;
97         $prog = shift @files ;
98     }
99
100     # fix up some paths
101     if ($^O eq 'MacOS') {
102         $prog =~ s|require "./abc(d)?";|require ":abc$1";|g;
103         $prog =~ s|"\."|":"|g;
104     }
105
106     open TEST, ">$tmpfile" or die "Cannot open >$tmpfile: $!";
107     print TEST q{
108         BEGIN {
109             open(STDERR, ">&STDOUT")
110               or die "Can't dup STDOUT->STDERR: $!;";
111         }
112     };
113     print TEST "\n#line 1\n";  # So the line numbers don't get messed up.
114     print TEST $prog,"\n";
115     close TEST or die "Cannot close $tmpfile: $!";
116     my $results = runperl( switches => [$switch], stderr => 1, progfile => $tmpfile );
117     my $status = $?;
118     $results =~ s/\n+$//;
119     # allow expected output to be written as if $prog is on STDIN
120     $results =~ s/tmp\d+/-/g;
121     if ($^O eq 'VMS') {
122         # some tests will trigger VMS messages that won't be expected
123         $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
124     }
125 # bison says 'parse error' instead of 'syntax error',
126 # various yaccs may or may not capitalize 'syntax'.
127     $results =~ s/^(syntax|parse) error/syntax error/mig;
128     # allow all tests to run when there are leaks
129     $results =~ s/Scalars leaked: \d+\n//g;
130
131     # fix up some paths
132     if ($^O eq 'MacOS') {
133         $results =~ s|:abc\.pm\b|abc.pm|g;
134         $results =~ s|:abc(d)?\b|./abc$1|g;
135     }
136
137     $expected =~ s/\n+$//;
138     my $prefix = ($results =~ s#^PREFIX(\n|$)##) ;
139     # any special options? (OPTIONS foo bar zap)
140     my $option_regex = 0;
141     my $option_random = 0;
142     if ($expected =~ s/^OPTIONS? (.+)\n//) {
143         foreach my $option (split(' ', $1)) {
144             if ($option eq 'regex') { # allow regular expressions
145                 $option_regex = 1;
146             }
147             elsif ($option eq 'random') { # all lines match, but in any order
148                 $option_random = 1;
149             }
150             else {
151                 die "$0: Unknown OPTION '$option'\n";
152             }
153         }
154     }
155     die "$0: can't have OPTION regex and random\n"
156         if $option_regex + option_random > 1;
157     my $ok = 1;
158     if ( $results =~ s/^SKIPPED\n//) {
159         print "$results\n" ;
160     }
161     elsif ($option_random)
162     {
163         $ok = randomMatch($results, $expected);
164     }
165     elsif (($prefix  && (( $option_regex && $results !~ /^$expected/) ||
166                          (!$option_regex && $results !~ /^\Q$expected/))) or
167            (!$prefix && (( $option_regex && $results !~ /^$expected/) ||
168                          (!$option_regex && $results ne $expected)))) {
169         my $err_line = "PROG: $switch\n$prog\n" .
170                        "EXPECTED:\n$expected\n" .
171                        "GOT:\n$results\n";
172         if ($todo) {
173             $err_line =~ s/^/# /mg;
174             print $err_line;  # Harness can't filter it out from STDERR.
175         }
176         else {
177             print STDERR $err_line;
178         }
179         $ok = 0;
180     }
181
182     $TODO = $todo ? $todo_reason : 0;
183     ok($ok);
184
185     foreach (@temps)
186         { unlink $_ if $_ }
187     foreach (@temp_path)
188         { rmtree $_ if -d $_ }
189 }
190
191 sub randomMatch
192 {
193     my $got = shift ;
194     my $expected = shift;
195
196     my @got = sort split "\n", $got ;
197     my @expected = sort split "\n", $expected ;
198
199    return "@got" eq "@expected";
200
201 }