org-test.el 18 KB

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