Re: [PATCH] foreach defelem magic should only be applied to PL_sv_undef
[p5sagit/p5-mst-13.2.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 }
16
17 my $STDOUT = './results-0';
18 my $STDERR = './results-1';
19 my $PERL = './perl';
20 my $FAILURE_CODE = 119;
21
22 print "1..9\n";
23
24 # Run perl with specified environment and arguments returns a list.
25 # First element is true iff Perl's stdout and stderr match the
26 # supplied $stdout and $stderr argument strings exactly.
27 # second element is an explanation of the failure
28 sub runperl {
29   local *F;
30   my ($env, $args, $stdout, $stderr) = @_;
31
32   unshift @$args, '-I../lib';
33
34   $stdout = '' unless defined $stdout;
35   $stderr = '' unless defined $stderr;
36   my $pid = fork;
37   return (0, "Couldn't fork: $!") unless defined $pid;   # failure
38   if ($pid) {                   # parent
39     my ($actual_stdout, $actual_stderr);
40     wait;
41     return (0, "Failure in child.\n") if ($?>>8) == $FAILURE_CODE;
42
43     open F, "< $STDOUT" or return (0, "Couldn't read $STDOUT file");
44     { local $/; $actual_stdout = <F> }
45     open F, "< $STDERR" or return (0, "Couldn't read $STDERR file");
46     { local $/; $actual_stderr = <F> }
47
48     if ($actual_stdout ne $stdout) {
49       return (0, "Stdout mismatch: expected [$stdout], saw [$actual_stdout]");
50     } elsif ($actual_stderr ne $stderr) {
51       return (0, "Stderr mismatch: expected [$stderr], saw [$actual_stderr]");
52     } else {
53       return 1;                 # success
54     }
55   } else {                      # child
56     for my $k (keys %$env) {
57       $ENV{$k} = $env->{$k};
58     }
59     open STDOUT, "> $STDOUT" or exit $FAILURE_CODE;
60     open STDERR, "> $STDERR" or it_didnt_work();
61     { exec $PERL, @$args }
62     it_didnt_work();
63   }
64 }
65
66
67 sub it_didnt_work {
68     print STDOUT "IWHCWJIHCI\cNHJWCJQWKJQJWCQW\n";
69     exit $FAILURE_CODE;
70 }
71
72 sub try {
73   my $testno = shift;
74   my ($success, $reason) = runperl(@_);
75   if ($success) {
76     print "ok $testno\n";
77   } else {
78     $reason =~ s/\n/\\n/g;
79     print "not ok $testno # $reason\n";    
80   }
81 }
82
83 #  PERL5OPT    Command-line options (switches).  Switches in
84 #                    this variable are taken as if they were on
85 #                    every Perl command line.  Only the -[DIMUdmw]
86 #                    switches are allowed.  When running taint
87 #                    checks (because the program was running setuid
88 #                    or setgid, or the -T switch was used), this
89 #                    variable is ignored.  If PERL5OPT begins with
90 #                    -T, tainting will be enabled, and any
91 #                    subsequent options ignored.
92
93 my  $T = 1;
94 try($T++, {PERL5OPT => '-w'}, ['-e', 'print $::x'],
95     "", 
96     qq{Name "main::x" used only once: possible typo at -e line 1.\nUse of uninitialized value in print at -e line 1.\n});
97
98 try($T++, {PERL5OPT => '-Mstrict'}, ['-e', 'print $::x'],
99     "", "");
100
101 try($T++, {PERL5OPT => '-Mstrict'}, ['-e', 'print $x'],
102     "", 
103     qq{Global symbol "\$x" requires explicit package name at -e line 1.\nExecution of -e aborted due to compilation errors.\n});
104
105 # Fails in 5.6.0
106 try($T++, {PERL5OPT => '-Mstrict -w'}, ['-e', 'print $x'],
107     "", 
108     qq{Global symbol "\$x" requires explicit package name at -e line 1.\nExecution of -e aborted due to compilation errors.\n});
109
110 # Fails in 5.6.0
111 try($T++, {PERL5OPT => '-w -Mstrict'}, ['-e', 'print $::x'],
112     "", 
113     <<ERROR
114 Name "main::x" used only once: possible typo at -e line 1.
115 Use of uninitialized value in print at -e line 1.
116 ERROR
117     );
118
119 # Fails in 5.6.0
120 try($T++, {PERL5OPT => '-w -Mstrict'}, ['-e', 'print $::x'],
121     "", 
122     <<ERROR
123 Name "main::x" used only once: possible typo at -e line 1.
124 Use of uninitialized value in print at -e line 1.
125 ERROR
126     );
127
128 try($T++, {PERL5OPT => '-MExporter'}, ['-e0'],
129     "", 
130     "");
131
132 # Fails in 5.6.0
133 try($T++, {PERL5OPT => '-MExporter -MExporter'}, ['-e0'],
134     "", 
135     "");
136
137 try($T++, {PERL5OPT => '-Mstrict -Mwarnings'}, 
138     ['-e', 'print "ok" if $INC{"strict.pm"} and $INC{"warnings.pm"}'],
139     "ok",
140     "");
141
142 print "# ", $T-1, " tests total.\n";
143
144 END {
145     1 while unlink $STDOUT;
146     1 while unlink $STDERR;
147 }