changeset:   25359:00545b5ac005
branch:      stable
tag:         tip
parent:      25352:3d5f953e2ef6
user:        Karl Wette <karl.wette@anu.edu.au>
date:        Tue May 08 16:10:32 2018 +1000
summary:     Add test for correct exit status behaviour

diff -r 3d5f953e2ef6 -r 00545b5ac005 test/module.mk
--- a/test/module.mk	Fri May 04 13:08:39 2018 -0700
+++ b/test/module.mk	Tue May 08 16:10:32 2018 +1000
@@ -94,6 +94,7 @@
 endef
 
 check-local: $(GENERATED_TEST_FILES) | $(OCTAVE_INTERPRETER_TARGETS) %reldir%/$(octave_dirstamp)
+	$(AM_V_AT)( cd %reldir% && $(SHELL) ./test-octave-exit-status.sh ../run-octave $(RUN_OCTAVE_OPTIONS) --norc --silent --no-history )
 	$(AM_V_at)$(call run-octave-tests)
 
 if AMCOND_HAVE_LLVM
diff -r 3d5f953e2ef6 -r 00545b5ac005 test/test-octave-exit-status.sh
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test-octave-exit-status.sh	Tue May 08 16:10:32 2018 +1000
@@ -0,0 +1,109 @@
+## Copyright (C) 2018 Karl Wette
+##
+## This file is part of Octave.
+##
+## Octave is free software: you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <https://www.gnu.org/licenses/>.
+
+## Tests that Octave returns the appropriate exit status
+
+# Check for command-line arguments
+if test "x$1" = x; then
+    echo "usage: $0 <octave-executable> <octave-options>..." >&2
+    exit 1
+fi
+
+# Create a temporary directory $dir in $TMPDIR (default /tmp).
+# Use mktemp if possible; otherwise fall back on mkdir,
+# with $RANDOM to make collisions less likely.
+# (Code snippet taken from Autoconf manual.)
+: "${TMPDIR:=/tmp}"
+{
+    tmpdir=`(umask 077 && mktemp -d "$TMPDIR/$0-XXXXXX") 2>/dev/null` && test -d "$tmpdir"
+} || {
+    tmpdir=$TMPDIR/$0-$$-$RANDOM
+    (umask 077 && mkdir "$tmpdir")
+} || {
+    echo "$0: could not create temporary directory" >&2
+    exit $?
+}
+
+# Begin tests
+echo
+echo -n "Testing Octave exit status behaviour: "
+tmpscript=$tmpdir/script.m
+
+# Normal completion should return exit status = 0
+echo '0;' > $tmpscript
+exitstatus=0
+"$@" $tmpscript >/dev/null 2>&1
+if test $? -ne $exitstatus; then
+    echo "FAIL"
+    echo "  execution of script '`cat $tmpscript`' should return $exitstatus"
+    echo
+    exit 1
+fi
+"$@" --eval "`cat $tmpscript`" >/dev/null 2>&1
+if test $? -ne $exitstatus; then
+    echo "FAIL"
+    echo "  evaluation of '`cat $tmpscript`' should return $exitstatus"
+    echo
+    exit 1
+fi
+
+# Raising an error() should return exit status = 1
+echo 'error("message");' > $tmpscript
+exitstatus=1
+"$@" $tmpscript >/dev/null 2>&1
+if test $? -ne $exitstatus; then
+    echo "FAIL"
+    echo "  execution of script '`cat $tmpscript`' should return $exitstatus"
+    echo
+    exit 1
+fi
+"$@" --eval "`cat $tmpscript`" >/dev/null 2>&1
+if test $? -ne $exitstatus; then
+    echo "FAIL"
+    echo "  evaluation of '`cat $tmpscript`' should return $exitstatus"
+    echo
+    exit 1
+fi
+
+# Calling exit() should return the requested exit status
+for existatus in 0 1 37; do
+    echo "exit($exitstatus);" > $tmpscript
+    "$@" $tmpscript >/dev/null 2>&1
+    if test $? -ne $exitstatus; then
+        echo "FAIL"
+        echo "  execution of script '`cat $tmpscript`' should return $exitstatus"
+        echo
+        exit 1
+    fi
+    "$@" --eval "`cat $tmpscript`" >/dev/null 2>&1
+    if test $? -ne $exitstatus; then
+        echo "FAIL"
+        echo "  evaluation of '`cat $tmpscript`' should return $exitstatus"
+        echo
+        exit 1
+    fi
+done
+
+# All tests passed
+echo "PASS"
+
+# Remove temporary directory
+rm -f $tmpscript
+rmdir $tmpdir
+
+exit 0

