Stop "initializing" in triplicate the GC control symbols
[sbcl.git] / tests / run-tests.lisp
blobadac66796ec38e968dc7d86075f069acd08d7bd3
1 (load "test-util.lisp")
3 (defpackage :run-tests
4 (:use :cl :test-util :sb-ext))
6 (load "assertoid.lisp")
8 (in-package run-tests)
10 (load "colorize.lisp")
12 (defvar *test-evaluator-mode* :compile)
13 (defvar *all-failures* nil)
14 (defvar *break-on-error* nil)
15 (defvar *report-skipped-tests* nil)
16 (defvar *explicit-test-files* nil)
18 (defun run-all ()
19 (loop :with remainder = (rest *posix-argv*)
20 :while remainder
21 :for arg = (pop remainder)
22 :do (cond
23 ((string= arg "--evaluator-mode")
24 (let ((mode (pop remainder)))
25 (cond
26 ((string= mode "interpret")
27 (setf *test-evaluator-mode* :interpret))
28 ((string= mode "compile")
29 (setf *test-evaluator-mode* :compile))
31 (error "~@<Invalid evaluator mode: ~A. Must be one ~
32 of interpret, compile.~@:>"
33 mode)))))
34 ((string= arg "--break-on-failure")
35 (setf *break-on-error* t)
36 (setf test-util:*break-on-failure* t))
37 ((string= arg "--break-on-expected-failure")
38 (setf test-util:*break-on-expected-failure* t))
39 ((string= arg "--report-skipped-tests")
40 (setf *report-skipped-tests* t))
41 ((string= arg "--no-color"))
43 (push (truename (parse-namestring arg)) *explicit-test-files*))))
44 (setf *explicit-test-files* (nreverse *explicit-test-files*))
45 (pure-runner (pure-load-files) #'load-test)
46 (pure-runner (pure-cload-files) #'cload-test)
47 (impure-runner (impure-load-files) #'load-test)
48 (impure-runner (impure-cload-files) #'cload-test)
49 #-win32 (impure-runner (sh-files) #'sh-test)
50 (report)
51 (sb-ext:exit :code (if (unexpected-failures)
53 104)))
55 (defun report ()
56 (terpri)
57 (format t "Finished running tests.~%")
58 (let ((skipcount 0)
59 (*print-pretty* nil))
60 (cond (*all-failures*
61 (format t "Status:~%")
62 (dolist (fail (reverse *all-failures*))
63 (cond ((eq (car fail) :unhandled-error)
64 (output-colored-text (car fail)
65 " Unhandled Error")
66 (format t " ~a~%"
67 (enough-namestring (second fail))))
68 ((eq (car fail) :invalid-exit-status)
69 (output-colored-text (car fail)
70 " Invalid exit status:")
71 (format t " ~a~%"
72 (enough-namestring (second fail))))
73 ((eq (car fail) :skipped-disabled)
74 (when *report-skipped-tests*
75 (format t " ~20a ~a / ~a~%"
76 "Skipped (irrelevant):"
77 (enough-namestring (second fail))
78 (third fail)))
79 (incf skipcount))
81 (output-colored-text
82 (first fail)
83 (ecase (first fail)
84 (:expected-failure " Expected failure:")
85 (:unexpected-failure " Failure:")
86 (:leftover-thread " Leftover thread (broken):")
87 (:unexpected-success " Unexpected success:")
88 (:skipped-broken " Skipped (broken):")
89 (:skipped-disabled " Skipped (irrelevant):")))
90 (format t " ~a / ~a~%"
91 (enough-namestring (second fail))
92 (third fail)))))
93 (when (> skipcount 0)
94 (format t " (~a tests skipped for this combination of platform and features)~%"
95 skipcount)))
97 (format t "All tests succeeded~%")))))
99 (defun pure-runner (files test-fun)
100 (when files
101 (format t "// Running pure tests (~a)~%" test-fun)
102 (let ((*package* (find-package :cl-user))
103 (*failures* nil))
104 (setup-cl-user)
105 (dolist (file files)
106 (format t "// Running ~a in ~a evaluator mode~%"
107 file *test-evaluator-mode*)
108 (restart-case
109 (handler-bind ((error (make-error-handler file)))
110 (let* ((sb-ext:*evaluator-mode* *test-evaluator-mode*)
111 (*features*
112 (if (eq sb-ext:*evaluator-mode* :interpret)
113 (cons :interpreter *features*)
114 *features*)))
115 (eval (funcall test-fun file))))
116 (skip-file ())))
117 (append-failures))))
119 (defun run-in-child-sbcl (load-forms forms)
120 (process-exit-code
121 (sb-ext:run-program
122 (first *POSIX-ARGV*)
123 (list* "--core" SB-INT:*CORE-STRING*
124 "--noinform"
125 "--no-sysinit"
126 "--no-userinit"
127 "--noprint"
128 "--disable-debugger"
129 (loop for form in (append load-forms forms)
130 collect "--eval"
131 collect (write-to-string form)))
132 :output t
133 :input t)))
135 (defun clear-test-status ()
136 (with-open-file (stream "test-status.lisp-expr"
137 :direction :output
138 :if-exists :supersede)
139 (write-line "NIL" stream)))
141 (defun run-impure-in-child-sbcl (test-file test-code)
142 (clear-test-status)
143 (run-in-child-sbcl
144 `((load "test-util")
145 (load "assertoid")
146 (defpackage :run-tests
147 (:use :cl :test-util :sb-ext)))
149 `((in-package :cl-user)
150 (use-package :test-util)
151 (use-package :assertoid)
152 (setf test-util:*break-on-failure* ,test-util:*break-on-failure*)
153 (setf test-util:*break-on-expected-failure*
154 ,test-util:*break-on-expected-failure*)
155 (let* ((file ,test-file)
156 (sb-ext:*evaluator-mode* ,*test-evaluator-mode*)
157 (*features*
158 (if (eq sb-ext:*evaluator-mode* :interpret)
159 (cons :interpreter *features*)
160 *features*))
161 (*break-on-error* ,run-tests::*break-on-error*))
162 (declare (special *break-on-error*))
163 (format t "// Running ~a in ~a evaluator mode~%"
164 file sb-ext:*evaluator-mode*)
165 (restart-case
166 (handler-bind
167 ((error (lambda (condition)
168 (push (list :unhandled-error file)
169 test-util::*failures*)
170 (cond (*break-on-error*
171 (test-util:really-invoke-debugger condition))
173 (format *error-output* "~&Unhandled ~a: ~a~%"
174 (type-of condition) condition)
175 (sb-debug:print-backtrace)))
176 (invoke-restart 'skip-file))))
177 ,test-code)
178 (skip-file ()
179 (format t ">>>~a<<<~%" test-util::*failures*)))
180 (test-util:report-test-status)
181 (sb-ext:exit :code 104)))))
183 (defun impure-runner (files test-fun)
184 (when files
185 (format t "// Running impure tests (~a)~%" test-fun)
186 (let ((*package* (find-package :cl-user)))
187 (setup-cl-user)
188 (dolist (file files)
189 (force-output)
190 (let ((exit-code (run-impure-in-child-sbcl file
191 (funcall test-fun file))))
192 (if (= exit-code 104)
193 (with-open-file (stream "test-status.lisp-expr"
194 :direction :input
195 :if-does-not-exist :error)
196 (append-failures (read stream)))
197 (push (list :invalid-exit-status file)
198 *all-failures*)))))))
200 (defun make-error-handler (file)
201 (lambda (condition)
202 (push (list :unhandled-error file) *failures*)
203 (cond (*break-on-error*
204 (test-util:really-invoke-debugger condition))
206 (format *error-output* "~&Unhandled ~a: ~a~%"
207 (type-of condition) condition)
208 (sb-debug:print-backtrace)))
209 (invoke-restart 'skip-file)))
211 (defun append-failures (&optional (failures *failures*))
212 (setf *all-failures* (append failures *all-failures*)))
214 (defun unexpected-failures ()
215 (remove-if (lambda (x)
216 (or (eq (car x) :expected-failure)
217 (eq (car x) :unexpected-success)
218 (eq (car x) :skipped-broken)
219 (eq (car x) :skipped-disabled)))
220 *all-failures*))
222 (defun setup-cl-user ()
223 (use-package :test-util)
224 (use-package :assertoid))
226 (defun load-test (file)
227 `(load ,file :external-format :utf-8))
229 (defun cload-test (file)
230 `(let ((compile-name (compile-file-pathname ,file)))
231 (unwind-protect
232 (progn
233 (compile-file ,file :print nil)
234 (load compile-name))
235 (ignore-errors
236 (delete-file compile-name)))))
238 (defun sh-test (file)
239 ;; What? No SB-POSIX:EXECV?
240 (clear-test-status)
241 `(progn
242 (sb-posix:setenv "TEST_SBCL_EVALUATOR_MODE"
243 (string-downcase ,*test-evaluator-mode*)
245 (let ((process (sb-ext:run-program "/bin/sh"
246 (list (native-namestring ,file))
247 :output *error-output*)))
248 (let ((*failures* nil))
249 (test-util:report-test-status))
250 (sb-ext:exit :code (process-exit-code process)))))
252 (defun filter-test-files (wild-mask)
253 (if *explicit-test-files*
254 (loop for file in *explicit-test-files*
255 when (pathname-match-p file wild-mask)
256 collect file)
257 (directory wild-mask)))
259 (defun pure-load-files ()
260 (filter-test-files "*.pure.lisp"))
262 (defun pure-cload-files ()
263 (filter-test-files "*.pure-cload.lisp"))
265 (defun impure-load-files ()
266 (filter-test-files "*.impure.lisp"))
268 (defun impure-cload-files ()
269 (filter-test-files "*.impure-cload.lisp"))
271 (defun sh-files ()
272 (filter-test-files "*.test.sh"))