cus-test.el 17 KB

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