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