Update URLs in the comments.
[emacs.git] / admin / cus-test.el
blobcf00bf214fbdfc421c683fb8584102d31af6fa15
1 ;;; cus-test.el --- tests for custom types and load problems
3 ;; Copyright (C) 1998, 2000, 2002 Free Software Foundation, Inc.
5 ;; Author: Markus Rost <markus.rost@mathematik.uni-regensburg.de>
6 ;; Maintainer: Markus Rost <rost@math.ohio-state.edu>
7 ;; Created: 13 Sep 1998
8 ;; Keywords: maint
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; This file provides simple tests to detect custom options with
30 ;; incorrect customization types and load problems for custom and
31 ;; autoload dependencies.
33 ;; The basic tests can be run in batch mode. Invoke them with
35 ;; src/emacs -batch -l admin/cus-test.el -f cus-test-opts
37 ;; src/emacs -batch -l admin/cus-test.el -f cus-test-deps
39 ;; src/emacs -batch -l admin/cus-test.el -f cus-test-libs
41 ;; src/emacs -batch -l admin/cus-test.el -f cus-test-noloads
43 ;; in the emacs source directory.
45 ;; For interactive use: Load this file. Then
47 ;; M-x cus-test-apropos REGEXP RET
49 ;; checks the options matching REGEXP. In particular
51 ;; M-x cus-test-apropos RET
53 ;; checks all options. The detected options are stored in the
54 ;; variable `cus-test-errors'.
56 ;; Only those options are checked which have been already loaded.
57 ;; Therefore `cus-test-apropos' is more efficient after loading many
58 ;; libraries.
60 ;; M-x cus-test-load-custom-loads
62 ;; loads all (!) custom dependencies and
64 ;; M-x cus-test-load-libs
66 ;; loads all (!) libraries with autoloads.
68 ;; Options with a custom-get property, usually defined by a :get
69 ;; declaration, are stored in the variable
71 ;; `cus-test-vars-with-custom-get'
73 ;; Options with a state of 'changed ("changed outside the customize
74 ;; buffer") are stored in the variable
76 ;; `cus-test-vars-with-changed-state'
78 ;; These lists are prepared just in case one wants to investigate
79 ;; those options further.
81 ;; The command `cus-test-opts' tests many (all?) custom options.
83 ;; The command `cus-test-deps' is like `cus-test-load-custom-loads'
84 ;; but reports about load errors.
86 ;; The command `cus-test-libs' runs for all libraries with autoloads
87 ;; separate emacs processes of the form "emacs -batch -l LIB".
89 ;; The command `cus-test-noloads' returns a list of variables which
90 ;; are somewhere declared as custom options, but not loaded by
91 ;; `custom-load-symbol'.
93 ;; Some results from October 2002:
95 ;; 4523 options tested
96 ;; The following variables might have problems:
97 ;; ps-mule-font-info-database-default
98 ;; grep-tree-command
99 ;; grep-find-command
101 ;; 288 features required
102 ;; 10 files loaded
103 ;; The following load problems appeared:
104 ;; (killing x-win (file-error Cannot open load file x-win))
105 ;; Symbol faces has loaddefs as custom dependency
106 ;; (reftex-index-support reftex-vars (void-function reftex-set-dirty))
107 ;; (eshell-script em-script (void-variable eshell-directory-name))
108 ;; (pcomplete em-cmpl (void-function eshell-under-windows-p))
109 ;; (eshell-ext esh-ext (void-function eshell-under-windows-p))
110 ;; ...
112 ;; 422 libraries had no load errors
113 ;; The following load problems appeared:
114 ;; (eudc-export error 255)
115 ;; (ada-xref error 255)
116 ;; (ada-stmt error 255)
118 ;; The following options were not loaded by custom-load-symbol:
119 ;; edt-bottom-scroll-margin
120 ;; edt-keep-current-page-delimiter
121 ;; edt-top-scroll-margin
122 ;; edt-use-EDT-control-key-bindings
123 ;; edt-word-entities
124 ;; grep-find-use-xargs
125 ;; master-mode-hook
126 ;; outline-level
127 ;; outline-minor-mode-hook
128 ;; refill-mode-hook
131 ;;; Code:
133 ;;; Workarounds. For a smooth run and to avoid some side effects.
135 (defvar cus-test-after-load-libs-hook nil
136 "Used to switch off undesired side effects of loading libraries.")
138 (defvar cus-test-skip-list nil
139 "List of variables to disregard by `cus-test-apropos'.")
141 (defvar cus-test-libs-noloads nil
142 "List of libraries not to load by `cus-test-load-libs'.")
144 ;; The file eudc-export.el loads libraries "bbdb" and "bbdb-com" which
145 ;; are not part of GNU Emacs: (locate-library "bbdb") => nil
146 ;; We avoid the resulting errors from loading eudc-export.el:
147 (provide 'bbdb)
148 (provide 'bbdb-com)
150 ;; This avoids a hang of `cus-test-apropos' in 21.2.
151 ;; (add-to-list 'cus-test-skip-list 'sh-alias-alist)
153 ;; Loading dunnet in batch mode leads to a Dead end.
154 (let (noninteractive) (load "dunnet"))
155 (add-to-list 'cus-test-libs-noloads "dunnet")
157 ;; Never Viperize.
158 (setq viper-mode nil)
160 ;; Don't create a file `save-place-file'.
161 (eval-after-load "saveplace"
162 '(remove-hook 'kill-emacs-hook 'save-place-kill-emacs-hook))
164 ;; Don't create a file `abbrev-file-name'.
165 (setq save-abbrevs nil)
167 ;; Avoid compile logs from adviced functions.
168 (eval-after-load "bytecomp"
169 '(setq ad-default-compilation-action 'never))
172 ;;; Main code:
174 ;; We want to log all messages.
175 (setq message-log-max t)
177 (require 'cus-edit)
178 (require 'cus-load)
180 (defvar cus-test-errors nil
181 "List of problematic variables found by `cus-test-apropos'.")
183 (defvar cus-test-tested-variables nil
184 "List of options tested by last call of `cus-test-apropos'.")
186 ;; I haven't understood this :get stuff. The symbols with a
187 ;; custom-get property are stored here.
188 (defvar cus-test-vars-with-custom-get nil
189 "Set by `cus-test-apropos' to a list of options with :get property.")
191 (defvar cus-test-vars-with-changed-state nil
192 "Set by `cus-test-apropos' to a list of options with state 'changed.")
194 (defvar cus-test-deps-errors nil
195 "List of require/load problems found by `cus-test-deps'.")
197 (defvar cus-test-deps-required nil
198 "List of dependencies required by `cus-test-deps'.
199 Only unloaded features will be require'd.")
201 (defvar cus-test-deps-loaded nil
202 "List of dependencies loaded by `cus-test-deps'.")
204 (defvar cus-test-libs-errors nil
205 "List of load problems found by `cus-test-load-libs' or `cus-test-libs'.")
207 (defvar cus-test-libs-loaded nil
208 "List of files loaded by `cus-test-load-libs' or `cus-test-libs'.")
210 (defvar cus-test-vars-not-cus-loaded nil
211 "A list of options not loaded by `custom-load-symbol'.
212 Set by `cus-test-noloads'.")
214 ;; (defvar cus-test-vars-cus-loaded nil
215 ;; "A list of options loaded by `custom-load-symbol'.")
217 (defun cus-test-apropos (regexp)
218 "Check the options matching REGEXP.
219 The detected problematic options are stored in `cus-test-errors'."
220 (interactive "sVariable regexp: ")
221 (setq cus-test-errors nil)
222 (setq cus-test-tested-variables nil)
223 (mapc
224 (lambda (symbol)
225 (push symbol cus-test-tested-variables)
226 ;; Be verbose in case we hang.
227 (message "Cus Test running...%s %s"
228 (length cus-test-tested-variables) symbol)
229 (condition-case alpha
230 (let* ((type (custom-variable-type symbol))
231 (conv (widget-convert type))
232 (get (or (get symbol 'custom-get) 'default-value))
233 values
234 mismatch)
235 (when (default-boundp symbol)
236 (push (funcall get symbol) values)
237 (push (eval (car (get symbol 'standard-value))) values))
238 (if (boundp symbol)
239 (push (symbol-value symbol) values))
240 ;; That does not work.
241 ;; (push (widget-get conv :value) values)
243 ;; Check the values
244 (mapc (lambda (value)
245 (unless (widget-apply conv :match value)
246 (setq mismatch 'mismatch)))
247 values)
249 ;; Store symbols with a custom-get property.
250 (when (get symbol 'custom-get)
251 (add-to-list 'cus-test-vars-with-custom-get symbol))
253 ;; Changed outside the customize buffer?
254 ;; This routine is not very much tested.
255 (let ((c-value
256 (or (get symbol 'customized-value)
257 (get symbol 'saved-value)
258 (get symbol 'standard-value))))
259 (and (consp c-value)
260 (boundp symbol)
261 (not (equal (eval (car c-value)) (symbol-value symbol)))
262 (add-to-list 'cus-test-vars-with-changed-state symbol)))
264 (if mismatch
265 (push symbol cus-test-errors)))
267 (error
268 (push symbol cus-test-errors)
269 (message "Error for %s: %s" symbol alpha))))
270 (cus-test-get-options regexp))
271 (message "%s options tested"
272 (length cus-test-tested-variables))
273 (cus-test-errors-display))
275 (defun cus-test-get-options (regexp)
276 "Return a list of custom options matching REGEXP."
277 (let (found)
278 (mapatoms
279 (lambda (symbol)
280 (and
282 ;; (user-variable-p symbol)
283 (get symbol 'standard-value)
284 ;; (get symbol 'saved-value)
285 (get symbol 'custom-type))
286 (string-match regexp (symbol-name symbol))
287 (not (member symbol cus-test-skip-list))
288 (push symbol found))))
289 found))
291 (defun cus-test-errors-display ()
292 "Report about the errors found by cus-test."
293 (with-output-to-temp-buffer "*cus-test-errors*"
294 (set-buffer standard-output)
295 (insert (format "Cus Test tested %s variables.\
296 See `cus-test-tested-variables'.\n\n"
297 (length cus-test-tested-variables)))
298 (if (not cus-test-errors)
299 (insert "No errors found by cus-test.")
300 (insert "The following variables seem to have problems:\n\n")
301 (dolist (e cus-test-errors)
302 (insert (symbol-name e) "\n")))))
304 (defun cus-test-load-custom-loads ()
305 "Call `custom-load-symbol' on all atoms."
306 (interactive)
307 (mapatoms 'custom-load-symbol)
308 (run-hooks 'cus-test-after-load-libs-hook))
310 (defun cus-test-load-libs ()
311 "Load the libraries with autoloads.
312 Don't load libraries in `cus-test-libs-noloads'."
313 (interactive)
314 (setq cus-test-libs-errors nil)
315 (setq cus-test-libs-loaded nil)
316 (mapc
317 (lambda (file)
318 (condition-case alpha
319 (unless (member file cus-test-libs-noloads)
320 (load file)
321 (push file cus-test-libs-loaded))
322 (error
323 (push (cons file alpha) cus-test-libs-errors)
324 (message "Error for %s: %s" file alpha))))
325 (cus-test-get-autoload-deps))
326 (message "%s libraries loaded successfully"
327 (length cus-test-libs-loaded))
328 (if (not cus-test-libs-errors)
329 (message "No load problems encountered")
330 (message "The following load problems appeared:")
331 (cus-test-message cus-test-libs-errors))
332 (run-hooks 'cus-test-after-load-libs-hook))
334 (defun cus-test-get-autoload-deps ()
335 "Return the list of libraries with autoloads."
336 (with-temp-buffer
337 (insert-file-contents (locate-library "loaddefs"))
338 ;; This is from `customize-option'.
339 (let (deps file)
340 (while
341 (search-forward "\n;;; Generated autoloads from " nil t)
342 (goto-char (match-end 0))
343 (setq file (buffer-substring (point)
344 (progn (end-of-line) (point))))
345 (setq file (file-name-nondirectory file))
346 (string-match "\\.el\\'" file)
347 (setq file (substring file 0 (match-beginning 0)))
348 (setq deps (nconc deps (list file))))
349 deps)))
351 (defun cus-test-message (list)
352 "Print the members of LIST line by line."
353 (dolist (m list) (message "%s" m)))
356 ;;; The routines for batch mode:
358 (defun cus-test-opts ()
359 "Test custom options.
360 This function is suitable for batch mode. E.g., invoke
362 src/emacs -batch -l admin/cus-test.el -f cus-test-opts
364 in the Emacs source directory."
365 (interactive)
366 (message "Running %s" 'cus-test-load-libs)
367 (cus-test-load-libs)
368 (message "Running %s" 'cus-test-load-custom-loads)
369 (cus-test-load-custom-loads)
370 (message "Running %s" 'cus-test-apropos)
371 (cus-test-apropos "")
372 (if (not cus-test-errors)
373 (message "No problems found")
374 (message "The following options might have problems:")
375 (cus-test-message cus-test-errors)))
377 (defun cus-test-deps ()
378 "Run a verbose version of `custom-load-symbol' on all atoms.
379 This function is suitable for batch mode. E.g., invoke
381 src/emacs -batch -l admin/cus-test.el -f cus-test-deps
383 in the Emacs source directory."
384 (interactive)
385 (setq cus-test-deps-errors nil)
386 (setq cus-test-deps-required nil)
387 (setq cus-test-deps-loaded nil)
388 (mapatoms
389 ;; This code is mainly from `custom-load-symbol'.
390 (lambda (symbol)
391 (let ((custom-load-recursion t))
392 (dolist (load (get symbol 'custom-loads))
393 (cond
394 ((symbolp load)
395 ;; (condition-case nil (require load) (error nil))
396 (condition-case alpha
397 (unless (featurep load)
398 (require load)
399 (push (list symbol load) cus-test-deps-required))
400 (error
401 (push (list symbol load alpha) cus-test-deps-errors)
402 (message "Require problem: %s %s %s" symbol load alpha))))
403 ((equal load "loaddefs")
404 (push
405 (message "Symbol %s has loaddefs as custom dependency" symbol)
406 cus-test-deps-errors))
407 ;; This is subsumed by the test below, but it's much
408 ;; faster.
409 ((assoc load load-history))
410 ;; This was just
411 ;; (assoc (locate-library load) load-history)
412 ;; but has been optimized not to load locate-library
413 ;; if not necessary.
414 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
415 "\\(\\'\\|\\.\\)"))
416 (found nil))
417 (dolist (loaded load-history)
418 (and (stringp (car loaded))
419 (string-match regexp (car loaded))
420 (setq found t)))
421 found))
422 ;; Without this, we would load cus-edit recursively.
423 ;; We are still loading it when we call this,
424 ;; and it is not in load-history yet.
425 ((equal load "cus-edit"))
426 ;; This would ignore load problems with files in
427 ;; lisp/term/
428 ;; ((locate-library (concat term-file-prefix load)))
430 ;; (condition-case nil (load load) (error nil))
431 (condition-case alpha
432 (progn
433 (load load)
434 (push (list symbol load) cus-test-deps-loaded))
435 (error
436 (push (list symbol load alpha) cus-test-deps-errors)
437 (message "Load Problem: %s %s %s" symbol load alpha))))
438 )))))
439 (message "%s features required"
440 (length cus-test-deps-required))
441 (message "%s files loaded"
442 (length cus-test-deps-loaded))
443 (if (not cus-test-deps-errors)
444 (message "No load problems encountered")
445 (message "The following load problems appeared:")
446 (cus-test-message cus-test-deps-errors))
447 (run-hooks 'cus-test-after-load-libs-hook))
449 (defun cus-test-libs ()
450 "Load the libraries with autoloads in separate processes.
451 This function is useful to detect load problems of libraries.
452 It is suitable for batch mode. E.g., invoke
454 src/emacs -batch -l admin/cus-test.el -f cus-test-libs
456 in the Emacs source directory."
457 (interactive)
458 (with-temp-buffer
459 (setq cus-test-libs-errors nil)
460 (setq cus-test-libs-loaded nil)
461 (cd source-directory)
462 (if (not (file-executable-p "src/emacs"))
463 (error "No Emacs executable in %ssrc" default-directory))
464 (mapc
465 (lambda (file)
466 (condition-case alpha
467 (let (fn cmd status)
468 (setq fn (locate-library file))
469 (if (not fn)
470 (error "Library %s not found" file))
471 (setq cmd (concat "src/emacs -batch -l " fn))
472 (setq status (call-process shell-file-name nil nil nil
473 shell-command-switch cmd))
474 (if (equal status 0)
475 (message "%s" file)
476 (error "%s" status))
477 (push file cus-test-libs-loaded))
478 (error
479 (push (cons file alpha) cus-test-libs-errors)
480 (message "Error for %s: %s" file alpha))))
481 (cus-test-get-autoload-deps))
482 (message "Default Directory: %s" default-directory)
483 (message "%s libraries had no load errors"
484 (length cus-test-libs-loaded))
485 (if (not cus-test-libs-errors)
486 (message "No load problems encountered")
487 (message "The following load problems appeared:")
488 (cus-test-message cus-test-libs-errors))
489 (run-hooks 'cus-test-after-load-libs-hook)))
491 (defun cus-test-noloads ()
492 "Find custom options not loaded by `custom-load-symbol'.
493 Calling this function after `cus-test-load-libs' is not meaningful.
494 It is suitable for batch mode. E.g., invoke
496 src/emacs -batch -l admin/cus-test.el -f cus-test-noloads
498 in the Emacs source directory."
499 (interactive)
500 (let (cus-loaded)
502 (message "Running %s" 'cus-test-load-custom-loads)
503 (cus-test-load-custom-loads)
504 (setq cus-loaded
505 (cus-test-get-options ""))
507 (message "Running %s" 'cus-test-load-libs)
508 (cus-test-load-libs)
509 (setq cus-test-vars-not-cus-loaded
510 (cus-test-get-options ""))
512 (dolist (o cus-loaded)
513 (setq cus-test-vars-not-cus-loaded
514 (delete o cus-test-vars-not-cus-loaded)))
516 (if (not cus-test-vars-not-cus-loaded)
517 (message "No options not loaded by custom-load-symbol found")
518 (message "The following options were not loaded by custom-load-symbol:")
519 (cus-test-message
520 (sort cus-test-vars-not-cus-loaded 'string<)))))
522 ;; And last but not least a quiz:
524 ;; Evaluation of the form (customize-option 'debug-on-error) yields a
525 ;; *Customize* buffer with a mismatch mess. Why?
527 (provide 'cus-test)
529 ;;; arch-tag: a4991a31-548d-48fb-8ba1-1ebbe68eb2e7
530 ;;; cus-test.el ends here