test-ob-tangle.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. ;;; test-ob-tangle.el --- tests for ob-tangle.el
  2. ;; Copyright (c) 2010-2016, 2019 Eric Schulte
  3. ;; Authors: Eric Schulte
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Comments:
  16. ;; Template test file for Org tests
  17. ;;; Code:
  18. (require 'subr-x)
  19. ;; TODO
  20. ;; (ert-deftest ob-tangle/noweb-on-tangle ()
  21. ;; "Noweb header arguments tangle correctly.
  22. ;; - yes expand on both export and tangle
  23. ;; - no expand on neither export or tangle
  24. ;; - tangle expand on only tangle not export"
  25. ;; (let ((target-file (make-temp-file "ob-tangle-test-")))
  26. ;; (org-test-at-id "eb1f6498-5bd9-45e0-9c56-50717053e7b7"
  27. ;; (org-narrow-to-subtree)
  28. ;; (org-babel-tangle target-file))
  29. ;; (let ((tang (with-temp-buffer
  30. ;; (insert-file-contents target-file)
  31. ;; (buffer-string))))
  32. ;; (flet ((exp-p (arg)
  33. ;; (and
  34. ;; (string-match
  35. ;; (format "noweb-%s-start\\([^\000]*\\)noweb-%s-end" arg arg)
  36. ;; tang)
  37. ;; (string-match "expanded" (match-string 1 tang)))))
  38. ;; (should (exp-p "yes"))
  39. ;; (should-not (exp-p "no"))
  40. ;; (should (exp-p "tangle"))))))
  41. (ert-deftest ob-tangle/no-excessive-id-insertion-on-tangle ()
  42. "Don't add IDs to headings without tangling code blocks."
  43. (org-test-at-id "ef06fd7f-012b-4fde-87a2-2ae91504ea7e"
  44. (org-babel-next-src-block)
  45. (org-narrow-to-subtree)
  46. (org-babel-tangle)
  47. (should (null (org-id-get)))))
  48. (ert-deftest ob-tangle/continued-code-blocks-w-noweb-ref ()
  49. "Test that the :noweb-ref header argument is used correctly."
  50. (org-test-at-id "54d68d4b-1544-4745-85ab-4f03b3cbd8a0"
  51. (let ((tangled
  52. "df|sed '1d'|awk '{print $5 \" \" $6}'|sort -n |tail -1|awk '{print $2}'"))
  53. (org-narrow-to-subtree)
  54. (org-babel-tangle)
  55. (should (unwind-protect
  56. (with-temp-buffer
  57. (insert-file-contents "babel.sh")
  58. (goto-char (point-min))
  59. (re-search-forward (regexp-quote tangled) nil t))
  60. (when (file-exists-p "babel.sh") (delete-file "babel.sh")))))))
  61. (ert-deftest ob-tangle/expand-headers-as-noweb-references ()
  62. "Test that references to headers are expanded during noweb expansion."
  63. (org-test-at-id "2409e8ba-7b5f-4678-8888-e48aa02d8cb4"
  64. (org-babel-next-src-block 2)
  65. (let ((expanded (org-babel-expand-noweb-references)))
  66. (should (string-match (regexp-quote "simple") expanded))
  67. (should (string-match (regexp-quote "length 14") expanded)))))
  68. (ert-deftest ob-tangle/comment-links-at-left-margin ()
  69. "Test commenting of links at left margin."
  70. (should
  71. (string-match
  72. (regexp-quote "# [[https://orgmode.org][Org mode]]")
  73. (org-test-with-temp-text-in-file
  74. "[[https://orgmode.org][Org mode]]
  75. #+header: :comments org :results output :tangle \"test-ob-tangle.sh\"
  76. #+begin_src sh
  77. echo 1
  78. #+end_src"
  79. (unwind-protect
  80. (progn (org-babel-tangle)
  81. (with-temp-buffer (insert-file-contents "test-ob-tangle.sh")
  82. (buffer-string)))
  83. (delete-file "test-ob-tangle.sh"))))))
  84. (ert-deftest ob-tangle/comment-links-numbering ()
  85. "Test numbering of source blocks when commenting with links."
  86. (should
  87. (org-test-with-temp-text-in-file
  88. "* H
  89. #+header: :tangle \"test-ob-tangle.el\" :comments link
  90. #+begin_src emacs-lisp
  91. 1
  92. #+end_src
  93. #+header: :tangle \"test-ob-tangle.el\" :comments link
  94. #+begin_src emacs-lisp
  95. 2
  96. #+end_src"
  97. (unwind-protect
  98. (progn
  99. (org-babel-tangle)
  100. (with-temp-buffer
  101. (insert-file-contents "test-ob-tangle.el")
  102. (buffer-string)
  103. (goto-char (point-min))
  104. (and (search-forward "[H:1]]" nil t)
  105. (search-forward "[H:2]]" nil t))))
  106. (delete-file "test-ob-tangle.el")))))
  107. (ert-deftest ob-tangle/comment-links-relative-file ()
  108. "Test relative file name handling when commenting with links."
  109. (should
  110. (org-test-with-temp-text-in-file
  111. "* H
  112. #+header: :tangle \"test-ob-tangle.el\" :comments link
  113. #+begin_src emacs-lisp
  114. 1
  115. #+end_src"
  116. (unwind-protect
  117. (let ((org-babel-tangle-use-relative-file-links t))
  118. (org-babel-tangle)
  119. (with-temp-buffer
  120. (insert-file-contents "test-ob-tangle.el")
  121. (buffer-string)
  122. (goto-char (point-min))
  123. (search-forward
  124. (concat "[file:" (file-name-nondirectory file))
  125. nil t)))
  126. (delete-file "test-ob-tangle.el"))))
  127. (should
  128. (org-test-with-temp-text-in-file
  129. "* H
  130. #+header: :tangle \"test-ob-tangle.el\" :comments link
  131. #+begin_src emacs-lisp
  132. 1
  133. #+end_src"
  134. (unwind-protect
  135. (let ((org-babel-tangle-use-relative-file-links nil))
  136. (org-babel-tangle)
  137. (with-temp-buffer
  138. (insert-file-contents "test-ob-tangle.el")
  139. (buffer-string)
  140. (goto-char (point-min))
  141. (search-forward (concat "[file:" file) nil t)))
  142. (delete-file "test-ob-tangle.el")))))
  143. (ert-deftest ob-tangle/jump-to-org ()
  144. "Test `org-babel-tangle-jump-to-org' specifications."
  145. ;; Standard test.
  146. (let ((org-file-apps '((t . emacs))))
  147. (should
  148. (equal
  149. "* H\n#+begin_src emacs-lisp\n1\n#+end_src"
  150. (org-test-with-temp-text-in-file
  151. "* H\n#+begin_src emacs-lisp\n1\n#+end_src"
  152. (let ((file (buffer-file-name)))
  153. (org-test-with-temp-text
  154. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  155. (file-name-nondirectory file))
  156. (org-babel-tangle-jump-to-org)
  157. (buffer-string))))))
  158. ;; Multiple blocks in the same section.
  159. (should
  160. (equal
  161. "2"
  162. (org-test-with-temp-text-in-file
  163. "* H
  164. first block
  165. #+begin_src emacs-lisp
  166. 1
  167. #+end_src
  168. another block
  169. #+begin_src emacs-lisp
  170. 2
  171. #+end_src
  172. "
  173. (let ((file (buffer-file-name)))
  174. (org-test-with-temp-text
  175. (format ";; [[file:%s][H:2]]\n<point>2\n;; H:2 ends here\n"
  176. (file-name-nondirectory file))
  177. (org-babel-tangle-jump-to-org)
  178. (buffer-substring (line-beginning-position)
  179. (line-end-position)))))))
  180. ;; Preserve position within the source code.
  181. (should
  182. (equal
  183. "1)"
  184. (org-test-with-temp-text-in-file
  185. "* H\n#+begin_src emacs-lisp\n(+ 1 1)\n#+end_src"
  186. (let ((file (buffer-file-name)))
  187. (org-test-with-temp-text
  188. (format ";; [[file:%s][H:1]]\n(+ 1 <point>1)\n;; H:1 ends here\n"
  189. (file-name-nondirectory file))
  190. (org-babel-tangle-jump-to-org)
  191. (buffer-substring-no-properties (point) (line-end-position)))))))
  192. ;; Blocks before first heading.
  193. (should
  194. (equal
  195. "Buffer start\n#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  196. (org-test-with-temp-text-in-file
  197. "Buffer start\n#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  198. (let ((file (buffer-file-name)))
  199. (org-test-with-temp-text
  200. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  201. (file-name-nondirectory file))
  202. (org-babel-tangle-jump-to-org)
  203. (buffer-string))))))
  204. ;; Special case: buffer starts with a source block.
  205. (should
  206. (equal
  207. "#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  208. (org-test-with-temp-text-in-file
  209. "#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  210. (let ((file (buffer-file-name)))
  211. (org-test-with-temp-text
  212. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  213. (file-name-nondirectory file))
  214. (org-babel-tangle-jump-to-org)
  215. (buffer-string))))))))
  216. (ert-deftest ob-tangle/nested-block ()
  217. "Test tangling of org file with nested block."
  218. (should
  219. (string=
  220. "#+begin_src org
  221. ,#+begin_src emacs-lisp
  222. 1
  223. ,#+end_src
  224. #+end_src
  225. "
  226. (org-test-with-temp-text-in-file
  227. "#+header: :tangle \"test-ob-tangle.org\"
  228. #+begin_src org
  229. ,#+begin_src org
  230. ,,#+begin_src emacs-lisp
  231. 1
  232. ,,#+end_src
  233. ,#+end_src
  234. #+end_src"
  235. (unwind-protect
  236. (progn (org-babel-tangle)
  237. (with-temp-buffer (insert-file-contents "test-ob-tangle.org")
  238. (buffer-string)))
  239. (delete-file "test-ob-tangle.org"))))))
  240. (ert-deftest ob-tangle/block-order ()
  241. "Test order of tangled blocks."
  242. ;; Order per language.
  243. (should
  244. (equal '("1" "2")
  245. (let ((file (make-temp-file "org-tangle-")))
  246. (unwind-protect
  247. (progn
  248. (org-test-with-temp-text-in-file
  249. (format "#+property: header-args :tangle %S
  250. #+begin_src emacs-lisp
  251. 1
  252. #+end_src
  253. #+begin_src emacs-lisp
  254. 2
  255. #+end_src"
  256. file)
  257. (org-babel-tangle))
  258. (with-temp-buffer
  259. (insert-file-contents file)
  260. (org-split-string (buffer-string))))
  261. (delete-file file)))))
  262. ;; Order per source block.
  263. (should
  264. (equal '("1" "2")
  265. (let ((file (make-temp-file "org-tangle-")))
  266. (unwind-protect
  267. (progn
  268. (org-test-with-temp-text-in-file
  269. (format "#+property: header-args :tangle %S
  270. #+begin_src foo
  271. 1
  272. #+end_src
  273. #+begin_src bar
  274. 2
  275. #+end_src"
  276. file)
  277. (org-babel-tangle))
  278. (with-temp-buffer
  279. (insert-file-contents file)
  280. (org-split-string (buffer-string))))
  281. (delete-file file)))))
  282. ;; Preserve order with mixed languages.
  283. (should
  284. (equal '("1" "3" "2" "4")
  285. (let ((file (make-temp-file "org-tangle-")))
  286. (unwind-protect
  287. (progn
  288. (org-test-with-temp-text-in-file
  289. (format "#+property: header-args :tangle %S
  290. #+begin_src foo
  291. 1
  292. #+end_src
  293. #+begin_src bar
  294. 2
  295. #+end_src
  296. #+begin_src foo
  297. 3
  298. #+end_src
  299. #+begin_src bar
  300. 4
  301. #+end_src"
  302. file)
  303. (org-babel-tangle))
  304. (with-temp-buffer
  305. (insert-file-contents file)
  306. (org-split-string (buffer-string))))
  307. (delete-file file))))))
  308. (ert-deftest ob-tangle/commented-src-blocks ()
  309. "Test omission of commented src blocks."
  310. (should
  311. (equal '("A")
  312. (let ((file (make-temp-file "org-tangle-")))
  313. (unwind-protect
  314. (progn
  315. (org-test-with-temp-text-in-file
  316. (format "#+property: header-args :tangle %S
  317. * A
  318. #+begin_src emacs-lisp
  319. A
  320. #+end_src
  321. * COMMENT B
  322. #+begin_src emacs-lisp
  323. B
  324. #+end_src
  325. * C
  326. # #+begin_src emacs-lisp
  327. # C
  328. # #+end_src
  329. * D
  330. #+begin_comment
  331. #+begin_src emacs-lisp
  332. D
  333. #+end_src
  334. #+end_comment"
  335. file)
  336. (org-babel-tangle))
  337. (with-temp-buffer
  338. (insert-file-contents file)
  339. (org-split-string (buffer-string))))
  340. (delete-file file)))))
  341. (should
  342. (equal '("A")
  343. (let ((file (make-temp-file "org-tangle-")))
  344. (unwind-protect
  345. (progn
  346. (org-test-with-temp-text-in-file
  347. (format "#+property: header-args :tangle %S
  348. * A
  349. #+begin_src elisp :noweb yes
  350. A
  351. <<B>>
  352. <<C>>
  353. <<D>>
  354. #+end_src
  355. * COMMENT B
  356. #+begin_src elisp :noweb-ref B
  357. B
  358. #+end_src
  359. * C
  360. # #+begin_src elisp :noweb-ref C
  361. # C
  362. # #+end_src
  363. * D
  364. #+begin_comment
  365. #+begin_src elisp :noweb-ref D
  366. D
  367. #+end_src
  368. #+end_comment"
  369. file)
  370. (let (org-babel-noweb-error-all-langs
  371. org-babel-noweb-error-langs)
  372. (org-babel-tangle)))
  373. (with-temp-buffer
  374. (insert-file-contents file)
  375. (org-split-string (buffer-string))))
  376. (delete-file file))))))
  377. (ert-deftest ob-tangle/multiple-noweb-in-line ()
  378. "Test handling of multiple noweb references in a single line."
  379. (should
  380. (equal '("1" "2" "1")
  381. (let ((file (make-temp-file "org-tangle-")))
  382. (unwind-protect
  383. (progn
  384. (org-test-with-temp-text-in-file
  385. (format "
  386. #+name: block1
  387. #+begin_src elisp
  388. 1
  389. #+end_src
  390. #+name: block2
  391. #+begin_src elisp
  392. 2
  393. #+end_src
  394. #+name: block3
  395. #+begin_src elisp :noweb yes :tangle %s
  396. <<block1>> <<block2>> <<block1>>
  397. #+end_src"
  398. file)
  399. (let ((org-babel-noweb-error-all-langs nil)
  400. (org-babel-noweb-error-langs nil))
  401. (org-babel-tangle)))
  402. (with-temp-buffer
  403. (insert-file-contents file)
  404. (org-split-string (buffer-string))))
  405. (delete-file file))))))
  406. (ert-deftest ob-tangle/detangle-false-positive ()
  407. "Test handling of false positive link during detangle."
  408. (let (buffer)
  409. (unwind-protect
  410. (org-test-in-example-file (expand-file-name "babel.el" org-test-example-dir)
  411. (org-babel-detangle)
  412. (org-test-at-id "73115FB0-6565-442B-BB95-50195A499EF4"
  413. (setq buffer (current-buffer))
  414. (org-babel-next-src-block)
  415. (should (equal (string-trim (org-element-property
  416. :value (org-element-at-point)))
  417. ";; detangle changes"))))
  418. (kill-buffer buffer))))
  419. (provide 'test-ob-tangle)
  420. ;;; test-ob-tangle.el ends here