tests: Add --report-skipped-tests option to the test runner.
[sbcl.git] / tests / run-tests.lisp
1 #+#.(cl:if (cl:find-package "ASDF") '(or) '(and))
2 (load (merge-pathnames "../contrib/asdf/asdf.fasl"))
3
4 #+#.(cl:if (cl:find-package "SB-POSIX") '(or) '(and))
5 (let ((asdf:*central-registry*
6        (cons "../contrib/systems/" asdf:*central-registry*)))
7   (asdf:oos 'asdf:load-op 'sb-posix))
8
9 (load "test-util.lisp")
10
11 (defpackage :run-tests
12     (:use :cl :test-util :sb-ext))
13
14 (load "assertoid.lisp")
15
16 (in-package run-tests)
17
18 (defvar *all-failures* nil)
19 (defvar *break-on-error* nil)
20 (defvar *report-skipped-tests* nil)
21 (defvar *accept-files* nil)
22
23 (defun run-all ()
24   (dolist (arg (cdr *posix-argv*))
25     (cond ((string= arg "--break-on-failure")
26            (setf *break-on-error* t)
27            (setf test-util:*break-on-failure* t))
28           ((string= arg "--break-on-expected-failure")
29            (setf test-util:*break-on-expected-failure* t))
30           ((string= arg "--report-skipped-tests")
31            (setf *report-skipped-tests* t))
32           (t
33            (push (truename (parse-namestring arg)) *accept-files*))))
34   (pure-runner (pure-load-files) #'load-test)
35   (pure-runner (pure-cload-files) #'cload-test)
36   (impure-runner (impure-load-files) #'load-test)
37   (impure-runner (impure-cload-files) #'cload-test)
38   #-win32 (impure-runner (sh-files) #'sh-test)
39   (report)
40   (sb-ext:quit :unix-status (if (unexpected-failures)
41                                 1
42                                 104)))
43
44 (defun report ()
45   (terpri)
46   (format t "Finished running tests.~%")
47   (let ((skipcount 0))
48     (cond (*all-failures*
49            (format t "Status:~%")
50            (dolist (fail (reverse *all-failures*))
51              (cond ((eq (car fail) :unhandled-error)
52                     (format t " ~20a ~a~%"
53                             "Unhandled error"
54                             (enough-namestring (second fail))))
55                    ((eq (car fail) :invalid-exit-status)
56                     (format t " ~20a ~a~%"
57                             "Invalid exit status:"
58                             (enough-namestring (second fail))))
59                    ((eq (car fail) :skipped-disabled)
60                     (when *report-skipped-tests*
61                       (format t " ~20a ~a / ~a~%"
62                               "Skipped (irrelevant):"
63                               (enough-namestring (second fail))
64                               (third fail)))
65                     (incf skipcount))
66                    (t
67                     (format t " ~20a ~a / ~a~%"
68                             (ecase (first fail)
69                               (:expected-failure "Expected failure:")
70                               (:unexpected-failure "Failure:")
71                               (:unexpected-success "Unexpected success:")
72                               (:skipped-broken "Skipped (broken):")
73                               (:skipped-disabled "Skipped (irrelevant):"))
74                             (enough-namestring (second fail))
75                             (third fail)))))
76            (when (> skipcount 0)
77              (format t " (~a tests skipped for this combination of platform and features)~%"
78                      skipcount)))
79           (t
80            (format t "All tests succeeded~%")))))
81
82 (defun pure-runner (files test-fun)
83   (format t "// Running pure tests (~a)~%" test-fun)
84   (let ((*package* (find-package :cl-user))
85         (*failures* nil))
86     (setup-cl-user)
87     (dolist (file files)
88       (when (accept-test-file file)
89         (format t "// Running ~a~%" file)
90         (restart-case
91             (handler-bind ((error (make-error-handler file)))
92               (eval (funcall test-fun file)))
93           (skip-file ()))))
94     (append-failures)))
95
96 (defun run-in-child-sbcl (load-forms forms)
97   ;; We used to fork() for POSIX platforms, and use this for Windows.
98   ;; However, it seems better to use the same solution everywhere.
99   (process-exit-code
100    (#-win32 with-open-file #-win32 (devnull "/dev/null") #+win32 progn
101      (sb-ext:run-program
102       (first *POSIX-ARGV*)
103       (append
104        (list "--core" SB-INT:*CORE-STRING*
105              "--noinform"
106              "--no-sysinit"
107              "--no-userinit"
108              "--noprint"
109              "--disable-debugger")
110        (loop for form in (append load-forms forms)
111              collect "--eval"
112              collect (write-to-string form)))
113       :output sb-sys:*stdout*
114       :input #-win32 devnull #+win32 sb-sys:*stdin*))))
115
116 (defun run-impure-in-child-sbcl (test-file test-code)
117   (run-in-child-sbcl
118     `((load "test-util")
119       (load "assertoid")
120       (defpackage :run-tests
121         (:use :cl :test-util :sb-ext)))
122
123     `((in-package :cl-user)
124       (use-package :test-util)
125       (use-package :assertoid)
126       (setf test-util:*break-on-failure* ,test-util:*break-on-failure*)
127       (setf test-util:*break-on-expected-failure*
128             ,test-util:*break-on-expected-failure*)
129       (let ((file ,test-file)
130             (*break-on-error* ,run-tests::*break-on-error*))
131         (format t "// Running ~a~%" file)
132         (restart-case
133             (handler-bind
134                 ((error (lambda (condition)
135                           (push (list :unhandled-error file)
136                                 test-util::*failures*)
137                           (cond (*break-on-error*
138                                  (test-util:really-invoke-debugger condition))
139                                 (t
140                                  (format *error-output* "~&Unhandled ~a: ~a~%"
141                                          (type-of condition) condition)
142                                  (sb-debug:backtrace)))
143                           (invoke-restart 'skip-file))))
144               ,test-code)
145           (skip-file ()
146             (format t ">>>~a<<<~%" test-util::*failures*)))
147         (test-util:report-test-status)
148         (sb-ext:quit :unix-status 104)))))
149
150 (defun impure-runner (files test-fun)
151   (format t "// Running impure tests (~a)~%" test-fun)
152   (let ((*package* (find-package :cl-user)))
153     (setup-cl-user)
154     (dolist (file files)
155       (when (accept-test-file file)
156         (force-output)
157         (let ((exit-code (run-impure-in-child-sbcl file
158                                                    (funcall test-fun file))))
159           (if (= exit-code 104)
160               (with-open-file (stream "test-status.lisp-expr"
161                                       :direction :input
162                                       :if-does-not-exist :error)
163                 (append-failures (read stream)))
164               (push (list :invalid-exit-status file)
165                     *all-failures*)))))))
166
167 (defun make-error-handler (file)
168   (lambda (condition)
169     (push (list :unhandled-error file) *failures*)
170     (cond (*break-on-error*
171            (test-util:really-invoke-debugger condition))
172           (t
173            (format *error-output* "~&Unhandled ~a: ~a~%"
174                    (type-of condition) condition)
175            (sb-debug:backtrace)))
176     (invoke-restart 'skip-file)))
177
178 (defun append-failures (&optional (failures *failures*))
179   (setf *all-failures* (append failures *all-failures*)))
180
181 (defun unexpected-failures ()
182   (remove-if (lambda (x)
183                (or (eq (car x) :expected-failure)
184                    (eq (car x) :unexpected-success)
185                    (eq (car x) :skipped-broken)
186                    (eq (car x) :skipped-disabled)))
187              *all-failures*))
188
189 (defun setup-cl-user ()
190   (use-package :test-util)
191   (use-package :assertoid))
192
193 (defun load-test (file)
194   `(load ,file))
195
196 (defun cload-test (file)
197   `(let ((compile-name (compile-file-pathname ,file)))
198      (unwind-protect
199           (progn
200             (compile-file ,file)
201             (load compile-name))
202        (ignore-errors
203          (delete-file compile-name)))))
204
205 (defun sh-test (file)
206   ;; What? No SB-POSIX:EXECV?
207   `(let ((process (sb-ext:run-program "/bin/sh"
208                                       (list (native-namestring ,file))
209                                       :environment (test-util::test-env)
210                                       :output *error-output*)))
211      (let ((*failures* nil))
212        (test-util:report-test-status))
213      (sb-ext:quit :unix-status (process-exit-code process))))
214
215 (defun accept-test-file (file)
216   (if *accept-files*
217       (find (truename file) *accept-files* :test #'equalp)
218       t))
219
220 (defun pure-load-files ()
221   (directory "*.pure.lisp"))
222
223 (defun pure-cload-files ()
224   (directory "*.pure-cload.lisp"))
225
226 (defun impure-load-files ()
227   (directory "*.impure.lisp"))
228
229 (defun impure-cload-files ()
230   (directory "*.impure-cload.lisp"))
231
232 (defun sh-files ()
233   (directory "*.test.sh"))