1 # This code is used by lib/warnings.t and lib/feature.t
9 use File::Spec::Functions;
17 my $tmpfile = tempfile();
23 { print "ARGV = [@ARGV]\n" ;
24 @w_files = map { s#^#./lib/$pragma_name/#; $_ } @ARGV
27 { @w_files = sort glob(catfile(curdir(), "lib", $pragma_name, "*")) }
30 foreach my $file (@w_files) {
32 next if $file =~ /(~|\.orig|,v)$/;
33 next if $file =~ /perlio$/ && !(find PerlIO::Layer 'perlio');
36 open F, "<$file" or die "Cannot open $file: $!\n" ;
46 @prgs = (@prgs, $file, split "\n########\n", <F>) ;
53 plan tests => (scalar(@prgs)-$files);
67 my($prog,$expected) = split(/\nEXPECT(?:\n|$)/, $_, 2);
70 foreach my $what (qw(skip todo)) {
71 $prog =~ s/^#\s*\U$what\E\s*(.*)\n//m and $reason{$what} = $1;
72 # If the SKIP reason starts ? then it's taken as a code snippet to
73 # evaluate. This provides the flexibility to have conditional SKIPs
74 if ($reason{$what} && $reason{$what} =~ s/^\?//) {
75 my $temp = eval $reason{$what};
77 die "# In \U$what\E code reason:\n# $reason{$what}\n$@";
79 $reason{$what} = $temp;
83 if ( $prog =~ /--FILE--/) {
84 my(@files) = split(/\n--FILE--\s*([^\s\n]*)\s*\n/, $prog) ;
86 die "Internal error: test $_ didn't split into pairs, got " .
87 scalar(@files) . "[" . join("%%%%", @files) ."]\n"
90 my $filename = shift @files ;
91 my $code = shift @files ;
92 push @temps, $filename ;
93 if ($filename =~ m#(.*)/#) {
97 open F, ">$filename" or die "Cannot open $filename: $!\n" ;
99 close F or die "Cannot close $filename: $!\n";
102 $prog = shift @files ;
105 open TEST, ">$tmpfile" or die "Cannot open >$tmpfile: $!";
108 open(STDERR, ">&STDOUT")
109 or die "Can't dup STDOUT->STDERR: $!;";
112 print TEST "\n#line 1\n"; # So the line numbers don't get messed up.
113 print TEST $prog,"\n";
114 close TEST or die "Cannot close $tmpfile: $!";
115 my $results = runperl( switches => [$switch], stderr => 1, progfile => $tmpfile );
117 $results =~ s/\n+$//;
118 # allow expected output to be written as if $prog is on STDIN
119 $results =~ s/$::tempfile_regexp/-/g;
121 # some tests will trigger VMS messages that won't be expected
122 $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
124 # pipes double these sometimes
125 $results =~ s/\n\n/\n/g;
127 # bison says 'parse error' instead of 'syntax error',
128 # various yaccs may or may not capitalize 'syntax'.
129 $results =~ s/^(syntax|parse) error/syntax error/mig;
130 # allow all tests to run when there are leaks
131 $results =~ s/Scalars leaked: \d+\n//g;
133 $expected =~ s/\n+$//;
134 my $prefix = ($results =~ s#^PREFIX(\n|$)##) ;
135 # any special options? (OPTIONS foo bar zap)
136 my $option_regex = 0;
137 my $option_random = 0;
138 if ($expected =~ s/^OPTIONS? (.+)\n//) {
139 foreach my $option (split(' ', $1)) {
140 if ($option eq 'regex') { # allow regular expressions
143 elsif ($option eq 'random') { # all lines match, but in any order
147 die "$0: Unknown OPTION '$option'\n";
151 die "$0: can't have OPTION regex and random\n"
152 if $option_regex + $option_random > 1;
154 if ($results =~ s/^SKIPPED\n//) {
158 elsif ($option_random) {
159 $ok = randomMatch($results, $expected);
161 elsif ($option_regex) {
162 $ok = $results =~ /^$expected/;
165 $ok = $results =~ /^\Q$expected/;
168 $ok = $results eq $expected;
171 local $::TODO = $reason{todo};
172 print_err_line( $switch, $prog, $expected, $results, $::TODO ) unless $ok;
179 { rmtree $_ if -d $_ }
185 my $expected = shift;
187 my @got = sort split "\n", $got ;
188 my @expected = sort split "\n", $expected ;
190 return "@got" eq "@expected";
195 my($switch, $prog, $expected, $results, $todo) = @_;
196 my $err_line = "PROG: $switch\n$prog\n" .
197 "EXPECTED:\n$expected\n" .
200 $err_line =~ s/^/# /mg;
201 print $err_line; # Harness can't filter it out from STDERR.
204 print STDERR $err_line;