This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In taint.t, add violates_taint(), to replace a repeated is()/like() pair.
[perl5.git] / t / op / inccode.t
CommitLineData
69026470 1#!./perl -w
e5d18500
AMS
2
3# Tests for the coderef-in-@INC feature
4
240b6766
AT
5use Config;
6
7my $can_fork = 0;
8my $minitest = $ENV{PERL_CORE_MINITEST};
9my $has_perlio = $Config{useperlio};
ab742322 10
e5d18500 11BEGIN {
f8973f08 12 chdir 't' if -d 't';
69026470 13 @INC = qw(. ../lib);
e5d18500 14}
ab742322
RB
15
16if (!$minitest) {
240b6766 17 if ($Config{d_fork} && eval 'require POSIX; 1') {
6b75eab3
NC
18 $can_fork = 1;
19 }
20}
f8973f08 21
6b75eab3 22use strict;
69026470
JH
23
24require "test.pl";
6e592b3a 25plan(tests => 49 + !$minitest * (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{
7b903762 198 my $filename = './Foo.pm';
c38a6530
RD
199 #local @INC; # local fails on tied @INC
200 my @old_INC = @INC; # because local doesn't work on tied arrays
ab742322
RB
201 @INC = sub { $filename = 'seen'; return undef; };
202 eval { require $filename; };
203 is( $filename, 'seen', 'the coderef sees fully-qualified pathnames' );
c38a6530 204 @INC = @old_INC;
ab742322
RB
205}
206
6e592b3a
BM
207# this will segfault if it fails
208
209sub PVBM () { 'foo' }
210{ my $dummy = index 'foo', PVBM }
211
212# I don't know whether these requires should succeed or fail. 5.8 failed
213# all of them; 5.10 with an ordinary constant in place of PVBM lets the
214# latter two succeed. For now I don't care, as long as they don't
215# segfault :).
216
217unshift @INC, sub { PVBM };
218eval 'require foo';
219ok( 1, 'returning PVBM doesn\'t segfault require' );
220eval 'use foo';
221ok( 1, 'returning PVBM doesn\'t segfault use' );
222shift @INC;
223unshift @INC, sub { \PVBM };
224eval 'require foo';
225ok( 1, 'returning PVBM ref doesn\'t segfault require' );
226eval 'use foo';
227ok( 1, 'returning PVBM ref doesn\'t segfault use' );
228shift @INC;
229
ab742322
RB
230exit if $minitest;
231
240b6766
AT
232SKIP: {
233 skip( "No PerlIO available", 3 ) unless $has_perlio;
234 pop @INC;
235
236 push @INC, sub {
237 my ($cr, $filename) = @_;
238 my $module = $filename; $module =~ s,/,::,g; $module =~ s/\.pm$//;
239 open my $fh, '<',
240 \"package $module; sub complain { warn q() }; \$::file = __FILE__;"
241 or die $!;
242 $INC{$filename} = "/custom/path/to/$filename";
243 return $fh;
244 };
245
246 require Publius::Vergilius::Maro;
247 is( $INC{'Publius/Vergilius/Maro.pm'},
248 '/custom/path/to/Publius/Vergilius/Maro.pm', '%INC set correctly');
249 is( our $file, '/custom/path/to/Publius/Vergilius/Maro.pm',
250 '__FILE__ set correctly' );
251 {
252 my $warning;
253 local $SIG{__WARN__} = sub { $warning = shift };
254 Publius::Vergilius::Maro::complain();
255 like( $warning, qr{something's wrong at /custom/path/to/Publius/Vergilius/Maro.pm}, 'warn() reports correct file source' );
256 }
a3b58a99 257}
a3b58a99
RGS
258pop @INC;
259
6b75eab3
NC
260if ($can_fork) {
261 require PerlIO::scalar;
262 # This little bundle of joy generates n more recursive use statements,
263 # with each module chaining the next one down to 0. If it works, then we
264 # can safely nest subprocesses
265 my $use_filter_too;
266 push @INC, sub {
267 return unless $_[1] =~ /^BBBLPLAST(\d+)\.pm/;
268 my $pid = open my $fh, "-|";
269 if ($pid) {
270 # Parent
271 return $fh unless $use_filter_too;
272 # Try filters and state in addition.
273 return ($fh, sub {s/$_[1]/pass/; return}, "die")
274 }
275 die "Can't fork self: $!" unless defined $pid;
276
277 # Child
278 my $count = $1;
279 # Lets force some fun with odd sized reads.
280 $| = 1;
281 print 'push @main::bbblplast, ';
282 print "$count;\n";
283 if ($count--) {
284 print "use BBBLPLAST$count;\n";
285 }
286 if ($use_filter_too) {
287 print "die('In $_[1]');";
288 } else {
289 print "pass('In $_[1]');";
290 }
291 print '"Truth"';
292 POSIX::_exit(0);
293 die "Can't get here: $!";
294 };
295
296 @::bbblplast = ();
297 require BBBLPLAST5;
298 is ("@::bbblplast", "0 1 2 3 4 5", "All ran");
299
300 foreach (keys %INC) {
301 delete $INC{$_} if /^BBBLPLAST/;
302 }
303
304 @::bbblplast = ();
305 $use_filter_too = 1;
306
307 require BBBLPLAST5;
308
309 is ("@::bbblplast", "0 1 2 3 4 5", "All ran with a filter");
310}