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