428
|
1 ;; test-harness.el --- Run Emacs Lisp test suites.
|
|
2
|
|
3 ;;; Copyright (C) 1998 Free Software Foundation, Inc.
|
826
|
4 ;;; Copyright (C) 2002 Ben Wing.
|
428
|
5
|
|
6 ;; Author: Martin Buchholz
|
|
7 ;; Keywords: testing
|
|
8
|
|
9 ;; This file is part of XEmacs.
|
|
10
|
|
11 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
12 ;; under the terms of the GNU General Public License as published by
|
|
13 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
14 ;; any later version.
|
|
15
|
|
16 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
19 ;; General Public License for more details.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
|
22 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
24 ;; Boston, MA 02111-1307, USA.
|
|
25
|
|
26 ;;; Synched up with: Not in FSF.
|
|
27
|
|
28 ;;; Commentary:
|
|
29
|
|
30 ;;; A test suite harness for testing XEmacs.
|
|
31 ;;; The actual tests are in other files in this directory.
|
|
32 ;;; Basically you just create files of emacs-lisp, and use the
|
1095
|
33 ;;; Assert, Check-Error, Check-Message, and Check-Error-Message functions
|
|
34 ;;; to create tests. See `test-harness-from-buffer' below.
|
|
35 ;;; Don't suppress tests just because they're due to known bugs not yet
|
|
36 ;;; fixed -- use the Known-Bug-Expect-Failure wrapper macro to mark them.
|
|
37 ;;; A lot of the tests we run push limits; suppress Ebola message with the
|
|
38 ;;; Ignore-Ebola wrapper macro.
|
|
39 ;;;
|
428
|
40 ;;; You run the tests using M-x test-emacs-test-file,
|
|
41 ;;; or $(EMACS) -batch -l .../test-harness.el -f batch-test-emacs file ...
|
|
42 ;;; which is run for you by the `make check' target in the top-level Makefile.
|
|
43
|
|
44 (require 'bytecomp)
|
|
45
|
|
46 (defvar test-harness-verbose
|
|
47 (and (not noninteractive) (> (device-baud-rate) search-slow-speed))
|
|
48 "*Non-nil means print messages describing progress of emacs-tester.")
|
|
49
|
|
50 (defvar test-harness-current-file nil)
|
|
51
|
|
52 (defvar emacs-lisp-file-regexp (purecopy "\\.el\\'")
|
|
53 "*Regexp which matches Emacs Lisp source files.")
|
|
54
|
|
55 ;;;###autoload
|
|
56 (defun test-emacs-test-file (filename)
|
|
57 "Test a file of Lisp code named FILENAME.
|
|
58 The output file's name is made by appending `c' to the end of FILENAME."
|
|
59 (interactive
|
|
60 (let ((file buffer-file-name)
|
|
61 (file-name nil)
|
|
62 (file-dir nil))
|
|
63 (and file
|
|
64 (eq (cdr (assq 'major-mode (buffer-local-variables)))
|
|
65 'emacs-lisp-mode)
|
|
66 (setq file-name (file-name-nondirectory file)
|
|
67 file-dir (file-name-directory file)))
|
|
68 (list (read-file-name "Test file: " file-dir nil nil file-name))))
|
|
69 ;; Expand now so we get the current buffer's defaults
|
|
70 (setq filename (expand-file-name filename))
|
|
71
|
|
72 ;; If we're testing a file that's in a buffer and is modified, offer
|
|
73 ;; to save it first.
|
|
74 (or noninteractive
|
|
75 (let ((b (get-file-buffer (expand-file-name filename))))
|
|
76 (if (and b (buffer-modified-p b)
|
|
77 (y-or-n-p (format "save buffer %s first? " (buffer-name b))))
|
|
78 (save-excursion (set-buffer b) (save-buffer)))))
|
|
79
|
|
80 (if (or noninteractive test-harness-verbose)
|
|
81 (message "Testing %s..." filename))
|
|
82 (let ((test-harness-current-file filename)
|
|
83 input-buffer)
|
|
84 (save-excursion
|
|
85 (setq input-buffer (get-buffer-create " *Test Input*"))
|
|
86 (set-buffer input-buffer)
|
|
87 (erase-buffer)
|
|
88 (insert-file-contents filename)
|
|
89 ;; Run hooks including the uncompression hook.
|
|
90 ;; If they change the file name, then change it for the output also.
|
|
91 (let ((buffer-file-name filename)
|
|
92 (default-major-mode 'emacs-lisp-mode)
|
|
93 (enable-local-eval nil))
|
|
94 (normal-mode)
|
|
95 (setq filename buffer-file-name)))
|
|
96 (test-harness-from-buffer input-buffer filename)
|
|
97 (kill-buffer input-buffer)
|
|
98 ))
|
|
99
|
|
100 (defun test-harness-read-from-buffer (buffer)
|
|
101 "Read forms from BUFFER, and turn it into a lambda test form."
|
|
102 (let ((body nil))
|
|
103 (goto-char (point-min) buffer)
|
|
104 (condition-case error-info
|
|
105 (while t
|
|
106 (setq body (cons (read buffer) body)))
|
|
107 (end-of-file nil)
|
|
108 (error
|
928
|
109 (princ (format "Unexpected error %S reading forms from buffer\n" error-info))))
|
428
|
110 `(lambda ()
|
|
111 (defvar passes)
|
|
112 (defvar assertion-failures)
|
|
113 (defvar no-error-failures)
|
|
114 (defvar wrong-error-failures)
|
|
115 (defvar missing-message-failures)
|
|
116 (defvar other-failures)
|
|
117
|
|
118 (defvar unexpected-test-suite-failure)
|
|
119 (defvar trick-optimizer)
|
|
120
|
|
121 ,@(nreverse body))))
|
|
122
|
|
123 (defun test-harness-from-buffer (inbuffer filename)
|
|
124 "Run tests in buffer INBUFFER, visiting FILENAME."
|
|
125 (defvar trick-optimizer)
|
|
126 (let ((passes 0)
|
|
127 (assertion-failures 0)
|
|
128 (no-error-failures 0)
|
|
129 (wrong-error-failures 0)
|
|
130 (missing-message-failures 0)
|
|
131 (other-failures 0)
|
|
132
|
973
|
133 ;; #### perhaps this should be a defvar, and output at the very end
|
|
134 ;; OTOH, this way AC types can use a null EMACSPACKAGEPATH to find
|
|
135 ;; what stuff is needed, and ways to avoid using them
|
|
136 (skipped-test-reasons (make-hash-table :test 'equal))
|
|
137
|
428
|
138 (trick-optimizer nil)
|
|
139 (unexpected-test-suite-failure nil)
|
826
|
140 (debug-on-error t)
|
|
141 (pass-stream nil))
|
428
|
142 (with-output-to-temp-buffer "*Test-Log*"
|
826
|
143 (princ (format "Testing %s...\n\n" filename))
|
1095
|
144
|
|
145 (defconst test-harness-expect-bug nil)
|
|
146
|
|
147 (defmacro Known-Bug-Expect-Failure (&rest body)
|
|
148 `(let ((test-harness-expect-bug t)) ,@body))
|
826
|
149
|
|
150 (defun Print-Failure (fmt &rest args)
|
1095
|
151 (setq fmt (format "%s: %s"
|
|
152 (if test-harness-expect-bug
|
|
153 "KNOWN BUG"
|
|
154 "FAIL")
|
|
155 fmt))
|
826
|
156 (if (noninteractive) (apply #'message fmt args))
|
|
157 (princ (concat (apply #'format fmt args) "\n")))
|
|
158
|
|
159 (defun Print-Pass (fmt &rest args)
|
|
160 (setq fmt (concat "PASS: " fmt))
|
|
161 (and test-harness-verbose
|
|
162 (princ (concat (apply #'format fmt args) "\n"))))
|
|
163
|
973
|
164 (defun Print-Skip (test reason &optional fmt &rest args)
|
1095
|
165 (setq fmt (concat "SKIP: %S BECAUSE %S" fmt))
|
973
|
166 (princ (concat (apply #'format fmt test reason args) "\n")))
|
|
167
|
1095
|
168 (defmacro Skip-Test-Unless (condition reason description &rest body)
|
|
169 "Unless CONDITION is satisfied, skip test BODY.
|
|
170 REASON is a description of the condition failure, and must be unique (it
|
|
171 is used as a hash key). DESCRIPTION describes the tests that were skipped.
|
|
172 BODY is a sequence of expressions and may contain several tests."
|
|
173 `(if (not ,condition)
|
|
174 (let ((count (gethash ,reason skipped-test-reasons)))
|
|
175 (puthash ,reason (if (null count) 1 (1+ count))
|
|
176 skipped-test-reasons)
|
|
177 (Print-Skip ,description ,reason))
|
|
178 ,@body))
|
428
|
179
|
|
180 (defmacro Assert (assertion)
|
|
181 `(condition-case error-info
|
|
182 (progn
|
|
183 (assert ,assertion)
|
826
|
184 (Print-Pass "%S" (quote ,assertion))
|
428
|
185 (incf passes))
|
|
186 (cl-assertion-failed
|
826
|
187 (Print-Failure "Assertion failed: %S" (quote ,assertion))
|
428
|
188 (incf assertion-failures))
|
826
|
189 (t (Print-Failure "%S ==> error: %S" (quote ,assertion) error-info)
|
428
|
190 (incf other-failures)
|
|
191 )))
|
|
192
|
|
193 (defmacro Check-Error (expected-error &rest body)
|
|
194 (let ((quoted-body (if (= 1 (length body))
|
|
195 `(quote ,(car body)) `(quote (progn ,@body)))))
|
|
196 `(condition-case error-info
|
|
197 (progn
|
|
198 (setq trick-optimizer (progn ,@body))
|
826
|
199 (Print-Failure "%S executed successfully, but expected error %S"
|
428
|
200 ,quoted-body
|
826
|
201 ',expected-error)
|
428
|
202 (incf no-error-failures))
|
|
203 (,expected-error
|
826
|
204 (Print-Pass "%S ==> error %S, as expected"
|
|
205 ,quoted-body ',expected-error)
|
428
|
206 (incf passes))
|
|
207 (error
|
826
|
208 (Print-Failure "%S ==> expected error %S, got error %S instead"
|
|
209 ,quoted-body ',expected-error error-info)
|
428
|
210 (incf wrong-error-failures)))))
|
|
211
|
826
|
212 (defmacro Check-Error-Message (expected-error expected-error-regexp
|
|
213 &rest body)
|
428
|
214 (let ((quoted-body (if (= 1 (length body))
|
|
215 `(quote ,(car body)) `(quote (progn ,@body)))))
|
|
216 `(condition-case error-info
|
|
217 (progn
|
|
218 (setq trick-optimizer (progn ,@body))
|
826
|
219 (Print-Failure "%S executed successfully, but expected error %S"
|
|
220 ,quoted-body ',expected-error)
|
428
|
221 (incf no-error-failures))
|
|
222 (,expected-error
|
|
223 (let ((error-message (second error-info)))
|
|
224 (if (string-match ,expected-error-regexp error-message)
|
|
225 (progn
|
826
|
226 (Print-Pass "%S ==> error %S %S, as expected"
|
|
227 ,quoted-body error-message ',expected-error)
|
428
|
228 (incf passes))
|
826
|
229 (Print-Failure "%S ==> got error %S as expected, but error message %S did not match regexp %S"
|
|
230 ,quoted-body ',expected-error error-message ,expected-error-regexp)
|
428
|
231 (incf wrong-error-failures))))
|
|
232 (error
|
826
|
233 (Print-Failure "%S ==> expected error %S, got error %S instead"
|
|
234 ,quoted-body ',expected-error error-info)
|
428
|
235 (incf wrong-error-failures)))))
|
|
236
|
|
237
|
|
238 (defmacro Check-Message (expected-message-regexp &rest body)
|
1095
|
239 (Skip-Test-Unless (fboundp 'defadvice)
|
|
240 "can't defadvice"
|
|
241 expected-message-regexp
|
973
|
242 (let ((quoted-body (if (= 1 (length body))
|
|
243 `(quote ,(car body))
|
|
244 `(quote (progn ,@body)))))
|
|
245 `(let ((messages ""))
|
|
246 (defadvice message (around collect activate)
|
|
247 (defvar messages)
|
|
248 (let ((msg-string (apply 'format (ad-get-args 0))))
|
|
249 (setq messages (concat messages msg-string))
|
|
250 msg-string))
|
|
251 (condition-case error-info
|
|
252 (progn
|
|
253 (setq trick-optimizer (progn ,@body))
|
|
254 (if (string-match ,expected-message-regexp messages)
|
|
255 (progn
|
|
256 (Print-Pass "%S ==> value %S, message %S, matching %S, as expected"
|
|
257 ,quoted-body trick-optimizer messages ',expected-message-regexp)
|
|
258 (incf passes))
|
|
259 (Print-Failure "%S ==> value %S, message %S, NOT matching expected %S"
|
|
260 ,quoted-body trick-optimizer messages
|
|
261 ',expected-message-regexp)
|
|
262 (incf missing-message-failures)))
|
|
263 (error
|
|
264 (Print-Failure "%S ==> unexpected error %S"
|
|
265 ,quoted-body error-info)
|
|
266 (incf other-failures)))
|
|
267 (ad-unadvise 'message)))))
|
428
|
268
|
|
269 (defmacro Ignore-Ebola (&rest body)
|
|
270 `(let ((debug-issue-ebola-notices -42)) ,@body))
|
|
271
|
|
272 (defun Int-to-Marker (pos)
|
|
273 (save-excursion
|
|
274 (set-buffer standard-output)
|
|
275 (save-excursion
|
|
276 (goto-char pos)
|
|
277 (point-marker))))
|
|
278
|
|
279 (princ "Testing Interpreted Lisp\n\n")
|
|
280 (condition-case error-info
|
|
281 (funcall (test-harness-read-from-buffer inbuffer))
|
|
282 (error
|
|
283 (setq unexpected-test-suite-failure t)
|
|
284 (princ (format "Unexpected error %S while executing interpreted code\n"
|
|
285 error-info))
|
|
286 (message "Unexpected error %S while executing interpreted code." error-info)
|
|
287 (message "Test suite execution aborted." error-info)
|
|
288 ))
|
|
289 (princ "\nTesting Compiled Lisp\n\n")
|
|
290 (let (code)
|
|
291 (condition-case error-info
|
446
|
292 (setq code
|
|
293 ;; our lisp code is often intentionally dubious,
|
|
294 ;; so throw away _all_ the byte compiler warnings.
|
|
295 (letf (((symbol-function 'byte-compile-warn) 'ignore))
|
|
296 (byte-compile (test-harness-read-from-buffer inbuffer))))
|
428
|
297 (error
|
|
298 (princ (format "Unexpected error %S while byte-compiling code\n"
|
|
299 error-info))))
|
|
300 (condition-case error-info
|
|
301 (if code (funcall code))
|
|
302 (error
|
|
303 (princ (format "Unexpected error %S while executing byte-compiled code\n"
|
|
304 error-info))
|
|
305 (message "Unexpected error %S while executing byte-compiled code." error-info)
|
|
306 (message "Test suite execution aborted." error-info)
|
|
307 )))
|
826
|
308 (princ (format "\nSUMMARY for %s:\n" filename))
|
428
|
309 (princ (format "\t%5d passes\n" passes))
|
|
310 (princ (format "\t%5d assertion failures\n" assertion-failures))
|
|
311 (princ (format "\t%5d errors that should have been generated, but weren't\n" no-error-failures))
|
|
312 (princ (format "\t%5d wrong-error failures\n" wrong-error-failures))
|
|
313 (princ (format "\t%5d missing-message failures\n" missing-message-failures))
|
|
314 (princ (format "\t%5d other failures\n" other-failures))
|
|
315 (let* ((total (+ passes
|
|
316 assertion-failures
|
|
317 no-error-failures
|
|
318 wrong-error-failures
|
|
319 missing-message-failures
|
|
320 other-failures))
|
|
321 (basename (file-name-nondirectory filename))
|
|
322 (summary-msg
|
|
323 (if (> total 0)
|
|
324 (format "%s: %d of %d (%d%%) tests successful."
|
|
325 basename passes total (/ (* 100 passes) total))
|
973
|
326 (format "%s: No tests run" basename)))
|
|
327 (reasons ""))
|
|
328 (maphash (lambda (key value)
|
|
329 (setq reasons
|
|
330 (concat reasons
|
1095
|
331 (format "\n %d tests skipped because %s."
|
973
|
332 value key))))
|
|
333 skipped-test-reasons)
|
|
334 (when (> (length reasons) 1)
|
|
335 (setq summary-msg (concat summary-msg reasons "
|
|
336 Probably XEmacs cannot find your installed packages. Set EMACSPACKAGEPATH
|
|
337 to the package hierarchy root or configure with --package-path to enable
|
|
338 the skipped tests.")))
|
428
|
339 (message "%s" summary-msg))
|
|
340 (when unexpected-test-suite-failure
|
|
341 (message "Test suite execution failed unexpectedly."))
|
|
342 (fmakunbound 'Assert)
|
|
343 (fmakunbound 'Check-Error)
|
863
|
344 (fmakunbound 'Check-Message)
|
|
345 (fmakunbound 'Check-Error-Message)
|
428
|
346 (fmakunbound 'Ignore-Ebola)
|
|
347 (fmakunbound 'Int-to-Marker)
|
|
348 )))
|
|
349
|
|
350 (defvar test-harness-results-point-max nil)
|
|
351 (defmacro displaying-emacs-test-results (&rest body)
|
|
352 `(let ((test-harness-results-point-max test-harness-results-point-max))
|
|
353 ;; Log the file name.
|
|
354 (test-harness-log-file)
|
|
355 ;; Record how much is logged now.
|
|
356 ;; We will display the log buffer if anything more is logged
|
|
357 ;; before the end of BODY.
|
|
358 (or test-harness-results-point-max
|
|
359 (save-excursion
|
|
360 (set-buffer (get-buffer-create "*Test-Log*"))
|
|
361 (setq test-harness-results-point-max (point-max))))
|
|
362 (unwind-protect
|
|
363 (condition-case error-info
|
|
364 (progn ,@body)
|
|
365 (error
|
|
366 (test-harness-report-error error-info)))
|
|
367 (save-excursion
|
|
368 ;; If there were compilation warnings, display them.
|
|
369 (set-buffer "*Test-Log*")
|
|
370 (if (= test-harness-results-point-max (point-max))
|
|
371 nil
|
|
372 (if temp-buffer-show-function
|
|
373 (let ((show-buffer (get-buffer-create "*Test-Log-Show*")))
|
|
374 (save-excursion
|
|
375 (set-buffer show-buffer)
|
|
376 (setq buffer-read-only nil)
|
|
377 (erase-buffer))
|
|
378 (copy-to-buffer show-buffer
|
|
379 (save-excursion
|
|
380 (goto-char test-harness-results-point-max)
|
|
381 (forward-line -1)
|
|
382 (point))
|
|
383 (point-max))
|
|
384 (funcall temp-buffer-show-function show-buffer))
|
|
385 (select-window
|
|
386 (prog1 (selected-window)
|
|
387 (select-window (display-buffer (current-buffer)))
|
|
388 (goto-char test-harness-results-point-max)
|
|
389 (recenter 1)))))))))
|
|
390
|
|
391 (defun batch-test-emacs-1 (file)
|
|
392 (condition-case error-info
|
|
393 (progn (test-emacs-test-file file) t)
|
|
394 (error
|
|
395 (princ ">>Error occurred processing ")
|
|
396 (princ file)
|
|
397 (princ ": ")
|
|
398 (display-error error-info nil)
|
|
399 (terpri)
|
|
400 nil)))
|
|
401
|
|
402 (defun batch-test-emacs ()
|
|
403 "Run `test-harness' on the files remaining on the command line.
|
|
404 Use this from the command line, with `-batch';
|
|
405 it won't work in an interactive Emacs.
|
|
406 Each file is processed even if an error occurred previously.
|
|
407 For example, invoke \"xemacs -batch -f batch-test-emacs tests/*.el\""
|
|
408 ;; command-line-args-left is what is left of the command line (from
|
|
409 ;; startup.el)
|
|
410 (defvar command-line-args-left) ;Avoid 'free variable' warning
|
|
411 (defvar debug-issue-ebola-notices)
|
|
412 (if (not noninteractive)
|
|
413 (error "`batch-test-emacs' is to be used only with -batch"))
|
|
414 (let ((error nil))
|
|
415 (dolist (file command-line-args-left)
|
|
416 (if (file-directory-p file)
|
|
417 (dolist (file-in-dir (directory-files file t))
|
|
418 (when (and (string-match emacs-lisp-file-regexp file-in-dir)
|
|
419 (not (or (auto-save-file-name-p file-in-dir)
|
|
420 (backup-file-name-p file-in-dir)
|
|
421 (equal (file-name-nondirectory file-in-dir)
|
|
422 "test-harness.el"))))
|
|
423 (or (batch-test-emacs-1 file-in-dir)
|
|
424 (setq error t))))
|
|
425 (or (batch-test-emacs-1 file)
|
|
426 (setq error t))))
|
|
427 ;;(message "%s" (buffer-string nil nil "*Test-Log*"))
|
|
428 (message "Done")
|
|
429 (kill-emacs (if error 1 0))))
|
|
430
|
|
431 (provide 'test-harness)
|
|
432
|
|
433 ;;; test-harness.el ends here
|