c0e67f8e1288449110819659deca46b7df6ad04b
[sbcl.git] / tests / subr.sh
1 # To be sourced by shell scripts in the test suite.
2
3 # This software is part of the SBCL system. See the README file for
4 # more information.
5 #
6 # While most of SBCL is derived from the CMU CL system, the test
7 # files (like this one) were written from scratch after the fork
8 # from CMU CL.
9 #
10 # This software is in the public domain and is provided with
11 # absolutely no warranty. See the COPYING and CREDITS files for
12 # more information.
13
14 # Before sbcl-1.0.13 or so, we set up some environment variables to
15 # the absolute (POSIX) pathname naming the SBCL runtime, core, and
16 # home; but this runs afoul of the Bourne shell's repeated
17 # tokenization of its inputs, so now we use some shell functions.
18 . ../sbcl-pwd.sh
19 sbcl_pwd
20
21 # Make the shell bomb out whenever an unset shell variable is used.
22 # Note that scripts may toggle this if necessary.
23 set -u
24
25 # Initialize variables.
26 set -a # export all variables at assignment-time.
27 # Note: any script that uses the variables that name files should
28 # quote them (with double quotes), to contend with whitespace.
29 SBCL_HOME="$SBCL_PWD/../contrib"
30 SBCL_CORE="$SBCL_PWD/../output/sbcl.core"
31 SBCL_RUNTIME="$SBCL_PWD/../src/runtime/sbcl"
32 SBCL_ARGS="--noinform --no-sysinit --no-userinit --noprint --disable-debugger"
33
34 # Scripts that use these variables should quote them.
35 TEST_BASENAME="`basename $0`"
36 TEST_FILESTEM="`echo ${TEST_BASENAME%.sh} | sed 's/\./-/g'`"
37 TEST_DIRECTORY="$SBCL_PWD/$TEST_FILESTEM-$$"
38
39 # "Ten four" is the closest numerical slang I can find to "OK", so
40 # it's the Unix status value that we expect from a successful test.
41 # (Of course, zero is the usual success value, but we don't want to
42 # use that because SBCL returns that by default, so we might think
43 # we passed a test when in fact some error caused us to exit SBCL
44 # in a weird unexpected way. In contrast, 104 is unlikely to be
45 # returned unless we exit through the intended explicit "test
46 # successful" path.
47 EXIT_TEST_WIN=104
48 # Shell scripts in this test suite also return 104, so we need a
49 # convention for distinguishing successful execution of SBCL in one of
50 # our scripts.
51 EXIT_LISP_WIN=52
52 # Any test that exits with status 1 is an explicit failure.
53 EXIT_LOSE=1
54
55 LANG=C
56 LC_ALL=C
57 set +a
58
59 run_sbcl () (
60     set -u
61     if [ $# -gt 0 ]; then
62         "$SBCL_RUNTIME" --core "$SBCL_CORE" $SBCL_ARGS "$@"
63     else
64         "$SBCL_RUNTIME" --core "$SBCL_CORE" $SBCL_ARGS
65     fi
66 )
67
68 run_sbcl_with_core () (
69     set -u
70     core="$1"
71     shift
72     if [ $# -gt 0 ]; then
73         "$SBCL_RUNTIME" --core "$core" "$@"
74     else
75         "$SBCL_RUNTIME" --core "$core" $SBCL_ARGS
76     fi
77 )
78
79 # Most tests that run an SBCL have to check whether the child's exit
80 # status.  Our convention is that SBCL exits with status
81 # $EXIT_LISP_WIN to indicate a successful run; but some tests can't do
82 # this (e.g., ones that end in S-L-A-D), or need to indicate some
83 # other ways of succeeding.  So this routine takes a test name, the
84 # exit status of the child, and then an arbitrary number extra
85 # arguments that will be treated as status-code/message pairs for
86 # unusual successful ways for the inferior SBCL to exit.  If the exit
87 # code of the SBCL isn't found in the status-codes, the calling script
88 # will exit with a failure code.
89 check_status_maybe_lose () {
90     testname=$1
91     status=$2
92     lose=1
93     if [ $status = $EXIT_LISP_WIN ]; then
94         echo "test $testname ok"
95         lose=0
96     else
97         shift; shift;
98         while [ $# -gt 0 ]; do
99             if [ $status = $1 ]; then
100                 shift;
101                 echo "test $testname ok $1"
102                 lose=0
103                 break
104             fi
105             shift; shift
106         done
107     fi
108     if [ $lose = 1 ]; then
109         echo "test $testname failed: $status"
110         exit $EXIT_LOSE
111     fi
112     unset lose
113     unset status
114     unset testname
115 }
116
117 # Not every test needs to touch the file system, but enough do to have
118 # them consistently do so in subdirectories.  Note that such tests
119 # should not change their exit action, or do so only very carefully.
120 use_test_subdirectory () {
121     mkdir "$TEST_DIRECTORY"
122     cd "$TEST_DIRECTORY"
123     trap "cleanup_test_subdirectory" EXIT
124 }
125
126 cleanup_test_subdirectory () {
127     cd "$SBCL_PWD"
128     ( set -f; rm -r "$TEST_DIRECTORY" )
129 }