3 # Tests for Perl run-time environment variable settings
5 # $PERL5OPT, $PERL5LIB, etc.
10 require Config; import Config;
12 skip_all_without_config('d_fork');
17 my $STDOUT = tempfile();
18 my $STDERR = tempfile();
19 my $PERL = $ENV{PERL} || './perl';
20 my $FAILURE_CODE = 119;
23 delete $ENV{PERL5LIB};
24 delete $ENV{PERL5OPT};
27 # Run perl with specified environment and arguments, return (STDOUT, STDERR)
28 sub runperl_and_capture {
30 my ($env, $args) = @_;
34 delete $ENV{PERL5LIB};
35 delete $ENV{PERL5OPT};
37 return (0, "Couldn't fork: $!") unless defined $pid; # failure
40 return (0, "Failure in child.\n") if ($?>>8) == $FAILURE_CODE;
42 open my $stdout, '<', $STDOUT
43 or return (0, "Couldn't read $STDOUT file: $!");
44 open my $stderr, '<', $STDERR
45 or return (0, "Couldn't read $STDERR file: $!");
47 # Empty file with <$stderr> returns nothing in list context
48 # (because there are no lines) Use scalar to force it to ''
49 return (scalar <$stdout>, scalar <$stderr>);
51 for my $k (keys %$env) {
52 $ENV{$k} = $env->{$k};
54 open STDOUT, '>', $STDOUT or exit $FAILURE_CODE;
55 open STDERR, '>', $STDERR and do { exec $PERL, @$args };
57 print STDOUT "IWHCWJIHCI\cNHJWCJQWKJQJWCQW\n";
63 my ($env, $args, $stdout, $stderr) = @_;
64 my ($actual_stdout, $actual_stderr) = runperl_and_capture($env, $args);
65 local $::Level = $::Level + 1;
66 is ($stdout, $actual_stdout);
67 is ($stderr, $actual_stderr);
70 # PERL5OPT Command-line options (switches). Switches in
71 # this variable are taken as if they were on
72 # every Perl command line. Only the -[DIMUdmtw]
73 # switches are allowed. When running taint
74 # checks (because the program was running setuid
75 # or setgid, or the -T switch was used), this
76 # variable is ignored. If PERL5OPT begins with
77 # -T, tainting will be enabled, and any
78 # subsequent options ignored.
80 try({PERL5OPT => '-w'}, ['-e', 'print $::x'],
82 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});
84 try({PERL5OPT => '-Mstrict'}, ['-I../lib', '-e', 'print $::x'],
87 try({PERL5OPT => '-Mstrict'}, ['-I../lib', '-e', 'print $x'],
89 qq{Global symbol "\$x" requires explicit package name at -e line 1.\nExecution of -e aborted due to compilation errors.\n});
92 try({PERL5OPT => '-Mstrict -w'}, ['-I../lib', '-e', 'print $x'],
94 qq{Global symbol "\$x" requires explicit package name at -e line 1.\nExecution of -e aborted due to compilation errors.\n});
97 try({PERL5OPT => '-w -Mstrict'}, ['-I../lib', '-e', 'print $::x'],
100 Name "main::x" used only once: possible typo at -e line 1.
101 Use of uninitialized value \$x in print at -e line 1.
106 try({PERL5OPT => '-w -Mstrict'}, ['-I../lib', '-e', 'print $::x'],
109 Name "main::x" used only once: possible typo at -e line 1.
110 Use of uninitialized value \$x in print at -e line 1.
114 try({PERL5OPT => '-MExporter'}, ['-I../lib', '-e0'],
119 try({PERL5OPT => '-MExporter -MExporter'}, ['-I../lib', '-e0'],
123 try({PERL5OPT => '-Mstrict -Mwarnings'},
124 ['-I../lib', '-e', 'print "ok" if $INC{"strict.pm"} and $INC{"warnings.pm"}'],
128 open my $fh, ">", "Oooof.pm" or die "Can't write Oooof.pm: $!";
129 print $fh "package Oooof; 1;\n";
131 END { 1 while unlink "Oooof.pm" }
133 try({PERL5OPT => '-I. -MOooof'},
134 ['-e', 'print "ok" if $INC{"Oooof.pm"} eq "Oooof.pm"'],
138 try({PERL5OPT => '-I./ -MOooof'},
139 ['-e', 'print "ok" if $INC{"Oooof.pm"} eq "Oooof.pm"'],
143 try({PERL5OPT => '-w -w'},
144 ['-e', 'print $ENV{PERL5OPT}'],
148 try({PERL5OPT => '-t'},
149 ['-e', 'print ${^TAINT}'],
153 try({PERL5OPT => '-W'},
154 ['-I../lib','-e', 'local $^W = 0; no warnings; print $x'],
157 Name "main::x" used only once: possible typo at -e line 1.
158 Use of uninitialized value \$x in print at -e line 1.
162 try({PERLLIB => "foobar$Config{path_sep}42"},
163 ['-e', 'print grep { $_ eq "foobar" } @INC'],
167 try({PERLLIB => "foobar$Config{path_sep}42"},
168 ['-e', 'print grep { $_ eq "42" } @INC'],
172 try({PERL5LIB => "foobar$Config{path_sep}42"},
173 ['-e', 'print grep { $_ eq "foobar" } @INC'],
177 try({PERL5LIB => "foobar$Config{path_sep}42"},
178 ['-e', 'print grep { $_ eq "42" } @INC'],
182 try({PERL5LIB => "foo",
184 ['-e', 'print grep { $_ eq "foo" } @INC'],
188 try({PERL5LIB => "foo",
190 ['-e', 'print grep { $_ eq "bar" } @INC'],
194 # Tests for S_incpush_use_sep():
196 my @dump_inc = ('-e', 'print "$_\n" foreach @INC');
198 my ($out, $err) = runperl_and_capture({}, [@dump_inc]);
200 is ($err, '', 'No errors when determining @INC');
202 my @default_inc = split /\n/, $out;
204 is ($default_inc[-1], '.', '. is last in @INC');
206 my $sep = $Config{path_sep};
207 foreach (['nothing', ''],
208 ['something', 'zwapp', 'zwapp'],
209 ['two things', "zwapp${sep}bam", 'zwapp', 'bam'],
210 ['two things, ::', "zwapp${sep}${sep}bam", 'zwapp', 'bam'],
211 [': at start', "${sep}zwapp", 'zwapp'],
212 [': at end', "zwapp${sep}", 'zwapp'],
213 [':: sandwich ::', "${sep}${sep}zwapp${sep}${sep}", 'zwapp'],
215 ['::', "${sep}${sep}"],
216 [':::', "${sep}${sep}${sep}"],
217 ['two things and :', "zwapp${sep}bam${sep}", 'zwapp', 'bam'],
218 [': and two things', "${sep}zwapp${sep}bam", 'zwapp', 'bam'],
219 [': two things :', "${sep}zwapp${sep}bam${sep}", 'zwapp', 'bam'],
220 ['three things', "zwapp${sep}bam${sep}${sep}owww",
221 'zwapp', 'bam', 'owww'],
223 my ($name, $lib, @expect) = @$_;
224 push @expect, @default_inc;
226 ($out, $err) = runperl_and_capture({PERL5LIB => $lib}, [@dump_inc]);
228 is ($err, '', "No errors when determining \@INC for $name");
230 my @inc = split /\n/, $out;
232 is (scalar @inc, scalar @expect,
233 "expected number of elements in \@INC for $name");
235 is ("@inc", "@expect", "expected elements in \@INC for $name");
238 # PERL5LIB tests with included arch directories still missing