xref: /openbsd-src/gnu/usr.bin/perl/t/comp/require.t (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    @INC = '.';
6    push @INC, '../lib';
7}
8
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
16# don't make this lexical
17$i = 1;
18
19my @files_to_delete = qw (bleah.pm bleah.do bleah.flg urkkk.pm urkkk.pmc
20krunch.pm krunch.pmc whap.pm whap.pmc);
21
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
34
35my $Is_EBCDIC = (ord('A') == 193) ? 1 : 0;
36my $Is_UTF8   = (${^OPEN} || "") =~ /:utf8/;
37my $total_tests = 56;
38if ($Is_EBCDIC || $Is_UTF8) { $total_tests -= 3; }
39print "1..$total_tests\n";
40
41sub write_file {
42    my $f = shift;
43    open(REQ,">$f") or die "Can't write '$f': $!";
44    binmode REQ;
45    print REQ @_;
46    close REQ or die "Could not close $f: $!";
47}
48
49eval {require 5.005};
50print "# $@\nnot " if $@;
51print "ok ",$i++," - require 5.005 try 1\n";
52
53eval { require 5.005 };
54print "# $@\nnot " if $@;
55print "ok ",$i++," - require 5.005 try 2\n";
56
57eval { require 5.005; };
58print "# $@\nnot " if $@;
59print "ok ",$i++," - require 5.005 try 3\n";
60
61eval {
62    require 5.005
63};
64print "# $@\nnot " if $@;
65print "ok ",$i++," - require 5.005 try 4\n";
66
67# new style version numbers
68
69eval { require v5.5.630; };
70print "# $@\nnot " if $@;
71print "ok ",$i++," - require 5.5.630\n";
72
73sub v5 { die }
74eval { require v5; };
75print "# $@\nnot " if $@;
76print "ok ",$i++," - require v5 ignores sub named v5\n";
77
78eval { require 10.0.2; };
79print "# $@\nnot " unless $@ =~ /^Perl v10\.0\.2 required/;
80print "ok ",$i++," - require 10.0.2\n";
81
82my $ver = 5.005_63;
83eval { require $ver; };
84print "# $@\nnot " if $@;
85print "ok ",$i++," - require 5.005_63\n";
86
87# check inaccurate fp
88$ver = 10.2;
89eval { require $ver; };
90print "# $@\nnot " unless $@ =~ /^Perl v10\.200.0 required/;
91print "ok ",$i++," - require 10.2\n";
92
93$ver = 10.000_02;
94eval { require $ver; };
95print "# $@\nnot " unless $@ =~ /^Perl v10\.0\.20 required/;
96print "ok ",$i++," - require 10.000_02\n";
97
98print "not " unless 5.5.1 gt v5.5;
99print "ok ",$i++," - 5.5.1 gt v5.5\n";
100
101{
102    print "not " unless v5.5.640 eq "\x{5}\x{5}\x{280}";
103    print "ok ",$i++," - v5.5.640 eq \\x{5}\\x{5}\\x{280}\n";
104
105    print "not " unless v7.15 eq "\x{7}\x{f}";
106    print "ok ",$i++," - v7.15 eq \\x{7}\\x{f}\n";
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}";
110    print "ok ",$i++," - v1.20.300.4000.50000.600000 eq ...\n";
111}
112
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 $@;
117print "ok ",$i++," - require 5.11.0\n";
118eval 'BEGIN {require 5.11.0} ${"foo"} = "bar";';
119print "# $@\nnot " if $@;
120print "ok ",$i++,"\ - BEGIN { require 5.11.0}\n";
121
122# interaction with pod (see the eof)
123write_file('bleah.pm', "print 'ok $i - require bleah.pm\n'; 1;\n");
124require "bleah.pm";
125$i++;
126
127# run-time failure in require
128do_require "0;\n";
129print "# $@\nnot " unless $@ =~ /did not return a true/;
130print "ok ",$i++," - require returning 0\n";
131
132print "not " if exists $INC{'bleah.pm'};
133print "ok ",$i++," - %INC not updated\n";
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;
140    print "ok ",$i++," - exp $expected_compile; bleah.flg\n";
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' };
143    print "ok ",$i++," - exp $expected_compile; require bleah.pm with flag file\n";
144    print "not " unless -e $flag_file xor $expected_compile;
145    print "ok ",$i++," - exp $expected_compile; -e flag_file\n";
146    print "not " unless exists $INC{'bleah.pm'};
147    print "ok ",$i++," - exp $expected_compile; exists \$INC{'bleah.pm}\n";
148}
149
150# compile-time failure in require
151do_require "1)\n";
152# bison says 'parse error' instead of 'syntax error',
153# various yaccs may or may not capitalize 'syntax'.
154print "# $@\nnot " unless $@ =~ /(syntax|parse) error/mi;
155print "ok ",$i++," - syntax error\n";
156
157# previous failure cached in %INC
158print "not " unless exists $INC{'bleah.pm'};
159print "ok ",$i++," - cached %INC\n";
160write_file($flag_file, 1);
161write_file('bleah.pm', "unlink '$flag_file'; 1");
162print "# $@\nnot " if eval { require 'bleah.pm' };
163print "ok ",$i++," - eval { require 'bleah.pm' }\n";
164print "# $@\nnot " unless $@ =~ /Compilation failed/i;
165print "ok ",$i++," - Compilation failed\n";
166print "not " unless -e $flag_file;
167print "ok ",$i++," - -e flag_file\n";
168print "not " unless exists $INC{'bleah.pm'};
169print "ok ",$i++," - \$INC{'bleah.pm'}\n";
170
171# successful require
172do_require "1";
173print "# $@\nnot " if $@;
174print "ok ",$i++," - do_require '1';\n";
175
176# do FILE shouldn't see any outside lexicals
177my $x = "ok $i - bleah.do\n";
178write_file("bleah.do", <<EOT);
179\$x = "not ok $i - bleah.do\\n";
180EOT
181do "bleah.do" or die $@;
182dofile();
183sub dofile { do "bleah.do" or die $@; };
184print $x;
185
186# Test that scalar context is forced for require
187
188write_file('bleah.pm', <<'**BLEAH**'
189print "not " if !defined wantarray || wantarray ne '';
190print "ok $i - require() context\n";
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;
198       eval q{$_=$_+2;require bleah}; delete $INC{"bleah.pm"}; ++$::i;
199       eval q{return require bleah}; delete $INC{"bleah.pm"}; ++$::i;
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
204# Test for fix of RT #24404 : "require $scalar" may load a directory
205my $r = "threads";
206eval { require $r };
207$i++;
208if($@ =~ /Can't locate threads in \@INC/) {
209    print "ok $i - RT #24404\n";
210} else {
211    print "not ok - RT #24404$i\n";
212}
213
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
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/) {
228    print "ok $i - expected error\n";
229} else {
230    print "not ok $i - expected error\n";
231}
232
233sub write_file_not_thing {
234    my ($file, $thing, $test) = @_;
235    write_file($file, <<"EOT");
236    print "not ok $test - write_file_not_thing $file\n";
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);
255	write_file('urkkk.pm', qq(print "ok $simple - urkkk.pm branch A\n"));
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
261	write_file('krunch.pm', qq(print "ok $pmc_older - krunch.pm branch A\n"));
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";
266	write_file('krunch.pmc', qq(print "ok $pmc_older - krunch.pm branch B\n";));
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);
274	write_file('urkkk.pmc', qq(print "ok $simple - urkkk.pm branch B\n";));
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/) {
282	print "ok $pmc_dies - pmc_dies\n";
283    } else {
284	print "not ok $pmc_dies - pmc_dies\n";
285    }
286}
287
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
303# Test "require func()" with abs path when there is no .pmc file.
304++$::i;
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/) {
312	print "ok $i - require(func())\n";
313    } else {
314	print "not ok $i - require(func())\n";
315    }
316} else {
317    print "ok $i # SKIP Cwd may not be available in miniperl\n";
318}
319
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
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}
338
339##########################################
340# What follows are UTF-8 specific tests. #
341# Add generic tests before this point.   #
342##########################################
343
344# UTF-encoded things - skipped on EBCDIC machines and on UTF-8 input
345
346if ($Is_EBCDIC || $Is_UTF8) { exit; }
347
348my %templates = (
349		 'UTF-8'    => 'C0U',
350		 'UTF-16BE' => 'n',
351		 'UTF-16LE' => 'v',
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}
360
361foreach (sort keys %templates) {
362    $i++; do_require(bytes_to_utf($_, qq(print "ok $i # $_\\n"; 1;\n), 1));
363    if ($@ =~ /^(Unsupported script encoding \Q$_\E)/) {
364	print "ok $i # skip $1\n";
365    }
366}
367
368END {
369    foreach my $file (@files_to_delete) {
370	1 while unlink $file;
371    }
372}
373
374# ***interaction with pod (don't put any thing after here)***
375
376=pod
377