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