Commit | Line | Data |
14cf881c |
1 | #!./perl |
2 | # |
3 | # Tests for perl exit codes, playing with $?, etc... |
4 | |
5 | |
6 | BEGIN { |
7 | chdir 't' if -d 't'; |
69026470 |
8 | @INC = qw(. ../lib); |
14cf881c |
9 | } |
10 | |
14cf881c |
11 | # Run some code, return its wait status. |
12 | sub run { |
13 | my($code) = shift; |
755bf813 |
14 | return system($^X, "-e", $code); |
14cf881c |
15 | } |
16 | |
0ab78052 |
17 | BEGIN { |
dc459aad |
18 | # MacOS system() doesn't have good return value |
6d016528 |
19 | $numtests = ($^O eq 'VMS') ? 9 : ($^O eq 'MacOS') ? 0 : 17; |
0ab78052 |
20 | } |
14cf881c |
21 | |
69026470 |
22 | require "test.pl"; |
23 | plan(tests => $numtests); |
55a9fe1d |
24 | |
dc459aad |
25 | if ($^O ne 'MacOS') { |
55a9fe1d |
26 | my $exit, $exit_arg; |
14cf881c |
27 | |
28 | $exit = run('exit'); |
29 | is( $exit >> 8, 0, 'Normal exit' ); |
e5218da5 |
30 | is( $exit, $?, 'Normal exit $?' ); |
31 | is( ${^CHILD_ERROR_NATIVE}, 0, 'Normal exit ${^CHILD_ERROR_NATIVE}' ); |
14cf881c |
32 | |
55a9fe1d |
33 | if ($^O ne 'VMS') { |
e5218da5 |
34 | my $posix_ok = eval { require POSIX; }; |
5c5be41c |
35 | my $wait_macros_ok = defined &POSIX::WIFEXITED; |
55a9fe1d |
36 | |
37 | $exit = run('exit 42'); |
38 | is( $exit >> 8, 42, 'Non-zero exit' ); |
e5218da5 |
39 | is( $exit, $?, 'Non-zero exit $?' ); |
40 | isnt( !${^CHILD_ERROR_NATIVE}, 0, 'Non-zero exit ${^CHILD_ERROR_NATIVE}' ); |
5c5be41c |
41 | SKIP: { |
42 | skip("No POSIX", 3) unless $posix_ok; |
43 | skip("No POSIX wait macros", 3) unless $wait_macros_ok; |
44 | ok(POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}), "WIFEXITED"); |
45 | ok(!POSIX::WIFSIGNALED(${^CHILD_ERROR_NATIVE}), "WIFSIGNALED"); |
46 | is(POSIX::WEXITSTATUS(${^CHILD_ERROR_NATIVE}), 42, "WEXITSTATUS"); |
e5218da5 |
47 | } |
48 | |
5c5be41c |
49 | SKIP: { |
50 | skip("Skip signals and core dump tests on Win32", 7) if $^O eq 'MSWin32'; |
51 | |
52 | $exit = run('kill 15, $$; sleep(1);'); |
e5218da5 |
53 | |
5c5be41c |
54 | is( $exit & 127, 15, 'Term by signal' ); |
55 | ok( !($exit & 128), 'No core dump' ); |
56 | is( $? & 127, 15, 'Term by signal $?' ); |
57 | isnt( ${^CHILD_ERROR_NATIVE}, 0, 'Term by signal ${^CHILD_ERROR_NATIVE}' ); |
58 | SKIP: { |
e5218da5 |
59 | skip("No POSIX", 3) unless $posix_ok; |
5c5be41c |
60 | skip("No POSIX wait macros", 3) unless $wait_macros_ok; |
e5218da5 |
61 | ok(!POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}), "WIFEXITED"); |
62 | ok(POSIX::WIFSIGNALED(${^CHILD_ERROR_NATIVE}), "WIFSIGNALED"); |
63 | is(POSIX::WTERMSIG(${^CHILD_ERROR_NATIVE}), 15, "WTERMSIG"); |
5c5be41c |
64 | } |
e5218da5 |
65 | } |
55a9fe1d |
66 | |
67 | } else { |
68 | |
69 | # On VMS, successful returns from system() are always 0, warnings are 1, |
70 | # errors are 2, and fatal errors are 4. |
71 | |
72 | $exit = run("exit 196609"); # %CLI-S-NORMAL |
73 | is( $exit >> 8, 0, 'success exit' ); |
74 | |
75 | $exit = run("exit 196611"); # %CLI-I-NORMAL |
76 | is( $exit >> 8, 0, 'informational exit' ); |
77 | |
78 | $exit = run("exit 196608"); # %CLI-W-NORMAL |
79 | is( $exit >> 8, 1, 'warning exit' ); |
80 | |
81 | $exit = run("exit 196610"); # %CLI-E-NORMAL |
82 | is( $exit >> 8, 2, 'error exit' ); |
83 | |
84 | $exit = run("exit 196612"); # %CLI-F-NORMAL |
85 | is( $exit >> 8, 4, 'fatal error exit' ); |
86 | } |
14cf881c |
87 | |
412a0271 |
88 | $exit_arg = 42; |
89 | $exit = run("END { \$? = $exit_arg }"); |
90 | |
91 | # On VMS, in the child process the actual exit status will be SS$_ABORT, |
92 | # which is what you get from any non-zero value of $? that has been |
e5218da5 |
93 | # dePOSIXified by STATUS_UNIX_SET. In the parent process, all we'll |
412a0271 |
94 | # see are the severity bits (0-2) shifted left by 8. |
95 | $exit_arg = (44 & 7) if $^O eq 'VMS'; |
96 | |
97 | is( $exit >> 8, $exit_arg, 'Changing $? in END block' ); |
dc459aad |
98 | } |