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