]> code.delx.au - gnu-emacs/blob - admin/cus-test.el
Merge from trunk, configury not fixed yet.
[gnu-emacs] / admin / cus-test.el
1 ;;; cus-test.el --- tests for custom types and load problems
2
3 ;; Copyright (C) 1998, 2000, 2002-2013 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 3 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This file provides simple tests to detect custom options with
28 ;; incorrect customization types and load problems for custom and
29 ;; autoload dependencies.
30 ;;
31 ;; The basic tests can be run in batch mode. Invoke them with
32 ;;
33 ;; src/emacs -batch -l admin/cus-test.el -f cus-test-opts [all]
34 ;;
35 ;; src/emacs -batch -l admin/cus-test.el -f cus-test-deps
36 ;;
37 ;; src/emacs -batch -l admin/cus-test.el -f cus-test-libs [all]
38 ;;
39 ;; src/emacs -batch -l admin/cus-test.el -f cus-test-noloads
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 ;; The command `cus-test-noloads' returns a list of variables which
88 ;; are somewhere declared as custom options, but not loaded by
89 ;; `custom-load-symbol'.
90
91 \f
92 ;;; Code:
93
94 ;;; Workarounds. For a smooth run and to avoid some side effects.
95
96 (defvar cus-test-after-load-libs-hook nil
97 "Used to switch off undesired side effects of loading libraries.")
98
99 (defvar cus-test-skip-list nil
100 "List of variables to disregard by `cus-test-apropos'.")
101
102 (defvar cus-test-libs-noloads
103 ;; Loading dunnet in batch mode leads to a Dead end.
104 ;; blessmail writes a file.
105 ;; characters cannot be loaded twice ("Category `a' is already defined").
106 '("play/dunnet.el" "emulation/edt-mapper.el"
107 "loadup.el" "mail/blessmail.el" "international/characters.el"
108 "cedet/ede/loaddefs.el" "cedet/semantic/loaddefs.el"
109 "net/tramp-loaddefs.el")
110 "List of files not to load by `cus-test-load-libs'.
111 Names should be as they appear in loaddefs.el.")
112
113 ;; This avoids a hang of `cus-test-apropos' in 21.2.
114 ;; (add-to-list 'cus-test-skip-list 'sh-alias-alist)
115
116 (or noninteractive
117 ;; Never Viperize.
118 (setq viper-mode nil))
119
120 ;; Don't create a file `save-place-file'.
121 (eval-after-load "saveplace"
122 '(remove-hook 'kill-emacs-hook 'save-place-kill-emacs-hook))
123
124 ;; Don't create a file `abbrev-file-name'.
125 (setq save-abbrevs nil)
126
127 ;; Avoid compile logs from adviced functions.
128 (eval-after-load "bytecomp"
129 '(setq ad-default-compilation-action 'never))
130
131 \f
132 ;;; Main code:
133
134 ;; We want to log all messages.
135 (setq message-log-max t)
136
137 (require 'cus-edit)
138 (require 'cus-load)
139
140 (defvar cus-test-errors nil
141 "List of problematic variables found by `cus-test-apropos'.")
142
143 (defvar cus-test-tested-variables nil
144 "List of options tested by last call of `cus-test-apropos'.")
145
146 ;; I haven't understood this :get stuff. The symbols with a
147 ;; custom-get property are stored here.
148 (defvar cus-test-vars-with-custom-get nil
149 "Set by `cus-test-apropos' to a list of options with :get property.")
150
151 (defvar cus-test-vars-with-changed-state nil
152 "Set by `cus-test-apropos' to a list of options with state 'changed.")
153
154 (defvar cus-test-deps-errors nil
155 "List of require/load problems found by `cus-test-deps'.")
156
157 (defvar cus-test-deps-required nil
158 "List of dependencies required by `cus-test-deps'.
159 Only unloaded features will be require'd.")
160
161 (defvar cus-test-deps-loaded nil
162 "List of dependencies loaded by `cus-test-deps'.")
163
164 (defvar cus-test-libs-errors nil
165 "List of load problems found by `cus-test-load-libs' or `cus-test-libs'.")
166
167 (defvar cus-test-libs-loaded nil
168 "List of files loaded by `cus-test-load-libs' or `cus-test-libs'.")
169
170 (defvar cus-test-vars-not-cus-loaded nil
171 "A list of options not loaded by `custom-load-symbol'.
172 Set by `cus-test-noloads'.")
173
174 ;; (defvar cus-test-vars-cus-loaded nil
175 ;; "A list of options loaded by `custom-load-symbol'.")
176
177 (defun cus-test-apropos (regexp)
178 "Check the options matching REGEXP.
179 The detected problematic options are stored in `cus-test-errors'."
180 (interactive "sVariable regexp: ")
181 (setq cus-test-errors nil)
182 (setq cus-test-tested-variables nil)
183 (mapc
184 (lambda (symbol)
185 (push symbol cus-test-tested-variables)
186 ;; Be verbose in case we hang.
187 (message "Cus Test running...%s %s"
188 (length cus-test-tested-variables) symbol)
189 (condition-case alpha
190 (let* ((type (custom-variable-type symbol))
191 (conv (widget-convert type))
192 (get (or (get symbol 'custom-get) 'default-value))
193 values
194 mismatch)
195 (when (default-boundp symbol)
196 (push (funcall get symbol) values)
197 (push (eval (car (get symbol 'standard-value))) values))
198 (if (boundp symbol)
199 (push (symbol-value symbol) values))
200 ;; That does not work.
201 ;; (push (widget-get conv :value) values)
202
203 ;; Check the values
204 (mapc (lambda (value)
205 (unless (widget-apply conv :match value)
206 (setq mismatch 'mismatch)))
207 values)
208
209 ;; Store symbols with a custom-get property.
210 (when (get symbol 'custom-get)
211 (add-to-list 'cus-test-vars-with-custom-get symbol))
212
213 ;; Changed outside the customize buffer?
214 ;; This routine is not very much tested.
215 (let ((c-value
216 (or (get symbol 'customized-value)
217 (get symbol 'saved-value)
218 (get symbol 'standard-value))))
219 (and (consp c-value)
220 (boundp symbol)
221 (not (equal (eval (car c-value)) (symbol-value symbol)))
222 (add-to-list 'cus-test-vars-with-changed-state symbol)))
223
224 (if mismatch
225 (push symbol cus-test-errors)))
226
227 (error
228 (push symbol cus-test-errors)
229 (message "Error for %s: %s" symbol alpha))))
230 (cus-test-get-options regexp))
231 (message "%s options tested"
232 (length cus-test-tested-variables))
233 (cus-test-errors-display))
234
235 (defun cus-test-get-options (regexp)
236 "Return a list of custom options matching REGEXP."
237 (let (found)
238 (mapatoms
239 (lambda (symbol)
240 (and
241 (or
242 ;; (user-variable-p symbol)
243 (get symbol 'standard-value)
244 ;; (get symbol 'saved-value)
245 (get symbol 'custom-type))
246 (string-match regexp (symbol-name symbol))
247 (not (member symbol cus-test-skip-list))
248 (push symbol found))))
249 found))
250
251 (defun cus-test-errors-display ()
252 "Report about the errors found by cus-test."
253 (with-output-to-temp-buffer "*cus-test-errors*"
254 (set-buffer standard-output)
255 (insert (format "Cus Test tested %s variables.\
256 See `cus-test-tested-variables'.\n\n"
257 (length cus-test-tested-variables)))
258 (if (not cus-test-errors)
259 (insert "No errors found by cus-test.")
260 (insert "The following variables seem to have problems:\n\n")
261 (dolist (e cus-test-errors)
262 (insert (symbol-name e) "\n")))))
263
264 (defun cus-test-load-custom-loads ()
265 "Call `custom-load-symbol' on all atoms."
266 (interactive)
267 (if noninteractive (let (noninteractive) (require 'dunnet)))
268 (mapatoms 'custom-load-symbol)
269 (run-hooks 'cus-test-after-load-libs-hook))
270
271 (defmacro cus-test-load-1 (&rest body)
272 `(progn
273 (setq cus-test-libs-errors nil
274 cus-test-libs-loaded nil)
275 ,@body
276 (message "%s libraries loaded successfully"
277 (length cus-test-libs-loaded))
278 (if (not cus-test-libs-errors)
279 (message "No load problems encountered")
280 (message "The following load problems appeared:")
281 (cus-test-message cus-test-libs-errors))
282 (run-hooks 'cus-test-after-load-libs-hook)))
283
284 ;; This is just cus-test-libs, but loading in the current Emacs process.
285 (defun cus-test-load-libs (&optional more)
286 "Load the libraries with autoloads.
287 Don't load libraries in `cus-test-libs-noloads'.
288 If optional argument MORE is \"defcustom\", load all files with defcustoms.
289 If it is \"all\", load all Lisp files."
290 (interactive)
291 (cus-test-load-1
292 (let ((lispdir (file-name-directory (locate-library "loaddefs"))))
293 (mapc
294 (lambda (file)
295 (condition-case alpha
296 (unless (member file cus-test-libs-noloads)
297 (load (file-name-sans-extension (expand-file-name file lispdir)))
298 (push file cus-test-libs-loaded))
299 (error
300 (push (cons file alpha) cus-test-libs-errors)
301 (message "Error for %s: %s" file alpha))))
302 (if more
303 (cus-test-get-lisp-files (equal more "all"))
304 (cus-test-get-autoload-deps))))))
305
306 (defun cus-test-get-autoload-deps ()
307 "Return the list of files with autoloads."
308 (with-temp-buffer
309 (insert-file-contents (locate-library "loaddefs"))
310 (let (files)
311 (while (search-forward "\n;;; Generated autoloads from " nil t)
312 (push (buffer-substring (match-end 0) (line-end-position)) files))
313 files)))
314
315 (defun cus-test-get-lisp-files (&optional all)
316 "Return list of all Lisp files with defcustoms.
317 Optional argument ALL non-nil means list all (non-obsolete) Lisp files."
318 (let ((default-directory (expand-file-name "lisp/" source-directory))
319 (msg "Finding files..."))
320 (message "%s" msg)
321 (prog1
322 ;; Hack to remove leading "./".
323 (mapcar (lambda (e) (substring e 2))
324 (apply 'process-lines find-program
325 "-name" "obsolete" "-prune" "-o"
326 "-name" "[^.]*.el" ; ignore .dir-locals.el
327 (if all
328 '("-print")
329 (list "-exec" grep-program
330 "-l" "^[ \t]*(defcustom" "{}" "+"))))
331 (message "%sdone" msg))))
332
333 (defun cus-test-message (list)
334 "Print the members of LIST line by line."
335 (dolist (m list) (message "%s" m)))
336
337 \f
338 ;;; The routines for batch mode:
339
340 (defun cus-test-opts (&optional all)
341 "Test custom options.
342 This function is suitable for batch mode. E.g., invoke
343
344 src/emacs -batch -l admin/cus-test.el -f cus-test-opts
345
346 in the Emacs source directory.
347 Normally only tests options belonging to files in loaddefs.el.
348 If optional argument ALL is non-nil, test all files with defcustoms."
349 (interactive)
350 (and noninteractive
351 command-line-args-left
352 (setq all (pop command-line-args-left)))
353 (message "Running %s" 'cus-test-load-libs)
354 (cus-test-load-libs (if all "defcustom"))
355 (message "Running %s" 'cus-test-load-custom-loads)
356 (cus-test-load-custom-loads)
357 (message "Running %s" 'cus-test-apropos)
358 (cus-test-apropos "")
359 (if (not cus-test-errors)
360 (message "No problems found")
361 (message "The following options might have problems:")
362 (cus-test-message cus-test-errors)))
363
364 (defun cus-test-deps ()
365 "Run a verbose version of `custom-load-symbol' on all atoms.
366 This function is suitable for batch mode. E.g., invoke
367
368 src/emacs -batch -l admin/cus-test.el -f cus-test-deps
369
370 in the Emacs source directory."
371 (interactive)
372 (setq cus-test-deps-errors nil)
373 (setq cus-test-deps-required nil)
374 (setq cus-test-deps-loaded nil)
375 (mapatoms
376 ;; This code is mainly from `custom-load-symbol'.
377 (lambda (symbol)
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 (or (featurep load)
385 (and noninteractive (eq load 'dunnet)))
386 (require load)
387 (push (list symbol load) cus-test-deps-required))
388 (error
389 (push (list symbol load alpha) cus-test-deps-errors)
390 (message "Require problem: %s %s %s" symbol load alpha))))
391 ((equal load "loaddefs")
392 (push
393 (message "Symbol %s has loaddefs as custom dependency" symbol)
394 cus-test-deps-errors))
395 ;; This is subsumed by the test below, but it's much
396 ;; faster.
397 ((assoc load load-history))
398 ;; This was just
399 ;; (assoc (locate-library load) load-history)
400 ;; but has been optimized not to load locate-library
401 ;; if not necessary.
402 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
403 "\\(\\'\\|\\.\\)"))
404 (found nil))
405 (dolist (loaded load-history)
406 (and (stringp (car loaded))
407 (string-match regexp (car loaded))
408 (setq found t)))
409 found))
410 ;; Without this, we would load cus-edit recursively.
411 ;; We are still loading it when we call this,
412 ;; and it is not in load-history yet.
413 ((equal load "cus-edit"))
414 ;; This would ignore load problems with files in
415 ;; lisp/term/
416 ;; ((locate-library (concat term-file-prefix load)))
417 (t
418 ;; (condition-case nil (load load) (error nil))
419 (condition-case alpha
420 (progn
421 (load load)
422 (push (list symbol load) cus-test-deps-loaded))
423 (error
424 (push (list symbol load alpha) cus-test-deps-errors)
425 (message "Load Problem: %s %s %s" symbol load alpha))))
426 )))))
427 (message "%s features required"
428 (length cus-test-deps-required))
429 (message "%s files loaded"
430 (length cus-test-deps-loaded))
431 (if (not cus-test-deps-errors)
432 (message "No load problems encountered")
433 (message "The following load problems appeared:")
434 (cus-test-message cus-test-deps-errors))
435 (run-hooks 'cus-test-after-load-libs-hook))
436
437 (defun cus-test-libs (&optional more)
438 "Load the libraries with autoloads in separate processes.
439 This function is useful to detect load problems of libraries.
440 It is suitable for batch mode. E.g., invoke
441
442 ./src/emacs -batch -l admin/cus-test.el -f cus-test-libs
443
444 in the Emacs source directory.
445
446 If optional argument MORE is \"defcustom\", load all files with defcustoms.
447 If it is \"all\", load all Lisp files."
448 (interactive)
449 (and noninteractive
450 command-line-args-left
451 (setq more (pop command-line-args-left)))
452 (cus-test-load-1
453 (let* ((default-directory source-directory)
454 (emacs (expand-file-name "src/emacs"))
455 skipped)
456 (or (file-executable-p emacs)
457 (error "No such executable `%s'" emacs))
458 (mapc
459 (lambda (file)
460 (if (member file cus-test-libs-noloads)
461 (push file skipped)
462 (condition-case alpha
463 (let* ((fn (expand-file-name file "lisp/"))
464 (elc (concat fn "c"))
465 status)
466 (if (file-readable-p elc) ; load compiled if present (faster)
467 (setq fn elc)
468 (or (file-readable-p fn)
469 (error "Library %s not found" file)))
470 (if (equal 0 (setq status (call-process emacs nil nil nil
471 "-batch" "-l" fn)))
472 (message "%s" file)
473 (error "%s" status))
474 (push file cus-test-libs-loaded))
475 (error
476 (push (cons file alpha) cus-test-libs-errors)
477 (message "Error for %s: %s" file alpha)))))
478 (if more
479 (cus-test-get-lisp-files (equal more "all"))
480 (cus-test-get-autoload-deps)))
481 (message "Default directory: %s" default-directory)
482 (when skipped
483 (message "The following libraries were skipped:")
484 (cus-test-message skipped)))))
485
486 (defun cus-test-noloads ()
487 "Find custom options not loaded by `custom-load-symbol'.
488 Calling this function after `cus-test-load-libs' is not meaningful.
489 It is suitable for batch mode. E.g., invoke
490
491 src/emacs -batch -l admin/cus-test.el -f cus-test-noloads
492
493 in the Emacs source directory."
494 (interactive)
495 (let (cus-loaded)
496
497 (message "Running %s" 'cus-test-load-custom-loads)
498 (cus-test-load-custom-loads)
499 (setq cus-loaded
500 (cus-test-get-options ""))
501
502 (message "Running %s" 'cus-test-load-libs)
503 (cus-test-load-libs "all")
504 (setq cus-test-vars-not-cus-loaded
505 (cus-test-get-options ""))
506
507 (dolist (o cus-loaded)
508 (setq cus-test-vars-not-cus-loaded
509 (delete o cus-test-vars-not-cus-loaded)))
510
511 (if (not cus-test-vars-not-cus-loaded)
512 (message "No options not loaded by custom-load-symbol found")
513 (message "The following options were not loaded by custom-load-symbol:")
514 (cus-test-message
515 (sort cus-test-vars-not-cus-loaded 'string<)))))
516
517 (provide 'cus-test)
518
519 ;;; cus-test.el ends here