This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Cwd 2.17_03
[perl5.git] / lib / warnings.t
CommitLineData
f126f811 1#!./perl
bd4dea8e
JH
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 $ENV{PERL5LIB} = '../lib';
7 require Config; import Config;
8}
9
99ef548b 10use File::Path;
e69a2255 11use File::Spec::Functions;
99ef548b 12
bd4dea8e
JH
13$| = 1;
14
15my $Is_VMS = $^O eq 'VMS';
16my $Is_MSWin32 = $^O eq 'MSWin32';
17my $Is_NetWare = $^O eq 'NetWare';
e69a2255 18my $Is_MacOS = $^O eq 'MacOS';
bd4dea8e
JH
19my $tmpfile = "tmp0000";
20my $i = 0 ;
e69a2255 211 while -e ++$tmpfile;
bd4dea8e
JH
22END { if ($tmpfile) { 1 while unlink $tmpfile} }
23
24my @prgs = () ;
25my @w_files = () ;
26
27if (@ARGV)
e69a2255
JH
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 }
bd4dea8e 35else
e69a2255 36 { @w_files = sort glob(catfile(curdir(), "lib", "warnings", "*")) }
bd4dea8e
JH
37
38my $files = 0;
39foreach my $file (@w_files) {
40
41 next if $file =~ /(~|\.orig|,v)$/;
6b5da1a3 42 next if $file =~ /perlio$/ && !(find PerlIO::Layer 'perlio');
cbc25c42 43 next if -d $file;
bd4dea8e
JH
44
45 open F, "<$file" or die "Cannot open $file: $!\n" ;
46 my $line = 0;
47 while (<F>) {
f126f811 48 $line++;
bd4dea8e
JH
49 last if /^__END__/ ;
50 }
51
52 {
53 local $/ = undef;
f126f811 54 $files++;
bd4dea8e
JH
55 @prgs = (@prgs, $file, split "\n########\n", <F>) ;
56 }
57 close F ;
58}
59
60undef $/;
61
82c44e33 62print "1.." . (scalar(@prgs)-$files) . "\n";
f126f811
A
63
64
bd4dea8e
JH
65for (@prgs){
66 unless (/\n/)
67 {
f126f811
A
68 print "# From $_\n";
69 next;
bd4dea8e
JH
70 }
71 my $switch = "";
72 my @temps = () ;
99ef548b 73 my @temp_path = () ;
bd4dea8e
JH
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/, $_);
0b1f5750
A
79 my ($todo, $todo_reason);
80 $todo = $prog =~ s/^#\s*TODO(.*)\n//m and $todo_reason = $1;
bd4dea8e
JH
81 if ( $prog =~ /--FILE--/) {
82 my(@files) = split(/\n--FILE--\s*([^\s\n]*)\s*\n/, $prog) ;
83 shift @files ;
f126f811 84 die "Internal error test $i didn't split into pairs, got " .
bd4dea8e
JH
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 ;
99ef548b
PM
91 if ($filename =~ m#(.*)/#) {
92 mkpath($1);
93 push(@temp_path, $1);
94 }
bd4dea8e
JH
95 open F, ">$filename" or die "Cannot open $filename: $!\n" ;
96 print F $code ;
f126f811 97 close F or die "Cannot close $filename: $!\n";
bd4dea8e
JH
98 }
99 shift @files ;
100 $prog = shift @files ;
101 }
e69a2255
JH
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
f126f811 109 open TEST, ">$tmpfile" or die "Cannot open >$tmpfile: $!";
d6920b13 110 print TEST q{
f126f811
A
111 BEGIN {
112 open(STDERR, ">&STDOUT")
d6920b13
MS
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.
bd4dea8e 117 print TEST $prog,"\n";
f126f811 118 close TEST or die "Cannot close $tmpfile: $!";
bd4dea8e 119 my $results = $Is_VMS ?
be708cc0 120 `./perl "-I../lib" $switch $tmpfile` :
bd4dea8e 121 $Is_MSWin32 ?
be708cc0 122 `.\\perl -I../lib $switch $tmpfile` :
bd4dea8e 123 $Is_NetWare ?
be708cc0
JH
124 `perl -I../lib $switch $tmpfile` :
125 $Is_MacOS ?
126 `$^X -I::lib $switch -MMac::err=unix $tmpfile` :
d6920b13 127 `./perl -I../lib $switch $tmpfile`;
bd4dea8e
JH
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;
765ebaf2
CB
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 }
bd4dea8e
JH
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;
e69a2255
JH
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
bd4dea8e
JH
151 $expected =~ s/\n+$//;
152 my $prefix = ($results =~ s#^PREFIX(\n|$)##) ;
153 # any special options? (OPTIONS foo bar zap)
154 my $option_regex = 0;
247c8d76 155 my $option_random = 0;
bd4dea8e
JH
156 if ($expected =~ s/^OPTIONS? (.+)\n//) {
157 foreach my $option (split(' ', $1)) {
158 if ($option eq 'regex') { # allow regular expressions
159 $option_regex = 1;
6b5da1a3 160 }
247c8d76
JH
161 elsif ($option eq 'random') { # all lines match, but in any order
162 $option_random = 1;
163 }
164 else {
bd4dea8e
JH
165 die "$0: Unknown OPTION '$option'\n";
166 }
167 }
168 }
247c8d76
JH
169 die "$0: can't have OPTION regex and random\n"
170 if $option_regex + option_random > 1;
bd4dea8e
JH
171 if ( $results =~ s/^SKIPPED\n//) {
172 print "$results\n" ;
173 }
247c8d76
JH
174 elsif ($option_random)
175 {
176 print "not " if !randomMatch($results, $expected);
177 }
bd4dea8e
JH
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)))) {
0b1f5750
A
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 }
bd4dea8e
JH
192 print "not ";
193 }
0b1f5750
A
194 print "ok " . ++$i;
195 print " # TODO$todo_reason" if $todo;
196 print "\n";
f126f811
A
197 foreach (@temps)
198 { unlink $_ if $_ }
99ef548b
PM
199 foreach (@temp_path)
200 { rmtree $_ if -d $_ }
bd4dea8e 201}
247c8d76
JH
202
203sub 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}