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