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