xref: /openbsd-src/gnu/usr.bin/perl/lib/warnings.t (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    @INC = '../lib';
6    $ENV{PERL5LIB} = '../lib';
7    require Config; import Config;
8}
9
10use File::Path;
11use File::Spec::Functions;
12
13$| = 1;
14
15my $Is_VMS     = $^O eq 'VMS';
16my $Is_MSWin32 = $^O eq 'MSWin32';
17my $Is_NetWare = $^O eq 'NetWare';
18my $Is_MacOS   = $^O eq 'MacOS';
19my $tmpfile = "tmp0000";
20my $i = 0 ;
211 while -e ++$tmpfile;
22END {  if ($tmpfile) { 1 while unlink $tmpfile} }
23
24my @prgs = () ;
25my @w_files = () ;
26
27if (@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  }
35else
36  { @w_files = sort glob(catfile(curdir(), "lib", "warnings", "*")) }
37
38my $files = 0;
39foreach my $file (@w_files) {
40
41    next if $file =~ /(~|\.orig|,v)$/;
42    next if $file =~ /perlio$/ && !(find PerlIO::Layer 'perlio');
43
44    open F, "<$file" or die "Cannot open $file: $!\n" ;
45    my $line = 0;
46    while (<F>) {
47        $line++;
48	last if /^__END__/ ;
49    }
50
51    {
52        local $/ = undef;
53        $files++;
54        @prgs = (@prgs, $file, split "\n########\n", <F>) ;
55    }
56    close F ;
57}
58
59undef $/;
60
61print "1..", scalar(@prgs)-$files, "\n";
62
63
64for (@prgs){
65    unless (/\n/)
66     {
67      print "# From $_\n";
68      next;
69     }
70    my $switch = "";
71    my @temps = () ;
72    my @temp_path = () ;
73    if (s/^\s*-\w+//){
74        $switch = $&;
75        $switch =~ s/(-\S*[A-Z]\S*)/"$1"/ if $Is_VMS; # protect uc switches
76    }
77    my($prog,$expected) = split(/\nEXPECT\n/, $_);
78    if ( $prog =~ /--FILE--/) {
79        my(@files) = split(/\n--FILE--\s*([^\s\n]*)\s*\n/, $prog) ;
80	shift @files ;
81	die "Internal error test $i 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 = $Is_VMS ?
117	              `./perl "-I../lib" $switch $tmpfile` :
118		  $Is_MSWin32 ?
119		      `.\\perl -I../lib $switch $tmpfile` :
120		  $Is_NetWare ?
121		      `perl -I../lib $switch $tmpfile` :
122		  $Is_MacOS ?
123		      `$^X -I::lib $switch -MMac::err=unix $tmpfile` :
124                  `./perl -I../lib $switch $tmpfile`;
125    my $status = $?;
126    $results =~ s/\n+$//;
127    # allow expected output to be written as if $prog is on STDIN
128    $results =~ s/tmp\d+/-/g;
129    if ($^O eq 'VMS') {
130        # some tests will trigger VMS messages that won't be expected
131        $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
132
133        # pipes double these sometimes
134        $results =~ s/\n\n/\n/g;
135    }
136# bison says 'parse error' instead of 'syntax error',
137# various yaccs may or may not capitalize 'syntax'.
138    $results =~ s/^(syntax|parse) error/syntax error/mig;
139    # allow all tests to run when there are leaks
140    $results =~ s/Scalars leaked: \d+\n//g;
141
142    # fix up some paths
143    if ($^O eq 'MacOS') {
144	$results =~ s|:abc\.pm\b|abc.pm|g;
145	$results =~ s|:abc(d)?\b|./abc$1|g;
146    }
147
148    $expected =~ s/\n+$//;
149    my $prefix = ($results =~ s#^PREFIX(\n|$)##) ;
150    # any special options? (OPTIONS foo bar zap)
151    my $option_regex = 0;
152    my $option_random = 0;
153    if ($expected =~ s/^OPTIONS? (.+)\n//) {
154	foreach my $option (split(' ', $1)) {
155	    if ($option eq 'regex') { # allow regular expressions
156		$option_regex = 1;
157	    }
158	    elsif ($option eq 'random') { # all lines match, but in any order
159		$option_random = 1;
160	    }
161	    else {
162		die "$0: Unknown OPTION '$option'\n";
163	    }
164	}
165    }
166    die "$0: can't have OPTION regex and random\n"
167        if $option_regex + option_random > 1;
168    if ( $results =~ s/^SKIPPED\n//) {
169	print "$results\n" ;
170    }
171    elsif ($option_random)
172    {
173        print "not " if !randomMatch($results, $expected);
174    }
175    elsif (($prefix  && (( $option_regex && $results !~ /^$expected/) ||
176			 (!$option_regex && $results !~ /^\Q$expected/))) or
177	   (!$prefix && (( $option_regex && $results !~ /^$expected/) ||
178			 (!$option_regex && $results ne $expected)))) {
179        print STDERR "PROG: $switch\n$prog\n";
180        print STDERR "EXPECTED:\n$expected\n";
181        print STDERR "GOT:\n$results\n";
182        print "not ";
183    }
184    print "ok ", ++$i, "\n";
185    foreach (@temps)
186	{ unlink $_ if $_ }
187    foreach (@temp_path)
188	{ rmtree $_ if -d $_ }
189}
190
191sub randomMatch
192{
193    my $got = shift ;
194    my $expected = shift;
195
196    my @got = sort split "\n", $got ;
197    my @expected = sort split "\n", $expected ;
198
199   return "@got" eq "@expected";
200
201}
202