@@@ work in progress
[runlisp] / tests.at
CommitLineData
e29834b8
MW
1### -*-autotest-*-
2###
3### Test script for `runlisp'
4###
5### (c) 2020 Mark Wooding
6###
7
8###----- Licensing notice ---------------------------------------------------
9###
10### This file is part of Runlisp, a tool for invoking Common Lisp scripts.
11###
12### Runlisp is free software: you can redistribute it and/or modify it
13### under the terms of the GNU General Public License as published by the
14### Free Software Foundation; either version 3 of the License, or (at your
15### option) any later version.
16###
17### Runlisp is distributed in the hope that it will be useful, but WITHOUT
18### ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19### FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20### for more details.
21###
22### You should have received a copy of the GNU General Public License
23### along with Runlisp. If not, see <https://www.gnu.org/licenses/>.
24
25m4_define([RUNLISP_PATH], [$abs_top_builddir/runlisp])
26
27m4_define([_FOREACH], [dnl
28m4_if([$#], [1], [_foreach_func($1)],
29 [_foreach_func($1)[]_FOREACH(m4_shift($@))])])
30m4_define([FOREACH], [dnl
31m4_pushdef([_foreach_func], [$2])dnl
32_FOREACH($1)[]dnl
33m4_popdef([_foreach_func])])
34
35m4_define([LISP_SYSTEMS],
36 [sbcl, sbcl/noimage,
37 ccl, ccl/noimage,
38 clisp, clisp/noimage,
39 ecl, ecl/noimage,
40 cmucl, cmucl/noimage,
41 abcl, abcl/noimage])
42
43m4_define([PREPARE_LISP_TEST],
44[lisp=$1
45LISP=$m4_translit(m4_bregexp([$1], [/.*$], []), [a-z], [A-Z])
46AT_SKIP_IF([test "x$LISP" = x])
47case $lisp in
48 */*) opt=${lisp#*/} lisp=${lisp%%/*} ;;
49 *) opt="" ;;
50esac
51case /$opt/ in
52 */noimage/*) RUNLISP_IMAGEDIR=./notexist ;;
53 *) RUNLISP_IMAGEDIR=$abs_top_builddir ;;
54esac
55export RUNLISP_IMAGEDIR])
56
57m4_define([WHICH_LISP],
58[(or #+sbcl "sbcl" #+ccl "ccl" #+clisp "clisp"
59 #+ecl "ecl" #+cmu "cmucl" #+abcl "abcl"
60 "unknown")])
61
62m4_define([NL], [
63])
64
65m4_define([SETUP_RUNLISP_IMAGEDIR],
66[RUNLISP_IMAGEDIR=$abs_top_builddir; export RUNLISP_IMAGEDIR])
67
68m4_define([SETUP_RUNLISP_EVAL],
69[RUNLISP_EVAL=$abs_top_srcdir/eval.lisp; export RUNLISP_EVAL])
70
71###--------------------------------------------------------------------------
72### A basic smoke test.
73
74## Check that the system basically works, by running a trivial test program.
75## Also try to verify that we're not running user or site startup code,
76## though this is hard to do in general.
77FOREACH([LISP_SYSTEMS],
78[AT_SETUP([$1 smoke])
79AT_KEYWORDS([script smoke $1])
80PREPARE_LISP_TEST([$1])
81
82## Prepare a user-init file which will break the test if it's run by printing
83## something unexpected.
84mkdir HOME
85case $lisp in
86 sbcl) initfile=.sbclrc ;;
87 ccl) initfile=.ccl-init.lisp ;;
88 clisp) initfile=.clisprc.lisp ;;
89 ecl) initfile=.eclrc ;;
90 cmucl) initfile=.cmucl-init.lisp ;;
91 abcl) initfile=.abclrc ;;
92esac
93cat >HOME/$initfile <<EOF
94(format t "*** I should not be seen~%")
95EOF
96HOME=$(pwd)/HOME; export HOME
97
98## Prepare the script.
99cat >test-script <<EOF
100[#!] RUNLISP_PATH -L$lisp
101
102;; Print a greeting to \`*standard-output*', identifying the Lisp system, so
103;; that we can tell whether we called the right one.
104(format t "Hello from ~A (~A)!~%" (lisp-implementation-type) WHICH_LISP)
105
106#! this should be a comment everywhere
107
108;; Make sure that \`*error-output*' is hooked up properly.
109(format *error-output* "to stderr~%")
110
111;; Make sure that \`*standard-input*' is hooked up properly, by reading a line
112;; and echoing it.
113(format t "from stdin: ~S~%" (read-line))
114
115;; Check that \`:runlisp-script' is set in \`*features*'. If not, \`assert'
116;; will at least write a complaint to some stream, which will fail the test.
117(assert (member :runlisp-script *features*))
118
119;; Check that there are no symbols present (interned or imported) in the
120;; \`common-lisp-user' package. Obviously, we must avoid interning any
121;; ourselves. Alas, ABCL and ECL pollute \`cl-user' out of the box. (ECL
122;; does this deliberately; ABCL's ``adjoin.lisp' lacks an \`in-package' form.
123(let ((#1=#:syms (sort (loop :for #2=#:s :being :the :present-symbols
124 :of *package*
125 :collect #2#)
126 #'string<)))
127 (format t "package \`~A' [~:[ok~;has unexpected symbols ~:*~S~]]~%"
128 (package-name *package*) #1#))
129
130;; Print the program name and command-line arguments.
131(format t "program name = ~S~%~
132 arguments = ~:S~%"
133 (uiop:argv0)
134 uiop:*command-line-arguments*)
135EOF
136chmod +x test-script
137
138case $lisp in
139 sbcl) impl="SBCL" ;;
140 ccl) impl="Clozure Common Lisp" ;;
141 clisp) impl="CLISP" ;;
142 ecl) impl="ECL" ;;
143 cmucl) impl="CMU Common Lisp" ;;
144 abcl) impl="Armed Bear Common Lisp" ;;
145 *) AT_FAIL_IF([:]) ;;
146esac
147
148## Prepare an input file.
149echo some random text >stdin
150
151## Prepare the reference stdout and stderr.
152cat >stdout.ref <<EOF
153Hello from $impl ($lisp)!
154from stdin: "some random text"
155package \`COMMON-LISP-USER' ok
156program name = "./test-script"
157arguments = ("--eval" "nonsense" "--" "more" "args" "here")
158EOF
159cat >stderr.ref <<EOF
160to stderr
161EOF
162
163AT_CHECK([echo "lisp=$lisp opt=$opt"; env | grep RUNLISP | sort],, [stdout])
164AT_CHECK([./test-script --eval nonsense -- more args here <stdin],,
165 [stdout], [stderr])
166AT_CHECK([diff -u stdout.ref stdout])
167AT_CHECK([diff -u stderr.ref stderr])
168AT_CLEANUP])
169
170###--------------------------------------------------------------------------
171### Check error handling.
172
173FOREACH([LISP_SYSTEMS],
174[AT_SETUP([$1 errors])
175AT_KEYWORDS([script error $1])
176PREPARE_LISP_TEST([$1])
177
178## A simple script which signals an error without catching it.
179cat >test <<EOF
180[#!] RUNLISP_PATH -L$lisp
181(error "just kill me now")
182EOF
183chmod +x test
184
185## As long as it exits with a nonzero status, I'm happy. Some Lisps
186## desperately want to drop the user into an interactive debugger, which is
187## possibly useful for a developer, but an end user is now faced with a
188## confusing internal error message /and/ a confusing prompt which won't go
189## away. The output may still be confusing and (certainly in CCL's case)
190## voluminous, but that's not significantly worse than Tcl or Java.
191./test >out >err; rc=$?
192AT_FAIL_IF([test $rc = 0])
193
194AT_CLEANUP])
195
196###--------------------------------------------------------------------------
197### Check eval mode.
198
199### Eval mode is implemented centrally through a script, so we don't need to
200### test it separately for each Lisp implementation.
201
202AT_SETUP([eval mode])
203AT_KEYWORDS([eval common])
204SETUP_RUNLISP_IMAGEDIR
205SETUP_RUNLISP_EVAL
206
207## A very basic smoke test.
208AT_CHECK([RUNLISP_PATH -e '(format t "Just another Lisp hacker!~%")'],,
209[Just another Lisp hacker!
210])
211
212## The `:runlisp-script' keyword should /not/ be in `*features*'.
213traceon
214AT_CHECK([RUNLISP_PATH -p '(find :runlisp-script *features*)'],, [NIL
215])
216
217## Check a mixture of all the kinds of evaluation. We'll need a stunt script
218## to make this work. Also check that the individual forms are read and
219## evaluated one at a time, so that each one can affect the way the reader
220## interprets the next.
221cat >script.lisp <<EOF
222#! just want to check that Lisp doesn't choke on a shebang line here
223(format t "And we're running the script...~%~
224 Command-line arguments: ~:S~%~
225 Symbols in package \`~A': ~:S~%"
226 uiop:*command-line-arguments*
227 (package-name *package*)
228 (sort (loop :for #2=#:s :being :the :present-symbols :of *package*
229 :collect #2#)
230 #'string<))
231EOF
232AT_CHECK([RUNLISP_PATH \
233 -e '(defpackage [#:]runlisp-test (:export [#:]foo))
234 (defvar runlisp-test:foo 1)' \
235 -p runlisp-test:foo \
236 -e '(incf runlisp-test:foo)' \
237 -l script.lisp \
238 -p runlisp-test:foo \
239 -- -e one two three],,
240[1
241And we're running the script...
242Command-line arguments: ("-e" "one" "two" "three")
243Symbols in package `COMMON-LISP-USER': ()
2442
245])
246
247AT_CLEANUP
248
249###--------------------------------------------------------------------------
250### Check Lisp system selection and preference work.
251
252AT_SETUP([preferences])
253AT_KEYWORDS([prefs common])
254SETUP_RUNLISP_IMAGEDIR
255SETUP_RUNLISP_EVAL
256
257## Before we can make this happen, we need to decide on three Lisp systems,
258## two of which actually work, and one other. These are ordered by startup
259## speed.
260unset lisp0 lisp1 badlisp; win=nil
261set -- cmucl sbcl ccl clisp ecl abcl
262while :; do
263 case $# in 0) break ;; esac
264 lisp=$1; shift
265 if RUNLISP_PATH -L$lisp -enil 2>/dev/null; then good=t; else good=nil; fi
266 case ${lisp0+t},${badlisp+t},$good in
267 ,*,t) lisp0=$lisp ;;
268 t,*,t) lisp1=$lisp win=t; break ;;
269 *,,nil) badlisp=$lisp ;;
270 esac
271done
272AT_CHECK([case $win in nil) exit 77 ;; esac])
273case ${badlisp+t} in t) ;; *) badlisp=$1 ;; esac
274BADLISP=$(echo $badlisp | tr a-z A-Z)
275eval $BADLISP=/notexist/definitely-wrong
276export $BADLISP
277echo Primary Lisp = $lisp0
278echo Secondary Lisp = $lisp1
279echo Bad Lisp = $badlisp
280
281## Check that our selection worked.
282AT_CHECK_UNQUOTED([RUNLISP_PATH -L$lisp0 -p 'WHICH_LISP'],, ["$lisp0"NL])
283AT_CHECK_UNQUOTED([RUNLISP_PATH -L$lisp1 -p 'WHICH_LISP'],, ["$lisp1"NL])
284AT_CHECK([RUNLISP_PATH -L$badlisp -p 'WHICH_LISP'], [127],,
285[runlisp: no supported Lisp systems found[]NL])
286
287## Unset all of the user preference mechanisms.
288unset RUNLISP_OPTIONS
289here=$(pwd)
290mkdir HOME config
291HOME=$here/HOME XDG_CONFIG_HOME=$here/config; export HOME XDG_CONFIG_HOME
292
293## We generally take the first one listed that exists.
294AT_CHECK_UNQUOTED([RUNLISP_PATH -L$lisp0,$lisp1 -p 'WHICH_LISP'],, ["$lisp0"NL])
295AT_CHECK_UNQUOTED([RUNLISP_PATH -L$lisp1,$lisp0 -p 'WHICH_LISP'],, ["$lisp1"NL])
296AT_CHECK_UNQUOTED([RUNLISP_PATH -L$badlisp,$lisp0,$lisp1 -p 'WHICH_LISP'],,
297 ["$lisp0"NL])
298
299## Check parsing of embedded options.
300for i in 0 1; do
301 j=$(( 1 - $i )); eval lisp=\$lisp$i olisp=\$lisp$j
302 cat >script$i <<EOF
303[#!] RUNLISP_PATH
304;;; -z @RUNLISP: -L$lisp -*- -z -*- -L$olisp -- -z
305(prin1 WHICH_LISP) (terpri)
306EOF
307 chmod +x script$i
308 AT_CHECK_UNQUOTED([./script$i],, ["$lisp"NL])
309done
310
311## Preferences will override the order of acceptable implementations.
312AT_CHECK_UNQUOTED([RUNLISP_OPTIONS=-P$badlisp,$lisp0 ./script0],, ["$lisp0"NL])
313AT_CHECK_UNQUOTED([RUNLISP_OPTIONS=-P$badlisp,$lisp0 ./script1],, ["$lisp0"NL])
314
315## But doesn't affect the preference order of unmentioned Lisps.
316AT_CHECK_UNQUOTED([RUNLISP_OPTIONS=-P$badlisp ./script0],, ["$lisp0"NL])
317AT_CHECK_UNQUOTED([RUNLISP_OPTIONS=-P$badlisp ./script1],, ["$lisp1"NL])
318
319## Test configuration files and interactions with the environment.
320for conf in HOME/.runlisprc config/runlisprc; do
321 for i in 0 1; do
322 j=$(( 1 - $i )); eval lisp=\$lisp$i olisp=\$lisp$j
323 cat >$conf <<EOF
324### -*-conf-*-
325-P$lisp
326EOF
327
328 ## Basic check.
329 AT_CHECK_UNQUOTED([./script0],, ["$lisp"NL])
330 AT_CHECK_UNQUOTED([./script1],, ["$lisp"NL])
331
332 ## Environment variable only appends.
333 AT_CHECK_UNQUOTED([RUNLISP_OPTIONS=-P$olisp ./script0],, ["$lisp"NL])
334 AT_CHECK_UNQUOTED([RUNLISP_OPTIONS=-P$olisp ./script1],, ["$lisp"NL])
335
336 ## But we can clear the preferred list.
337 AT_CHECK_UNQUOTED([RUNLISP_OPTIONS="-C -P$olisp" ./script0],, ["$olisp"NL])
338 AT_CHECK_UNQUOTED([RUNLISP_OPTIONS="-C -P$olisp" ./script1],, ["$olisp"NL])
339
340 done
341 rm -f $conf
342done
343
344
345
346AT_CLEANUP
347
348###----- That's all, folks --------------------------------------------------