This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Gisle noted an unused variable
[perl5.git] / t / run / exit.t
index 20c2c49..ef39e04 100644 (file)
@@ -5,28 +5,87 @@
 
 BEGIN {
     chdir 't' if -d 't';
 
 BEGIN {
     chdir 't' if -d 't';
-    @INC = '../lib';
+    @INC = qw(. ../lib);
 }
 
 }
 
-# VMS and Windows need -e "...", most everything else works better with '
-my $quote = $^O =~ /^(VMS|MSWin\d+)$/ ? q{"} : q{'};
-
 # Run some code, return its wait status.
 sub run {
     my($code) = shift;
 # Run some code, return its wait status.
 sub run {
     my($code) = shift;
-    my $cmd = "$^X -e ";
-    return system($cmd.$quote.$code.$quote);
+    return system($^X, "-e", $code);
 }
 
 }
 
-use Test::More tests => 3;
+BEGIN {
+    # MacOS system() doesn't have good return value
+    $numtests = ($^O eq 'VMS') ? 9 : ($^O eq 'MacOS') ? 0 : 17;
+}
 
 
-my $exit;
+require "test.pl";
+plan(tests => $numtests);
+
+if ($^O ne 'MacOS') {
+my $exit, $exit_arg;
 
 $exit = run('exit');
 is( $exit >> 8, 0,              'Normal exit' );
 
 $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; };
+
+  $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;
+      ok(POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}), "WIFEXITED");
+      ok(!POSIX::WIFSIGNALED(${^CHILD_ERROR_NATIVE}), "WIFSIGNALED");
+      is(POSIX::WEXITSTATUS(${^CHILD_ERROR_NATIVE}), 42, "WEXITSTATUS");
+  }
+
+  $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;
+      ok(!POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}), "WIFEXITED");
+      ok(POSIX::WIFSIGNALED(${^CHILD_ERROR_NATIVE}), "WIFSIGNALED");
+      is(POSIX::WTERMSIG(${^CHILD_ERROR_NATIVE}), 15, "WTERMSIG");
+  }
+
+} else {
 
 
-$exit = run('exit 42');
-is( $exit >> 8, 42,             'Non-zero exit' );
+# On VMS, successful returns from system() are always 0, warnings are 1,
+# errors are 2, and fatal errors are 4.
 
 
-$exit = run('END { $? = 42 }');
-is( $exit >> 8, 42,             'Changing $? in END block' );
+  $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' );
+}