cus-test.el 18 KB

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