org-test.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. ;;;; org-test.el --- Tests for Org -*- lexical-binding: t; -*-
  2. ;; Copyright (c) 2010-2015 Sebastian Rose, Eric Schulte
  3. ;; Authors:
  4. ;; Sebastian Rose, Hannover, Germany, sebastian_rose gmx de
  5. ;; Eric Schulte, Santa Fe, New Mexico, USA, schulte.eric gmail com
  6. ;; David Maus, Brunswick, Germany, dmaus ictsoc de
  7. ;; Released under the GNU General Public License version 3
  8. ;; see: https://www.gnu.org/licenses/gpl-3.0.html
  9. ;; Definition of `special-mode' copied from Emacs23's simple.el to be
  10. ;; provide a testing environment for Emacs22.
  11. ;;;; Comments:
  12. ;; Interactive testing for Org mode.
  13. ;; The heart of all this is the commands `org-test-current-defun'. If
  14. ;; called while in a `defun' all ert tests with names matching the
  15. ;; name of the function are run.
  16. ;;; Test Development
  17. ;; For test development purposes a number of navigation and test
  18. ;; function construction routines are available as a git submodule
  19. ;; (jump.el)
  20. ;; Install with...
  21. ;; $ git submodule init
  22. ;; $ git submodule update
  23. ;;;; Code:
  24. (require 'org)
  25. (require 'org-id)
  26. ;;; Ob constants
  27. (defconst org-test-file-ob-anchor
  28. "94839181-184f-4ff4-a72f-94214df6f5ba")
  29. (defconst org-test-link-in-heading-file-ob-anchor
  30. "a8b1d111-eca8-49f0-8930-56d4f0875155")
  31. (unless (and (boundp 'org-batch-test) org-batch-test)
  32. (let* ((org-test-dir (expand-file-name
  33. (file-name-directory
  34. (or load-file-name buffer-file-name))))
  35. (org-lisp-dir (expand-file-name
  36. (concat org-test-dir "../lisp"))))
  37. (unless (featurep 'org)
  38. (setq load-path (cons org-lisp-dir load-path))
  39. (require 'org)
  40. (require 'org-id)
  41. (require 'ox)
  42. (org-babel-do-load-languages
  43. 'org-babel-load-languages '((shell . t) (org . t))))
  44. (let ((load-path (cons org-test-dir
  45. (cons (expand-file-name "jump" org-test-dir)
  46. load-path))))
  47. (require 'cl-lib)
  48. (require 'ert)
  49. (require 'ert-x)
  50. (when (file-exists-p (expand-file-name "jump/jump.el" org-test-dir))
  51. (require 'jump)
  52. (require 'which-func)))))
  53. (defconst org-test-default-test-file-name "tests.el"
  54. "For each defun a separate file with tests may be defined.
  55. tests.el is the fallback or default if you like.")
  56. (defconst org-test-default-directory-name "testing"
  57. "Basename or the directory where the tests live.
  58. org-test searches this directory up the directory tree.")
  59. (defconst org-test-dir
  60. (expand-file-name (file-name-directory (or load-file-name buffer-file-name))))
  61. (defconst org-base-dir ;; FIXME: Use `org-test-' prefix.
  62. (expand-file-name ".." org-test-dir))
  63. (defconst org-test-example-dir
  64. (expand-file-name "examples" org-test-dir))
  65. (defconst org-test-file
  66. (expand-file-name "normal.org" org-test-example-dir))
  67. (defconst org-test-no-heading-file
  68. (expand-file-name "no-heading.org" org-test-example-dir))
  69. (defconst org-test-attachments-file
  70. (expand-file-name "attachments.org" org-test-example-dir))
  71. (defconst org-test-link-in-heading-file
  72. (expand-file-name "link-in-heading.org" org-test-dir))
  73. ;; FIXME: Merely loading a file shouldn't override a user's settings.
  74. (setq org-id-locations-file
  75. (expand-file-name ".test-org-id-locations" org-test-dir))
  76. ;;; Functions for writing tests
  77. (put 'missing-test-dependency ;FIXME: Use `define-error'.
  78. 'error-conditions
  79. '(error missing-test-dependency))
  80. (defun org-test-for-executable (exe)
  81. "Throw an error if EXE is not available.
  82. This can be used at the top of code-block-language specific test
  83. files to avoid loading the file on systems without the
  84. executable."
  85. (unless (cl-reduce
  86. (lambda (acc dir)
  87. (or acc (file-exists-p (expand-file-name exe dir))))
  88. exec-path :initial-value nil)
  89. (signal 'missing-test-dependency (list exe))))
  90. (defun org-test-buffer (&optional _file)
  91. "TODO: Setup and return a buffer to work with.
  92. If file is non-nil insert its contents in there.")
  93. (defun org-test-compare-with-file (&optional _file)
  94. "TODO: Compare the contents of the test buffer with FILE.
  95. If file is not given, search for a file named after the test
  96. currently executed.")
  97. (defmacro org-test-at-id (id &rest body)
  98. "Run body after placing the point in the headline identified by ID."
  99. (declare (indent 1) (debug t))
  100. `(let* ((id-location (org-id-find ,id))
  101. (id-file (car id-location))
  102. (visited-p (get-file-buffer id-file))
  103. to-be-removed)
  104. (unwind-protect
  105. (save-window-excursion
  106. (save-match-data
  107. (org-id-goto ,id)
  108. (setq to-be-removed (current-buffer))
  109. (condition-case nil
  110. (progn
  111. (org-show-subtree)
  112. (org-show-all '(blocks)))
  113. (error nil))
  114. (save-restriction ,@body)))
  115. (unless (or visited-p (not to-be-removed))
  116. (kill-buffer to-be-removed)))))
  117. (defmacro org-test-in-example-file (file &rest body)
  118. "Execute body in the Org example file."
  119. (declare (indent 1) (debug t))
  120. `(let* ((my-file (or ,file org-test-file))
  121. (visited-p (get-file-buffer my-file))
  122. to-be-removed
  123. results)
  124. (save-window-excursion
  125. (save-match-data
  126. (find-file my-file)
  127. (unless (eq major-mode 'org-mode)
  128. (org-mode))
  129. (setq to-be-removed (current-buffer))
  130. (goto-char (point-min))
  131. (condition-case nil
  132. (progn
  133. (outline-next-visible-heading 1)
  134. (org-show-subtree)
  135. (org-show-all '(blocks)))
  136. (error nil))
  137. (setq results (save-restriction ,@body))))
  138. (unless visited-p
  139. (kill-buffer to-be-removed))
  140. results))
  141. (defmacro org-test-at-marker (file marker &rest body)
  142. "Run body after placing the point at MARKER in FILE.
  143. Note the uuidgen command-line command can be useful for
  144. generating unique markers for insertion as anchors into org
  145. files."
  146. (declare (indent 2) (debug t))
  147. `(org-test-in-example-file ,file
  148. (goto-char (point-min))
  149. (re-search-forward (regexp-quote ,marker))
  150. ,@body))
  151. (defmacro org-test-with-temp-text (text &rest body)
  152. "Run body in a temporary buffer with Org mode as the active
  153. mode holding TEXT. If the string \"<point>\" appears in TEXT
  154. then remove it and place the point there before running BODY,
  155. otherwise place the point at the beginning of the inserted text."
  156. (declare (indent 1) (debug t))
  157. `(let ((inside-text (if (stringp ,text) ,text (eval ,text)))
  158. (org-mode-hook nil))
  159. (with-temp-buffer
  160. (org-mode)
  161. (let ((point (string-match "<point>" inside-text)))
  162. (if point
  163. (progn
  164. (insert (replace-match "" nil nil inside-text))
  165. (goto-char (1+ (match-beginning 0))))
  166. (insert inside-text)
  167. (goto-char (point-min))))
  168. (font-lock-ensure (point-min) (point-max))
  169. ,@body)))
  170. (defmacro org-test-with-temp-text-in-file (text &rest body)
  171. "Run body in a temporary file buffer with Org mode as the active mode.
  172. If the string \"<point>\" appears in TEXT then remove it and
  173. place the point there before running BODY, otherwise place the
  174. point at the beginning of the buffer."
  175. (declare (indent 1) (debug t))
  176. `(let ((file (make-temp-file "org-test"))
  177. (inside-text (if (stringp ,text) ,text (eval ,text)))
  178. buffer)
  179. (with-temp-file file (insert inside-text))
  180. (unwind-protect
  181. (progn
  182. ;; FIXME: For the rare cases where we do need to mess with windows,
  183. ;; we should let `body' take care of displaying this buffer!
  184. (setq buffer (find-file file))
  185. (when (re-search-forward "<point>" nil t)
  186. (replace-match ""))
  187. (org-mode)
  188. (progn ,@body))
  189. (let ((kill-buffer-query-functions nil))
  190. (when buffer
  191. (set-buffer buffer)
  192. ;; Ignore changes, we're deleting the file in the next step
  193. ;; anyways.
  194. (set-buffer-modified-p nil)
  195. (kill-buffer))
  196. (delete-file file)))))
  197. (defun org-test-table-target-expect (target &optional expect laps &rest tblfm)
  198. "For all TBLFM: Apply the formula to TARGET, compare EXPECT with result.
  199. Either LAPS and TBLFM are nil and the table will only be aligned
  200. or LAPS is the count of recalculations that should be made on
  201. each TBLFM. To save ERT run time keep LAPS as low as possible to
  202. get the table stable. Anyhow, if LAPS is `iterate' then iterate,
  203. but this will run one recalculation longer. When EXPECT is nil
  204. it will be set to TARGET.
  205. When running a test interactively in ERT is not enough and you
  206. need to examine the target table with e. g. the Org formula
  207. debugger or an Emacs Lisp debugger (e. g. with point in a data
  208. field and calling the instrumented `org-table-eval-formula') then
  209. copy and paste the table with formula from the ERT results buffer
  210. or temporarily substitute the `org-test-with-temp-text' of this
  211. function with `org-test-with-temp-text-in-file'. Also consider
  212. setting `pp-escape-newlines' to nil manually."
  213. (require 'pp)
  214. (require 'ert)
  215. (let ((back pp-escape-newlines) (current-tblfm))
  216. (unless tblfm
  217. (should-not laps)
  218. (push "" tblfm)) ; Dummy formula.
  219. (unless expect (setq expect target))
  220. (while (setq current-tblfm (pop tblfm))
  221. (org-test-with-temp-text (concat target current-tblfm)
  222. ;; Search the last of possibly several tables, let the ERT
  223. ;; test fail if not found.
  224. (goto-char (point-max))
  225. (while (not (org-at-table-p))
  226. (should (eq 0 (forward-line -1))))
  227. (when laps
  228. (if (and (symbolp laps) (eq laps 'iterate))
  229. (should (org-table-recalculate 'iterate t))
  230. (should (integerp laps))
  231. (should (< 0 laps))
  232. (let ((cnt laps))
  233. (while (< 0 cnt)
  234. (should (org-table-recalculate 'all t))
  235. (setq cnt (1- cnt))))))
  236. (org-table-align)
  237. (setq pp-escape-newlines nil)
  238. ;; Declutter the ERT results buffer by giving only variables
  239. ;; and not directly the forms to `should'.
  240. (let ((expect (concat expect current-tblfm))
  241. (result (buffer-substring-no-properties
  242. (point-min) (point-max))))
  243. (should (equal expect result)))
  244. ;; If `should' passed then set back `pp-escape-newlines' here,
  245. ;; else leave it nil as a side effect to see the failed table
  246. ;; on multiple lines in the ERT results buffer.
  247. (setq pp-escape-newlines back)))))
  248. (defun org-test-with-tramp-remote-dir--worker (body)
  249. "Worker for `org-test-with-tramp-remote-dir'."
  250. (let ((env-def (getenv "REMOTE_TEMPORARY_FILE_DIRECTORY")))
  251. (cond
  252. (env-def (funcall body env-def))
  253. ((eq system-type 'windows-nt) (funcall body null-device))
  254. (t (require 'tramp)
  255. (defvar tramp-methods)
  256. (defvar tramp-default-host-alist)
  257. (let ((tramp-methods
  258. (cons '("mock"
  259. (tramp-login-program "sh")
  260. (tramp-login-args (("-i")))
  261. (tramp-remote-shell "/bin/sh")
  262. (tramp-remote-shell-args ("-c"))
  263. (tramp-connection-timeout 10))
  264. tramp-methods))
  265. (tramp-default-host-alist
  266. `(("\\`mock\\'" nil ,(system-name)))))
  267. (funcall body (format "/mock::%s" temporary-file-directory)))))))
  268. (defmacro org-test-with-tramp-remote-dir (dir &rest body)
  269. "Bind the symbol DIR to a remote directory and execute BODY.
  270. Return the value of the last form in BODY. The directory DIR
  271. will be something like \"/mock::/tmp/\", which allows to test
  272. Tramp related features. We mostly follow
  273. `tramp-test-temporary-file-directory' from GNU Emacs tests."
  274. (declare (debug (sexp body)) (indent 2))
  275. `(org-test-with-tramp-remote-dir--worker (lambda (,dir) ,@body)))
  276. ;;; Navigation Functions
  277. (defmacro org--compile-when (test &rest body)
  278. (declare (debug t) (indent 1))
  279. (let ((exp `(progn ,@body)))
  280. (if (eval test t)
  281. exp
  282. `(when ,test (eval ',exp t)))))
  283. (org--compile-when (featurep 'jump)
  284. (defjump org-test-jump
  285. (("lisp/\\1.el" . "testing/lisp/test-\\1.el")
  286. ("lisp/\\1.el" . "testing/lisp/\\1.el/test.*.el")
  287. ("testing/lisp/test-\\1.el" . "lisp/\\1.el")
  288. ("testing/lisp/\\1.el" . "lisp/\\1.el/test.*.el"))
  289. (concat org-base-dir "/")
  290. "Jump between Org files and their tests."
  291. (lambda (path)
  292. (let* ((full-path (expand-file-name path org-base-dir))
  293. (file-name (file-name-nondirectory path))
  294. (name (file-name-sans-extension file-name)))
  295. (find-file full-path)
  296. (insert
  297. ";;; " file-name "\n\n"
  298. ";; Copyright (c) " (nth 5 (decode-time (current-time)))
  299. " " user-full-name "\n"
  300. ";; Authors: " user-full-name "\n\n"
  301. ";; Released under the GNU General Public License version 3\n"
  302. ";; see: https://www.gnu.org/licenses/gpl-3.0.html\n\n"
  303. ";;;; Comments:\n\n"
  304. ";; Template test file for Org tests\n\n"
  305. " \n"
  306. ";;; Code:\n"
  307. "(let ((load-path (cons (expand-file-name\n"
  308. " \"..\" (file-name-directory\n"
  309. " (or load-file-name buffer-file-name)))\n"
  310. " load-path)))\n"
  311. " (require 'org-test)\n\n"
  312. " \n"
  313. ";;; Tests\n"
  314. "(ert-deftest " name "/example-test ()\n"
  315. " \"Just an example to get you started.\"\n"
  316. " (should t)\n"
  317. " (should-not nil)\n"
  318. " (should-error (error \"errr...\")))\n\n\n"
  319. "(provide '" name ")\n\n"
  320. ";;; " file-name " ends here\n")
  321. full-path))
  322. (lambda () ((lambda (res) (if (listp res) (car res) res)) (which-function)))))
  323. (define-key emacs-lisp-mode-map "\M-\C-j" #'org-test-jump)
  324. ;;; Miscellaneous helper functions
  325. (defun org-test-strip-text-props (s)
  326. "Return S without any text properties."
  327. (let ((noprop (copy-sequence s)))
  328. (set-text-properties 0 (length noprop) nil noprop)
  329. noprop))
  330. (defun org-test-string-exact-match (regex string &optional start)
  331. "Case sensitive string-match"
  332. (let ((case-fold-search nil)
  333. (case-replace nil))
  334. (if(and (equal regex "")
  335. (not(equal string "")))
  336. nil
  337. (if (equal 0 (string-match regex string start))
  338. t
  339. nil))))
  340. ;;; Load and Run tests
  341. (defun org-test-load ()
  342. "Load up the Org test suite."
  343. (interactive)
  344. (cl-flet ((rld (base)
  345. ;; Recursively load all files, if files throw errors
  346. ;; then silently ignore the error and continue to the
  347. ;; next file. This allows files to error out if
  348. ;; required executables aren't available.
  349. (mapc
  350. (lambda (path)
  351. (if (file-directory-p path)
  352. (rld path)
  353. (condition-case nil
  354. (when (string-match "\\`[A-Za-z].*\\.el\\'"
  355. (file-name-nondirectory path))
  356. (let ((feature-name
  357. (intern
  358. (file-name-base
  359. (file-name-nondirectory path)))))
  360. (require feature-name path)))
  361. (missing-test-dependency
  362. (let ((name (intern
  363. (concat "org-missing-dependency/"
  364. (file-name-nondirectory
  365. (file-name-sans-extension path))))))
  366. (eval `(ert-deftest ,name ()
  367. :expected-result :failed (should nil))))))))
  368. (directory-files base 'full
  369. "\\`\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.el\\'"))))
  370. (rld (expand-file-name "lisp" org-test-dir))))
  371. (defun org-test-current-defun ()
  372. "Test the current function."
  373. (interactive)
  374. (ert (which-function)))
  375. (defun org-test-current-file ()
  376. "Run all tests for current file."
  377. (interactive)
  378. (ert (concat "test-"
  379. (file-name-sans-extension
  380. (file-name-nondirectory (buffer-file-name)))
  381. "/")))
  382. (defvar org-test-buffers nil
  383. "Hold buffers open for running Org tests.")
  384. (defun org-test-touch-all-examples ()
  385. (dolist (file (directory-files
  386. org-test-example-dir 'full
  387. "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.org$"))
  388. (unless (get-file-buffer file)
  389. (add-to-list 'org-test-buffers (find-file file)))))
  390. (defun org-test-kill-all-examples ()
  391. (while org-test-buffers
  392. (let ((b (pop org-test-buffers)))
  393. (when (buffer-live-p b) (kill-buffer b)))))
  394. (defun org-test-update-id-locations ()
  395. (org-id-update-id-locations
  396. (directory-files
  397. org-test-example-dir 'full
  398. "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.org$")))
  399. (defun org-test-run-batch-tests (&optional org-test-selector)
  400. "Run all tests matching an optional regex which defaults to \"\\(org\\|ob\\)\".
  401. Load all test files first."
  402. (interactive)
  403. (let ((org-id-track-globally t)
  404. (org-test-selector
  405. (if org-test-selector org-test-selector "\\(org\\|ob\\)"))
  406. org-confirm-babel-evaluate org-startup-folded vc-handled-backends)
  407. (org-test-touch-all-examples)
  408. (org-test-update-id-locations)
  409. (org-test-load)
  410. (message "selected tests: %s" org-test-selector)
  411. (ert-run-tests-batch-and-exit org-test-selector)))
  412. (defun org-test-run-all-tests ()
  413. "Run all defined tests matching \"\\(org\\|ob\\)\".
  414. Load all test files first."
  415. (interactive)
  416. (org-test-touch-all-examples)
  417. (org-test-update-id-locations)
  418. (org-test-load)
  419. (ert "\\(org\\|ob\\)")
  420. (org-test-kill-all-examples))
  421. (defmacro org-test-at-time (time &rest body)
  422. "Run body while pretending that the current time is TIME.
  423. TIME can be a non-nil Lisp time value, or a string specifying a date and time."
  424. (declare (indent 1))
  425. (let ((tm (cl-gensym))
  426. (at (cl-gensym)))
  427. `(let* ((,tm ,time)
  428. (,at (if (stringp ,tm)
  429. (org-time-string-to-time ,tm)
  430. ,tm)))
  431. (cl-letf
  432. ;; Wrap builtins whose behavior can depend on the current time.
  433. (((symbol-function 'current-time)
  434. (lambda () ,at))
  435. ((symbol-function 'current-time-string)
  436. (lambda (&optional time &rest args)
  437. (apply ,(symbol-function 'current-time-string)
  438. (or time ,at) args)))
  439. ((symbol-function 'current-time-zone)
  440. (lambda (&optional time &rest args)
  441. (apply ,(symbol-function 'current-time-zone)
  442. (or time ,at) args)))
  443. ((symbol-function 'decode-time)
  444. (lambda (&optional time zone form)
  445. (condition-case nil
  446. (funcall ,(symbol-function 'decode-time)
  447. (or time ,at) zone form)
  448. (wrong-number-of-arguments
  449. (funcall ,(symbol-function 'decode-time)
  450. (or time ,at))))))
  451. ((symbol-function 'encode-time)
  452. (lambda (time &rest args)
  453. (apply ,(symbol-function 'encode-time) (or time ,at) args)))
  454. ((symbol-function 'float-time)
  455. (lambda (&optional time)
  456. (funcall ,(symbol-function 'float-time) (or time ,at))))
  457. ((symbol-function 'format-time-string)
  458. (lambda (format &optional time &rest args)
  459. (apply ,(symbol-function 'format-time-string)
  460. format (or time ,at) args)))
  461. ((symbol-function 'set-file-times)
  462. (lambda (file &optional time)
  463. (funcall ,(symbol-function 'set-file-times) file (or time ,at))))
  464. ((symbol-function 'time-add)
  465. (lambda (a b) (funcall ,(symbol-function 'time-add)
  466. (or a ,at) (or b ,at))))
  467. ((symbol-function 'time-equal-p)
  468. (lambda (a b) (funcall ,(symbol-function 'time-equal-p)
  469. (or a ,at) (or b ,at))))
  470. ((symbol-function 'time-less-p)
  471. (lambda (a b) (funcall ,(symbol-function 'time-less-p)
  472. (or a ,at) (or b ,at))))
  473. ((symbol-function 'time-subtract)
  474. (lambda (a b) (funcall ,(symbol-function 'time-subtract)
  475. (or a ,at) (or b ,at)))))
  476. ,@body))))
  477. (provide 'org-test)
  478. ;;; org-test.el ends here