This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
RT 74444 - install_lib.pl emits warnings when installperl is run with destdir
[perl5.git] / t / lib / common.pl
CommitLineData
6fc551a0
NC
1# This code is used by lib/charnames.t, lib/feature.t, lib/subs.t,
2# lib/strict.t and lib/warnings.t
3d243db0
KW
3#
4# On input, $::local_tests is the number of tests in the caller; or
5# 'no_plan' if unknown, in which case it is the caller's responsibility
6# to call cur_test() to find out how many this executed
25ae1130
RGS
7
8BEGIN {
25ae1130
RGS
9 require './test.pl';
10}
11
afb1190b 12use Config;
25ae1130
RGS
13use File::Path;
14use File::Spec::Functions;
15
16use strict;
742dc32d 17use warnings;
a80e93c2
NC
18my (undef, $file) = caller;
19my ($pragma_name) = $file =~ /([A-Za-z_0-9]+)\.t$/
20 or die "Can't identify pragama to test from file name '$file'";
25ae1130
RGS
21
22$| = 1;
23
f9cd5d9f 24my $tmpfile = tempfile();
25ae1130
RGS
25
26my @prgs = () ;
27my @w_files = () ;
28
29if (@ARGV)
30 { print "ARGV = [@ARGV]\n" ;
25ae1130 31 @w_files = map { s#^#./lib/$pragma_name/#; $_ } @ARGV
25ae1130
RGS
32 }
33else
34 { @w_files = sort glob(catfile(curdir(), "lib", $pragma_name, "*")) }
35
36my $files = 0;
37foreach my $file (@w_files) {
38
39 next if $file =~ /(~|\.orig|,v)$/;
40 next if $file =~ /perlio$/ && !(find PerlIO::Layer 'perlio');
41 next if -d $file;
42
43 open F, "<$file" or die "Cannot open $file: $!\n" ;
44 my $line = 0;
45 while (<F>) {
46 $line++;
47 last if /^__END__/ ;
48 }
49
50 {
51 local $/ = undef;
52 $files++;
53 @prgs = (@prgs, $file, split "\n########\n", <F>) ;
54 }
55 close F ;
56}
57
3d243db0 58local $/ = undef;
25ae1130 59
3d243db0
KW
60my $tests = $::local_tests || 0;
61$tests = scalar(@prgs)-$files + $tests if $tests !~ /\D/;
62plan $tests; # If input is 'no_plan', pass it on unchanged
25ae1130 63
25ae1130
RGS
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 }
76 my($prog,$expected) = split(/\nEXPECT(?:\n|$)/, $_, 2);
a983b08d 77
6ee4ed10
NC
78 my %reason;
79 foreach my $what (qw(skip todo)) {
80 $prog =~ s/^#\s*\U$what\E\s*(.*)\n//m and $reason{$what} = $1;
81 # If the SKIP reason starts ? then it's taken as a code snippet to
82 # evaluate. This provides the flexibility to have conditional SKIPs
83 if ($reason{$what} && $reason{$what} =~ s/^\?//) {
84 my $temp = eval $reason{$what};
85 if ($@) {
86 die "# In \U$what\E code reason:\n# $reason{$what}\n$@";
87 }
88 $reason{$what} = $temp;
8e5f6690 89 }
8e5f6690 90 }
6ee4ed10 91
25ae1130
RGS
92 if ( $prog =~ /--FILE--/) {
93 my(@files) = split(/\n--FILE--\s*([^\s\n]*)\s*\n/, $prog) ;
94 shift @files ;
95 die "Internal error: test $_ didn't split into pairs, got " .
96 scalar(@files) . "[" . join("%%%%", @files) ."]\n"
97 if @files % 2 ;
98 while (@files > 2) {
99 my $filename = shift @files ;
100 my $code = shift @files ;
101 push @temps, $filename ;
ea065648 102 if ($filename =~ m#(.*)/# && $filename !~ m#^\.\./#) {
25ae1130
RGS
103 mkpath($1);
104 push(@temp_path, $1);
ea065648 105 }
25ae1130
RGS
106 open F, ">$filename" or die "Cannot open $filename: $!\n" ;
107 print F $code ;
108 close F or die "Cannot close $filename: $!\n";
109 }
110 shift @files ;
111 $prog = shift @files ;
112 }
113
25ae1130
RGS
114 open TEST, ">$tmpfile" or die "Cannot open >$tmpfile: $!";
115 print TEST q{
116 BEGIN {
117 open(STDERR, ">&STDOUT")
118 or die "Can't dup STDOUT->STDERR: $!;";
119 }
120 };
121 print TEST "\n#line 1\n"; # So the line numbers don't get messed up.
122 print TEST $prog,"\n";
123 close TEST or die "Cannot close $tmpfile: $!";
124 my $results = runperl( switches => [$switch], stderr => 1, progfile => $tmpfile );
125 my $status = $?;
126 $results =~ s/\n+$//;
127 # allow expected output to be written as if $prog is on STDIN
f9cd5d9f 128 $results =~ s/$::tempfile_regexp/-/g;
25ae1130
RGS
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
25ae1130
RGS
142 $expected =~ s/\n+$//;
143 my $prefix = ($results =~ s#^PREFIX(\n|$)##) ;
144 # any special options? (OPTIONS foo bar zap)
145 my $option_regex = 0;
146 my $option_random = 0;
147 if ($expected =~ s/^OPTIONS? (.+)\n//) {
148 foreach my $option (split(' ', $1)) {
149 if ($option eq 'regex') { # allow regular expressions
150 $option_regex = 1;
151 }
152 elsif ($option eq 'random') { # all lines match, but in any order
153 $option_random = 1;
154 }
155 else {
156 die "$0: Unknown OPTION '$option'\n";
157 }
158 }
159 }
160 die "$0: can't have OPTION regex and random\n"
161 if $option_regex + $option_random > 1;
7702f076
RGS
162 my $ok = 0;
163 if ($results =~ s/^SKIPPED\n//) {
25ae1130 164 print "$results\n" ;
7702f076 165 $ok = 1;
25ae1130 166 }
afb1190b 167 elsif ($option_random) {
25ae1130
RGS
168 $ok = randomMatch($results, $expected);
169 }
7702f076
RGS
170 elsif ($option_regex) {
171 $ok = $results =~ /^$expected/;
172 }
173 elsif ($prefix) {
174 $ok = $results =~ /^\Q$expected/;
175 }
176 else {
177 $ok = $results eq $expected;
25ae1130 178 }
7702f076 179
4a4b8592
NC
180 local $::TODO = $reason{todo};
181 print_err_line( $switch, $prog, $expected, $results, $::TODO ) unless $ok;
25ae1130 182
25ae1130
RGS
183 ok($ok);
184
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
afb1190b
RGS
203sub print_err_line {
204 my($switch, $prog, $expected, $results, $todo) = @_;
205 my $err_line = "PROG: $switch\n$prog\n" .
206 "EXPECTED:\n$expected\n" .
207 "GOT:\n$results\n";
208 if ($todo) {
209 $err_line =~ s/^/# /mg;
210 print $err_line; # Harness can't filter it out from STDERR.
211 }
212 else {
213 print STDERR $err_line;
214 }
215
216 return 1;
217}
218
25ae1130 2191;