#
[bpt/emacs.git] / admin / cus-test.el
CommitLineData
6be19e60 1;;; cus-test.el --- tests for custom types and load problems
82e74860
MR
2
3;; Copyright (C) 1998, 2000, 2002 Free Software Foundation, Inc.
4
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
9
10;; This file is part of GNU Emacs.
11
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.
16
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.
21
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.
26
27;;; Commentary:
28
6be19e60
MR
29;; This file provides simple tests to detect custom options with
30;; incorrect customization types and load problems for custom and
31;; autoload dependencies.
82e74860 32;;
6be19e60
MR
33;; The basic tests can be run in batch mode. Invoke them with
34;;
35;; src/emacs -batch -l admin/cus-test.el -f cus-test-opts
36;;
37;; src/emacs -batch -l admin/cus-test.el -f cus-test-deps
38;;
39;; src/emacs -batch -l admin/cus-test.el -f cus-test-libs
40;;
41;; in the emacs source directory.
42;;
43;; For interactive use: Load this file. Then
82e74860
MR
44;;
45;; M-x cus-test-apropos REGEXP RET
46;;
47;; checks the options matching REGEXP. In particular
48;;
49;; M-x cus-test-apropos RET
50;;
51;; checks all options. The detected options are stored in the
52;; variable `cus-test-errors'.
53;;
54;; Only those options are checked which have been already loaded.
55;; Therefore `cus-test-apropos' is more efficient after loading many
56;; libraries.
57;;
6be19e60
MR
58;; M-x cus-test-load-custom-loads
59;;
60;; loads all (!) custom dependencies and
82e74860 61;;
6be19e60
MR
62;; M-x cus-test-load-libs
63;;
64;; loads all (!) libraries with autoloads.
82e74860 65;;
82e74860 66;; Options with a custom-get property, usually defined by a :get
bd08d86a
MR
67;; declaration, are stored in the variable
68;;
69;; `cus-test-vars-with-custom-get'
70;;
71;; Options with a state of 'changed ("changed outside the customize
72;; buffer") are stored in the variable
73;;
74;; `cus-test-vars-with-changed-state'
75;;
76;; These lists are prepared just in case one wants to investigate
77;; those options further.
78;;
6be19e60 79;; The command `cus-test-opts' tests many (all?) custom options.
bd08d86a 80;;
6be19e60
MR
81;; The command `cus-test-deps' is like `cus-test-load-custom-loads'
82;; but reports about load errors.
592cd48b 83;;
6be19e60
MR
84;; The command `cus-test-libs' runs for all libraries with autoloads
85;; separate emacs processes of the form "emacs -batch -l LIB".
592cd48b 86;;
6be19e60 87;; Some results from October 2002:
592cd48b 88;;
6be19e60
MR
89;; 4523 options tested
90;; The following variables might have problems:
91;; ps-mule-font-info-database-default
592cd48b 92;;
6be19e60
MR
93;; 288 features required
94;; 10 files loaded
95;; The following load problems appeared:
6be19e60 96;; Symbol faces has loaddefs as custom dependency
95dc5eeb
MR
97;; (reftex-index-support reftex-vars (void-function reftex-set-dirty))
98;; (eshell-script em-script (void-variable eshell-directory-name))
99;; (pcomplete em-cmpl (void-function eshell-under-windows-p))
100;; (eshell-ext esh-ext (void-function eshell-under-windows-p))
6be19e60 101;; ...
592cd48b 102;;
6be19e60 103;; 422 libraries had no load errors
592cd48b 104;; The following load problems appeared:
6be19e60 105;; (eudc-export error 255)
95dc5eeb
MR
106;; (ada-xref error 255)
107;; (ada-stmt error 255)
82e74860 108
592cd48b 109;;; Code:
82e74860 110
6be19e60 111;;; Workarounds. For a smooth run and to avoid some side effects.
82e74860 112
bd08d86a 113(defvar cus-test-after-load-libs-hook nil
6be19e60
MR
114 "Used to switch off undesired side effects of loading libraries.")
115
116(defvar cus-test-skip-list nil
117 "List of variables to disregard by `cus-test-apropos'.")
bd08d86a 118
6be19e60
MR
119(defvar cus-test-noloads nil
120 "List of libraries not to load by `cus-test-load-libs'.")
121
122;; The file eudc-export.el loads libraries "bbdb" and "bbdb-com" which
123;; are not part of GNU Emacs: (locate-library "bbdb") => nil
124;; We avoid the resulting errors from loading eudc-export.el:
125(provide 'bbdb)
126(provide 'bbdb-com)
82e74860 127
6be19e60
MR
128;; This avoids a hang of `cus-test-apropos' in 21.2.
129;; (add-to-list 'cus-test-skip-list 'sh-alias-alist)
82e74860 130
6be19e60
MR
131;; Don't create a file `filesets-menu-cache-file'.
132(setq filesets-menu-cache-file "")
133;; Disable filesets hooks.
82e74860 134(add-hook
bd08d86a 135 'cus-test-after-load-libs-hook
82e74860
MR
136 (lambda nil
137 (remove-hook 'menu-bar-update-hook 'filesets-build-menu-maybe)
138 (remove-hook 'kill-emacs-hook 'filesets-exit)
139 (remove-hook 'kill-buffer-hook 'filesets-remove-from-ubl)
140 (remove-hook 'first-change-hook 'filesets-reset-filename-on-change)
141 ))
592cd48b
MR
142
143;; Loading dunnet in batch mode leads to a Dead end.
6be19e60
MR
144(let (noninteractive) (load "dunnet"))
145(add-to-list 'cus-test-noloads "dunnet")
592cd48b 146
6be19e60
MR
147;; Never Viperize.
148(setq viper-mode nil)
82e74860 149
592cd48b 150;; Don't create a file `save-place-file'.
82e74860
MR
151(eval-after-load "saveplace"
152 '(remove-hook 'kill-emacs-hook 'save-place-kill-emacs-hook))
153
592cd48b 154;; Don't create a file `abbrev-file-name'.
82e74860
MR
155(setq save-abbrevs nil)
156
157;; Avoid compile logs from adviced functions.
158(eval-after-load "bytecomp"
159 '(setq ad-default-compilation-action 'never))
160
6be19e60
MR
161\f
162;;; Main code:
163
82e74860
MR
164;; We want to log all messages.
165(setq message-log-max t)
166
592cd48b
MR
167(require 'cus-edit)
168(require 'cus-load)
169
82e74860
MR
170(defvar cus-test-errors nil
171 "List of problematic variables found by `cus-test-apropos'.")
172
6be19e60
MR
173(defvar cus-test-tested-variables nil
174 "List of options tested by last call of `cus-test-apropos'.")
175
592cd48b
MR
176(defvar cus-test-deps-errors nil
177 "List of require/load problems found by `cus-test-deps'.")
178
6be19e60
MR
179(defvar cus-test-deps-required nil
180 "List of dependencies required by `cus-test-deps'.
181Only unloaded features will be require'd.")
182
bd9aba20 183(defvar cus-test-deps-loaded nil
6be19e60 184 "List of dependencies loaded by `cus-test-deps'.")
592cd48b
MR
185
186(defvar cus-test-libs-errors nil
6be19e60 187 "List of load problems found by `cus-test-load-libs' or `cus-test-libs'.")
592cd48b
MR
188
189(defvar cus-test-libs-loaded nil
6be19e60 190 "List of files loaded by `cus-test-load-libs' or `cus-test-libs'.")
592cd48b 191
6be19e60
MR
192;; I haven't understood this :get stuff. The symbols with a
193;; custom-get property are stored here.
bd08d86a 194(defvar cus-test-vars-with-custom-get nil
82e74860
MR
195 "Set by `cus-test-apropos' to a list of options with :get property.")
196
bd08d86a
MR
197(defvar cus-test-vars-with-changed-state nil
198 "Set by `cus-test-apropos' to a list of options with state 'changed.")
199
82e74860
MR
200(defun cus-test-apropos (regexp)
201 "Check the options matching REGEXP.
202The detected problematic options are stored in `cus-test-errors'."
203 (interactive "sVariable regexp: ")
204 (setq cus-test-errors nil)
205 (setq cus-test-tested-variables nil)
6be19e60 206 (mapc
82e74860
MR
207 (lambda (symbol)
208 (push symbol cus-test-tested-variables)
6be19e60
MR
209 ;; Be verbose in case we hang.
210 (message "Cus Test running...%s %s"
211 (length cus-test-tested-variables) symbol)
82e74860
MR
212 (condition-case alpha
213 (let* ((type (custom-variable-type symbol))
214 (conv (widget-convert type))
82e74860
MR
215 (get (or (get symbol 'custom-get) 'default-value))
216 values
217 mismatch)
218 (when (default-boundp symbol)
592cd48b
MR
219 (push (funcall get symbol) values)
220 (push (eval (car (get symbol 'standard-value))) values))
82e74860 221 (if (boundp symbol)
592cd48b 222 (push (symbol-value symbol) values))
82e74860 223 ;; That does not work.
592cd48b 224 ;; (push (widget-get conv :value) values)
82e74860
MR
225
226 ;; Check the values
6be19e60
MR
227 (mapc (lambda (value)
228 (unless (widget-apply conv :match value)
229 (setq mismatch 'mismatch)))
230 values)
82e74860 231
592cd48b
MR
232 ;; Store symbols with a custom-get property.
233 (when (get symbol 'custom-get)
6be19e60
MR
234 (add-to-list 'cus-test-vars-with-custom-get
235 symbol 'append))
592cd48b 236
82e74860 237 ;; Changed outside the customize buffer?
bd08d86a
MR
238 ;; This routine is not very much tested.
239 (let ((c-value
240 (or (get symbol 'customized-value)
241 (get symbol 'saved-value)
242 (get symbol 'standard-value))))
243 (and (consp c-value)
244 (boundp symbol)
245 (not (equal (eval (car c-value)) (symbol-value symbol)))
6be19e60
MR
246 (add-to-list 'cus-test-vars-with-changed-state
247 symbol 'append)))
82e74860
MR
248
249 (if mismatch
592cd48b 250 (push symbol cus-test-errors)))
82e74860
MR
251
252 (error
592cd48b
MR
253 (push symbol cus-test-errors)
254 (message "Error for %s: %s" symbol alpha))))
82e74860 255 (cus-test-get-options regexp))
6be19e60 256 (message "%s options tested"
82e74860 257 (length cus-test-tested-variables))
592cd48b 258 (cus-test-errors-display))
82e74860
MR
259
260(defun cus-test-get-options (regexp)
261 "Return a list of custom options matching REGEXP."
262 (let (found)
263 (mapatoms
264 (lambda (symbol)
265 (and
266 (or
267 ;; (user-variable-p symbol)
268 (get symbol 'standard-value)
269 ;; (get symbol 'saved-value)
270 (get symbol 'custom-type))
271 (string-match regexp (symbol-name symbol))
6be19e60 272 (not (member symbol cus-test-skip-list))
82e74860
MR
273 (push symbol found))))
274 found))
275
276(defun cus-test-errors-display ()
277 "Report about the errors found by cus-test."
278 (with-output-to-temp-buffer "*cus-test-errors*"
279 (set-buffer standard-output)
280 (insert (format "Cus Test tested %s variables.\
281 See `cus-test-tested-variables'.\n\n"
282 (length cus-test-tested-variables)))
6be19e60
MR
283 (if (not cus-test-errors)
284 (insert "No errors found by cus-test.")
285 (insert "The following variables seem to have problems:\n\n")
286 (dolist (E cus-test-errors)
287 (insert (symbol-name E) "\n")))))
288
289(defun cus-test-load-custom-loads ()
82e74860
MR
290 "Call `custom-load-symbol' on all atoms."
291 (interactive)
292 (mapatoms 'custom-load-symbol)
bd08d86a 293 (run-hooks 'cus-test-after-load-libs-hook))
82e74860 294
6be19e60
MR
295(defun cus-test-load-libs ()
296 "Load the libraries with autoloads.
297Don't load libraries in `cus-test-noloads'."
298 (interactive)
299 (setq cus-test-libs-errors nil)
300 (setq cus-test-libs-loaded nil)
301 (mapc
302 (lambda (file)
303 (condition-case alpha
304 (unless (member file cus-test-noloads)
305 (load file)
306 (push file cus-test-libs-loaded))
307 (error
308 (push (cons file alpha) cus-test-libs-errors)
309 (message "Error for %s: %s" file alpha))))
310 (cus-test-get-autoload-deps))
311 (message "%s libraries loaded successfully"
312 (length cus-test-libs-loaded))
313 (if (not cus-test-libs-errors)
314 (message "No load problems encountered")
315 (message "The following load problems appeared:")
316 (cus-test-message cus-test-libs-errors))
317 (run-hooks 'cus-test-after-load-libs-hook))
318
319(defun cus-test-get-autoload-deps ()
320 "Return the list of libraries with autoloads."
321 (with-temp-buffer
322 (insert-file-contents (locate-library "loaddefs"))
323 ;; This is from `customize-option'.
324 (let (deps file)
325 (while
326 (search-forward "\n;;; Generated autoloads from " nil t)
327 (goto-char (match-end 0))
328 (setq file (buffer-substring (point)
329 (progn (end-of-line) (point))))
330 (setq file (file-name-nondirectory file))
331 (string-match "\\.el\\'" file)
332 (setq file (substring file 0 (match-beginning 0)))
333 (setq deps (nconc deps (list file))))
334 deps)))
335
336(defun cus-test-message (list)
337 "Print the members of LIST line by line."
338 (dolist (m list) (message "%s" m)))
339
340\f
592cd48b
MR
341;;; The routines for batch mode:
342
6be19e60 343(defun cus-test-opts ()
592cd48b
MR
344 "Test custom options.
345This function is suitable for batch mode. E.g., invoke
346
347 src/emacs -batch -l admin/cus-test.el -f cus-test-opts
348
349in the emacs source directory."
350 (interactive)
351 (message "Running %s" 'cus-test-load-custom-loads)
352 (cus-test-load-custom-loads)
353 (message "Running %s" 'cus-test-apropos)
354 (cus-test-apropos "")
6be19e60
MR
355 (if (not cus-test-errors)
356 (message "No problems found")
357 (message "The following options might have problems:")
358 (cus-test-message cus-test-errors)))
592cd48b 359
6be19e60 360(defun cus-test-deps ()
592cd48b
MR
361 "Run a verbose version of `custom-load-symbol' on all atoms.
362This function is suitable for batch mode. E.g., invoke
363
364 src/emacs -batch -l admin/cus-test.el -f cus-test-deps
365
366in the emacs source directory."
367 (interactive)
368 (setq cus-test-deps-errors nil)
6be19e60 369 (setq cus-test-deps-required nil)
bd9aba20 370 (setq cus-test-deps-loaded nil)
592cd48b
MR
371 (mapatoms
372 ;; This code is mainly from `custom-load-symbol'.
373 (lambda (symbol)
374 (unless custom-load-recursion
375 (let ((custom-load-recursion t))
376 (dolist (load (get symbol 'custom-loads))
377 (cond
378 ((symbolp load)
379 ;; (condition-case nil (require load) (error nil))
380 (condition-case alpha
6be19e60 381 (unless (featurep load)
bd9aba20 382 (require load)
6be19e60 383 (push (list symbol load) cus-test-deps-required))
592cd48b
MR
384 (error
385 (push (list symbol load alpha) cus-test-deps-errors)
6be19e60
MR
386 (message "Require problem: %s %s %s" symbol load alpha))))
387 ((equal load "loaddefs")
388 (push
389 (message "Symbol %s has loaddefs as custom dependency" symbol)
390 cus-test-deps-errors))
592cd48b
MR
391 ;; This is subsumed by the test below, but it's much
392 ;; faster.
393 ((assoc load load-history))
394 ;; This was just
395 ;; (assoc (locate-library load) load-history)
396 ;; but has been optimized not to load locate-library
397 ;; if not necessary.
398 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
399 "\\(\\'\\|\\.\\)"))
400 (found nil))
401 (dolist (loaded load-history)
402 (and (stringp (car loaded))
403 (string-match regexp (car loaded))
404 (setq found t)))
405 found))
406 ;; Without this, we would load cus-edit recursively.
407 ;; We are still loading it when we call this,
408 ;; and it is not in load-history yet.
409 ((equal load "cus-edit"))
95dc5eeb
MR
410 ;; In this test, we don't care about deps in lisp/term/
411 ((locate-library (concat term-file-prefix load)))
592cd48b
MR
412 (t
413 ;; (condition-case nil (load load) (error nil))
414 (condition-case alpha
bd9aba20
MR
415 (progn
416 (load load)
417 (push (list symbol load) cus-test-deps-loaded))
592cd48b
MR
418 (error
419 (push (list symbol load alpha) cus-test-deps-errors)
6be19e60 420 (message "Load Problem: %s %s %s" symbol load alpha))))
592cd48b 421 ))))))
6be19e60
MR
422 (message "%s features required"
423 (length cus-test-deps-required))
424 (message "%s files loaded"
bd9aba20 425 (length cus-test-deps-loaded))
6be19e60
MR
426 (if (not cus-test-deps-errors)
427 (message "No load problems encountered")
428 (message "The following load problems appeared:")
429 (cus-test-message cus-test-deps-errors))
592cd48b
MR
430 (run-hooks 'cus-test-after-load-libs-hook))
431
432(defun cus-test-libs ()
6be19e60 433 "Load the libraries with autoloads in separate processes.
592cd48b
MR
434This function is useful to detect load problems of libraries.
435It is suitable for batch mode. E.g., invoke
436
437 src/emacs -batch -l admin/cus-test.el -f cus-test-libs
438
439in the emacs source directory."
82e74860 440 (interactive)
6be19e60
MR
441 (with-temp-buffer
442 (setq cus-test-libs-errors nil)
443 (setq cus-test-libs-loaded nil)
444 (cd source-directory)
445 (if (not (file-executable-p "src/emacs"))
446 (error "No Emacs executable in %ssrc" default-directory))
447 (mapc
448 (lambda (file)
449 (condition-case alpha
450 (let (fn cmd status)
451 (setq fn (locate-library file))
452 (if (not fn)
453 (error "Library %s not found" file))
454 (setq cmd (concat "src/emacs -batch -l " fn))
455 (setq status (call-process shell-file-name nil nil nil
456 shell-command-switch cmd))
457 (if (= status 0)
458 (message "%s" file)
459 (error "%s" status))
460 (push file cus-test-libs-loaded))
461 (error
462 (push (cons file alpha) cus-test-libs-errors)
463 (message "Error for %s: %s" file alpha))))
464 (cus-test-get-autoload-deps))
465 (message "Default Directory: %s" default-directory)
466 (message "%s libraries had no load errors"
467 (length cus-test-libs-loaded))
468 (if (not cus-test-libs-errors)
469 (message "No load problems encountered")
470 (message "The following load problems appeared:")
471 (cus-test-message cus-test-libs-errors))
472 (run-hooks 'cus-test-after-load-libs-hook)))
82e74860 473
82e74860
MR
474(provide 'cus-test)
475
476;;; cus-test.el ends here