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))
1175
+ − 161 ;; #### should warn if expecting failure here!
826
+ − 162 (and test-harness-verbose
+ − 163 (princ (concat (apply #'format fmt args) "\n"))))
+ − 164
973
+ − 165 (defun Print-Skip (test reason &optional fmt &rest args)
1095
+ − 166 (setq fmt (concat "SKIP: %S BECAUSE %S" fmt))
973
+ − 167 (princ (concat (apply #'format fmt test reason args) "\n")))
+ − 168
1095
+ − 169 (defmacro Skip-Test-Unless (condition reason description &rest body)
+ − 170 "Unless CONDITION is satisfied, skip test BODY.
+ − 171 REASON is a description of the condition failure, and must be unique (it
+ − 172 is used as a hash key). DESCRIPTION describes the tests that were skipped.
+ − 173 BODY is a sequence of expressions and may contain several tests."
+ − 174 `(if (not ,condition)
+ − 175 (let ((count (gethash ,reason skipped-test-reasons)))
+ − 176 (puthash ,reason (if (null count) 1 (1+ count))
+ − 177 skipped-test-reasons)
+ − 178 (Print-Skip ,description ,reason))
+ − 179 ,@body))
428
+ − 180
+ − 181 (defmacro Assert (assertion)
+ − 182 `(condition-case error-info
+ − 183 (progn
+ − 184 (assert ,assertion)
826
+ − 185 (Print-Pass "%S" (quote ,assertion))
428
+ − 186 (incf passes))
+ − 187 (cl-assertion-failed
826
+ − 188 (Print-Failure "Assertion failed: %S" (quote ,assertion))
428
+ − 189 (incf assertion-failures))
826
+ − 190 (t (Print-Failure "%S ==> error: %S" (quote ,assertion) error-info)
428
+ − 191 (incf other-failures)
+ − 192 )))
+ − 193
+ − 194 (defmacro Check-Error (expected-error &rest body)
+ − 195 (let ((quoted-body (if (= 1 (length body))
+ − 196 `(quote ,(car body)) `(quote (progn ,@body)))))
+ − 197 `(condition-case error-info
+ − 198 (progn
+ − 199 (setq trick-optimizer (progn ,@body))
826
+ − 200 (Print-Failure "%S executed successfully, but expected error %S"
428
+ − 201 ,quoted-body
826
+ − 202 ',expected-error)
428
+ − 203 (incf no-error-failures))
+ − 204 (,expected-error
826
+ − 205 (Print-Pass "%S ==> error %S, as expected"
+ − 206 ,quoted-body ',expected-error)
428
+ − 207 (incf passes))
+ − 208 (error
826
+ − 209 (Print-Failure "%S ==> expected error %S, got error %S instead"
+ − 210 ,quoted-body ',expected-error error-info)
428
+ − 211 (incf wrong-error-failures)))))
+ − 212
826
+ − 213 (defmacro Check-Error-Message (expected-error expected-error-regexp
+ − 214 &rest body)
428
+ − 215 (let ((quoted-body (if (= 1 (length body))
+ − 216 `(quote ,(car body)) `(quote (progn ,@body)))))
+ − 217 `(condition-case error-info
+ − 218 (progn
+ − 219 (setq trick-optimizer (progn ,@body))
826
+ − 220 (Print-Failure "%S executed successfully, but expected error %S"
+ − 221 ,quoted-body ',expected-error)
428
+ − 222 (incf no-error-failures))
+ − 223 (,expected-error
+ − 224 (let ((error-message (second error-info)))
+ − 225 (if (string-match ,expected-error-regexp error-message)
+ − 226 (progn
826
+ − 227 (Print-Pass "%S ==> error %S %S, as expected"
+ − 228 ,quoted-body error-message ',expected-error)
428
+ − 229 (incf passes))
826
+ − 230 (Print-Failure "%S ==> got error %S as expected, but error message %S did not match regexp %S"
+ − 231 ,quoted-body ',expected-error error-message ,expected-error-regexp)
428
+ − 232 (incf wrong-error-failures))))
+ − 233 (error
826
+ − 234 (Print-Failure "%S ==> expected error %S, got error %S instead"
+ − 235 ,quoted-body ',expected-error error-info)
428
+ − 236 (incf wrong-error-failures)))))
+ − 237
+ − 238
+ − 239 (defmacro Check-Message (expected-message-regexp &rest body)
1095
+ − 240 (Skip-Test-Unless (fboundp 'defadvice)
+ − 241 "can't defadvice"
+ − 242 expected-message-regexp
973
+ − 243 (let ((quoted-body (if (= 1 (length body))
+ − 244 `(quote ,(car body))
+ − 245 `(quote (progn ,@body)))))
+ − 246 `(let ((messages ""))
+ − 247 (defadvice message (around collect activate)
+ − 248 (defvar messages)
+ − 249 (let ((msg-string (apply 'format (ad-get-args 0))))
+ − 250 (setq messages (concat messages msg-string))
+ − 251 msg-string))
+ − 252 (condition-case error-info
+ − 253 (progn
+ − 254 (setq trick-optimizer (progn ,@body))
+ − 255 (if (string-match ,expected-message-regexp messages)
+ − 256 (progn
+ − 257 (Print-Pass "%S ==> value %S, message %S, matching %S, as expected"
+ − 258 ,quoted-body trick-optimizer messages ',expected-message-regexp)
+ − 259 (incf passes))
+ − 260 (Print-Failure "%S ==> value %S, message %S, NOT matching expected %S"
+ − 261 ,quoted-body trick-optimizer messages
+ − 262 ',expected-message-regexp)
+ − 263 (incf missing-message-failures)))
+ − 264 (error
+ − 265 (Print-Failure "%S ==> unexpected error %S"
+ − 266 ,quoted-body error-info)
+ − 267 (incf other-failures)))
+ − 268 (ad-unadvise 'message)))))
428
+ − 269
+ − 270 (defmacro Ignore-Ebola (&rest body)
+ − 271 `(let ((debug-issue-ebola-notices -42)) ,@body))
+ − 272
+ − 273 (defun Int-to-Marker (pos)
+ − 274 (save-excursion
+ − 275 (set-buffer standard-output)
+ − 276 (save-excursion
+ − 277 (goto-char pos)
+ − 278 (point-marker))))
+ − 279
+ − 280 (princ "Testing Interpreted Lisp\n\n")
+ − 281 (condition-case error-info
+ − 282 (funcall (test-harness-read-from-buffer inbuffer))
+ − 283 (error
+ − 284 (setq unexpected-test-suite-failure t)
+ − 285 (princ (format "Unexpected error %S while executing interpreted code\n"
+ − 286 error-info))
+ − 287 (message "Unexpected error %S while executing interpreted code." error-info)
+ − 288 (message "Test suite execution aborted." error-info)
+ − 289 ))
+ − 290 (princ "\nTesting Compiled Lisp\n\n")
+ − 291 (let (code)
+ − 292 (condition-case error-info
446
+ − 293 (setq code
+ − 294 ;; our lisp code is often intentionally dubious,
+ − 295 ;; so throw away _all_ the byte compiler warnings.
+ − 296 (letf (((symbol-function 'byte-compile-warn) 'ignore))
+ − 297 (byte-compile (test-harness-read-from-buffer inbuffer))))
428
+ − 298 (error
+ − 299 (princ (format "Unexpected error %S while byte-compiling code\n"
+ − 300 error-info))))
+ − 301 (condition-case error-info
+ − 302 (if code (funcall code))
+ − 303 (error
+ − 304 (princ (format "Unexpected error %S while executing byte-compiled code\n"
+ − 305 error-info))
+ − 306 (message "Unexpected error %S while executing byte-compiled code." error-info)
+ − 307 (message "Test suite execution aborted." error-info)
+ − 308 )))
826
+ − 309 (princ (format "\nSUMMARY for %s:\n" filename))
428
+ − 310 (princ (format "\t%5d passes\n" passes))
+ − 311 (princ (format "\t%5d assertion failures\n" assertion-failures))
+ − 312 (princ (format "\t%5d errors that should have been generated, but weren't\n" no-error-failures))
+ − 313 (princ (format "\t%5d wrong-error failures\n" wrong-error-failures))
+ − 314 (princ (format "\t%5d missing-message failures\n" missing-message-failures))
+ − 315 (princ (format "\t%5d other failures\n" other-failures))
+ − 316 (let* ((total (+ passes
+ − 317 assertion-failures
+ − 318 no-error-failures
+ − 319 wrong-error-failures
+ − 320 missing-message-failures
+ − 321 other-failures))
+ − 322 (basename (file-name-nondirectory filename))
+ − 323 (summary-msg
+ − 324 (if (> total 0)
1231
+ − 325 (format "%s: %d of %d tests successful (%d%%)."
428
+ − 326 basename passes total (/ (* 100 passes) total))
973
+ − 327 (format "%s: No tests run" basename)))
+ − 328 (reasons ""))
+ − 329 (maphash (lambda (key value)
+ − 330 (setq reasons
+ − 331 (concat reasons
1095
+ − 332 (format "\n %d tests skipped because %s."
973
+ − 333 value key))))
+ − 334 skipped-test-reasons)
+ − 335 (when (> (length reasons) 1)
+ − 336 (setq summary-msg (concat summary-msg reasons "
+ − 337 Probably XEmacs cannot find your installed packages. Set EMACSPACKAGEPATH
+ − 338 to the package hierarchy root or configure with --package-path to enable
+ − 339 the skipped tests.")))
428
+ − 340 (message "%s" summary-msg))
+ − 341 (when unexpected-test-suite-failure
+ − 342 (message "Test suite execution failed unexpectedly."))
+ − 343 (fmakunbound 'Assert)
+ − 344 (fmakunbound 'Check-Error)
863
+ − 345 (fmakunbound 'Check-Message)
+ − 346 (fmakunbound 'Check-Error-Message)
428
+ − 347 (fmakunbound 'Ignore-Ebola)
+ − 348 (fmakunbound 'Int-to-Marker)
+ − 349 )))
+ − 350
+ − 351 (defvar test-harness-results-point-max nil)
+ − 352 (defmacro displaying-emacs-test-results (&rest body)
+ − 353 `(let ((test-harness-results-point-max test-harness-results-point-max))
+ − 354 ;; Log the file name.
+ − 355 (test-harness-log-file)
+ − 356 ;; Record how much is logged now.
+ − 357 ;; We will display the log buffer if anything more is logged
+ − 358 ;; before the end of BODY.
+ − 359 (or test-harness-results-point-max
+ − 360 (save-excursion
+ − 361 (set-buffer (get-buffer-create "*Test-Log*"))
+ − 362 (setq test-harness-results-point-max (point-max))))
+ − 363 (unwind-protect
+ − 364 (condition-case error-info
+ − 365 (progn ,@body)
+ − 366 (error
+ − 367 (test-harness-report-error error-info)))
+ − 368 (save-excursion
+ − 369 ;; If there were compilation warnings, display them.
+ − 370 (set-buffer "*Test-Log*")
+ − 371 (if (= test-harness-results-point-max (point-max))
+ − 372 nil
+ − 373 (if temp-buffer-show-function
+ − 374 (let ((show-buffer (get-buffer-create "*Test-Log-Show*")))
+ − 375 (save-excursion
+ − 376 (set-buffer show-buffer)
+ − 377 (setq buffer-read-only nil)
+ − 378 (erase-buffer))
+ − 379 (copy-to-buffer show-buffer
+ − 380 (save-excursion
+ − 381 (goto-char test-harness-results-point-max)
+ − 382 (forward-line -1)
+ − 383 (point))
+ − 384 (point-max))
+ − 385 (funcall temp-buffer-show-function show-buffer))
+ − 386 (select-window
+ − 387 (prog1 (selected-window)
+ − 388 (select-window (display-buffer (current-buffer)))
+ − 389 (goto-char test-harness-results-point-max)
+ − 390 (recenter 1)))))))))
+ − 391
+ − 392 (defun batch-test-emacs-1 (file)
+ − 393 (condition-case error-info
+ − 394 (progn (test-emacs-test-file file) t)
+ − 395 (error
+ − 396 (princ ">>Error occurred processing ")
+ − 397 (princ file)
+ − 398 (princ ": ")
+ − 399 (display-error error-info nil)
+ − 400 (terpri)
+ − 401 nil)))
+ − 402
+ − 403 (defun batch-test-emacs ()
+ − 404 "Run `test-harness' on the files remaining on the command line.
+ − 405 Use this from the command line, with `-batch';
+ − 406 it won't work in an interactive Emacs.
+ − 407 Each file is processed even if an error occurred previously.
+ − 408 For example, invoke \"xemacs -batch -f batch-test-emacs tests/*.el\""
+ − 409 ;; command-line-args-left is what is left of the command line (from
+ − 410 ;; startup.el)
+ − 411 (defvar command-line-args-left) ;Avoid 'free variable' warning
+ − 412 (defvar debug-issue-ebola-notices)
+ − 413 (if (not noninteractive)
+ − 414 (error "`batch-test-emacs' is to be used only with -batch"))
+ − 415 (let ((error nil))
+ − 416 (dolist (file command-line-args-left)
+ − 417 (if (file-directory-p file)
+ − 418 (dolist (file-in-dir (directory-files file t))
+ − 419 (when (and (string-match emacs-lisp-file-regexp file-in-dir)
+ − 420 (not (or (auto-save-file-name-p file-in-dir)
+ − 421 (backup-file-name-p file-in-dir)
+ − 422 (equal (file-name-nondirectory file-in-dir)
+ − 423 "test-harness.el"))))
+ − 424 (or (batch-test-emacs-1 file-in-dir)
+ − 425 (setq error t))))
+ − 426 (or (batch-test-emacs-1 file)
+ − 427 (setq error t))))
+ − 428 ;;(message "%s" (buffer-string nil nil "*Test-Log*"))
+ − 429 (message "Done")
+ − 430 (kill-emacs (if error 1 0))))
+ − 431
+ − 432 (provide 'test-harness)
+ − 433
+ − 434 ;;; test-harness.el ends here