dump-runlisp-image.c: Delete traces of decoy `-d' (`--cleanup') option.
[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([SETUP_RUNLISP_ENV],
44 [RUNLISP_SYSCONFIG=$abs_top_srcdir/runlisp-base.conf; export RUNLISP_SYSCONFIG
45 RUNLISP_SYSCONFIG_DIR=/notexist; export RUNLISP_SYSCONFIG_DIR
46 RUNLISP_IMAGEDIR=$abs_top_builddir; export RUNLISP_IMAGEDIR
47 RUNLISP_EVAL=$abs_top_srcdir/eval.lisp; export RUNLISP_EVAL
48 unset RUNLISP_USERCONFIG
49 ])
50
51 m4_define([PREPARE_LISP_TEST],
52 [SETUP_RUNLISP_ENV
53 lisp=$1
54 LISP=$m4_translit(m4_bpatsubst([$1], [/.*$], []), [a-z], [A-Z])
55 AT_SKIP_IF([test "x$LISP" = x])
56 case $lisp in
57 */*) opt=${lisp#*/} lisp=${lisp%%/*} ;;
58 *) opt="" ;;
59 esac
60 case /$opt/ in */noimage/*) RUNLISP_IMAGEDIR=./notexist ;; esac])
61
62 m4_define([WHICH_LISP],
63 [(or #+sbcl "sbcl" #+ccl "ccl" #+clisp "clisp"
64 #+ecl "ecl" #+cmu "cmucl" #+abcl "abcl"
65 "unknown")])
66
67 m4_define([NL], [
68 ])
69
70 ###--------------------------------------------------------------------------
71 ### A basic smoke test.
72
73 ## Check that the system basically works, by running a trivial test program.
74 ## Also try to verify that we're not running user or site startup code,
75 ## though this is hard to do in general.
76 FOREACH([LISP_SYSTEMS],
77 [AT_SETUP([$1 smoke])
78 AT_KEYWORDS([script smoke $1])
79 PREPARE_LISP_TEST([$1])
80
81 ## Prepare a user-init file which will break the test if it's run by printing
82 ## something unexpected.
83 mkdir HOME
84 case $lisp in
85 sbcl) initfile=.sbclrc ;;
86 ccl) initfile=.ccl-init.lisp ;;
87 clisp) initfile=.clisprc.lisp ;;
88 ecl) initfile=.eclrc ;;
89 cmucl) initfile=.cmucl-init.lisp ;;
90 abcl) initfile=.abclrc ;;
91 esac
92 cat >HOME/$initfile <<EOF
93 (format t "*** I should not be seen~%")
94 EOF
95 HOME=$(pwd)/HOME; export HOME
96
97 ## Prepare the script.
98 cat >test-script <<EOF
99 [#!] RUNLISP_PATH -L$lisp
100
101 ;; Print a greeting to \`*standard-output*', identifying the Lisp system, so
102 ;; that we can tell whether we called the right one.
103 (format t "Hello from ~A (~A)!~%" (lisp-implementation-type) WHICH_LISP)
104
105 #! this should be a comment everywhere
106
107 ;; Make sure that \`*error-output*' is hooked up properly.
108 (format *error-output* "to stderr~%")
109
110 ;; Make sure that \`*standard-input*' is hooked up properly, by reading a line
111 ;; and echoing it.
112 (format t "from stdin: ~S~%" (read-line))
113
114 ;; Check that \`:runlisp-script' is set in \`*features*'. If not, \`assert'
115 ;; will at least write a complaint to some stream, which will fail the test.
116 (assert (member :runlisp-script *features*))
117
118 ;; Check that there are no symbols present (interned or imported) in the
119 ;; \`common-lisp-user' package. Obviously, we must avoid interning any
120 ;; ourselves. Alas, ABCL and ECL pollute \`cl-user' out of the box. (ECL
121 ;; does this deliberately; ABCL's ``adjoin.lisp' lacks an \`in-package'
122 ;; 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_ENV
205
206 ## A very basic smoke test.
207 AT_CHECK([RUNLISP_PATH -e '(format t "Just another Lisp hacker!~%")'],,
208 [Just another Lisp hacker!
209 ])
210
211 ## The `:runlisp-script' keyword should /not/ be in `*features*'.
212 traceon
213 AT_CHECK([RUNLISP_PATH -p '(find :runlisp-script *features*)'],, [NIL
214 ])
215
216 ## Check a mixture of all the kinds of evaluation. We'll need a stunt script
217 ## to make this work. Also check that the individual forms are read and
218 ## evaluated one at a time, so that each one can affect the way the reader
219 ## interprets the next.
220 cat >script.lisp <<EOF
221 #! just want to check that Lisp doesn't choke on a shebang line here
222 (format t "And we're running the script...~%~
223 Command-line arguments: ~:S~%~
224 Symbols in package \`~A': ~:S~%"
225 uiop:*command-line-arguments*
226 (package-name *package*)
227 (sort (loop :for #2=#:s :being :the :present-symbols :of *package*
228 :collect #2#)
229 #'string<))
230 EOF
231 AT_CHECK([RUNLISP_PATH \
232 -e '(defpackage [#:]runlisp-test (:export [#:]foo))
233 (defvar runlisp-test:foo 1)' \
234 -p runlisp-test:foo \
235 -e '(incf runlisp-test:foo)' \
236 -l script.lisp \
237 -p runlisp-test:foo \
238 -- -e one two three],,
239 [1
240 And we're running the script...
241 Command-line arguments: ("-e" "one" "two" "three")
242 Symbols in package `COMMON-LISP-USER': ()
243 2
244 ])
245
246 AT_CLEANUP
247
248 ###--------------------------------------------------------------------------
249 ### Check Lisp system selection and preference work.
250
251 AT_SETUP([preferences])
252 AT_KEYWORDS([prefs common])
253 SETUP_RUNLISP_ENV
254
255 ## Before we can make this happen, we need to decide on three Lisp systems,
256 ## two of which actually work, and one other. These are ordered by startup
257 ## speed.
258 unset lisp0 lisp1 badlisp; win=nil
259 set -- cmucl sbcl ccl clisp ecl abcl
260 while :; do
261 case $# in 0) break ;; esac
262 lisp=$1; shift
263 if RUNLISP_PATH -L$lisp -enil 2>/dev/null; then good=t; else good=nil; fi
264 case ${lisp0+t},${badlisp+t},$good in
265 ,*,t) lisp0=$lisp ;;
266 t,*,t) lisp1=$lisp win=t; break ;;
267 *,,nil) badlisp=$lisp ;;
268 esac
269 done
270 AT_CHECK([case $win in nil) exit 77 ;; esac])
271 case ${badlisp+t} in t) ;; *) badlisp=$1 ;; esac
272 BADLISP=$(echo $badlisp | tr a-z A-Z)
273 eval $BADLISP=/notexist/definitely-wrong
274 export $BADLISP
275 echo Primary Lisp = $lisp0
276 echo Secondary Lisp = $lisp1
277 echo Bad Lisp = $badlisp
278
279 ## Check that our selection worked.
280 AT_CHECK_UNQUOTED([RUNLISP_PATH -L$lisp0 -p 'WHICH_LISP'],, ["$lisp0"NL])
281 AT_CHECK_UNQUOTED([RUNLISP_PATH -L$lisp1 -p 'WHICH_LISP'],, ["$lisp1"NL])
282 AT_CHECK([RUNLISP_PATH -L$badlisp -p 'WHICH_LISP'], [127],,
283 [runlisp: no acceptable Lisp systems found[]NL])
284
285 ## Unset all of the user preference mechanisms.
286 here=$(pwd)
287 mkdir HOME config
288 HOME=$here/HOME XDG_CONFIG_HOME=$here/config; export HOME XDG_CONFIG_HOME
289
290 ## We generally take the first one listed that exists.
291 AT_CHECK_UNQUOTED([RUNLISP_PATH -L$lisp0,$lisp1 -p 'WHICH_LISP'],, ["$lisp0"NL])
292 AT_CHECK_UNQUOTED([RUNLISP_PATH -L$lisp1,$lisp0 -p 'WHICH_LISP'],, ["$lisp1"NL])
293 AT_CHECK_UNQUOTED([RUNLISP_PATH -L$badlisp,$lisp0,$lisp1 -p 'WHICH_LISP'],,
294 ["$lisp0"NL])
295
296 ## Check parsing of embedded options.
297 for i in 0 1; do
298 j=$(( 1 - $i )); eval lisp=\$lisp$i olisp=\$lisp$j
299 cat >script$i <<EOF
300 [#!] RUNLISP_PATH
301 ;;; -z @RUNLISP: -L$lisp -*- -z -*- -L$olisp -- -z
302 (prin1 WHICH_LISP) (terpri)
303 EOF
304 chmod +x script$i
305 AT_CHECK_UNQUOTED([./script$i],, ["$lisp"NL])
306 done
307
308 ## Preferences will override the order of acceptable implementations.
309 AT_CHECK_UNQUOTED([RUNLISP_PREFER=$badlisp,$lisp0 ./script0],, ["$lisp0"NL])
310 AT_CHECK_UNQUOTED([RUNLISP_PREFER=$badlisp,$lisp0 ./script1],, ["$lisp0"NL])
311
312 ## But doesn't affect the preference order of unmentioned Lisps.
313 AT_CHECK_UNQUOTED([RUNLISP_PREFER=$badlisp ./script0],, ["$lisp0"NL])
314 AT_CHECK_UNQUOTED([RUNLISP_PREFER=$badlisp ./script1],, ["$lisp1"NL])
315
316 ## Test configuration files and interactions with the environment.
317 for conf in HOME/.runlisp.conf config/runlisp.conf; do
318 for i in 0 1; do
319 j=$(( 1 - $i )); eval lisp=\$lisp$i olisp=\$lisp$j
320 cat >$conf <<EOF
321 ;;; -*-conf-*-
322 prefer = $lisp
323 EOF
324
325 ## Basic check.
326 AT_CHECK_UNQUOTED([./script0],, ["$lisp"NL])
327 AT_CHECK_UNQUOTED([./script1],, ["$lisp"NL])
328
329 ## Environment variable overrides.
330 AT_CHECK_UNQUOTED([RUNLISP_PREFER=$olisp ./script0],, ["$olisp"NL])
331 AT_CHECK_UNQUOTED([RUNLISP_PREFER=$olisp ./script1],, ["$olisp"NL])
332
333 done
334 rm -f $conf
335 done
336
337
338
339 AT_CLEANUP
340
341 ###----- That's all, folks --------------------------------------------------