This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Refactor inccode.t to use is_miniperl() instead of $ENV{PERL_CORE_MINITEST}
[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';
69026470 7 @INC = qw(. ../lib);
9c123506 8 require './test.pl';
e5d18500 9}
ab742322 10
9c123506
NC
11use Config;
12
13my $can_fork = 0;
14my $has_perlio = $Config{useperlio};
15
16unless (is_miniperl()) {
240b6766 17 if ($Config{d_fork} && eval 'require POSIX; 1') {
6b75eab3
NC
18 $can_fork = 1;
19 }
20}
f8973f08 21
6b75eab3 22use strict;
69026470 23
9c123506 24plan(tests => 49 + !is_miniperl() * (3 + 14 * $can_fork));
47de4e93
RGS
25
26sub get_temp_fh {
1c25d394 27 my $f = tempfile();
22e2837f 28 open my $fh, ">$f" or die "Can't create $f: $!";
3a5db825
GA
29 print $fh "package ".substr($_[0],0,-3).";\n1;\n";
30 print $fh $_[1] if @_ > 1;
d1e4d418 31 close $fh or die "Couldn't close: $!";
47de4e93
RGS
32 open $fh, $f or die "Can't open $f: $!";
33 return $fh;
34}
f8973f08 35
e5d18500
AMS
36sub fooinc {
37 my ($self, $filename) = @_;
38 if (substr($filename,0,3) eq 'Foo') {
47de4e93 39 return get_temp_fh($filename);
e5d18500
AMS
40 }
41 else {
f8973f08 42 return undef;
e5d18500
AMS
43 }
44}
45
46push @INC, \&fooinc;
47
d1e4d418
A
48my $evalret = eval { require Bar; 1 };
49ok( !$evalret, 'Trying non-magic package' );
50
51$evalret = eval { require Foo; 1 };
52die $@ if $@;
53ok( $evalret, 'require Foo; magic via code ref' );
54ok( exists $INC{'Foo.pm'}, ' %INC sees Foo.pm' );
55is( ref $INC{'Foo.pm'}, 'CODE', ' val Foo.pm is a coderef in %INC' );
56is( $INC{'Foo.pm'}, \&fooinc, ' val Foo.pm is correct in %INC' );
57
58$evalret = eval "use Foo1; 1;";
59die $@ if $@;
60ok( $evalret, 'use Foo1' );
61ok( exists $INC{'Foo1.pm'}, ' %INC sees Foo1.pm' );
62is( ref $INC{'Foo1.pm'}, 'CODE', ' val Foo1.pm is a coderef in %INC' );
63is( $INC{'Foo1.pm'}, \&fooinc, ' val Foo1.pm is correct in %INC' );
64
65$evalret = eval { do 'Foo2.pl'; 1 };
66die $@ if $@;
67ok( $evalret, 'do "Foo2.pl"' );
68ok( exists $INC{'Foo2.pl'}, ' %INC sees Foo2.pl' );
69is( ref $INC{'Foo2.pl'}, 'CODE', ' val Foo2.pl is a coderef in %INC' );
70is( $INC{'Foo2.pl'}, \&fooinc, ' val Foo2.pl is correct in %INC' );
e5d18500
AMS
71
72pop @INC;
73
f8973f08 74
e5d18500
AMS
75sub fooinc2 {
76 my ($self, $filename) = @_;
77 if (substr($filename, 0, length($self->[1])) eq $self->[1]) {
47de4e93 78 return get_temp_fh($filename);
e5d18500
AMS
79 }
80 else {
f8973f08 81 return undef;
e5d18500
AMS
82 }
83}
84
47de4e93
RGS
85my $arrayref = [ \&fooinc2, 'Bar' ];
86push @INC, $arrayref;
e5d18500 87
d1e4d418
A
88$evalret = eval { require Foo; 1; };
89die $@ if $@;
90ok( $evalret, 'Originally loaded packages preserved' );
91$evalret = eval { require Foo3; 1; };
92ok( !$evalret, 'Original magic INC purged' );
93
94$evalret = eval { require Bar; 1 };
95die $@ if $@;
96ok( $evalret, 'require Bar; magic via array ref' );
97ok( exists $INC{'Bar.pm'}, ' %INC sees Bar.pm' );
98is( ref $INC{'Bar.pm'}, 'ARRAY', ' val Bar.pm is an arrayref in %INC' );
99is( $INC{'Bar.pm'}, $arrayref, ' val Bar.pm is correct in %INC' );
100
101ok( eval "use Bar1; 1;", 'use Bar1' );
102ok( exists $INC{'Bar1.pm'}, ' %INC sees Bar1.pm' );
103is( ref $INC{'Bar1.pm'}, 'ARRAY', ' val Bar1.pm is an arrayref in %INC' );
104is( $INC{'Bar1.pm'}, $arrayref, ' val Bar1.pm is correct in %INC' );
105
106ok( eval { do 'Bar2.pl'; 1 }, 'do "Bar2.pl"' );
107ok( exists $INC{'Bar2.pl'}, ' %INC sees Bar2.pl' );
108is( ref $INC{'Bar2.pl'}, 'ARRAY', ' val Bar2.pl is an arrayref in %INC' );
109is( $INC{'Bar2.pl'}, $arrayref, ' val Bar2.pl is correct in %INC' );
e5d18500
AMS
110
111pop @INC;
112
113sub FooLoader::INC {
114 my ($self, $filename) = @_;
115 if (substr($filename,0,4) eq 'Quux') {
47de4e93 116 return get_temp_fh($filename);
e5d18500
AMS
117 }
118 else {
f8973f08 119 return undef;
e5d18500
AMS
120 }
121}
122
47de4e93
RGS
123my $href = bless( {}, 'FooLoader' );
124push @INC, $href;
e5d18500 125
d1e4d418
A
126$evalret = eval { require Quux; 1 };
127die $@ if $@;
128ok( $evalret, 'require Quux; magic via hash object' );
129ok( exists $INC{'Quux.pm'}, ' %INC sees Quux.pm' );
6ece0f6b 130is( ref $INC{'Quux.pm'}, 'FooLoader',
d1e4d418
A
131 ' val Quux.pm is an object in %INC' );
132is( $INC{'Quux.pm'}, $href, ' val Quux.pm is correct in %INC' );
e5d18500
AMS
133
134pop @INC;
135
47de4e93
RGS
136my $aref = bless( [], 'FooLoader' );
137push @INC, $aref;
e5d18500 138
d1e4d418
A
139$evalret = eval { require Quux1; 1 };
140die $@ if $@;
141ok( $evalret, 'require Quux1; magic via array object' );
142ok( exists $INC{'Quux1.pm'}, ' %INC sees Quux1.pm' );
6ece0f6b 143is( ref $INC{'Quux1.pm'}, 'FooLoader',
d1e4d418
A
144 ' val Quux1.pm is an object in %INC' );
145is( $INC{'Quux1.pm'}, $aref, ' val Quux1.pm is correct in %INC' );
e5d18500
AMS
146
147pop @INC;
148
47de4e93
RGS
149my $sref = bless( \(my $x = 1), 'FooLoader' );
150push @INC, $sref;
e5d18500 151
d1e4d418
A
152$evalret = eval { require Quux2; 1 };
153die $@ if $@;
154ok( $evalret, 'require Quux2; magic via scalar object' );
155ok( exists $INC{'Quux2.pm'}, ' %INC sees Quux2.pm' );
6ece0f6b 156is( ref $INC{'Quux2.pm'}, 'FooLoader',
d1e4d418
A
157 ' val Quux2.pm is an object in %INC' );
158is( $INC{'Quux2.pm'}, $sref, ' val Quux2.pm is correct in %INC' );
f8973f08
MS
159
160pop @INC;
9ae8cd5b
RGS
161
162push @INC, sub {
163 my ($self, $filename) = @_;
164 if (substr($filename,0,4) eq 'Toto') {
165 $INC{$filename} = 'xyz';
166 return get_temp_fh($filename);
167 }
168 else {
169 return undef;
170 }
171};
172
d1e4d418
A
173$evalret = eval { require Toto; 1 };
174die $@ if $@;
175ok( $evalret, 'require Toto; magic via anonymous code ref' );
176ok( exists $INC{'Toto.pm'}, ' %INC sees Toto.pm' );
177ok( ! ref $INC{'Toto.pm'}, q/ val Toto.pm isn't a ref in %INC/ );
178is( $INC{'Toto.pm'}, 'xyz', ' val Toto.pm is correct in %INC' );
9ae8cd5b
RGS
179
180pop @INC;
d820be44 181
3a5db825
GA
182push @INC, sub {
183 my ($self, $filename) = @_;
184 if ($filename eq 'abc.pl') {
185 return get_temp_fh($filename, qq(return "abc";\n));
186 }
187 else {
188 return undef;
189 }
190};
191
6b75eab3 192my $ret = "";
3a5db825
GA
193$ret ||= do 'abc.pl';
194is( $ret, 'abc', 'do "abc.pl" sees return value' );
195
ab742322 196{
7b903762 197 my $filename = './Foo.pm';
c38a6530
RD
198 #local @INC; # local fails on tied @INC
199 my @old_INC = @INC; # because local doesn't work on tied arrays
ab742322
RB
200 @INC = sub { $filename = 'seen'; return undef; };
201 eval { require $filename; };
202 is( $filename, 'seen', 'the coderef sees fully-qualified pathnames' );
c38a6530 203 @INC = @old_INC;
ab742322
RB
204}
205
6e592b3a
BM
206# this will segfault if it fails
207
208sub PVBM () { 'foo' }
209{ my $dummy = index 'foo', PVBM }
210
211# I don't know whether these requires should succeed or fail. 5.8 failed
212# all of them; 5.10 with an ordinary constant in place of PVBM lets the
213# latter two succeed. For now I don't care, as long as they don't
214# segfault :).
215
216unshift @INC, sub { PVBM };
217eval 'require foo';
218ok( 1, 'returning PVBM doesn\'t segfault require' );
219eval 'use foo';
220ok( 1, 'returning PVBM doesn\'t segfault use' );
221shift @INC;
222unshift @INC, sub { \PVBM };
223eval 'require foo';
224ok( 1, 'returning PVBM ref doesn\'t segfault require' );
225eval 'use foo';
226ok( 1, 'returning PVBM ref doesn\'t segfault use' );
227shift @INC;
228
9c123506 229exit if is_miniperl();
ab742322 230
240b6766
AT
231SKIP: {
232 skip( "No PerlIO available", 3 ) unless $has_perlio;
233 pop @INC;
234
235 push @INC, sub {
236 my ($cr, $filename) = @_;
237 my $module = $filename; $module =~ s,/,::,g; $module =~ s/\.pm$//;
238 open my $fh, '<',
239 \"package $module; sub complain { warn q() }; \$::file = __FILE__;"
240 or die $!;
241 $INC{$filename} = "/custom/path/to/$filename";
242 return $fh;
243 };
244
245 require Publius::Vergilius::Maro;
246 is( $INC{'Publius/Vergilius/Maro.pm'},
247 '/custom/path/to/Publius/Vergilius/Maro.pm', '%INC set correctly');
248 is( our $file, '/custom/path/to/Publius/Vergilius/Maro.pm',
249 '__FILE__ set correctly' );
250 {
251 my $warning;
252 local $SIG{__WARN__} = sub { $warning = shift };
253 Publius::Vergilius::Maro::complain();
254 like( $warning, qr{something's wrong at /custom/path/to/Publius/Vergilius/Maro.pm}, 'warn() reports correct file source' );
255 }
a3b58a99 256}
a3b58a99
RGS
257pop @INC;
258
6b75eab3
NC
259if ($can_fork) {
260 require PerlIO::scalar;
261 # This little bundle of joy generates n more recursive use statements,
262 # with each module chaining the next one down to 0. If it works, then we
263 # can safely nest subprocesses
264 my $use_filter_too;
265 push @INC, sub {
266 return unless $_[1] =~ /^BBBLPLAST(\d+)\.pm/;
267 my $pid = open my $fh, "-|";
268 if ($pid) {
269 # Parent
270 return $fh unless $use_filter_too;
271 # Try filters and state in addition.
272 return ($fh, sub {s/$_[1]/pass/; return}, "die")
273 }
274 die "Can't fork self: $!" unless defined $pid;
275
276 # Child
277 my $count = $1;
278 # Lets force some fun with odd sized reads.
279 $| = 1;
280 print 'push @main::bbblplast, ';
281 print "$count;\n";
282 if ($count--) {
283 print "use BBBLPLAST$count;\n";
284 }
285 if ($use_filter_too) {
286 print "die('In $_[1]');";
287 } else {
288 print "pass('In $_[1]');";
289 }
290 print '"Truth"';
291 POSIX::_exit(0);
292 die "Can't get here: $!";
293 };
294
295 @::bbblplast = ();
296 require BBBLPLAST5;
297 is ("@::bbblplast", "0 1 2 3 4 5", "All ran");
298
299 foreach (keys %INC) {
300 delete $INC{$_} if /^BBBLPLAST/;
301 }
302
303 @::bbblplast = ();
304 $use_filter_too = 1;
305
306 require BBBLPLAST5;
307
308 is ("@::bbblplast", "0 1 2 3 4 5", "All ran with a filter");
309}