This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In t/lib/common.pl, load File::Path on demand.
[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;
11ce4a76 13use File::Spec::Functions qw(catfile curdir rel2abs);
25ae1130
RGS
14
15use strict;
742dc32d 16use warnings;
a80e93c2
NC
17my (undef, $file) = caller;
18my ($pragma_name) = $file =~ /([A-Za-z_0-9]+)\.t$/
19 or die "Can't identify pragama to test from file name '$file'";
25ae1130
RGS
20
21$| = 1;
22
f9cd5d9f 23my $tmpfile = tempfile();
25ae1130
RGS
24
25my @prgs = () ;
26my @w_files = () ;
27
28if (@ARGV)
29 { print "ARGV = [@ARGV]\n" ;
25ae1130 30 @w_files = map { s#^#./lib/$pragma_name/#; $_ } @ARGV
25ae1130
RGS
31 }
32else
33 { @w_files = sort glob(catfile(curdir(), "lib", $pragma_name, "*")) }
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
11ce4a76 57$^X = rel2abs($^X);
463cf062
NC
58my $tempdir = tempfile;
59
60mkdir $tempdir, 0700 or die "Can't mkdir '$tempdir': $!";
61chdir $tempdir or die die "Can't chdir '$tempdir': $!";
62unshift @INC, '../../lib';
63my $cleanup = 1;
64my %tempfiles;
65
66END {
67 if ($cleanup) {
44177063 68 require File::Path;
463cf062 69 chdir '..' or die "Couldn't chdir .. for cleanup: $!";
44177063 70 File::Path::rmtree($tempdir);
463cf062
NC
71 }
72}
73
3d243db0 74local $/ = undef;
25ae1130 75
3d243db0
KW
76my $tests = $::local_tests || 0;
77$tests = scalar(@prgs)-$files + $tests if $tests !~ /\D/;
78plan $tests; # If input is 'no_plan', pass it on unchanged
25ae1130 79
25ae1130
RGS
80for (@prgs){
81 unless (/\n/)
82 {
83 print "# From $_\n";
84 next;
85 }
86 my $switch = "";
87 my @temps = () ;
88 my @temp_path = () ;
89 if (s/^\s*-\w+//){
90 $switch = $&;
91 }
92 my($prog,$expected) = split(/\nEXPECT(?:\n|$)/, $_, 2);
a983b08d 93
6ee4ed10
NC
94 my %reason;
95 foreach my $what (qw(skip todo)) {
96 $prog =~ s/^#\s*\U$what\E\s*(.*)\n//m and $reason{$what} = $1;
97 # If the SKIP reason starts ? then it's taken as a code snippet to
98 # evaluate. This provides the flexibility to have conditional SKIPs
99 if ($reason{$what} && $reason{$what} =~ s/^\?//) {
100 my $temp = eval $reason{$what};
101 if ($@) {
102 die "# In \U$what\E code reason:\n# $reason{$what}\n$@";
103 }
104 $reason{$what} = $temp;
8e5f6690 105 }
8e5f6690 106 }
6ee4ed10 107
25ae1130
RGS
108 if ( $prog =~ /--FILE--/) {
109 my(@files) = split(/\n--FILE--\s*([^\s\n]*)\s*\n/, $prog) ;
110 shift @files ;
111 die "Internal error: test $_ didn't split into pairs, got " .
112 scalar(@files) . "[" . join("%%%%", @files) ."]\n"
113 if @files % 2 ;
114 while (@files > 2) {
115 my $filename = shift @files ;
116 my $code = shift @files ;
117 push @temps, $filename ;
ea065648 118 if ($filename =~ m#(.*)/# && $filename !~ m#^\.\./#) {
44177063
NC
119 require File::Path;
120 File::Path::mkpath($1);
25ae1130 121 push(@temp_path, $1);
ea065648 122 }
25ae1130
RGS
123 open F, ">$filename" or die "Cannot open $filename: $!\n" ;
124 print F $code ;
125 close F or die "Cannot close $filename: $!\n";
126 }
127 shift @files ;
128 $prog = shift @files ;
129 }
130
25ae1130
RGS
131 open TEST, ">$tmpfile" or die "Cannot open >$tmpfile: $!";
132 print TEST q{
133 BEGIN {
134 open(STDERR, ">&STDOUT")
135 or die "Can't dup STDOUT->STDERR: $!;";
136 }
137 };
138 print TEST "\n#line 1\n"; # So the line numbers don't get messed up.
139 print TEST $prog,"\n";
140 close TEST or die "Cannot close $tmpfile: $!";
463cf062
NC
141 my $results = runperl( switches => ["-I../../lib", $switch], nolib => 1,
142 stderr => 1, progfile => $tmpfile );
25ae1130
RGS
143 my $status = $?;
144 $results =~ s/\n+$//;
145 # allow expected output to be written as if $prog is on STDIN
f9cd5d9f 146 $results =~ s/$::tempfile_regexp/-/g;
25ae1130
RGS
147 if ($^O eq 'VMS') {
148 # some tests will trigger VMS messages that won't be expected
149 $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
150
151 # pipes double these sometimes
152 $results =~ s/\n\n/\n/g;
153 }
154# bison says 'parse error' instead of 'syntax error',
155# various yaccs may or may not capitalize 'syntax'.
156 $results =~ s/^(syntax|parse) error/syntax error/mig;
157 # allow all tests to run when there are leaks
158 $results =~ s/Scalars leaked: \d+\n//g;
159
25ae1130
RGS
160 $expected =~ s/\n+$//;
161 my $prefix = ($results =~ s#^PREFIX(\n|$)##) ;
162 # any special options? (OPTIONS foo bar zap)
163 my $option_regex = 0;
164 my $option_random = 0;
165 if ($expected =~ s/^OPTIONS? (.+)\n//) {
166 foreach my $option (split(' ', $1)) {
167 if ($option eq 'regex') { # allow regular expressions
168 $option_regex = 1;
169 }
170 elsif ($option eq 'random') { # all lines match, but in any order
171 $option_random = 1;
172 }
173 else {
174 die "$0: Unknown OPTION '$option'\n";
175 }
176 }
177 }
178 die "$0: can't have OPTION regex and random\n"
179 if $option_regex + $option_random > 1;
7702f076
RGS
180 my $ok = 0;
181 if ($results =~ s/^SKIPPED\n//) {
25ae1130 182 print "$results\n" ;
7702f076 183 $ok = 1;
25ae1130 184 }
afb1190b 185 elsif ($option_random) {
7d467e20
NC
186 my @got = sort split "\n", $results;
187 my @expected = sort split "\n", $expected;
188
189 $ok = "@got" eq "@expected";
25ae1130 190 }
7702f076
RGS
191 elsif ($option_regex) {
192 $ok = $results =~ /^$expected/;
193 }
194 elsif ($prefix) {
195 $ok = $results =~ /^\Q$expected/;
196 }
197 else {
198 $ok = $results eq $expected;
25ae1130 199 }
7702f076 200
4a4b8592 201 local $::TODO = $reason{todo};
01c10dac
NC
202
203 unless ($ok) {
204 my $err_line = "PROG: $switch\n$prog\n" .
205 "EXPECTED:\n$expected\n" .
206 "GOT:\n$results\n";
207 if ($::TODO) {
208 $err_line =~ s/^/# /mg;
209 print $err_line; # Harness can't filter it out from STDERR.
210 }
211 else {
212 print STDERR $err_line;
213 }
214 }
25ae1130 215
25ae1130
RGS
216 ok($ok);
217
218 foreach (@temps)
219 { unlink $_ if $_ }
220 foreach (@temp_path)
44177063 221 { File::Path::rmtree $_ if -d $_ }
25ae1130
RGS
222}
223
25ae1130 2241;