Fix cross build.
[sbcl.git] / tests / subr.sh
blob1c115f15cd428315fffa78adc246215613f908f5
1 # To be sourced by shell scripts in the test suite.
3 # This software is part of the SBCL system. See the README file for
4 # more information.
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.
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.
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
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
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="${TEST_SBCL_HOME:-$SBCL_PWD/../obj/sbcl-home}"
30 SBCL_CORE="${TEST_SBCL_CORE:-$SBCL_PWD/../output/sbcl.core}"
31 SBCL_RUNTIME="${TEST_SBCL_RUNTIME:-$SBCL_PWD/../src/runtime/sbcl}"
32 SBCL_ARGS="${TEST_SBCL_ARGS:---disable-ldb --noinform --no-sysinit --no-userinit --noprint --disable-debugger}"
35 # Tests should probably not care about their own name.
36 script_basename=`basename $0`
37 # Scripts that use this variable should quote it.
38 TEST_FILESTEM=`basename "${script_basename}" | sed -e 's/\.sh$//' -e 's/\./-/g'`
40 TEST_DIRECTORY="${SBCL_PWD}/${TEST_FILESTEM}-$$"
41 export TEST_DIRECTORY
43 # "Ten four" is the closest numerical slang I can find to "OK", so
44 # it's the Unix status value that we expect from a successful test.
45 # (Of course, zero is the usual success value, but we don't want to
46 # use that because SBCL returns that by default, so we might think
47 # we passed a test when in fact some error caused us to exit SBCL
48 # in a weird unexpected way. In contrast, 104 is unlikely to be
49 # returned unless we exit through the intended explicit "test
50 # successful" path.
51 EXIT_TEST_WIN=104
52 # Shell scripts in this test suite also return 104, so we need a
53 # convention for distinguishing successful execution of SBCL in one of
54 # our scripts.
55 EXIT_LISP_WIN=52
56 # Any test that exits with status 1 is an explicit failure.
57 EXIT_LOSE=1
59 LANG=C
60 LC_ALL=C
61 set +a
63 run_sbcl () (
64 set -u
65 if [ $# -gt 0 ]; then
66 "$SBCL_RUNTIME" --lose-on-corruption --core "$SBCL_CORE" $SBCL_ARGS --eval "(setf sb-ext:*evaluator-mode* :${TEST_SBCL_EVALUATOR_MODE:-compile})" "$@"
67 else
68 "$SBCL_RUNTIME" --lose-on-corruption --core "$SBCL_CORE" $SBCL_ARGS --eval "(setf sb-ext:*evaluator-mode* :${TEST_SBCL_EVALUATOR_MODE:-compile})"
72 run_sbcl_with_args () (
73 set -u
74 "$SBCL_RUNTIME" --lose-on-corruption --core "$SBCL_CORE" "$@"
77 run_sbcl_with_core () (
78 set -u
79 core="$1"
80 shift
81 if [ $# -gt 0 ]; then
82 "$SBCL_RUNTIME" --lose-on-corruption --core "$core" "$@"
83 else
84 "$SBCL_RUNTIME" --lose-on-corruption --core "$core" $SBCL_ARGS --eval "(setf sb-ext:*evaluator-mode* :${TEST_SBCL_EVALUATOR_MODE:-compile})"
88 # Most tests that run an SBCL have to check whether the child's exit
89 # status. Our convention is that SBCL exits with status
90 # $EXIT_LISP_WIN to indicate a successful run; but some tests can't do
91 # this (e.g., ones that end in S-L-A-D), or need to indicate some
92 # other ways of succeeding. So this routine takes a test name, the
93 # exit status of the child, and then an arbitrary number extra
94 # arguments that will be treated as status-code/message pairs for
95 # unusual successful ways for the inferior SBCL to exit. If the exit
96 # code of the SBCL isn't found in the status-codes, the calling script
97 # will exit with a failure code.
98 check_status_maybe_lose () {
99 testname=$1
100 status=$2
101 lose=1
102 if [ $status = $EXIT_LISP_WIN ]; then
103 echo "test $testname ok"
104 lose=0
105 else
106 shift; shift;
107 while [ $# -gt 0 ]; do
108 if [ $status = $1 ]; then
109 shift;
110 echo "test $testname ok $1"
111 lose=0
112 break
114 shift; shift
115 done
117 if [ $lose = 1 ]; then
118 echo "test $testname failed: $status"
119 exit $EXIT_LOSE
121 unset lose
122 unset status
123 unset testname
126 # Picking an output dir is delayed until actually needed.
127 # In particular we can't read "$SBCL_SOFTWARE_TYPE" until after test-util
128 # assigns it into the environment, but this script is also sourced
129 # by run-tests.sh itself, which means that test-util hasn't done its thing.
131 pick_random_output_dir() {
132 # Avoid writing into the source tree as much as possible.
133 if [ "$SBCL_SOFTWARE_TYPE" != OpenBSD ]; then
134 # Don't try to run sbcl from /tmp on openbsd as it's unlikely to be
135 # mounted with wxallowed
136 # If a test doesn't create an executable core, it would work to use /tmp,
137 # but this utility is unaware of the intent of each test.
138 # This means that some tests might flake our, and/or leave junk in your tree.
139 base_dir=${TMPDIR:-/tmp}
140 else
141 base_dir="$SBCL_PWD"
143 TEST_DIRECTORY="${base_dir}/${TEST_FILESTEM}-$$"
146 # Not every test needs to touch the file system, but enough do to have
147 # them consistently do so in subdirectories. Note that such tests
148 # should not change their exit action, or do so only very carefully.
149 use_test_subdirectory () { # not a "subdirectory" now, but don't feel like renaming
150 if test -d "$TEST_DIRECTORY"
151 then
152 cleanup_test_subdirectory
154 pick_random_output_dir
155 mkdir "$TEST_DIRECTORY"
156 cd "$TEST_DIRECTORY"
157 trap "cleanup_test_subdirectory" EXIT
160 # FIXME: Is it really true that a test must alter the source tree? How pathetic.
161 # In particular, failures can occur in the :READDIR/DIRENT-NAME test of sb-posix
162 # when using parallel-exec, because "run-sbcl-test-NNNN" randomly appears
163 # in either the SB-POSIX:READDIR call or CL:DIRECTORY but not both.
164 use_test_subdirectory_in_source_tree () { # DON'T USE THIS!
165 if test -d "$TEST_DIRECTORY"
166 then
167 cleanup_test_subdirectory
169 mkdir "$TEST_DIRECTORY"
170 cd "$TEST_DIRECTORY"
171 trap "cleanup_test_subdirectory" EXIT
174 # Do the above but without changing the current directory
175 create_test_subdirectory () {
176 if test -d "$TEST_DIRECTORY"
177 then
178 cleanup_test_subdirectory
180 pick_random_output_dir
181 mkdir "$TEST_DIRECTORY"
182 trap "cleanup_test_subdirectory" EXIT
185 cleanup_test_subdirectory () {
186 cd "$SBCL_PWD"
187 ( set -f; rm -r "$TEST_DIRECTORY" )