Backslashes inside '' inside Makefiles seem to be non-portable in their
[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
69026470 23require "test.pl";
24plan(tests => $numtests);
55a9fe1d 25
6cd63c21 26my $native_success = 0;
27 $native_success = 1 if $^O eq 'VMS';
28
dc459aad 29if ($^O ne 'MacOS') {
55a9fe1d 30my $exit, $exit_arg;
14cf881c 31
32$exit = run('exit');
33is( $exit >> 8, 0, 'Normal exit' );
e5218da5 34is( $exit, $?, 'Normal exit $?' );
6cd63c21 35is( ${^CHILD_ERROR_NATIVE}, $native_success, 'Normal exit ${^CHILD_ERROR_NATIVE}' );
14cf881c 36
55a9fe1d 37if ($^O ne 'VMS') {
e5218da5 38 my $posix_ok = eval { require POSIX; };
5c5be41c 39 my $wait_macros_ok = defined &POSIX::WIFEXITED;
ac0638ad 40 eval { POSIX::WIFEXITED() };
41 $wait_macros_ok = 0 if $@;
55a9fe1d 42 $exit = run('exit 42');
43 is( $exit >> 8, 42, 'Non-zero exit' );
e5218da5 44 is( $exit, $?, 'Non-zero exit $?' );
45 isnt( !${^CHILD_ERROR_NATIVE}, 0, 'Non-zero exit ${^CHILD_ERROR_NATIVE}' );
5c5be41c 46 SKIP: {
47 skip("No POSIX", 3) unless $posix_ok;
48 skip("No POSIX wait macros", 3) unless $wait_macros_ok;
49 ok(POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}), "WIFEXITED");
50 ok(!POSIX::WIFSIGNALED(${^CHILD_ERROR_NATIVE}), "WIFSIGNALED");
51 is(POSIX::WEXITSTATUS(${^CHILD_ERROR_NATIVE}), 42, "WEXITSTATUS");
e5218da5 52 }
53
5c5be41c 54 SKIP: {
55 skip("Skip signals and core dump tests on Win32", 7) if $^O eq 'MSWin32';
56
57 $exit = run('kill 15, $$; sleep(1);');
e5218da5 58
5c5be41c 59 is( $exit & 127, 15, 'Term by signal' );
60 ok( !($exit & 128), 'No core dump' );
61 is( $? & 127, 15, 'Term by signal $?' );
62 isnt( ${^CHILD_ERROR_NATIVE}, 0, 'Term by signal ${^CHILD_ERROR_NATIVE}' );
63 SKIP: {
e5218da5 64 skip("No POSIX", 3) unless $posix_ok;
5c5be41c 65 skip("No POSIX wait macros", 3) unless $wait_macros_ok;
e5218da5 66 ok(!POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}), "WIFEXITED");
67 ok(POSIX::WIFSIGNALED(${^CHILD_ERROR_NATIVE}), "WIFSIGNALED");
68 is(POSIX::WTERMSIG(${^CHILD_ERROR_NATIVE}), 15, "WTERMSIG");
5c5be41c 69 }
e5218da5 70 }
55a9fe1d 71
72} else {
73
6cd63c21 74# On VMS, successful returns from system() are reported 0, VMS errors that
75# can not be translated to UNIX are reported as EVMSERR, which has a value
76# of 65535. Codes from 2 through 7 are assumed to be from non-compliant
77# VMS systems and passed through. Programs written to use _POSIX_EXIT()
78# codes like GNV will pass the numbers 2 through 255 encoded in the
79# C facility by multiplying the number by 8 and adding %x35A000 to it.
80# Perl will decode that number from children back to it's internal status.
81#
82# For native VMS status codes, success codes are odd numbered, error codes
83# are even numbered. The 3 LSBs of the code indicate if the success is
84# an informational message or the severity of the failure.
85#
86# Because the failure codes for the tests of the CLI facility status codes can
87# not be translated to UNIX error codes, they will be reported as EVMSERR,
88# even though Perl will exit with them having the VMS status codes.
89#
90# Note that this is testing the perl exit() routine, and not the VMS
91# DCL EXIT statement.
92#
93# The value %x1000000 has been added to the exit code to prevent the
94# status message from being sent to the STDOUT and STDERR stream.
95#
96# Double quotes are needed to pass these commands through DCL to PERL
55a9fe1d 97
6cd63c21 98 $exit = run("exit 268632065"); # %CLI-S-NORMAL
7a7fd8e0 99 is( $exit >> 8, 0, 'PERL success exit' );
6cd63c21 100 is( ${^CHILD_ERROR_NATIVE} & 7, 1, 'VMS success exit' );
55a9fe1d 101
6cd63c21 102 $exit = run("exit 268632067"); # %CLI-I-NORMAL
7a7fd8e0 103 is( $exit >> 8, 0, 'PERL informational exit' );
6cd63c21 104 is( ${^CHILD_ERROR_NATIVE} & 7, 3, 'VMS informational exit' );
55a9fe1d 105
6cd63c21 106 $exit = run("exit 268632064"); # %CLI-W-NORMAL
7a7fd8e0 107 is( $exit >> 8, 1, 'Perl warning exit' );
6cd63c21 108 is( ${^CHILD_ERROR_NATIVE} & 7, 0, 'VMS warning exit' );
55a9fe1d 109
6cd63c21 110 $exit = run("exit 268632066"); # %CLI-E-NORMAL
7a7fd8e0 111 is( $exit >> 8, 2, 'Perl error exit' );
6cd63c21 112 is( ${^CHILD_ERROR_NATIVE} & 7, 2, 'VMS error exit' );
55a9fe1d 113
6cd63c21 114 $exit = run("exit 268632068"); # %CLI-F-NORMAL
7a7fd8e0 115 is( $exit >> 8, 4, 'Perl fatal error exit' );
6cd63c21 116 is( ${^CHILD_ERROR_NATIVE} & 7, 4, 'VMS fatal exit' );
7a7fd8e0 117
118 $exit = run("exit 02015320012"); # POSIX exit code 1
119 is( $exit >> 8, 1, 'Posix exit code 1' );
120
121 $exit = run("exit 02015323771"); # POSIX exit code 255
122 is( $exit >> 8 , 255, 'Posix exit code 255' );
55a9fe1d 123}
14cf881c 124
412a0271 125$exit_arg = 42;
126$exit = run("END { \$? = $exit_arg }");
127
128# On VMS, in the child process the actual exit status will be SS$_ABORT,
6cd63c21 129# or 44, which is what you get from any non-zero value of $? except for
130# 65535 that has been dePOSIXified by STATUS_UNIX_SET. If $? is set to
131# 65535 internally when there is a VMS status code that is valid, and
132# when Perl exits, it will set that status code.
133#
134# In this test on VMS, the child process exit with a SS$_ABORT, which
135# the parent stores in ${^CHILD_ERROR_NATIVE}. The SS$_ABORT code is
136# then translated to the UNIX code EINTR which has the value of 4 on VMS.
137#
138# This is complex because Perl translates internally generated UNIX
139# status codes to SS$_ABORT on exit, but passes through unmodified UNIX
140# status codes that exit() is called with by scripts.
141
7a7fd8e0 142$exit_arg = (44 & 7) if $^O eq 'VMS';
412a0271 143
144is( $exit >> 8, $exit_arg, 'Changing $? in END block' );
dc459aad 145}