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