This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Get inccode.t working under miniperl
[perl5.git] / t / op / inccode.t
CommitLineData
69026470 1#!./perl -w
e5d18500
AMS
2
3# Tests for the coderef-in-@INC feature
4
5BEGIN {
f8973f08 6 chdir 't' if -d 't';
9c123506 7 require './test.pl';
a5dd241d
FC
8 @INC = () unless is_miniperl();
9 unshift @INC, '../lib';
e5d18500 10}
ab742322 11
9c123506
NC
12use Config;
13
14my $can_fork = 0;
15my $has_perlio = $Config{useperlio};
16
17unless (is_miniperl()) {
240b6766 18 if ($Config{d_fork} && eval 'require POSIX; 1') {
6b75eab3
NC
19 $can_fork = 1;
20 }
21}
f8973f08 22
6b75eab3 23use strict;
69026470 24
6567ce24 25plan(tests => 68 + !is_miniperl() * (3 + 14 * $can_fork));
47de4e93
RGS
26
27sub get_temp_fh {
1c25d394 28 my $f = tempfile();
22e2837f 29 open my $fh, ">$f" or die "Can't create $f: $!";
3a5db825
GA
30 print $fh "package ".substr($_[0],0,-3).";\n1;\n";
31 print $fh $_[1] if @_ > 1;
d1e4d418 32 close $fh or die "Couldn't close: $!";
47de4e93
RGS
33 open $fh, $f or die "Can't open $f: $!";
34 return $fh;
35}
f8973f08 36
e5d18500
AMS
37sub fooinc {
38 my ($self, $filename) = @_;
39 if (substr($filename,0,3) eq 'Foo') {
47de4e93 40 return get_temp_fh($filename);
e5d18500
AMS
41 }
42 else {
f8973f08 43 return undef;
e5d18500
AMS
44 }
45}
46
47push @INC, \&fooinc;
48
d1e4d418
A
49my $evalret = eval { require Bar; 1 };
50ok( !$evalret, 'Trying non-magic package' );
51
52$evalret = eval { require Foo; 1 };
53die $@ if $@;
54ok( $evalret, 'require Foo; magic via code ref' );
55ok( exists $INC{'Foo.pm'}, ' %INC sees Foo.pm' );
56is( ref $INC{'Foo.pm'}, 'CODE', ' val Foo.pm is a coderef in %INC' );
57is( $INC{'Foo.pm'}, \&fooinc, ' val Foo.pm is correct in %INC' );
58
59$evalret = eval "use Foo1; 1;";
60die $@ if $@;
61ok( $evalret, 'use Foo1' );
62ok( exists $INC{'Foo1.pm'}, ' %INC sees Foo1.pm' );
63is( ref $INC{'Foo1.pm'}, 'CODE', ' val Foo1.pm is a coderef in %INC' );
64is( $INC{'Foo1.pm'}, \&fooinc, ' val Foo1.pm is correct in %INC' );
65
66$evalret = eval { do 'Foo2.pl'; 1 };
67die $@ if $@;
68ok( $evalret, 'do "Foo2.pl"' );
69ok( exists $INC{'Foo2.pl'}, ' %INC sees Foo2.pl' );
70is( ref $INC{'Foo2.pl'}, 'CODE', ' val Foo2.pl is a coderef in %INC' );
71is( $INC{'Foo2.pl'}, \&fooinc, ' val Foo2.pl is correct in %INC' );
e5d18500
AMS
72
73pop @INC;
74
f8973f08 75
e5d18500
AMS
76sub fooinc2 {
77 my ($self, $filename) = @_;
78 if (substr($filename, 0, length($self->[1])) eq $self->[1]) {
47de4e93 79 return get_temp_fh($filename);
e5d18500
AMS
80 }
81 else {
f8973f08 82 return undef;
e5d18500
AMS
83 }
84}
85
47de4e93
RGS
86my $arrayref = [ \&fooinc2, 'Bar' ];
87push @INC, $arrayref;
e5d18500 88
d1e4d418
A
89$evalret = eval { require Foo; 1; };
90die $@ if $@;
91ok( $evalret, 'Originally loaded packages preserved' );
92$evalret = eval { require Foo3; 1; };
93ok( !$evalret, 'Original magic INC purged' );
94
95$evalret = eval { require Bar; 1 };
96die $@ if $@;
97ok( $evalret, 'require Bar; magic via array ref' );
98ok( exists $INC{'Bar.pm'}, ' %INC sees Bar.pm' );
99is( ref $INC{'Bar.pm'}, 'ARRAY', ' val Bar.pm is an arrayref in %INC' );
100is( $INC{'Bar.pm'}, $arrayref, ' val Bar.pm is correct in %INC' );
101
102ok( eval "use Bar1; 1;", 'use Bar1' );
103ok( exists $INC{'Bar1.pm'}, ' %INC sees Bar1.pm' );
104is( ref $INC{'Bar1.pm'}, 'ARRAY', ' val Bar1.pm is an arrayref in %INC' );
105is( $INC{'Bar1.pm'}, $arrayref, ' val Bar1.pm is correct in %INC' );
106
107ok( eval { do 'Bar2.pl'; 1 }, 'do "Bar2.pl"' );
108ok( exists $INC{'Bar2.pl'}, ' %INC sees Bar2.pl' );
109is( ref $INC{'Bar2.pl'}, 'ARRAY', ' val Bar2.pl is an arrayref in %INC' );
110is( $INC{'Bar2.pl'}, $arrayref, ' val Bar2.pl is correct in %INC' );
e5d18500
AMS
111
112pop @INC;
113
114sub FooLoader::INC {
115 my ($self, $filename) = @_;
116 if (substr($filename,0,4) eq 'Quux') {
47de4e93 117 return get_temp_fh($filename);
e5d18500
AMS
118 }
119 else {
f8973f08 120 return undef;
e5d18500
AMS
121 }
122}
123
47de4e93
RGS
124my $href = bless( {}, 'FooLoader' );
125push @INC, $href;
e5d18500 126
d1e4d418
A
127$evalret = eval { require Quux; 1 };
128die $@ if $@;
129ok( $evalret, 'require Quux; magic via hash object' );
130ok( exists $INC{'Quux.pm'}, ' %INC sees Quux.pm' );
6ece0f6b 131is( ref $INC{'Quux.pm'}, 'FooLoader',
d1e4d418
A
132 ' val Quux.pm is an object in %INC' );
133is( $INC{'Quux.pm'}, $href, ' val Quux.pm is correct in %INC' );
e5d18500
AMS
134
135pop @INC;
136
47de4e93
RGS
137my $aref = bless( [], 'FooLoader' );
138push @INC, $aref;
e5d18500 139
d1e4d418
A
140$evalret = eval { require Quux1; 1 };
141die $@ if $@;
142ok( $evalret, 'require Quux1; magic via array object' );
143ok( exists $INC{'Quux1.pm'}, ' %INC sees Quux1.pm' );
6ece0f6b 144is( ref $INC{'Quux1.pm'}, 'FooLoader',
d1e4d418
A
145 ' val Quux1.pm is an object in %INC' );
146is( $INC{'Quux1.pm'}, $aref, ' val Quux1.pm is correct in %INC' );
e5d18500
AMS
147
148pop @INC;
149
47de4e93
RGS
150my $sref = bless( \(my $x = 1), 'FooLoader' );
151push @INC, $sref;
e5d18500 152
d1e4d418
A
153$evalret = eval { require Quux2; 1 };
154die $@ if $@;
155ok( $evalret, 'require Quux2; magic via scalar object' );
156ok( exists $INC{'Quux2.pm'}, ' %INC sees Quux2.pm' );
6ece0f6b 157is( ref $INC{'Quux2.pm'}, 'FooLoader',
d1e4d418
A
158 ' val Quux2.pm is an object in %INC' );
159is( $INC{'Quux2.pm'}, $sref, ' val Quux2.pm is correct in %INC' );
f8973f08
MS
160
161pop @INC;
9ae8cd5b
RGS
162
163push @INC, sub {
164 my ($self, $filename) = @_;
165 if (substr($filename,0,4) eq 'Toto') {
166 $INC{$filename} = 'xyz';
167 return get_temp_fh($filename);
168 }
169 else {
170 return undef;
171 }
172};
173
d1e4d418
A
174$evalret = eval { require Toto; 1 };
175die $@ if $@;
176ok( $evalret, 'require Toto; magic via anonymous code ref' );
177ok( exists $INC{'Toto.pm'}, ' %INC sees Toto.pm' );
178ok( ! ref $INC{'Toto.pm'}, q/ val Toto.pm isn't a ref in %INC/ );
179is( $INC{'Toto.pm'}, 'xyz', ' val Toto.pm is correct in %INC' );
9ae8cd5b
RGS
180
181pop @INC;
d820be44 182
3a5db825
GA
183push @INC, sub {
184 my ($self, $filename) = @_;
185 if ($filename eq 'abc.pl') {
186 return get_temp_fh($filename, qq(return "abc";\n));
187 }
188 else {
189 return undef;
190 }
191};
192
6b75eab3 193my $ret = "";
3a5db825
GA
194$ret ||= do 'abc.pl';
195is( $ret, 'abc', 'do "abc.pl" sees return value' );
196
ab742322 197{
e6c837f5 198 my $got;
c38a6530
RD
199 #local @INC; # local fails on tied @INC
200 my @old_INC = @INC; # because local doesn't work on tied arrays
e6c837f5
NC
201 @INC = ('lib', 'lib/Devel', sub { $got = $_[1]; return undef; });
202 foreach my $filename ('/test_require.pm', './test_require.pm',
203 '../test_require.pm') {
204 local %INC;
205 undef $got;
206 undef $test_require::loaded;
207 eval { require $filename; };
208 is($got, $filename, "the coderef sees the pathname $filename");
209 is($test_require::loaded, undef, 'no module is loaded' );
210 }
211
212 local %INC;
213 undef $got;
214 undef $test_require::loaded;
215
216 eval { require 'test_require.pm'; };
217 is($got, undef, 'the directory is scanned for test_require.pm');
218 is($test_require::loaded, 1, 'the module is loaded');
c38a6530 219 @INC = @old_INC;
ab742322
RB
220}
221
6e592b3a
BM
222# this will segfault if it fails
223
224sub PVBM () { 'foo' }
225{ my $dummy = index 'foo', PVBM }
226
227# I don't know whether these requires should succeed or fail. 5.8 failed
228# all of them; 5.10 with an ordinary constant in place of PVBM lets the
229# latter two succeed. For now I don't care, as long as they don't
230# segfault :).
231
232unshift @INC, sub { PVBM };
233eval 'require foo';
234ok( 1, 'returning PVBM doesn\'t segfault require' );
235eval 'use foo';
236ok( 1, 'returning PVBM doesn\'t segfault use' );
237shift @INC;
238unshift @INC, sub { \PVBM };
239eval 'require foo';
240ok( 1, 'returning PVBM ref doesn\'t segfault require' );
241eval 'use foo';
242ok( 1, 'returning PVBM ref doesn\'t segfault use' );
243shift @INC;
244
d60d2019
JL
245# [perl #92252]
246{
247 my $die = sub { die };
248 my $data = [];
249 unshift @INC, sub { $die, $data };
250
6429869b
JL
251 my $initial_sub_refcnt = &Internals::SvREFCNT($die);
252 my $initial_data_refcnt = &Internals::SvREFCNT($data);
d60d2019
JL
253
254 do "foo";
6429869b
JL
255 is(&Internals::SvREFCNT($die), $initial_sub_refcnt, "no leaks");
256 is(&Internals::SvREFCNT($data), $initial_data_refcnt, "no leaks");
d60d2019
JL
257
258 do "bar";
6429869b
JL
259 is(&Internals::SvREFCNT($die), $initial_sub_refcnt, "no leaks");
260 is(&Internals::SvREFCNT($data), $initial_data_refcnt, "no leaks");
d60d2019
JL
261
262 shift @INC;
263}
264
c39fcc09
FC
265unshift @INC, sub { \(my $tmp = '$_ = "are temps freed prematurely?"') };
266eval { require foom };
267is $_||$@, "are temps freed prematurely?",
268 "are temps freed prematurely when returned from inc filters?";
269shift @INC;
270
901ee108
FC
271# [perl #120657]
272sub fake_module {
273 my (undef,$module_file) = @_;
274 !1
275}
276{
277 local @INC = @INC;
278 unshift @INC, (\&fake_module)x2;
279 eval { require "${\'bralbalhablah'}" };
280 like $@, qr/^Can't locate/,
281 'require PADTMP passing freed var when @INC has multiple subs';\r
282}
283
6567ce24
FC
284SKIP: {
285 skip ("Not applicable when run from inccode-tie.t", 6) if tied @INC;
286 require Tie::Scalar;
287 package INCtie {
288 sub TIESCALAR { bless \my $foo }
289 sub FETCH { study; our $count++; ${$_[0]} }
290 }
291 local @INC = undef;
292 my $t = tie $INC[0], 'INCtie';
293 my $called;
294 $$t = sub { $called ++; !1 };
295 delete $INC{'foo.pm'}; # in case another test uses foo
296 eval { require foo };
297 is $INCtie::count, 2, # 2nd time for "Can't locate" -- XXX correct?
298 'FETCH is called once on undef scalar-tied @INC elem';
299 is $called, 1, 'sub in scalar-tied @INC elem is called';
300 () = "$INC[0]"; # force a fetch, so the SV is ROK
301 $INCtie::count = 0;
302 eval { require foo };
303 is $INCtie::count, 2,
304 'FETCH is called once on scalar-tied @INC elem holding ref';
305 is $called, 2, 'sub in scalar-tied @INC elem holding ref is called';
306 $$t = [];
307 $INCtie::count = 0;
308 eval { require foo };
309 is $INCtie::count, 1,
310 'FETCH called once on scalar-tied @INC elem returning array';
311 $$t = "string";
312 $INCtie::count = 0;
313 eval { require foo };
314 is $INCtie::count, 2,
315 'FETCH called once on scalar-tied @INC elem returning string';
316}
317
901ee108 318
9c123506 319exit if is_miniperl();
ab742322 320
240b6766
AT
321SKIP: {
322 skip( "No PerlIO available", 3 ) unless $has_perlio;
323 pop @INC;
324
325 push @INC, sub {
326 my ($cr, $filename) = @_;
327 my $module = $filename; $module =~ s,/,::,g; $module =~ s/\.pm$//;
328 open my $fh, '<',
329 \"package $module; sub complain { warn q() }; \$::file = __FILE__;"
330 or die $!;
331 $INC{$filename} = "/custom/path/to/$filename";
332 return $fh;
333 };
334
335 require Publius::Vergilius::Maro;
336 is( $INC{'Publius/Vergilius/Maro.pm'},
337 '/custom/path/to/Publius/Vergilius/Maro.pm', '%INC set correctly');
338 is( our $file, '/custom/path/to/Publius/Vergilius/Maro.pm',
339 '__FILE__ set correctly' );
340 {
341 my $warning;
342 local $SIG{__WARN__} = sub { $warning = shift };
343 Publius::Vergilius::Maro::complain();
344 like( $warning, qr{something's wrong at /custom/path/to/Publius/Vergilius/Maro.pm}, 'warn() reports correct file source' );
345 }
a3b58a99 346}
a3b58a99
RGS
347pop @INC;
348
6b75eab3
NC
349if ($can_fork) {
350 require PerlIO::scalar;
351 # This little bundle of joy generates n more recursive use statements,
352 # with each module chaining the next one down to 0. If it works, then we
353 # can safely nest subprocesses
354 my $use_filter_too;
355 push @INC, sub {
356 return unless $_[1] =~ /^BBBLPLAST(\d+)\.pm/;
357 my $pid = open my $fh, "-|";
358 if ($pid) {
359 # Parent
360 return $fh unless $use_filter_too;
361 # Try filters and state in addition.
362 return ($fh, sub {s/$_[1]/pass/; return}, "die")
363 }
364 die "Can't fork self: $!" unless defined $pid;
365
366 # Child
367 my $count = $1;
368 # Lets force some fun with odd sized reads.
369 $| = 1;
370 print 'push @main::bbblplast, ';
371 print "$count;\n";
372 if ($count--) {
373 print "use BBBLPLAST$count;\n";
374 }
375 if ($use_filter_too) {
376 print "die('In $_[1]');";
377 } else {
378 print "pass('In $_[1]');";
379 }
380 print '"Truth"';
381 POSIX::_exit(0);
382 die "Can't get here: $!";
383 };
384
385 @::bbblplast = ();
386 require BBBLPLAST5;
387 is ("@::bbblplast", "0 1 2 3 4 5", "All ran");
388
389 foreach (keys %INC) {
390 delete $INC{$_} if /^BBBLPLAST/;
391 }
392
393 @::bbblplast = ();
394 $use_filter_too = 1;
395
396 require BBBLPLAST5;
397
398 is ("@::bbblplast", "0 1 2 3 4 5", "All ran with a filter");
399}