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
43 my ($actual_stdout, $actual_stderr);
45 return (0, "Failure in child.\n") if ($?>>8) == $FAILURE_CODE;
47 open F, "< $STDOUT" or return (0, "Couldn't read $STDOUT file");
48 { local $/; $actual_stdout = <F> }
49 open F, "< $STDERR" or return (0, "Couldn't read $STDERR file");
50 { local $/; $actual_stderr = <F> }
52 return ($actual_stdout, $actual_stderr);
54 for my $k (keys %$env) {
55 $ENV{$k} = $env->{$k};
57 open STDOUT, "> $STDOUT" or exit $FAILURE_CODE;
58 open STDERR, "> $STDERR" and do { exec $PERL, @$args };
60 print STDOUT "IWHCWJIHCI\cNHJWCJQWKJQJWCQW\n";
66 my ($env, $args, $stdout, $stderr) = @_;
67 my ($actual_stdout, $actual_stderr) = runperl_and_capture($env, $args);
68 local $::Level = $::Level + 1;
69 is ($stdout, $actual_stdout);
70 is ($stderr, $actual_stderr);
73 # PERL5OPT Command-line options (switches). Switches in
74 # this variable are taken as if they were on
75 # every Perl command line. Only the -[DIMUdmtw]
76 # switches are allowed. When running taint
77 # checks (because the program was running setuid
78 # or setgid, or the -T switch was used), this
79 # variable is ignored. If PERL5OPT begins with
80 # -T, tainting will be enabled, and any
81 # subsequent options ignored.
83 try({PERL5OPT => '-w'}, ['-e', 'print $::x'],
85 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});
87 try({PERL5OPT => '-Mstrict'}, ['-e', 'print $::x'],
90 try({PERL5OPT => '-Mstrict'}, ['-e', 'print $x'],
92 qq{Global symbol "\$x" requires explicit package name at -e line 1.\nExecution of -e aborted due to compilation errors.\n});
95 try({PERL5OPT => '-Mstrict -w'}, ['-e', 'print $x'],
97 qq{Global symbol "\$x" requires explicit package name at -e line 1.\nExecution of -e aborted due to compilation errors.\n});
100 try({PERL5OPT => '-w -Mstrict'}, ['-e', 'print $::x'],
103 Name "main::x" used only once: possible typo at -e line 1.
104 Use of uninitialized value \$x in print at -e line 1.
109 try({PERL5OPT => '-w -Mstrict'}, ['-e', 'print $::x'],
112 Name "main::x" used only once: possible typo at -e line 1.
113 Use of uninitialized value \$x in print at -e line 1.
117 try({PERL5OPT => '-MExporter'}, ['-e0'],
122 try({PERL5OPT => '-MExporter -MExporter'}, ['-e0'],
126 try({PERL5OPT => '-Mstrict -Mwarnings'},
127 ['-e', 'print "ok" if $INC{"strict.pm"} and $INC{"warnings.pm"}'],
131 open F, ">", "Oooof.pm" or die "Can't write Oooof.pm: $!";
132 print F "package Oooof; 1;\n";
134 END { 1 while unlink "Oooof.pm" }
136 try({PERL5OPT => '-I. -MOooof'},
137 ['-e', 'print "ok" if $INC{"Oooof.pm"} eq "Oooof.pm"'],
141 try({PERL5OPT => '-I./ -MOooof'},
142 ['-e', 'print "ok" if $INC{"Oooof.pm"} eq "Oooof.pm"'],
146 try({PERL5OPT => '-w -w'},
147 ['-e', 'print $ENV{PERL5OPT}'],
151 try({PERL5OPT => '-t'},
152 ['-e', 'print ${^TAINT}'],
156 try({PERL5OPT => '-W'},
157 ['-e', 'local $^W = 0; no warnings; print $x'],
160 Name "main::x" used only once: possible typo at -e line 1.
161 Use of uninitialized value \$x in print at -e line 1.
165 try({PERLLIB => "foobar$Config{path_sep}42"},
166 ['-e', 'print grep { $_ eq "foobar" } @INC'],
170 try({PERLLIB => "foobar$Config{path_sep}42"},
171 ['-e', 'print grep { $_ eq "42" } @INC'],
175 try({PERL5LIB => "foobar$Config{path_sep}42"},
176 ['-e', 'print grep { $_ eq "foobar" } @INC'],
180 try({PERL5LIB => "foobar$Config{path_sep}42"},
181 ['-e', 'print grep { $_ eq "42" } @INC'],
185 try({PERL5LIB => "foo",
187 ['-e', 'print grep { $_ eq "foo" } @INC'],
191 try({PERL5LIB => "foo",
193 ['-e', 'print grep { $_ eq "bar" } @INC'],
197 # Tests for S_incpush_use_sep():
199 my @dump_inc = ('-e', 'print "$_\n" foreach @INC');
201 my ($out, $err) = runperl_and_capture({}, [@dump_inc]);
203 is ($err, '', 'No errors when determining @INC');
205 my @default_inc = split /\n/, $out;
207 is (shift @default_inc, '../lib', 'Our -I../lib is at the front');
209 my $sep = $Config{path_sep};
210 foreach (['nothing', ''],
211 ['something', 'zwapp', 'zwapp'],
212 ['two things', "zwapp${sep}bam", 'zwapp', 'bam'],
213 ['two things, ::', "zwapp${sep}${sep}bam", 'zwapp', 'bam'],
214 [': at start', "${sep}zwapp", 'zwapp'],
215 [': at end', "zwapp${sep}", 'zwapp'],
216 [':: sandwich ::', "${sep}${sep}zwapp${sep}${sep}", 'zwapp'],
218 ['::', "${sep}${sep}"],
219 [':::', "${sep}${sep}${sep}"],
220 ['two things and :', "zwapp${sep}bam${sep}", 'zwapp', 'bam'],
221 [': and two things', "${sep}zwapp${sep}bam", 'zwapp', 'bam'],
222 [': two things :', "${sep}zwapp${sep}bam${sep}", 'zwapp', 'bam'],
223 ['three things', "zwapp${sep}bam${sep}${sep}owww",
224 'zwapp', 'bam', 'owww'],
226 my ($name, $lib, @expect) = @$_;
227 push @expect, @default_inc;
229 ($out, $err) = runperl_and_capture({PERL5LIB => $lib}, [@dump_inc]);
231 is ($err, '', "No errors when determining \@INC for $name");
233 my @inc = split /\n/, $out;
235 is (shift @inc, '../lib', 'Our -I../lib is at the front for $name');
237 is (scalar @inc, scalar @expect,
238 "expected number of elements in \@INC for $name");
240 is ("@inc", "@expect", "expected elements in \@INC for $name");
243 # PERL5LIB tests with included arch directories still missing