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