3 # Tests for Perl run-time environment variable settings
5 # $PERL5OPT, $PERL5LIB, etc.
10 require Config; import Config;
11 unless ($Config{'d_fork'}) {
12 print "1..0 # Skip: no fork\n";
20 my $STDOUT = tempfile();
21 my $STDERR = tempfile();
22 my $PERL = $ENV{PERL} || './perl';
23 my $FAILURE_CODE = 119;
26 delete $ENV{PERL5LIB};
27 delete $ENV{PERL5OPT};
30 # Run perl with specified environment and arguments, return (STDOUT, STDERR)
31 sub runperl_and_capture {
33 my ($env, $args) = @_;
34 unshift @$args, '-I../lib';
38 delete $ENV{PERL5LIB};
39 delete $ENV{PERL5OPT};
41 return (0, "Couldn't fork: $!") unless defined $pid; # failure
44 return (0, "Failure in child.\n") if ($?>>8) == $FAILURE_CODE;
46 open my $stdout, '<', $STDOUT
47 or return (0, "Couldn't read $STDOUT file: $!");
48 open my $stderr, '<', $STDERR
49 or return (0, "Couldn't read $STDERR file: $!");
51 # Empty file with <$stderr> returns nothing in list context
52 # (because there are no lines) Use scalar to force it to ''
53 return (scalar <$stdout>, scalar <$stderr>);
55 for my $k (keys %$env) {
56 $ENV{$k} = $env->{$k};
58 open STDOUT, '>', $STDOUT or exit $FAILURE_CODE;
59 open STDERR, '>', $STDERR and do { exec $PERL, @$args };
61 print STDOUT "IWHCWJIHCI\cNHJWCJQWKJQJWCQW\n";
67 my ($env, $args, $stdout, $stderr) = @_;
68 my ($actual_stdout, $actual_stderr) = runperl_and_capture($env, $args);
69 local $::Level = $::Level + 1;
70 is ($stdout, $actual_stdout);
71 is ($stderr, $actual_stderr);
74 # PERL5OPT Command-line options (switches). Switches in
75 # this variable are taken as if they were on
76 # every Perl command line. Only the -[DIMUdmtw]
77 # switches are allowed. When running taint
78 # checks (because the program was running setuid
79 # or setgid, or the -T switch was used), this
80 # variable is ignored. If PERL5OPT begins with
81 # -T, tainting will be enabled, and any
82 # subsequent options ignored.
84 try({PERL5OPT => '-w'}, ['-e', 'print $::x'],
86 qq{Name "main::x" used only once: possible typo at -e line 1.\nUse of uninitialized value \$x in print at -e line 1.\n});
88 try({PERL5OPT => '-Mstrict'}, ['-e', 'print $::x'],
91 try({PERL5OPT => '-Mstrict'}, ['-e', 'print $x'],
93 qq{Global symbol "\$x" requires explicit package name at -e line 1.\nExecution of -e aborted due to compilation errors.\n});
96 try({PERL5OPT => '-Mstrict -w'}, ['-e', 'print $x'],
98 qq{Global symbol "\$x" requires explicit package name at -e line 1.\nExecution of -e aborted due to compilation errors.\n});
101 try({PERL5OPT => '-w -Mstrict'}, ['-e', 'print $::x'],
104 Name "main::x" used only once: possible typo at -e line 1.
105 Use of uninitialized value \$x in print at -e line 1.
110 try({PERL5OPT => '-w -Mstrict'}, ['-e', 'print $::x'],
113 Name "main::x" used only once: possible typo at -e line 1.
114 Use of uninitialized value \$x in print at -e line 1.
118 try({PERL5OPT => '-MExporter'}, ['-e0'],
123 try({PERL5OPT => '-MExporter -MExporter'}, ['-e0'],
127 try({PERL5OPT => '-Mstrict -Mwarnings'},
128 ['-e', 'print "ok" if $INC{"strict.pm"} and $INC{"warnings.pm"}'],
132 open my $fh, ">", "Oooof.pm" or die "Can't write Oooof.pm: $!";
133 print $fh "package Oooof; 1;\n";
135 END { 1 while unlink "Oooof.pm" }
137 try({PERL5OPT => '-I. -MOooof'},
138 ['-e', 'print "ok" if $INC{"Oooof.pm"} eq "Oooof.pm"'],
142 try({PERL5OPT => '-I./ -MOooof'},
143 ['-e', 'print "ok" if $INC{"Oooof.pm"} eq "Oooof.pm"'],
147 try({PERL5OPT => '-w -w'},
148 ['-e', 'print $ENV{PERL5OPT}'],
152 try({PERL5OPT => '-t'},
153 ['-e', 'print ${^TAINT}'],
157 try({PERL5OPT => '-W'},
158 ['-e', 'local $^W = 0; no warnings; print $x'],
161 Name "main::x" used only once: possible typo at -e line 1.
162 Use of uninitialized value \$x in print at -e line 1.
166 try({PERLLIB => "foobar$Config{path_sep}42"},
167 ['-e', 'print grep { $_ eq "foobar" } @INC'],
171 try({PERLLIB => "foobar$Config{path_sep}42"},
172 ['-e', 'print grep { $_ eq "42" } @INC'],
176 try({PERL5LIB => "foobar$Config{path_sep}42"},
177 ['-e', 'print grep { $_ eq "foobar" } @INC'],
181 try({PERL5LIB => "foobar$Config{path_sep}42"},
182 ['-e', 'print grep { $_ eq "42" } @INC'],
186 try({PERL5LIB => "foo",
188 ['-e', 'print grep { $_ eq "foo" } @INC'],
192 try({PERL5LIB => "foo",
194 ['-e', 'print grep { $_ eq "bar" } @INC'],
198 # Tests for S_incpush_use_sep():
200 my @dump_inc = ('-e', 'print "$_\n" foreach @INC');
202 my ($out, $err) = runperl_and_capture({}, [@dump_inc]);
204 is ($err, '', 'No errors when determining @INC');
206 my @default_inc = split /\n/, $out;
208 is (shift @default_inc, '../lib', 'Our -I../lib is at the front');
210 my $sep = $Config{path_sep};
211 foreach (['nothing', ''],
212 ['something', 'zwapp', 'zwapp'],
213 ['two things', "zwapp${sep}bam", 'zwapp', 'bam'],
214 ['two things, ::', "zwapp${sep}${sep}bam", 'zwapp', 'bam'],
215 [': at start', "${sep}zwapp", 'zwapp'],
216 [': at end', "zwapp${sep}", 'zwapp'],
217 [':: sandwich ::', "${sep}${sep}zwapp${sep}${sep}", 'zwapp'],
219 ['::', "${sep}${sep}"],
220 [':::', "${sep}${sep}${sep}"],
221 ['two things and :', "zwapp${sep}bam${sep}", 'zwapp', 'bam'],
222 [': and two things', "${sep}zwapp${sep}bam", 'zwapp', 'bam'],
223 [': two things :', "${sep}zwapp${sep}bam${sep}", 'zwapp', 'bam'],
224 ['three things', "zwapp${sep}bam${sep}${sep}owww",
225 'zwapp', 'bam', 'owww'],
227 my ($name, $lib, @expect) = @$_;
228 push @expect, @default_inc;
230 ($out, $err) = runperl_and_capture({PERL5LIB => $lib}, [@dump_inc]);
232 is ($err, '', "No errors when determining \@INC for $name");
234 my @inc = split /\n/, $out;
236 is (shift @inc, '../lib', 'Our -I../lib is at the front for $name');
238 is (scalar @inc, scalar @expect,
239 "expected number of elements in \@INC for $name");
241 is ("@inc", "@expect", "expected elements in \@INC for $name");
244 # PERL5LIB tests with included arch directories still missing