This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In runenv.t, break apart running perl and testing the output into two functions.
[perl5.git] / t / run / runenv.t
1 #!./perl
2 #
3 # Tests for Perl run-time environment variable settings
4 #
5 # $PERL5OPT, $PERL5LIB, etc.
6
7 BEGIN {
8     chdir 't' if -d 't';
9     @INC = '../lib';
10     require Config; import Config;
11     unless ($Config{'d_fork'}) {
12         print "1..0 # Skip: no fork\n";
13             exit 0;
14     }
15     require './test.pl'
16 }
17
18 plan tests => 17;
19
20 my $STDOUT = tempfile();
21 my $STDERR = tempfile();
22 my $PERL = $ENV{PERL} || './perl';
23 my $FAILURE_CODE = 119;
24
25 delete $ENV{PERLLIB};
26 delete $ENV{PERL5LIB};
27 delete $ENV{PERL5OPT};
28
29
30 sub runperl_and_capture {
31   local *F;
32   my ($env, $args) = @_;
33   unshift @$args, '-I../lib';
34
35   local %ENV = %ENV;
36   delete $ENV{PERLLIB};
37   delete $ENV{PERL5LIB};
38   delete $ENV{PERL5OPT};
39   my $pid = fork;
40   return (0, "Couldn't fork: $!") unless defined $pid;   # failure
41   if ($pid) {                   # parent
42     my ($actual_stdout, $actual_stderr);
43     wait;
44     return (0, "Failure in child.\n") if ($?>>8) == $FAILURE_CODE;
45
46     open F, "< $STDOUT" or return (0, "Couldn't read $STDOUT file");
47     { local $/; $actual_stdout = <F> }
48     open F, "< $STDERR" or return (0, "Couldn't read $STDERR file");
49     { local $/; $actual_stderr = <F> }
50
51     return ($actual_stdout, $actual_stderr);
52   } else {                      # child
53     for my $k (keys %$env) {
54       $ENV{$k} = $env->{$k};
55     }
56     open STDOUT, "> $STDOUT" or exit $FAILURE_CODE;
57     open STDERR, "> $STDERR" or it_didnt_work();
58     { exec $PERL, @$args }
59     it_didnt_work();
60   }
61 }
62
63 # Run perl with specified environment and arguments returns a list.
64 # First element is true if Perl's stdout and stderr match the
65 # supplied $stdout and $stderr argument strings exactly.
66 # second element is an explanation of the failure
67 sub runperl {
68   local *F;
69   my ($env, $args, $stdout, $stderr) = @_;
70   my ($actual_stdout, $actual_stderr) = runperl_and_capture($env, $args);
71   if ($actual_stdout ne $stdout) {
72     return (0, "Stdout mismatch: expected [$stdout], saw [$actual_stdout]");
73   } elsif ($actual_stderr ne $stderr) {
74     return (0, "Stderr mismatch: expected [$stderr], saw [$actual_stderr]");
75   } else {
76     return 1;                 # success
77   }
78 }
79
80 sub it_didnt_work {
81     print STDOUT "IWHCWJIHCI\cNHJWCJQWKJQJWCQW\n";
82     exit $FAILURE_CODE;
83 }
84
85 sub try {
86   my ($success, $reason) = runperl(@_);
87   $reason =~ s/\n/\\n/g if defined $reason;
88   local $::Level = $::Level + 1;
89   ok( $success, $reason );
90 }
91
92 #  PERL5OPT    Command-line options (switches).  Switches in
93 #                    this variable are taken as if they were on
94 #                    every Perl command line.  Only the -[DIMUdmtw]
95 #                    switches are allowed.  When running taint
96 #                    checks (because the program was running setuid
97 #                    or setgid, or the -T switch was used), this
98 #                    variable is ignored.  If PERL5OPT begins with
99 #                    -T, tainting will be enabled, and any
100 #                    subsequent options ignored.
101
102 try({PERL5OPT => '-w'}, ['-e', 'print $::x'],
103     "", 
104     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});
105
106 try({PERL5OPT => '-Mstrict'}, ['-e', 'print $::x'],
107     "", "");
108
109 try({PERL5OPT => '-Mstrict'}, ['-e', 'print $x'],
110     "", 
111     qq{Global symbol "\$x" requires explicit package name at -e line 1.\nExecution of -e aborted due to compilation errors.\n});
112
113 # Fails in 5.6.0
114 try({PERL5OPT => '-Mstrict -w'}, ['-e', 'print $x'],
115     "", 
116     qq{Global symbol "\$x" requires explicit package name at -e line 1.\nExecution of -e aborted due to compilation errors.\n});
117
118 # Fails in 5.6.0
119 try({PERL5OPT => '-w -Mstrict'}, ['-e', 'print $::x'],
120     "", 
121     <<ERROR
122 Name "main::x" used only once: possible typo at -e line 1.
123 Use of uninitialized value \$x in print at -e line 1.
124 ERROR
125     );
126
127 # Fails in 5.6.0
128 try({PERL5OPT => '-w -Mstrict'}, ['-e', 'print $::x'],
129     "", 
130     <<ERROR
131 Name "main::x" used only once: possible typo at -e line 1.
132 Use of uninitialized value \$x in print at -e line 1.
133 ERROR
134     );
135
136 try({PERL5OPT => '-MExporter'}, ['-e0'],
137     "", 
138     "");
139
140 # Fails in 5.6.0
141 try({PERL5OPT => '-MExporter -MExporter'}, ['-e0'],
142     "", 
143     "");
144
145 try({PERL5OPT => '-Mstrict -Mwarnings'}, 
146     ['-e', 'print "ok" if $INC{"strict.pm"} and $INC{"warnings.pm"}'],
147     "ok",
148     "");
149
150 try({PERL5OPT => '-w -w'},
151     ['-e', 'print $ENV{PERL5OPT}'],
152     '-w -w',
153     '');
154
155 try({PERL5OPT => '-t'},
156     ['-e', 'print ${^TAINT}'],
157     '-1',
158     '');
159
160 try({PERLLIB => "foobar$Config{path_sep}42"},
161     ['-e', 'print grep { $_ eq "foobar" } @INC'],
162     'foobar',
163     '');
164
165 try({PERLLIB => "foobar$Config{path_sep}42"},
166     ['-e', 'print grep { $_ eq "42" } @INC'],
167     '42',
168     '');
169
170 try({PERL5LIB => "foobar$Config{path_sep}42"},
171     ['-e', 'print grep { $_ eq "foobar" } @INC'],
172     'foobar',
173     '');
174
175 try({PERL5LIB => "foobar$Config{path_sep}42"},
176     ['-e', 'print grep { $_ eq "42" } @INC'],
177     '42',
178     '');
179
180 try({PERL5LIB => "foo",
181      PERLLIB => "bar"},
182     ['-e', 'print grep { $_ eq "foo" } @INC'],
183     'foo',
184     '');
185
186 try({PERL5LIB => "foo",
187      PERLLIB => "bar"},
188     ['-e', 'print grep { $_ eq "bar" } @INC'],
189     '',
190     '');
191
192 # PERL5LIB tests with included arch directories still missing
193
194 END {
195     1 while unlink $STDOUT;
196     1 while unlink $STDERR;
197 }