Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

459 lines
18 KiB

  1. ;;; flycheck-ert.el --- Flycheck: ERT extensions -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017 Flycheck contributors
  3. ;; Copyright (C) 2013-2016 Sebastian Wiesner and Flycheck contributors
  4. ;; Author: Sebastian Wiesner <swiesner@lunaryorn.com>
  5. ;; Maintainer: Clément Pit-Claudel <clement.pitclaudel@live.com>
  6. ;; fmdkdd <fmdkdd@gmail.com>
  7. ;; URL: https://github.com/flycheck/flycheck
  8. ;; This file is not part of GNU Emacs.
  9. ;; This program is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; Unit testing library for Flycheck, the modern on-the-fly syntax checking
  21. ;; extension for GNU Emacs.
  22. ;; Provide various utility functions and unit test helpers to test Flycheck and
  23. ;; Flycheck extensions.
  24. ;;; Code:
  25. (require 'flycheck)
  26. (require 'ert)
  27. (require 'macroexp) ; For macro utilities
  28. ;;; Compatibility
  29. (eval-and-compile
  30. ;; Provide `ert-skip' and friends for Emacs 24.3
  31. (defconst flycheck-ert-ert-can-skip (fboundp 'ert-skip)
  32. "Whether ERT supports test skipping.")
  33. (unless (fboundp 'define-error)
  34. ;; from Emacs `subr.el'
  35. (defun define-error (name message &optional parent)
  36. "Define NAME as a new error signal.
  37. MESSAGE is a string that will be output to the echo area if such an error
  38. is signaled without being caught by a `condition-case'.
  39. PARENT is either a signal or a list of signals from which it inherits.
  40. Defaults to `error'."
  41. (unless parent (setq parent 'error))
  42. (let ((conditions
  43. (if (consp parent)
  44. (apply #'append
  45. (mapcar (lambda (parent)
  46. (cons parent
  47. (or (get parent 'error-conditions)
  48. (error "Unknown signal `%s'" parent))))
  49. parent))
  50. (cons parent (get parent 'error-conditions)))))
  51. (put name 'error-conditions
  52. (delete-dups (copy-sequence (cons name conditions))))
  53. (when message (put name 'error-message message)))))
  54. (unless flycheck-ert-ert-can-skip
  55. ;; Fake skipping
  56. (define-error 'flycheck-ert-skipped "Test skipped")
  57. (defun ert-skip (data)
  58. (signal 'flycheck-ert-skipped data))
  59. (defmacro skip-unless (form)
  60. `(unless (ignore-errors ,form)
  61. (signal 'flycheck-ert-skipped ',form)))
  62. (defun ert-test-skipped-p (result)
  63. (and (ert-test-failed-p result)
  64. (eq (car (ert-test-failed-condition result))
  65. 'flycheck-ert-skipped)))))
  66. ;;; Internal variables
  67. (defvar flycheck-ert--resource-directory nil
  68. "The directory to get resources from in this test suite.")
  69. ;;; Resource management macros
  70. (defmacro flycheck-ert-with-temp-buffer (&rest body)
  71. "Eval BODY within a temporary buffer.
  72. Like `with-temp-buffer', but resets the modification state of the
  73. temporary buffer to make sure that it is properly killed even if
  74. it has a backing file and is modified."
  75. (declare (indent 0))
  76. `(with-temp-buffer
  77. (unwind-protect
  78. ,(macroexp-progn body)
  79. ;; Reset modification state of the buffer, and unlink it from its backing
  80. ;; file, if any, because Emacs refuses to kill modified buffers with
  81. ;; backing files, even if they are temporary.
  82. (set-buffer-modified-p nil)
  83. (set-visited-file-name nil 'no-query))))
  84. (defmacro flycheck-ert-with-file-buffer (file-name &rest body)
  85. "Create a buffer from FILE-NAME and eval BODY.
  86. BODY is evaluated with `current-buffer' being a buffer with the
  87. contents FILE-NAME."
  88. (declare (indent 1))
  89. `(let ((file-name ,file-name))
  90. (unless (file-exists-p file-name)
  91. (error "%s does not exist" file-name))
  92. (flycheck-ert-with-temp-buffer
  93. (insert-file-contents file-name 'visit)
  94. (set-visited-file-name file-name 'no-query)
  95. (cd (file-name-directory file-name))
  96. ;; Mark the buffer as not modified, because we just loaded the file up to
  97. ;; now.
  98. (set-buffer-modified-p nil)
  99. ,@body)))
  100. (defmacro flycheck-ert-with-help-buffer (&rest body)
  101. "Execute BODY and kill the help buffer afterwards.
  102. Use this macro to test functions that create a Help buffer."
  103. (declare (indent 0))
  104. `(unwind-protect
  105. ,(macroexp-progn body)
  106. (when (buffer-live-p (get-buffer (help-buffer)))
  107. (kill-buffer (help-buffer)))))
  108. (defmacro flycheck-ert-with-global-mode (&rest body)
  109. "Execute BODY with Global Flycheck Mode enabled.
  110. After BODY, restore the old state of Global Flycheck Mode."
  111. (declare (indent 0))
  112. `(let ((old-state global-flycheck-mode))
  113. (unwind-protect
  114. (progn
  115. (global-flycheck-mode 1)
  116. ,@body)
  117. (global-flycheck-mode (if old-state 1 -1)))))
  118. (defmacro flycheck-ert-with-env (env &rest body)
  119. "Add ENV to `process-environment' in BODY.
  120. Execute BODY with a `process-environment' with contains all
  121. variables from ENV added.
  122. ENV is an alist, where each cons cell `(VAR . VALUE)' is a
  123. environment variable VAR to be added to `process-environment'
  124. with VALUE."
  125. (declare (indent 1))
  126. `(let ((process-environment (copy-sequence process-environment)))
  127. (pcase-dolist (`(,var . ,value) ,env)
  128. (setenv var value))
  129. ,@body))
  130. ;;; Test resources
  131. (defun flycheck-ert-resource-filename (resource-file)
  132. "Determine the absolute file name of a RESOURCE-FILE.
  133. Relative file names are expanded against
  134. `flycheck-ert-resources-directory'."
  135. (expand-file-name resource-file flycheck-ert--resource-directory))
  136. (defmacro flycheck-ert-with-resource-buffer (resource-file &rest body)
  137. "Create a temp buffer from a RESOURCE-FILE and execute BODY.
  138. The absolute file name of RESOURCE-FILE is determined with
  139. `flycheck-ert-resource-filename'."
  140. (declare (indent 1))
  141. `(flycheck-ert-with-file-buffer
  142. (flycheck-ert-resource-filename ,resource-file)
  143. ,@body))
  144. ;;; Test suite initialization
  145. (defun flycheck-ert-initialize (resource-dir)
  146. "Initialize a test suite with RESOURCE-DIR.
  147. RESOURCE-DIR is the directory, `flycheck-ert-resource-filename'
  148. should use to lookup resource files."
  149. (when flycheck-ert--resource-directory
  150. (error "Test suite already initialized"))
  151. (let ((tests (ert-select-tests t t)))
  152. ;; Select all tests
  153. (unless tests
  154. (error "No tests defined. Call `flycheck-ert-initialize' after defining all tests!"))
  155. (setq flycheck-ert--resource-directory resource-dir)
  156. ;; Emacs 24.3 don't support skipped tests, so we add poor man's test
  157. ;; skipping: We mark skipped tests as expected failures by adjusting the
  158. ;; expected result of all test cases. Not particularly pretty, but works :)
  159. (unless flycheck-ert-ert-can-skip
  160. (dolist (test tests)
  161. (let ((result (ert-test-expected-result-type test)))
  162. (setf (ert-test-expected-result-type test)
  163. `(or ,result (satisfies ert-test-skipped-p))))))))
  164. ;;; Test case definitions
  165. (defmacro flycheck-ert-def-checker-test (checker language name
  166. &rest keys-and-body)
  167. "Define a test case for a syntax CHECKER for LANGUAGE.
  168. CHECKER is a symbol or a list of symbols denoting syntax checkers
  169. being tested by the test. The test case is skipped, if any of
  170. these checkers cannot be used. LANGUAGE is a symbol or a list of
  171. symbols denoting the programming languages supported by the
  172. syntax checkers. This is currently only used for tagging the
  173. test appropriately.
  174. NAME is a symbol denoting the local name of the test. The test
  175. itself is ultimately named
  176. `flycheck-define-checker/CHECKER/NAME'. If CHECKER is a list,
  177. the first checker in the list is used for naming the test.
  178. Optionally, the keyword arguments `:tags' and `:expected-result'
  179. may be given. They have the same meaning as in `ert-deftest.',
  180. and are added to the tags and result expectations set up by this
  181. macro.
  182. The remaining forms KEYS-AND-BODY denote the body of the test
  183. case, including assertions and setup code."
  184. (declare (indent 3))
  185. (unless checker
  186. (error "No syntax checkers specified"))
  187. (unless language
  188. (error "No languages specified"))
  189. (let* ((checkers (if (symbolp checker) (list checker) checker))
  190. (checker (car checkers))
  191. (languages (if (symbolp language) (list language) language))
  192. (language-tags (mapcar (lambda (l) (intern (format "language-%s" l)))
  193. languages))
  194. (checker-tags (mapcar (lambda (c) (intern (format "checker-%s" c)))
  195. checkers))
  196. (local-name (or name 'default))
  197. (full-name (intern (format "flycheck-define-checker/%s/%s"
  198. checker local-name)))
  199. (keys-and-body (ert--parse-keys-and-body keys-and-body))
  200. (body (cadr keys-and-body))
  201. (keys (car keys-and-body))
  202. (default-tags '(syntax-checker external-tool)))
  203. `(ert-deftest ,full-name ()
  204. :expected-result
  205. (list 'or
  206. '(satisfies flycheck-ert-syntax-check-timed-out-p)
  207. ,(or (plist-get keys :expected-result) :passed))
  208. :tags (append ',(append default-tags language-tags checker-tags)
  209. ,(plist-get keys :tags))
  210. ,@(mapcar (lambda (c) `(skip-unless
  211. ;; Ignore non-command checkers
  212. (or (not (flycheck-checker-get ',c 'command))
  213. (executable-find (flycheck-checker-executable ',c)))))
  214. checkers)
  215. ,@body)))
  216. ;;; Test case results
  217. (defun flycheck-ert-syntax-check-timed-out-p (result)
  218. "Whether RESULT denotes a timed-out test.
  219. RESULT is an ERT test result object."
  220. (and (ert-test-failed-p result)
  221. (eq (car (ert-test-failed-condition result))
  222. 'flycheck-ert-syntax-check-timed-out)))
  223. ;;; Syntax checking in tests
  224. (defvar-local flycheck-ert-syntax-checker-finished nil
  225. "Non-nil if the current checker has finished.")
  226. (add-hook 'flycheck-after-syntax-check-hook
  227. (lambda () (setq flycheck-ert-syntax-checker-finished t)))
  228. (defconst flycheck-ert-checker-wait-time 10
  229. "Time to wait until a checker is finished in seconds.
  230. After this time has elapsed, the checker is considered to have
  231. failed, and the test aborted with failure.")
  232. (define-error 'flycheck-ert-syntax-check-timed-out "Syntax check timed out.")
  233. (defun flycheck-ert-wait-for-syntax-checker ()
  234. "Wait until the syntax check in the current buffer is finished."
  235. (let ((starttime (float-time)))
  236. (while (and (not flycheck-ert-syntax-checker-finished)
  237. (< (- (float-time) starttime) flycheck-ert-checker-wait-time))
  238. (sleep-for 1))
  239. (unless (< (- (float-time) starttime) flycheck-ert-checker-wait-time)
  240. (flycheck-stop)
  241. (signal 'flycheck-ert-syntax-check-timed-out nil)))
  242. (setq flycheck-ert-syntax-checker-finished nil))
  243. (defun flycheck-ert-buffer-sync ()
  244. "Like `flycheck-buffer', but synchronously."
  245. (setq flycheck-ert-syntax-checker-finished nil)
  246. (should (not (flycheck-running-p)))
  247. (flycheck-mode) ; This will only start a deferred check,
  248. (flycheck-buffer) ; so we need an explicit manual check
  249. ;; After starting the check, the checker should either be running now, or
  250. ;; already be finished (if it was fast).
  251. (should (or flycheck-current-syntax-check
  252. flycheck-ert-syntax-checker-finished))
  253. ;; Also there should be no deferred check pending anymore
  254. (should-not (flycheck-deferred-check-p))
  255. (flycheck-ert-wait-for-syntax-checker))
  256. (defun flycheck-ert-ensure-clear ()
  257. "Clear the current buffer.
  258. Raise an assertion error if the buffer is not clear afterwards."
  259. (flycheck-clear)
  260. (should (not flycheck-current-errors))
  261. (should (not (-any? (lambda (ov) (overlay-get ov 'flycheck-overlay))
  262. (overlays-in (point-min) (point-max))))))
  263. ;;; Test assertions
  264. (defun flycheck-ert-should-overlay (error)
  265. "Test that ERROR has a proper overlay in the current buffer.
  266. ERROR is a Flycheck error object."
  267. (let* ((overlay (-first (lambda (ov) (equal (overlay-get ov 'flycheck-error)
  268. error))
  269. (flycheck-overlays-in 0 (+ 1 (buffer-size)))))
  270. (region (flycheck-error-region-for-mode error 'symbols))
  271. (level (flycheck-error-level error))
  272. (category (flycheck-error-level-overlay-category level))
  273. (face (get category 'face))
  274. (fringe-bitmap (flycheck-error-level-fringe-bitmap level))
  275. (fringe-face (flycheck-error-level-fringe-face level))
  276. (fringe-icon (list 'left-fringe fringe-bitmap fringe-face)))
  277. (should overlay)
  278. (should (overlay-get overlay 'flycheck-overlay))
  279. (should (= (overlay-start overlay) (car region)))
  280. (should (= (overlay-end overlay) (cdr region)))
  281. (should (eq (overlay-get overlay 'face) face))
  282. (should (equal (get-char-property 0 'display
  283. (overlay-get overlay 'before-string))
  284. fringe-icon))
  285. (should (eq (overlay-get overlay 'category) category))
  286. (should (equal (overlay-get overlay 'flycheck-error) error))))
  287. (defun flycheck-ert-should-errors (&rest errors)
  288. "Test that the current buffers has ERRORS.
  289. ERRORS is a list of errors expected to be present in the current
  290. buffer. Each error is given as a list of arguments to
  291. `flycheck-error-new-at'.
  292. If ERRORS are omitted, test that there are no errors at all in
  293. the current buffer.
  294. With ERRORS, test that each error in ERRORS is present in the
  295. current buffer, and that the number of errors in the current
  296. buffer is equal to the number of given ERRORS. In other words,
  297. check that the buffer has all ERRORS, and no other errors."
  298. (let ((expected (mapcar (apply-partially #'apply #'flycheck-error-new-at)
  299. errors)))
  300. (should (equal expected flycheck-current-errors))
  301. (mapc #'flycheck-ert-should-overlay expected))
  302. (should (= (length errors)
  303. (length (flycheck-overlays-in (point-min) (point-max))))))
  304. (define-error 'flycheck-ert-suspicious-checker "Suspicious state from checker")
  305. (defun flycheck-ert-should-syntax-check (resource-file modes &rest errors)
  306. "Test a syntax check in RESOURCE-FILE with MODES.
  307. RESOURCE-FILE is the file to check. MODES is a single major mode
  308. symbol or a list thereof, specifying the major modes to syntax
  309. check with. If more than one major mode is specified, the test
  310. is run for each mode separately, so if you give three major
  311. modes, the entire test will run three times. ERRORS is the list
  312. of expected errors, as in `flycheck-ert-should-errors'. If
  313. omitted, the syntax check must not emit any errors. The errors
  314. are cleared after each test.
  315. The syntax checker is selected via standard syntax checker
  316. selection. To test a specific checker, you need to set
  317. `flycheck-checker' or `flycheck-disabled-checkers' accordingly
  318. before using this predicate, depending on whether you want to use
  319. manual or automatic checker selection.
  320. During the syntax check, configuration files of syntax checkers
  321. are also searched in the `config-files' sub-directory of the
  322. resource directory."
  323. (when (symbolp modes)
  324. (setq modes (list modes)))
  325. (dolist (mode modes)
  326. (unless (fboundp mode)
  327. (ert-skip (format "%S missing" mode)))
  328. (flycheck-ert-with-resource-buffer resource-file
  329. (funcall mode)
  330. ;; Load safe file-local variables because some tests depend on them
  331. (let ((enable-local-variables :safe)
  332. ;; Disable all hooks at this place, to prevent 3rd party packages
  333. ;; from interferring
  334. (hack-local-variables-hook))
  335. (hack-local-variables))
  336. ;; Configure config file locating for unit tests
  337. (let ((process-hook-called 0))
  338. (add-hook 'flycheck-process-error-functions
  339. (lambda (_err)
  340. (setq process-hook-called (1+ process-hook-called))
  341. nil)
  342. nil :local)
  343. (add-hook 'flycheck-status-changed-functions
  344. (lambda (status)
  345. (when (eq status 'suspicious)
  346. (signal 'flycheck-ert-suspicious-checker nil))))
  347. (flycheck-ert-buffer-sync)
  348. (apply #'flycheck-ert-should-errors errors)
  349. (should (= process-hook-called (length errors))))
  350. (flycheck-ert-ensure-clear))))
  351. (defun flycheck-ert-at-nth-error (n)
  352. "Determine whether point is at the N'th Flycheck error.
  353. Return non-nil if the point is at the N'th Flycheck error in the
  354. current buffer. Otherwise return nil."
  355. (let* ((error (nth (1- n) flycheck-current-errors))
  356. (mode flycheck-highlighting-mode)
  357. (region (flycheck-error-region-for-mode error mode)))
  358. (and (member error (flycheck-overlay-errors-at (point)))
  359. (= (point) (car region)))))
  360. (defun flycheck-ert-explain--at-nth-error (n)
  361. "Explain a failed at-nth-error predicate at N."
  362. (let ((errors (flycheck-overlay-errors-at (point))))
  363. (if (null errors)
  364. (format "Expected to be at error %s, but no error at point %s"
  365. n (point))
  366. (let ((pos (cl-position (car errors) flycheck-current-errors)))
  367. (format "Expected to be at error %s, but point %s is at error %s"
  368. n (point) (1+ pos))))))
  369. (put 'flycheck-ert-at-nth-error 'ert-explainer
  370. 'flycheck-ert-explain--at-nth-error)
  371. (provide 'flycheck-ert)
  372. ;;; flycheck-ert.el ends here