@@@ work in progress
[runlisp] / tests.at
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
25 m4_define([RUNLISP_PATH], [$abs_top_builddir/runlisp])
26
27 m4_define([_FOREACH], [dnl
28 m4_if([$#], [1], [_foreach_func($1)],
29 [_foreach_func($1)[]_FOREACH(m4_shift($@))])])
30 m4_define([FOREACH], [dnl
31 m4_pushdef([_foreach_func], [$2])dnl
32 _FOREACH($1)[]dnl
33 m4_popdef([_foreach_func])])
34
35 m4_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
43 m4_define([PREPARE_LISP_TEST],
44 [lisp=$1
45 LISP=$m4_translit(m4_bregexp([$1], [/.*$], []), [a-z], [A-Z])
46 AT_SKIP_IF([test "x$LISP" = x])
47 case $lisp in
48 */*) opt=${lisp#*/} lisp=${lisp%%/*} ;;
49 *) opt="" ;;
50 esac
51 case /$opt/ in
52 */noimage/*) RUNLISP_IMAGEDIR=./notexist ;;
53 *) RUNLISP_IMAGEDIR=$abs_top_builddir ;;
54 esac
55 export RUNLISP_IMAGEDIR])
56
57 m4_define([WHICH_LISP],
58 [(or #+sbcl "sbcl" #+ccl "ccl" #+clisp "clisp"
59 #+ecl "ecl" #+cmu "cmucl" #+abcl "abcl"
60 "unknown")])
61
62 m4_define([NL], [
63 ])
64
65 m4_define([SETUP_RUNLISP_IMAGEDIR],
66 [RUNLISP_IMAGEDIR=$abs_top_builddir; export RUNLISP_IMAGEDIR])
67
68 m4_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.
77 FOREACH([LISP_SYSTEMS],
78 [AT_SETUP([$1 smoke])
79 AT_KEYWORDS([script smoke $1])
80 PREPARE_LISP_TEST([$1])
81
82 ## Prepare a user-init file which will break the test if it's run by printing
83 ## something unexpected.
84 mkdir HOME
85 case $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 ;;
92 esac
93 cat >HOME/$initfile <<EOF
94 (format t "*** I should not be seen~%")
95 EOF
96 HOME=$(pwd)/HOME; export HOME
97
98 ## Prepare the script.
99 cat >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*)
135 EOF
136 chmod +x test-script
137
138 case $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([:]) ;;
146 esac
147
148 ## Prepare an input file.
149 echo some random text >stdin
150
151 ## Prepare the reference stdout and stderr.
152 cat >stdout.ref <<EOF
153 Hello from $impl ($lisp)!
154 from stdin: "some random text"
155 package \`COMMON-LISP-USER' ok
156 program name = "./test-script"
157 arguments = ("--eval" "nonsense" "--" "more" "args" "here")
158 EOF
159 cat >stderr.ref <<EOF
160 to stderr
161 EOF
162
163 AT_CHECK([echo "lisp=$lisp opt=$opt"; env | grep RUNLISP | sort],, [stdout])
164 AT_CHECK([./test-script --eval nonsense -- more args here <stdin],,
165 [stdout], [stderr])
166 AT_CHECK([diff -u stdout.ref stdout])
167 AT_CHECK([diff -u stderr.ref stderr])
168 AT_CLEANUP])
169
170 ###--------------------------------------------------------------------------
171 ### Check error handling.
172
173 FOREACH([LISP_SYSTEMS],
174 [AT_SETUP([$1 errors])
175 AT_KEYWORDS([script error $1])
176 PREPARE_LISP_TEST([$1])
177
178 ## A simple script which signals an error without catching it.
179 cat >test <<EOF
180 [#!] RUNLISP_PATH -L$lisp
181 (error "just kill me now")
182 EOF
183 chmod +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=$?
192 AT_FAIL_IF([test $rc = 0])
193
194 AT_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
202 AT_SETUP([eval mode])
203 AT_KEYWORDS([eval common])
204 SETUP_RUNLISP_IMAGEDIR
205 SETUP_RUNLISP_EVAL
206
207 ## A very basic smoke test.
208 AT_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*'.
213 traceon
214 AT_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.
221 cat >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<))
231 EOF
232 AT_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
241 And we're running the script...
242 Command-line arguments: ("-e" "one" "two" "three")
243 Symbols in package `COMMON-LISP-USER': ()
244 2
245 ])
246
247 AT_CLEANUP
248
249 ###--------------------------------------------------------------------------
250 ### Check Lisp system selection and preference work.
251
252 AT_SETUP([preferences])
253 AT_KEYWORDS([prefs common])
254 SETUP_RUNLISP_IMAGEDIR
255 SETUP_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.
260 unset lisp0 lisp1 badlisp; win=nil
261 set -- cmucl sbcl ccl clisp ecl abcl
262 while :; 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
271 done
272 AT_CHECK([case $win in nil) exit 77 ;; esac])
273 case ${badlisp+t} in t) ;; *) badlisp=$1 ;; esac
274 BADLISP=$(echo $badlisp | tr a-z A-Z)
275 eval $BADLISP=/notexist/definitely-wrong
276 export $BADLISP
277 echo Primary Lisp = $lisp0
278 echo Secondary Lisp = $lisp1
279 echo Bad Lisp = $badlisp
280
281 ## Check that our selection worked.
282 AT_CHECK_UNQUOTED([RUNLISP_PATH -L$lisp0 -p 'WHICH_LISP'],, ["$lisp0"NL])
283 AT_CHECK_UNQUOTED([RUNLISP_PATH -L$lisp1 -p 'WHICH_LISP'],, ["$lisp1"NL])
284 AT_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.
288 unset RUNLISP_OPTIONS
289 here=$(pwd)
290 mkdir HOME config
291 HOME=$here/HOME XDG_CONFIG_HOME=$here/config; export HOME XDG_CONFIG_HOME
292
293 ## We generally take the first one listed that exists.
294 AT_CHECK_UNQUOTED([RUNLISP_PATH -L$lisp0,$lisp1 -p 'WHICH_LISP'],, ["$lisp0"NL])
295 AT_CHECK_UNQUOTED([RUNLISP_PATH -L$lisp1,$lisp0 -p 'WHICH_LISP'],, ["$lisp1"NL])
296 AT_CHECK_UNQUOTED([RUNLISP_PATH -L$badlisp,$lisp0,$lisp1 -p 'WHICH_LISP'],,
297 ["$lisp0"NL])
298
299 ## Check parsing of embedded options.
300 for 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)
306 EOF
307 chmod +x script$i
308 AT_CHECK_UNQUOTED([./script$i],, ["$lisp"NL])
309 done
310
311 ## Preferences will override the order of acceptable implementations.
312 AT_CHECK_UNQUOTED([RUNLISP_OPTIONS=-P$badlisp,$lisp0 ./script0],, ["$lisp0"NL])
313 AT_CHECK_UNQUOTED([RUNLISP_OPTIONS=-P$badlisp,$lisp0 ./script1],, ["$lisp0"NL])
314
315 ## But doesn't affect the preference order of unmentioned Lisps.
316 AT_CHECK_UNQUOTED([RUNLISP_OPTIONS=-P$badlisp ./script0],, ["$lisp0"NL])
317 AT_CHECK_UNQUOTED([RUNLISP_OPTIONS=-P$badlisp ./script1],, ["$lisp1"NL])
318
319 ## Test configuration files and interactions with the environment.
320 for 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
326 EOF
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
342 done
343
344
345
346 AT_CLEANUP
347
348 ###----- That's all, folks --------------------------------------------------