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