dup saved_curcop in PL_parser
[p5sagit/p5-mst-13.2.git] / t / run / exit.t
CommitLineData
14cf881c 1#!./perl
2#
3# Tests for perl exit codes, playing with $?, etc...
4
5
6BEGIN {
7 chdir 't' if -d 't';
69026470 8 @INC = qw(. ../lib);
14cf881c 9}
10
14cf881c 11# Run some code, return its wait status.
12sub run {
13 my($code) = shift;
6cd63c21 14 $code = "\"" . $code . "\"" if $^O eq 'VMS'; #VMS needs quotes for this.
755bf813 15 return system($^X, "-e", $code);
14cf881c 16}
17
0ab78052 18BEGIN {
dc459aad 19 # MacOS system() doesn't have good return value
7a7fd8e0 20 $numtests = ($^O eq 'VMS') ? 16 : ($^O eq 'MacOS') ? 0 : 17;
0ab78052 21}
14cf881c 22
1a3aec58 23
24my $vms_exit_mode = 0;
25
26if ($^O eq 'VMS') {
27 if (eval 'require VMS::Feature') {
28 $vms_exit_mode = !(VMS::Feature::current("posix_exit"));
29 } else {
e08e1e1d 30 my $env_unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
31 my $env_posix_ex = $ENV{'PERL_VMS_POSIX_EXIT'} || '';
32 my $unix_rpt = $env_unix_rpt =~ /^[ET1]/i;
33 my $posix_ex = $env_posix_ex =~ /^[ET1]/i;
1a3aec58 34 if (($unix_rpt || $posix_ex) ) {
35 $vms_exit_mode = 0;
36 } else {
37 $vms_exit_mode = 1;
38 }
39 }
40 $numtests = 29 unless $vms_exit_mode;
41}
42
69026470 43require "test.pl";
44plan(tests => $numtests);
55a9fe1d 45
6cd63c21 46my $native_success = 0;
47 $native_success = 1 if $^O eq 'VMS';
48
dc459aad 49if ($^O ne 'MacOS') {
55a9fe1d 50my $exit, $exit_arg;
14cf881c 51
52$exit = run('exit');
53is( $exit >> 8, 0, 'Normal exit' );
e5218da5 54is( $exit, $?, 'Normal exit $?' );
6cd63c21 55is( ${^CHILD_ERROR_NATIVE}, $native_success, 'Normal exit ${^CHILD_ERROR_NATIVE}' );
14cf881c 56
1a3aec58 57if (!$vms_exit_mode) {
e5218da5 58 my $posix_ok = eval { require POSIX; };
5c5be41c 59 my $wait_macros_ok = defined &POSIX::WIFEXITED;
ac0638ad 60 eval { POSIX::WIFEXITED() };
61 $wait_macros_ok = 0 if $@;
55a9fe1d 62 $exit = run('exit 42');
63 is( $exit >> 8, 42, 'Non-zero exit' );
e5218da5 64 is( $exit, $?, 'Non-zero exit $?' );
65 isnt( !${^CHILD_ERROR_NATIVE}, 0, 'Non-zero exit ${^CHILD_ERROR_NATIVE}' );
5c5be41c 66 SKIP: {
67 skip("No POSIX", 3) unless $posix_ok;
68 skip("No POSIX wait macros", 3) unless $wait_macros_ok;
69 ok(POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}), "WIFEXITED");
70 ok(!POSIX::WIFSIGNALED(${^CHILD_ERROR_NATIVE}), "WIFSIGNALED");
71 is(POSIX::WEXITSTATUS(${^CHILD_ERROR_NATIVE}), 42, "WEXITSTATUS");
e5218da5 72 }
73
5c5be41c 74 SKIP: {
1a3aec58 75 skip("Skip signals and core dump tests on Win32 and VMS", 7)
76 if ($^O eq 'MSWin32' || $^O eq 'VMS');
77
78 #TODO VMS will backtrace on this test and exits with code of 0
79 #instead of 15.
5c5be41c 80
81 $exit = run('kill 15, $$; sleep(1);');
e5218da5 82
5c5be41c 83 is( $exit & 127, 15, 'Term by signal' );
84 ok( !($exit & 128), 'No core dump' );
85 is( $? & 127, 15, 'Term by signal $?' );
86 isnt( ${^CHILD_ERROR_NATIVE}, 0, 'Term by signal ${^CHILD_ERROR_NATIVE}' );
87 SKIP: {
e5218da5 88 skip("No POSIX", 3) unless $posix_ok;
5c5be41c 89 skip("No POSIX wait macros", 3) unless $wait_macros_ok;
e5218da5 90 ok(!POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}), "WIFEXITED");
91 ok(POSIX::WIFSIGNALED(${^CHILD_ERROR_NATIVE}), "WIFSIGNALED");
92 is(POSIX::WTERMSIG(${^CHILD_ERROR_NATIVE}), 15, "WTERMSIG");
5c5be41c 93 }
e5218da5 94 }
55a9fe1d 95
1a3aec58 96}
97
98if ($^O eq 'VMS') {
55a9fe1d 99
6cd63c21 100# On VMS, successful returns from system() are reported 0, VMS errors that
101# can not be translated to UNIX are reported as EVMSERR, which has a value
102# of 65535. Codes from 2 through 7 are assumed to be from non-compliant
103# VMS systems and passed through. Programs written to use _POSIX_EXIT()
104# codes like GNV will pass the numbers 2 through 255 encoded in the
105# C facility by multiplying the number by 8 and adding %x35A000 to it.
106# Perl will decode that number from children back to it's internal status.
107#
108# For native VMS status codes, success codes are odd numbered, error codes
109# are even numbered. The 3 LSBs of the code indicate if the success is
110# an informational message or the severity of the failure.
111#
112# Because the failure codes for the tests of the CLI facility status codes can
113# not be translated to UNIX error codes, they will be reported as EVMSERR,
114# even though Perl will exit with them having the VMS status codes.
115#
116# Note that this is testing the perl exit() routine, and not the VMS
117# DCL EXIT statement.
118#
119# The value %x1000000 has been added to the exit code to prevent the
120# status message from being sent to the STDOUT and STDERR stream.
121#
122# Double quotes are needed to pass these commands through DCL to PERL
55a9fe1d 123
6cd63c21 124 $exit = run("exit 268632065"); # %CLI-S-NORMAL
7a7fd8e0 125 is( $exit >> 8, 0, 'PERL success exit' );
6cd63c21 126 is( ${^CHILD_ERROR_NATIVE} & 7, 1, 'VMS success exit' );
55a9fe1d 127
6cd63c21 128 $exit = run("exit 268632067"); # %CLI-I-NORMAL
7a7fd8e0 129 is( $exit >> 8, 0, 'PERL informational exit' );
6cd63c21 130 is( ${^CHILD_ERROR_NATIVE} & 7, 3, 'VMS informational exit' );
55a9fe1d 131
6cd63c21 132 $exit = run("exit 268632064"); # %CLI-W-NORMAL
7a7fd8e0 133 is( $exit >> 8, 1, 'Perl warning exit' );
6cd63c21 134 is( ${^CHILD_ERROR_NATIVE} & 7, 0, 'VMS warning exit' );
55a9fe1d 135
6cd63c21 136 $exit = run("exit 268632066"); # %CLI-E-NORMAL
7a7fd8e0 137 is( $exit >> 8, 2, 'Perl error exit' );
6cd63c21 138 is( ${^CHILD_ERROR_NATIVE} & 7, 2, 'VMS error exit' );
55a9fe1d 139
6cd63c21 140 $exit = run("exit 268632068"); # %CLI-F-NORMAL
7a7fd8e0 141 is( $exit >> 8, 4, 'Perl fatal error exit' );
6cd63c21 142 is( ${^CHILD_ERROR_NATIVE} & 7, 4, 'VMS fatal exit' );
7a7fd8e0 143
144 $exit = run("exit 02015320012"); # POSIX exit code 1
145 is( $exit >> 8, 1, 'Posix exit code 1' );
146
147 $exit = run("exit 02015323771"); # POSIX exit code 255
148 is( $exit >> 8 , 255, 'Posix exit code 255' );
55a9fe1d 149}
14cf881c 150
412a0271 151$exit_arg = 42;
152$exit = run("END { \$? = $exit_arg }");
153
e08e1e1d 154# On VMS, in the child process the actual exit status will be SS$_ABORT,
6cd63c21 155# or 44, which is what you get from any non-zero value of $? except for
156# 65535 that has been dePOSIXified by STATUS_UNIX_SET. If $? is set to
157# 65535 internally when there is a VMS status code that is valid, and
158# when Perl exits, it will set that status code.
159#
160# In this test on VMS, the child process exit with a SS$_ABORT, which
161# the parent stores in ${^CHILD_ERROR_NATIVE}. The SS$_ABORT code is
162# then translated to the UNIX code EINTR which has the value of 4 on VMS.
163#
164# This is complex because Perl translates internally generated UNIX
165# status codes to SS$_ABORT on exit, but passes through unmodified UNIX
166# status codes that exit() is called with by scripts.
167
1a3aec58 168$exit_arg = (44 & 7) if $vms_exit_mode;
412a0271 169
170is( $exit >> 8, $exit_arg, 'Changing $? in END block' );
dc459aad 171}