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