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
1 #!./perl -w
2
3 # Tests for the coderef-in-@INC feature
4
5 use Config;
6
7 my $can_fork   = 0;
8 my $minitest   = $ENV{PERL_CORE_MINITEST};
9 my $has_perlio = $Config{useperlio};
10
11 BEGIN {
12     chdir 't' if -d 't';
13     @INC = qw(. ../lib);
14 }
15
16 if (!$minitest) {
17     if ($Config{d_fork} && eval 'require POSIX; 1') {
18         $can_fork = 1;
19     }
20 }
21
22 use strict;
23
24 require "test.pl";
25 plan(tests => 49 + !$minitest * (3 + 14 * $can_fork));
26
27 sub get_temp_fh {
28     my $f = tempfile();
29     open my $fh, ">$f" or die "Can't create $f: $!";
30     print $fh "package ".substr($_[0],0,-3).";\n1;\n";
31     print $fh $_[1] if @_ > 1;
32     close $fh or die "Couldn't close: $!";
33     open $fh, $f or die "Can't open $f: $!";
34     return $fh;
35 }
36
37 sub fooinc {
38     my ($self, $filename) = @_;
39     if (substr($filename,0,3) eq 'Foo') {
40         return get_temp_fh($filename);
41     }
42     else {
43         return undef;
44     }
45 }
46
47 push @INC, \&fooinc;
48
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' );
72
73 pop @INC;
74
75
76 sub fooinc2 {
77     my ($self, $filename) = @_;
78     if (substr($filename, 0, length($self->[1])) eq $self->[1]) {
79         return get_temp_fh($filename);
80     }
81     else {
82         return undef;
83     }
84 }
85
86 my $arrayref = [ \&fooinc2, 'Bar' ];
87 push @INC, $arrayref;
88
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' );
111
112 pop @INC;
113
114 sub FooLoader::INC {
115     my ($self, $filename) = @_;
116     if (substr($filename,0,4) eq 'Quux') {
117         return get_temp_fh($filename);
118     }
119     else {
120         return undef;
121     }
122 }
123
124 my $href = bless( {}, 'FooLoader' );
125 push @INC, $href;
126
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' );
131 is( ref $INC{'Quux.pm'}, 'FooLoader',
132                                    '  val Quux.pm is an object in %INC' );
133 is( $INC{'Quux.pm'}, $href,        '  val Quux.pm is correct in %INC' );
134
135 pop @INC;
136
137 my $aref = bless( [], 'FooLoader' );
138 push @INC, $aref;
139
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' );
144 is( ref $INC{'Quux1.pm'}, 'FooLoader',
145                                    '  val Quux1.pm is an object in %INC' );
146 is( $INC{'Quux1.pm'}, $aref,       '  val Quux1.pm  is correct in %INC' );
147
148 pop @INC;
149
150 my $sref = bless( \(my $x = 1), 'FooLoader' );
151 push @INC, $sref;
152
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' );
157 is( ref $INC{'Quux2.pm'}, 'FooLoader',
158                                    '  val Quux2.pm is an object in %INC' );
159 is( $INC{'Quux2.pm'}, $sref,       '  val Quux2.pm is correct in %INC' );
160
161 pop @INC;
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
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' );
180
181 pop @INC;
182
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
193 my $ret = "";
194 $ret ||= do 'abc.pl';
195 is( $ret, 'abc', 'do "abc.pl" sees return value' );
196
197 {
198     my $filename = './Foo.pm';
199     #local @INC; # local fails on tied @INC
200     my @old_INC = @INC; # because local doesn't work on tied arrays
201     @INC = sub { $filename = 'seen'; return undef; };
202     eval { require $filename; };
203     is( $filename, 'seen', 'the coderef sees fully-qualified pathnames' );
204     @INC = @old_INC;
205 }
206
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
230 exit if $minitest;
231
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     }
257 }
258 pop @INC;
259
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 }