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