More heuristics to make warnings.t pass under different
[p5sagit/p5-mst-13.2.git] / t / lib / common.pl
1 # This code is used by lib/warnings.t and lib/feature.t
2
3 BEGIN {
4     require Config; import Config;
5     require './test.pl';
6 }
7
8 use File::Path;
9 use File::Spec::Functions;
10
11 use strict;
12 our $pragma_name;
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:$pragma_name:#; $_ } @ARGV
28     } else {
29       @w_files = map { s#^#./lib/$pragma_name/#; $_ } @ARGV
30     }
31   }
32 else
33   { @w_files = sort glob(catfile(curdir(), "lib", $pragma_name, "*")) }
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 my $utf8_ok = exists $ENV{PERL_UNICODE} && (
62     $ENV{PERL_UNICODE} =~ m{[Dio]}
63     || ($ENV{PERL_UNICODE} eq ""
64             && ($ENV{LC_ALL} =~ /\butf-?8\b/i || $ENV{LANG} =~ /\butf-?8\b/i))
65 );
66
67 for (@prgs){
68     unless (/\n/)
69      {
70       print "# From $_\n";
71       next;
72      }
73     my $switch = "";
74     my @temps = () ;
75     my @temp_path = () ;
76     if (s/^\s*-\w+//){
77         $switch = $&;
78     }
79     my($prog,$expected) = split(/\nEXPECT(?:\n|$)/, $_, 2);
80     $expected =~ s{\b
81        UTF8 \s*
82            \? \s* '(.*?)'
83             : \s* '(.*?)'
84             }{$utf8_ok?$1:$2}gexs;
85
86     my ($todo, $todo_reason);
87     $todo = $prog =~ s/^#\s*TODO(.*)\n//m and $todo_reason = $1;
88     if ( $prog =~ /--FILE--/) {
89         my(@files) = split(/\n--FILE--\s*([^\s\n]*)\s*\n/, $prog) ;
90         shift @files ;
91         die "Internal error: test $_ didn't split into pairs, got " .
92                 scalar(@files) . "[" . join("%%%%", @files) ."]\n"
93             if @files % 2 ;
94         while (@files > 2) {
95             my $filename = shift @files ;
96             my $code = shift @files ;
97             push @temps, $filename ;
98             if ($filename =~ m#(.*)/#) {
99                 mkpath($1);
100                 push(@temp_path, $1);
101             }
102             open F, ">$filename" or die "Cannot open $filename: $!\n" ;
103             print F $code ;
104             close F or die "Cannot close $filename: $!\n";
105         }
106         shift @files ;
107         $prog = shift @files ;
108     }
109
110     # fix up some paths
111     if ($^O eq 'MacOS') {
112         $prog =~ s|require "./abc(d)?";|require ":abc$1";|g;
113         $prog =~ s|"\."|":"|g;
114     }
115
116     open TEST, ">$tmpfile" or die "Cannot open >$tmpfile: $!";
117     print TEST q{
118         BEGIN {
119             open(STDERR, ">&STDOUT")
120               or die "Can't dup STDOUT->STDERR: $!;";
121         }
122     };
123     print TEST "\n#line 1\n";  # So the line numbers don't get messed up.
124     print TEST $prog,"\n";
125     close TEST or die "Cannot close $tmpfile: $!";
126     my $results = runperl( switches => [$switch], stderr => 1, progfile => $tmpfile );
127     my $status = $?;
128     $results =~ s/\n+$//;
129     # allow expected output to be written as if $prog is on STDIN
130     $results =~ s/tmp\d+/-/g;
131     if ($^O eq 'VMS') {
132         # some tests will trigger VMS messages that won't be expected
133         $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
134
135         # pipes double these sometimes
136         $results =~ s/\n\n/\n/g;
137     }
138 # bison says 'parse error' instead of 'syntax error',
139 # various yaccs may or may not capitalize 'syntax'.
140     $results =~ s/^(syntax|parse) error/syntax error/mig;
141     # allow all tests to run when there are leaks
142     $results =~ s/Scalars leaked: \d+\n//g;
143
144     # fix up some paths
145     if ($^O eq 'MacOS') {
146         $results =~ s|:abc\.pm\b|abc.pm|g;
147         $results =~ s|:abc(d)?\b|./abc$1|g;
148     }
149
150     $expected =~ s/\n+$//;
151     my $prefix = ($results =~ s#^PREFIX(\n|$)##) ;
152     # any special options? (OPTIONS foo bar zap)
153     my $option_regex = 0;
154     my $option_random = 0;
155     if ($expected =~ s/^OPTIONS? (.+)\n//) {
156         foreach my $option (split(' ', $1)) {
157             if ($option eq 'regex') { # allow regular expressions
158                 $option_regex = 1;
159             }
160             elsif ($option eq 'random') { # all lines match, but in any order
161                 $option_random = 1;
162             }
163             else {
164                 die "$0: Unknown OPTION '$option'\n";
165             }
166         }
167     }
168     die "$0: can't have OPTION regex and random\n"
169         if $option_regex + $option_random > 1;
170     my $ok = 1;
171     if ( $results =~ s/^SKIPPED\n//) {
172         print "$results\n" ;
173     }
174     elsif ($option_random)
175     {
176         $ok = 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         $ok = 0;
193     }
194
195     our $TODO = $todo ? $todo_reason : 0;
196     ok($ok);
197
198     foreach (@temps)
199         { unlink $_ if $_ }
200     foreach (@temp_path)
201         { rmtree $_ if -d $_ }
202 }
203
204 sub randomMatch
205 {
206     my $got = shift ;
207     my $expected = shift;
208
209     my @got = sort split "\n", $got ;
210     my @expected = sort split "\n", $expected ;
211
212    return "@got" eq "@expected";
213
214 }
215
216 1;