This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate change #18420 from maint-5.8:
[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     if ( $prog =~ /--FILE--/) {
80         my(@files) = split(/\n--FILE--\s*([^\s\n]*)\s*\n/, $prog) ;
81         shift @files ;
82         die "Internal error test $i didn't split into pairs, got " .
83                 scalar(@files) . "[" . join("%%%%", @files) ."]\n"
84             if @files % 2 ;
85         while (@files > 2) {
86             my $filename = shift @files ;
87             my $code = shift @files ;
88             push @temps, $filename ;
89             if ($filename =~ m#(.*)/#) {
90                 mkpath($1);
91                 push(@temp_path, $1);
92             }
93             open F, ">$filename" or die "Cannot open $filename: $!\n" ;
94             print F $code ;
95             close F or die "Cannot close $filename: $!\n";
96         }
97         shift @files ;
98         $prog = shift @files ;
99     }
100
101     # fix up some paths
102     if ($^O eq 'MacOS') {
103         $prog =~ s|require "./abc(d)?";|require ":abc$1";|g;
104         $prog =~ s|"\."|":"|g;
105     }
106
107     open TEST, ">$tmpfile" or die "Cannot open >$tmpfile: $!";
108     print TEST q{
109         BEGIN {
110             open(STDERR, ">&STDOUT")
111               or die "Can't dup STDOUT->STDERR: $!;";
112         }
113     };
114     print TEST "\n#line 1\n";  # So the line numbers don't get messed up.
115     print TEST $prog,"\n";
116     close TEST or die "Cannot close $tmpfile: $!";
117     my $results = $Is_VMS ?
118                       `./perl "-I../lib" $switch $tmpfile` :
119                   $Is_MSWin32 ?
120                       `.\\perl -I../lib $switch $tmpfile` :
121                   $Is_NetWare ?
122                       `perl -I../lib $switch $tmpfile` :
123                   $Is_MacOS ?
124                       `$^X -I::lib $switch -MMac::err=unix $tmpfile` :
125                   `./perl -I../lib $switch $tmpfile`;
126     my $status = $?;
127     $results =~ s/\n+$//;
128     # allow expected output to be written as if $prog is on STDIN
129     $results =~ s/tmp\d+/-/g;
130     if ($^O eq 'VMS') {
131         # some tests will trigger VMS messages that won't be expected
132         $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
133
134         # pipes double these sometimes
135         $results =~ s/\n\n/\n/g;
136     }
137 # bison says 'parse error' instead of 'syntax error',
138 # various yaccs may or may not capitalize 'syntax'.
139     $results =~ s/^(syntax|parse) error/syntax error/mig;
140     # allow all tests to run when there are leaks
141     $results =~ s/Scalars leaked: \d+\n//g;
142
143     # fix up some paths
144     if ($^O eq 'MacOS') {
145         $results =~ s|:abc\.pm\b|abc.pm|g;
146         $results =~ s|:abc(d)?\b|./abc$1|g;
147     }
148
149     $expected =~ s/\n+$//;
150     my $prefix = ($results =~ s#^PREFIX(\n|$)##) ;
151     # any special options? (OPTIONS foo bar zap)
152     my $option_regex = 0;
153     my $option_random = 0;
154     if ($expected =~ s/^OPTIONS? (.+)\n//) {
155         foreach my $option (split(' ', $1)) {
156             if ($option eq 'regex') { # allow regular expressions
157                 $option_regex = 1;
158             }
159             elsif ($option eq 'random') { # all lines match, but in any order
160                 $option_random = 1;
161             }
162             else {
163                 die "$0: Unknown OPTION '$option'\n";
164             }
165         }
166     }
167     die "$0: can't have OPTION regex and random\n"
168         if $option_regex + option_random > 1;
169     if ( $results =~ s/^SKIPPED\n//) {
170         print "$results\n" ;
171     }
172     elsif ($option_random)
173     {
174         print "not " if !randomMatch($results, $expected);
175     }
176     elsif (($prefix  && (( $option_regex && $results !~ /^$expected/) ||
177                          (!$option_regex && $results !~ /^\Q$expected/))) or
178            (!$prefix && (( $option_regex && $results !~ /^$expected/) ||
179                          (!$option_regex && $results ne $expected)))) {
180         print STDERR "PROG: $switch\n$prog\n";
181         print STDERR "EXPECTED:\n$expected\n";
182         print STDERR "GOT:\n$results\n";
183         print "not ";
184     }
185     print "ok ", ++$i, "\n";
186     foreach (@temps)
187         { unlink $_ if $_ }
188     foreach (@temp_path)
189         { rmtree $_ if -d $_ }
190 }
191
192 sub randomMatch
193 {
194     my $got = shift ;
195     my $expected = shift;
196
197     my @got = sort split "\n", $got ;
198     my @expected = sort split "\n", $expected ;
199
200    return "@got" eq "@expected";
201
202 }