Commit | Line | Data |
---|---|---|
69026470 | 1 | #!./perl -w |
e5d18500 AMS |
2 | |
3 | # Tests for the coderef-in-@INC feature | |
4 | ||
240b6766 AT |
5 | use Config; |
6 | ||
7 | my $can_fork = 0; | |
8 | my $minitest = $ENV{PERL_CORE_MINITEST}; | |
9 | my $has_perlio = $Config{useperlio}; | |
ab742322 | 10 | |
e5d18500 | 11 | BEGIN { |
f8973f08 | 12 | chdir 't' if -d 't'; |
69026470 | 13 | @INC = qw(. ../lib); |
e5d18500 | 14 | } |
ab742322 RB |
15 | |
16 | if (!$minitest) { | |
240b6766 | 17 | if ($Config{d_fork} && eval 'require POSIX; 1') { |
6b75eab3 NC |
18 | $can_fork = 1; |
19 | } | |
20 | } | |
f8973f08 | 21 | |
6b75eab3 | 22 | use strict; |
69026470 JH |
23 | |
24 | require "test.pl"; | |
6e592b3a | 25 | plan(tests => 49 + !$minitest * (3 + 14 * $can_fork)); |
47de4e93 RGS |
26 | |
27 | sub 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 |
37 | sub 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 | ||
47 | push @INC, \&fooinc; | |
48 | ||
d1e4d418 A |
49 | my $evalret = eval { require Bar; 1 }; |
50 | ok( !$evalret, 'Trying non-magic package' ); | |
51 | ||
52 | $evalret = eval { require Foo; 1 }; | |
53 | die $@ if $@; | |
54 | ok( $evalret, 'require Foo; magic via code ref' ); | |
55 | ok( exists $INC{'Foo.pm'}, ' %INC sees Foo.pm' ); | |
56 | is( ref $INC{'Foo.pm'}, 'CODE', ' val Foo.pm is a coderef in %INC' ); | |
57 | is( $INC{'Foo.pm'}, \&fooinc, ' val Foo.pm is correct in %INC' ); | |
58 | ||
59 | $evalret = eval "use Foo1; 1;"; | |
60 | die $@ if $@; | |
61 | ok( $evalret, 'use Foo1' ); | |
62 | ok( exists $INC{'Foo1.pm'}, ' %INC sees Foo1.pm' ); | |
63 | is( ref $INC{'Foo1.pm'}, 'CODE', ' val Foo1.pm is a coderef in %INC' ); | |
64 | is( $INC{'Foo1.pm'}, \&fooinc, ' val Foo1.pm is correct in %INC' ); | |
65 | ||
66 | $evalret = eval { do 'Foo2.pl'; 1 }; | |
67 | die $@ if $@; | |
68 | ok( $evalret, 'do "Foo2.pl"' ); | |
69 | ok( exists $INC{'Foo2.pl'}, ' %INC sees Foo2.pl' ); | |
70 | is( ref $INC{'Foo2.pl'}, 'CODE', ' val Foo2.pl is a coderef in %INC' ); | |
71 | is( $INC{'Foo2.pl'}, \&fooinc, ' val Foo2.pl is correct in %INC' ); | |
e5d18500 AMS |
72 | |
73 | pop @INC; | |
74 | ||
f8973f08 | 75 | |
e5d18500 AMS |
76 | sub 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 |
86 | my $arrayref = [ \&fooinc2, 'Bar' ]; |
87 | push @INC, $arrayref; | |
e5d18500 | 88 | |
d1e4d418 A |
89 | $evalret = eval { require Foo; 1; }; |
90 | die $@ if $@; | |
91 | ok( $evalret, 'Originally loaded packages preserved' ); | |
92 | $evalret = eval { require Foo3; 1; }; | |
93 | ok( !$evalret, 'Original magic INC purged' ); | |
94 | ||
95 | $evalret = eval { require Bar; 1 }; | |
96 | die $@ if $@; | |
97 | ok( $evalret, 'require Bar; magic via array ref' ); | |
98 | ok( exists $INC{'Bar.pm'}, ' %INC sees Bar.pm' ); | |
99 | is( ref $INC{'Bar.pm'}, 'ARRAY', ' val Bar.pm is an arrayref in %INC' ); | |
100 | is( $INC{'Bar.pm'}, $arrayref, ' val Bar.pm is correct in %INC' ); | |
101 | ||
102 | ok( eval "use Bar1; 1;", 'use Bar1' ); | |
103 | ok( exists $INC{'Bar1.pm'}, ' %INC sees Bar1.pm' ); | |
104 | is( ref $INC{'Bar1.pm'}, 'ARRAY', ' val Bar1.pm is an arrayref in %INC' ); | |
105 | is( $INC{'Bar1.pm'}, $arrayref, ' val Bar1.pm is correct in %INC' ); | |
106 | ||
107 | ok( eval { do 'Bar2.pl'; 1 }, 'do "Bar2.pl"' ); | |
108 | ok( exists $INC{'Bar2.pl'}, ' %INC sees Bar2.pl' ); | |
109 | is( ref $INC{'Bar2.pl'}, 'ARRAY', ' val Bar2.pl is an arrayref in %INC' ); | |
110 | is( $INC{'Bar2.pl'}, $arrayref, ' val Bar2.pl is correct in %INC' ); | |
e5d18500 AMS |
111 | |
112 | pop @INC; | |
113 | ||
114 | sub 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 |
124 | my $href = bless( {}, 'FooLoader' ); |
125 | push @INC, $href; | |
e5d18500 | 126 | |
d1e4d418 A |
127 | $evalret = eval { require Quux; 1 }; |
128 | die $@ if $@; | |
129 | ok( $evalret, 'require Quux; magic via hash object' ); | |
130 | ok( exists $INC{'Quux.pm'}, ' %INC sees Quux.pm' ); | |
6ece0f6b | 131 | is( ref $INC{'Quux.pm'}, 'FooLoader', |
d1e4d418 A |
132 | ' val Quux.pm is an object in %INC' ); |
133 | is( $INC{'Quux.pm'}, $href, ' val Quux.pm is correct in %INC' ); | |
e5d18500 AMS |
134 | |
135 | pop @INC; | |
136 | ||
47de4e93 RGS |
137 | my $aref = bless( [], 'FooLoader' ); |
138 | push @INC, $aref; | |
e5d18500 | 139 | |
d1e4d418 A |
140 | $evalret = eval { require Quux1; 1 }; |
141 | die $@ if $@; | |
142 | ok( $evalret, 'require Quux1; magic via array object' ); | |
143 | ok( exists $INC{'Quux1.pm'}, ' %INC sees Quux1.pm' ); | |
6ece0f6b | 144 | is( ref $INC{'Quux1.pm'}, 'FooLoader', |
d1e4d418 A |
145 | ' val Quux1.pm is an object in %INC' ); |
146 | is( $INC{'Quux1.pm'}, $aref, ' val Quux1.pm is correct in %INC' ); | |
e5d18500 AMS |
147 | |
148 | pop @INC; | |
149 | ||
47de4e93 RGS |
150 | my $sref = bless( \(my $x = 1), 'FooLoader' ); |
151 | push @INC, $sref; | |
e5d18500 | 152 | |
d1e4d418 A |
153 | $evalret = eval { require Quux2; 1 }; |
154 | die $@ if $@; | |
155 | ok( $evalret, 'require Quux2; magic via scalar object' ); | |
156 | ok( exists $INC{'Quux2.pm'}, ' %INC sees Quux2.pm' ); | |
6ece0f6b | 157 | is( ref $INC{'Quux2.pm'}, 'FooLoader', |
d1e4d418 A |
158 | ' val Quux2.pm is an object in %INC' ); |
159 | is( $INC{'Quux2.pm'}, $sref, ' val Quux2.pm is correct in %INC' ); | |
f8973f08 MS |
160 | |
161 | pop @INC; | |
9ae8cd5b RGS |
162 | |
163 | push @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 }; |
175 | die $@ if $@; | |
176 | ok( $evalret, 'require Toto; magic via anonymous code ref' ); | |
177 | ok( exists $INC{'Toto.pm'}, ' %INC sees Toto.pm' ); | |
178 | ok( ! ref $INC{'Toto.pm'}, q/ val Toto.pm isn't a ref in %INC/ ); | |
179 | is( $INC{'Toto.pm'}, 'xyz', ' val Toto.pm is correct in %INC' ); | |
9ae8cd5b RGS |
180 | |
181 | pop @INC; | |
d820be44 | 182 | |
3a5db825 GA |
183 | push @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 | 193 | my $ret = ""; |
3a5db825 GA |
194 | $ret ||= do 'abc.pl'; |
195 | is( $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 | ||
209 | sub 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 | ||
217 | unshift @INC, sub { PVBM }; | |
218 | eval 'require foo'; | |
219 | ok( 1, 'returning PVBM doesn\'t segfault require' ); | |
220 | eval 'use foo'; | |
221 | ok( 1, 'returning PVBM doesn\'t segfault use' ); | |
222 | shift @INC; | |
223 | unshift @INC, sub { \PVBM }; | |
224 | eval 'require foo'; | |
225 | ok( 1, 'returning PVBM ref doesn\'t segfault require' ); | |
226 | eval 'use foo'; | |
227 | ok( 1, 'returning PVBM ref doesn\'t segfault use' ); | |
228 | shift @INC; | |
229 | ||
ab742322 RB |
230 | exit if $minitest; |
231 | ||
240b6766 AT |
232 | SKIP: { |
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 |
258 | pop @INC; |
259 | ||
6b75eab3 NC |
260 | if ($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 | } |