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