This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix the new exit() tests, by Gisle Aas :
[perl5.git] / t / run / exit.t
CommitLineData
14cf881c
MS
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
MS
9}
10
ee2f883e
GS
11# VMS and Windows need -e "...", most everything else works better with '
12my $quote = $^O =~ /^(VMS|MSWin\d+)$/ ? q{"} : q{'};
14cf881c
MS
13
14# Run some code, return its wait status.
15sub run {
16 my($code) = shift;
755bf813 17 return system($^X, "-e", $code);
14cf881c
MS
18}
19
0ab78052 20BEGIN {
dc459aad 21 # MacOS system() doesn't have good return value
6d016528 22 $numtests = ($^O eq 'VMS') ? 9 : ($^O eq 'MacOS') ? 0 : 17;
0ab78052 23}
14cf881c 24
69026470
JH
25require "test.pl";
26plan(tests => $numtests);
55a9fe1d 27
dc459aad 28if ($^O ne 'MacOS') {
55a9fe1d 29my $exit, $exit_arg;
14cf881c
MS
30
31$exit = run('exit');
32is( $exit >> 8, 0, 'Normal exit' );
e5218da5
GA
33is( $exit, $?, 'Normal exit $?' );
34is( ${^CHILD_ERROR_NATIVE}, 0, 'Normal exit ${^CHILD_ERROR_NATIVE}' );
14cf881c 35
55a9fe1d 36if ($^O ne 'VMS') {
e5218da5 37 my $posix_ok = eval { require POSIX; };
55a9fe1d
CB
38
39 $exit = run('exit 42');
40 is( $exit >> 8, 42, 'Non-zero exit' );
e5218da5
GA
41 is( $exit, $?, 'Non-zero exit $?' );
42 isnt( !${^CHILD_ERROR_NATIVE}, 0, 'Non-zero exit ${^CHILD_ERROR_NATIVE}' );
43 SKIP: {
44 skip("No POSIX", 3) unless $posix_ok;
45 ok(POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}), "WIFEXITED");
46 ok(!POSIX::WIFSIGNALED(${^CHILD_ERROR_NATIVE}), "WIFSIGNALED");
47 is(POSIX::WEXITSTATUS(${^CHILD_ERROR_NATIVE}), 42, "WEXITSTATUS");
48 }
49
50 $exit = run('kill 15, $$; sleep(1);');
51
52 is( $exit & 127, 15, 'Term by signal' );
53 ok( !($exit & 128), 'No core dump' );
54 is( $? & 127, 15, 'Term by signal $?' );
55 isnt( ${^CHILD_ERROR_NATIVE}, 0, 'Term by signal ${^CHILD_ERROR_NATIVE}' );
56 SKIP: {
57 skip("No POSIX", 3) unless $posix_ok;
58 ok(!POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}), "WIFEXITED");
59 ok(POSIX::WIFSIGNALED(${^CHILD_ERROR_NATIVE}), "WIFSIGNALED");
60 is(POSIX::WTERMSIG(${^CHILD_ERROR_NATIVE}), 15, "WTERMSIG");
61 }
55a9fe1d
CB
62
63} else {
64
65# On VMS, successful returns from system() are always 0, warnings are 1,
66# errors are 2, and fatal errors are 4.
67
68 $exit = run("exit 196609"); # %CLI-S-NORMAL
69 is( $exit >> 8, 0, 'success exit' );
70
71 $exit = run("exit 196611"); # %CLI-I-NORMAL
72 is( $exit >> 8, 0, 'informational exit' );
73
74 $exit = run("exit 196608"); # %CLI-W-NORMAL
75 is( $exit >> 8, 1, 'warning exit' );
76
77 $exit = run("exit 196610"); # %CLI-E-NORMAL
78 is( $exit >> 8, 2, 'error exit' );
79
80 $exit = run("exit 196612"); # %CLI-F-NORMAL
81 is( $exit >> 8, 4, 'fatal error exit' );
82}
14cf881c 83
412a0271
CB
84$exit_arg = 42;
85$exit = run("END { \$? = $exit_arg }");
86
87# On VMS, in the child process the actual exit status will be SS$_ABORT,
88# which is what you get from any non-zero value of $? that has been
e5218da5 89# dePOSIXified by STATUS_UNIX_SET. In the parent process, all we'll
412a0271
CB
90# see are the severity bits (0-2) shifted left by 8.
91$exit_arg = (44 & 7) if $^O eq 'VMS';
92
93is( $exit >> 8, $exit_arg, 'Changing $? in END block' );
dc459aad 94}