summaryrefslogtreecommitdiff
path: root/t/run/exit.t
blob: 5075b9368e55b4fd7350be9218e4f8a8f0ae8d60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!./perl
#
# Tests for perl exit codes, playing with $?, etc...


BEGIN {
    chdir 't' if -d 't';
    @INC = qw(. ../lib);
}

# Run some code, return its wait status.
sub run {
    my($code) = shift;
    return system($^X, "-e", $code);
}

BEGIN {
    # MacOS system() doesn't have good return value
    $numtests = ($^O eq 'VMS') ? 9 : ($^O eq 'MacOS') ? 0 : 17;
}

require "test.pl";
plan(tests => $numtests);

if ($^O ne 'MacOS') {
my $exit, $exit_arg;

$exit = run('exit');
is( $exit >> 8, 0,              'Normal exit' );
is( $exit, $?,                  'Normal exit $?' );
is( ${^CHILD_ERROR_NATIVE}, 0,  'Normal exit ${^CHILD_ERROR_NATIVE}' );

if ($^O ne 'VMS') {
  my $posix_ok = eval { require POSIX; };
  my $wait_macros_ok = defined &POSIX::WIFEXITED;

  $exit = run('exit 42');
  is( $exit >> 8, 42,             'Non-zero exit' );
  is( $exit, $?,                  'Non-zero exit $?' );
  isnt( !${^CHILD_ERROR_NATIVE}, 0, 'Non-zero exit ${^CHILD_ERROR_NATIVE}' );
  SKIP: {
    skip("No POSIX", 3) unless $posix_ok;
    skip("No POSIX wait macros", 3) unless $wait_macros_ok;
    ok(POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}), "WIFEXITED");
    ok(!POSIX::WIFSIGNALED(${^CHILD_ERROR_NATIVE}), "WIFSIGNALED");
    is(POSIX::WEXITSTATUS(${^CHILD_ERROR_NATIVE}), 42, "WEXITSTATUS");
  }

  SKIP: {
    skip("Skip signals and core dump tests on Win32", 7) if $^O eq 'MSWin32';

    $exit = run('kill 15, $$; sleep(1);');

    is( $exit & 127, 15,            'Term by signal' );
    ok( !($exit & 128),             'No core dump' );
    is( $? & 127, 15,               'Term by signal $?' );
    isnt( ${^CHILD_ERROR_NATIVE},  0, 'Term by signal ${^CHILD_ERROR_NATIVE}' );
    SKIP: {
      skip("No POSIX", 3) unless $posix_ok;
      skip("No POSIX wait macros", 3) unless $wait_macros_ok;
      ok(!POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}), "WIFEXITED");
      ok(POSIX::WIFSIGNALED(${^CHILD_ERROR_NATIVE}), "WIFSIGNALED");
      is(POSIX::WTERMSIG(${^CHILD_ERROR_NATIVE}), 15, "WTERMSIG");
    }
  }

} else {

# On VMS, successful returns from system() are always 0, warnings are 1,
# errors are 2, and fatal errors are 4.

  $exit = run("exit 196609"); # %CLI-S-NORMAL
  is( $exit >> 8, 0,             'success exit' );

  $exit = run("exit 196611");  # %CLI-I-NORMAL
  is( $exit >> 8, 0,             'informational exit' );

  $exit = run("exit 196608");  # %CLI-W-NORMAL
  is( $exit >> 8, 1,             'warning exit' );

  $exit = run("exit 196610");  # %CLI-E-NORMAL
  is( $exit >> 8, 2,             'error exit' );

  $exit = run("exit 196612");  # %CLI-F-NORMAL
  is( $exit >> 8, 4,             'fatal error exit' );
}

$exit_arg = 42;
$exit = run("END { \$? = $exit_arg }");

# On VMS, in the child process the actual exit status will be SS$_ABORT, 
# which is what you get from any non-zero value of $? that has been 
# dePOSIXified by STATUS_UNIX_SET.  In the parent process, all we'll 
# see are the severity bits (0-2) shifted left by 8.
$exit_arg = (44 & 7) if $^O eq 'VMS';  

is( $exit >> 8, $exit_arg,             'Changing $? in END block' );
}