This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
'$! = EACCESS; require ...' could fail
[perl5.git] / t / comp / require.t
CommitLineData
f46d017c
GS
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61
MG
5 @INC = '.';
6 push @INC, '../lib';
f46d017c
GS
7}
8
9e7c6af3
NC
9sub do_require {
10 %INC = ();
11 write_file('bleah.pm',@_);
12 eval { require "bleah.pm" };
13 my @a; # magic guard for scope violations (must be first lexical in file)
14}
15
f46d017c
GS
16# don't make this lexical
17$i = 1;
d2f5bb60 18
c106f444 19my @files_to_delete = qw (bleah.pm bleah.do bleah.flg urkkk.pm urkkk.pmc
b0927e10
NC
20krunch.pm krunch.pmc whap.pm whap.pmc);
21
c106f444
DM
22# there may be another copy of this test script running, or the files may
23# just not have been deleted at the end of the last run; if the former, we
24# wait a while so that creating and unlinking these files won't interfere
25# with the other process; if the latter, then the delay is harmless. As
26# to why there might be multiple execution of this test file, I don't
27# know; but this is an experiment to see if random smoke failures go away.
28
29if (grep -e, @files_to_delete) {
30 print "# Sleeping for 20 secs waiting for other process to finish\n";
31 sleep 20;
32}
33
b0927e10 34
d2f5bb60 35my $Is_EBCDIC = (ord('A') == 193) ? 1 : 0;
8a220075 36my $Is_UTF8 = (${^OPEN} || "") =~ /:utf8/;
b2da7ead 37my $total_tests = 56;
76b0784a 38if ($Is_EBCDIC || $Is_UTF8) { $total_tests -= 3; }
d2f5bb60 39print "1..$total_tests\n";
f46d017c 40
4a9ae47a
GS
41sub write_file {
42 my $f = shift;
43 open(REQ,">$f") or die "Can't write '$f': $!";
9f82a649 44 binmode REQ;
4a9ae47a 45 print REQ @_;
d1e4d418 46 close REQ or die "Could not close $f: $!";
4a9ae47a
GS
47}
48
9f3d182e
GS
49eval {require 5.005};
50print "# $@\nnot " if $@;
b3008037 51print "ok ",$i++," - require 5.005 try 1\n";
9f3d182e
GS
52
53eval { require 5.005 };
54print "# $@\nnot " if $@;
b3008037 55print "ok ",$i++," - require 5.005 try 2\n";
9f3d182e
GS
56
57eval { require 5.005; };
58print "# $@\nnot " if $@;
b3008037 59print "ok ",$i++," - require 5.005 try 3\n";
9f3d182e
GS
60
61eval {
62 require 5.005
63};
64print "# $@\nnot " if $@;
b3008037 65print "ok ",$i++," - require 5.005 try 4\n";
9f3d182e 66
a7cb1f99
GS
67# new style version numbers
68
69eval { require v5.5.630; };
70print "# $@\nnot " if $@;
b3008037 71print "ok ",$i++," - require 5.5.630\n";
a7cb1f99 72
1647a8f2
FC
73sub v5 { die }
74eval { require v5; };
75print "# $@\nnot " if $@;
76print "ok ",$i++," - require v5 ignores sub named v5\n";
77
dd629d5b 78eval { require 10.0.2; };
a7cb1f99 79print "# $@\nnot " unless $@ =~ /^Perl v10\.0\.2 required/;
b3008037 80print "ok ",$i++," - require 10.0.2\n";
a7cb1f99 81
44dcb63b 82my $ver = 5.005_63;
a7cb1f99
GS
83eval { require $ver; };
84print "# $@\nnot " if $@;
b3008037 85print "ok ",$i++," - require 5.005_63\n";
a7cb1f99 86
44dcb63b 87# check inaccurate fp
dbe7b177
GS
88$ver = 10.2;
89eval { require $ver; };
9137345a 90print "# $@\nnot " unless $@ =~ /^Perl v10\.200.0 required/;
b3008037 91print "ok ",$i++," - require 10.2\n";
44dcb63b
GS
92
93$ver = 10.000_02;
a7cb1f99 94eval { require $ver; };
44dcb63b 95print "# $@\nnot " unless $@ =~ /^Perl v10\.0\.20 required/;
b3008037 96print "ok ",$i++," - require 10.000_02\n";
a7cb1f99 97
dd629d5b 98print "not " unless 5.5.1 gt v5.5;
b3008037 99print "ok ",$i++," - 5.5.1 gt v5.5\n";
a7cb1f99 100
a7cb1f99 101{
a7cb1f99 102 print "not " unless v5.5.640 eq "\x{5}\x{5}\x{280}";
b3008037 103 print "ok ",$i++," - v5.5.640 eq \\x{5}\\x{5}\\x{280}\n";
a7cb1f99
GS
104
105 print "not " unless v7.15 eq "\x{7}\x{f}";
b3008037 106 print "ok ",$i++," - v7.15 eq \\x{7}\\x{f}\n";
a7cb1f99
GS
107
108 print "not "
109 unless v1.20.300.4000.50000.600000 eq "\x{1}\x{14}\x{12c}\x{fa0}\x{c350}\x{927c0}";
b3008037 110 print "ok ",$i++," - v1.20.300.4000.50000.600000 eq ...\n";
a7cb1f99
GS
111}
112
91937335
NC
113# "use 5.11.0" (and higher) loads strictures.
114# check that this doesn't happen with require
115eval 'require 5.11.0; ${"foo"} = "bar";';
116print "# $@\nnot " if $@;
b3008037 117print "ok ",$i++," - require 5.11.0\n";
88e9444c
NC
118eval 'BEGIN {require 5.11.0} ${"foo"} = "bar";';
119print "# $@\nnot " if $@;
b3008037 120print "ok ",$i++,"\ - BEGIN { require 5.11.0}\n";
91937335 121
4a9ae47a 122# interaction with pod (see the eof)
b3008037 123write_file('bleah.pm', "print 'ok $i - require bleah.pm\n'; 1;\n");
4a9ae47a
GS
124require "bleah.pm";
125$i++;
126
f46d017c
GS
127# run-time failure in require
128do_require "0;\n";
129print "# $@\nnot " unless $@ =~ /did not return a true/;
b3008037 130print "ok ",$i++," - require returning 0\n";
f46d017c 131
4d8b06f1 132print "not " if exists $INC{'bleah.pm'};
b3008037 133print "ok ",$i++," - %INC not updated\n";
4d8b06f1
RD
134
135my $flag_file = 'bleah.flg';
136# run-time error in require
137for my $expected_compile (1,0) {
138 write_file($flag_file, 1);
139 print "not " unless -e $flag_file;
b3008037 140 print "ok ",$i++," - exp $expected_compile; bleah.flg\n";
4d8b06f1
RD
141 write_file('bleah.pm', "unlink '$flag_file' or die; \$a=0; \$b=1/\$a; 1;\n");
142 print "# $@\nnot " if eval { require 'bleah.pm' };
b3008037 143 print "ok ",$i++," - exp $expected_compile; require bleah.pm with flag file\n";
4d8b06f1 144 print "not " unless -e $flag_file xor $expected_compile;
b3008037 145 print "ok ",$i++," - exp $expected_compile; -e flag_file\n";
27bcc0a7 146 print "not " unless exists $INC{'bleah.pm'};
b3008037 147 print "ok ",$i++," - exp $expected_compile; exists \$INC{'bleah.pm}\n";
4d8b06f1
RD
148}
149
f46d017c
GS
150# compile-time failure in require
151do_require "1)\n";
f0ec1f9a 152# bison says 'parse error' instead of 'syntax error',
d91e2bdb 153# various yaccs may or may not capitalize 'syntax'.
f0ec1f9a 154print "# $@\nnot " unless $@ =~ /(syntax|parse) error/mi;
b3008037 155print "ok ",$i++," - syntax error\n";
f46d017c 156
27bcc0a7
RGS
157# previous failure cached in %INC
158print "not " unless exists $INC{'bleah.pm'};
b3008037 159print "ok ",$i++," - cached %INC\n";
4d8b06f1
RD
160write_file($flag_file, 1);
161write_file('bleah.pm', "unlink '$flag_file'; 1");
162print "# $@\nnot " if eval { require 'bleah.pm' };
b3008037 163print "ok ",$i++," - eval { require 'bleah.pm' }\n";
4d8b06f1 164print "# $@\nnot " unless $@ =~ /Compilation failed/i;
b3008037 165print "ok ",$i++," - Compilation failed\n";
4d8b06f1 166print "not " unless -e $flag_file;
b3008037 167print "ok ",$i++," - -e flag_file\n";
27bcc0a7 168print "not " unless exists $INC{'bleah.pm'};
b3008037 169print "ok ",$i++," - \$INC{'bleah.pm'}\n";
4d8b06f1 170
f46d017c
GS
171# successful require
172do_require "1";
173print "# $@\nnot " if $@;
b3008037 174print "ok ",$i++," - do_require '1';\n";
f46d017c 175
faa7e5bb 176# do FILE shouldn't see any outside lexicals
b3008037 177my $x = "ok $i - bleah.do\n";
faa7e5bb 178write_file("bleah.do", <<EOT);
b3008037 179\$x = "not ok $i - bleah.do\\n";
faa7e5bb 180EOT
e81465be 181do "bleah.do" or die $@;
faa7e5bb 182dofile();
e81465be 183sub dofile { do "bleah.do" or die $@; };
faa7e5bb 184print $x;
faa7e5bb 185
a89be09a
RGS
186# Test that scalar context is forced for require
187
188write_file('bleah.pm', <<'**BLEAH**'
189print "not " if !defined wantarray || wantarray ne '';
021f53de 190print "ok $i - require() context\n";
a89be09a
RGS
1911;
192**BLEAH**
193);
194 delete $INC{"bleah.pm"}; ++$::i;
195$foo = eval q{require bleah}; delete $INC{"bleah.pm"}; ++$::i;
196@foo = eval q{require bleah}; delete $INC{"bleah.pm"}; ++$::i;
197 eval q{require bleah}; delete $INC{"bleah.pm"}; ++$::i;
0c58d367 198 eval q{$_=$_+2;require bleah}; delete $INC{"bleah.pm"}; ++$::i;
5255f1f4 199 eval q{return require bleah}; delete $INC{"bleah.pm"}; ++$::i;
a89be09a
RGS
200$foo = eval {require bleah}; delete $INC{"bleah.pm"}; ++$::i;
201@foo = eval {require bleah}; delete $INC{"bleah.pm"}; ++$::i;
202 eval {require bleah};
203
2d6f15ea
NC
204# Test for fix of RT #24404 : "require $scalar" may load a directory
205my $r = "threads";
206eval { require $r };
207$i++;
6b845e56 208if($@ =~ /Can't locate threads in \@INC/) {
b3008037 209 print "ok $i - RT #24404\n";
2d6f15ea 210} else {
b3008037 211 print "not ok - RT #24404$i\n";
2d6f15ea
NC
212}
213
01b5ef50
FC
214# require CORE::foo
215eval ' require CORE::lc "THREADS" ';
216$i++;
217if($@ =~ /Can't locate threads in \@INC/) {
218 print "ok $i - [perl #24482] require CORE::foo\n";
219} else {
220 print "not ok - [perl #24482] require CORE::foo\n";
221}
222
57f3ff92
NC
223
224write_file('bleah.pm', qq(die "This is an expected error";\n));
225delete $INC{"bleah.pm"}; ++$::i;
226eval { CORE::require bleah; };
227if ($@ =~ /^This is an expected error/) {
b3008037 228 print "ok $i - expected error\n";
57f3ff92 229} else {
b3008037 230 print "not ok $i - expected error\n";
57f3ff92
NC
231}
232
b0927e10
NC
233sub write_file_not_thing {
234 my ($file, $thing, $test) = @_;
235 write_file($file, <<"EOT");
b3008037 236 print "not ok $test - write_file_not_thing $file\n";
b0927e10
NC
237 die "The $thing file should not be loaded";
238EOT
239}
240
241{
242 # Right. We really really need Config here.
243 require Config;
244 die "Failed to load Config for some reason"
245 unless $Config::Config{version};
246 my $ccflags = $Config::Config{ccflags};
247 die "Failed to get ccflags for some reason" unless defined $ccflags;
248
249 my $simple = ++$i;
250 my $pmc_older = ++$i;
251 my $pmc_dies = ++$i;
252 if ($ccflags =~ /(?:^|\s)-DPERL_DISABLE_PMC\b/) {
253 print "# .pmc files are ignored, so test that\n";
254 write_file_not_thing('krunch.pmc', '.pmc', $pmc_older);
b3008037 255 write_file('urkkk.pm', qq(print "ok $simple - urkkk.pm branch A\n"));
b0927e10
NC
256 write_file('whap.pmc', qq(die "This is not an expected error"));
257
258 print "# Sleeping for 2 seconds before creating some more files\n";
259 sleep 2;
260
b3008037 261 write_file('krunch.pm', qq(print "ok $pmc_older - krunch.pm branch A\n"));
b0927e10
NC
262 write_file_not_thing('urkkk.pmc', '.pmc', $simple);
263 write_file('whap.pm', qq(die "This is an expected error"));
264 } else {
265 print "# .pmc files should be loaded, so test that\n";
b3008037 266 write_file('krunch.pmc', qq(print "ok $pmc_older - krunch.pm branch B\n";));
b0927e10
NC
267 write_file_not_thing('urkkk.pm', '.pm', $simple);
268 write_file('whap.pmc', qq(die "This is an expected error"));
269
270 print "# Sleeping for 2 seconds before creating some more files\n";
271 sleep 2;
272
273 write_file_not_thing('krunch.pm', '.pm', $pmc_older);
b3008037 274 write_file('urkkk.pmc', qq(print "ok $simple - urkkk.pm branch B\n";));
b0927e10
NC
275 write_file_not_thing('whap.pm', '.pm', $pmc_dies);
276 }
277 require urkkk;
278 require krunch;
279 eval {CORE::require whap; 1} and die;
280
281 if ($@ =~ /^This is an expected error/) {
b3008037 282 print "ok $pmc_dies - pmc_dies\n";
b0927e10 283 } else {
b3008037 284 print "not ok $pmc_dies - pmc_dies\n";
b0927e10
NC
285 }
286}
287
b2da7ead
DM
288
289{
290 # if we 'require "op"', since we're in the t/ directory and '.' is the
291 # first thing in @INC, it will try to load t/op/; it should fail and
292 # move onto the next path; however, the previous value of $! was
293 # leaking into implementation if it was EACCES and we're accessing a
294 # directory.
295
296 $! = eval 'use Errno qw(EACCES); EACCES' || 0;
297 eval q{require 'op'};
298 $i++;
299 print "not " if $@ =~ /Permission denied/;
300 print "ok $i - require op\n";
301}
302
eb70bb4a
FC
303# Test "require func()" with abs path when there is no .pmc file.
304++$::i;
3712c77b
TC
305if (defined &DynaLoader::boot_DynaLoader) {
306 require Cwd;
307 require File::Spec::Functions;
308 eval {
309 CORE::require(File::Spec::Functions::catfile(Cwd::getcwd(),"bleah.pm"));
310 };
311 if ($@ =~ /^This is an expected error/) {
b3008037 312 print "ok $i - require(func())\n";
3712c77b 313 } else {
b3008037 314 print "not ok $i - require(func())\n";
3712c77b 315 }
eb70bb4a 316} else {
3712c77b 317 print "ok $i # SKIP Cwd may not be available in miniperl\n";
eb70bb4a
FC
318}
319
6406c527
FC
320{
321 BEGIN { ${^OPEN} = ":utf8\0"; }
322 %INC = ();
323 write_file('bleah.pm',"package F; \$x = '\xD1\x9E';\n");
324 eval { require "bleah.pm" };
325 $i++;
326 my $not = $F::x eq "\xD1\x9E" ? "" : "not ";
327 print "${not}ok $i - require ignores I/O layers\n";
328}
329
f45b078d
FC
330{
331 BEGIN { ${^OPEN} = ":utf8\0"; }
332 %INC = ();
333 write_file('bleah.pm',"require re; re->import('/x'); 1;\n");
334 my $not = eval 'use bleah; "ab" =~ /a b/' ? "" : "not ";
335 $i++;
336 print "${not}ok $i - require does not localise %^H at run time\n";
337}
eb70bb4a 338
d20ea72b
RGS
339##########################################
340# What follows are UTF-8 specific tests. #
341# Add generic tests before this point. #
342##########################################
2d6f15ea 343
8a220075 344# UTF-encoded things - skipped on EBCDIC machines and on UTF-8 input
d2f5bb60 345
8a220075 346if ($Is_EBCDIC || $Is_UTF8) { exit; }
d2f5bb60 347
b6357110 348my %templates = (
ee6ba15d
EB
349 'UTF-8' => 'C0U',
350 'UTF-16BE' => 'n',
351 'UTF-16LE' => 'v',
b6357110
NC
352 );
353
354sub bytes_to_utf {
355 my ($enc, $content, $do_bom) = @_;
356 my $template = $templates{$enc};
357 die "Unsupported encoding $enc" unless $template;
358 return pack "$template*", ($do_bom ? 0xFEFF : ()), unpack "C*", $content;
359}
dea0fc0b 360
b6357110 361foreach (sort keys %templates) {
91229223 362 $i++; do_require(bytes_to_utf($_, qq(print "ok $i # $_\\n"; 1;\n), 1));
ee6ba15d
EB
363 if ($@ =~ /^(Unsupported script encoding \Q$_\E)/) {
364 print "ok $i # skip $1\n";
365 }
dea0fc0b
JH
366}
367
4d8b06f1 368END {
c106f444 369 foreach my $file (@files_to_delete) {
b0927e10
NC
370 1 while unlink $file;
371 }
4d8b06f1 372}
4a9ae47a
GS
373
374# ***interaction with pod (don't put any thing after here)***
375
376=pod