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