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