test-ob-tangle.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 <http://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 :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/jump-to-org ()
  108. "Test `org-babel-tangle-jump-to-org' specifications."
  109. ;; Standard test.
  110. (let ((org-file-apps '((t . emacs))))
  111. (should
  112. (equal
  113. "* H\n#+begin_src emacs-lisp\n1\n#+end_src"
  114. (org-test-with-temp-text-in-file
  115. "* H\n#+begin_src emacs-lisp\n1\n#+end_src"
  116. (let ((file (buffer-file-name)))
  117. (org-test-with-temp-text
  118. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  119. (file-name-nondirectory file))
  120. (org-babel-tangle-jump-to-org)
  121. (buffer-string))))))
  122. ;; Multiple blocks in the same section.
  123. (should
  124. (equal
  125. "2"
  126. (org-test-with-temp-text-in-file
  127. "* H
  128. first block
  129. #+begin_src emacs-lisp
  130. 1
  131. #+end_src
  132. another block
  133. #+begin_src emacs-lisp
  134. 2
  135. #+end_src
  136. "
  137. (let ((file (buffer-file-name)))
  138. (org-test-with-temp-text
  139. (format ";; [[file:%s][H:2]]\n<point>2\n;; H:2 ends here\n"
  140. (file-name-nondirectory file))
  141. (org-babel-tangle-jump-to-org)
  142. (buffer-substring (line-beginning-position)
  143. (line-end-position)))))))
  144. ;; Preserve position within the source code.
  145. (should
  146. (equal
  147. "1)"
  148. (org-test-with-temp-text-in-file
  149. "* H\n#+begin_src emacs-lisp\n(+ 1 1)\n#+end_src"
  150. (let ((file (buffer-file-name)))
  151. (org-test-with-temp-text
  152. (format ";; [[file:%s][H:1]]\n(+ 1 <point>1)\n;; H:1 ends here\n"
  153. (file-name-nondirectory file))
  154. (org-babel-tangle-jump-to-org)
  155. (buffer-substring-no-properties (point) (line-end-position)))))))
  156. ;; Blocks before first heading.
  157. (should
  158. (equal
  159. "Buffer start\n#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  160. (org-test-with-temp-text-in-file
  161. "Buffer start\n#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  162. (let ((file (buffer-file-name)))
  163. (org-test-with-temp-text
  164. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  165. (file-name-nondirectory file))
  166. (org-babel-tangle-jump-to-org)
  167. (buffer-string))))))
  168. ;; Special case: buffer starts with a source block.
  169. (should
  170. (equal
  171. "#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  172. (org-test-with-temp-text-in-file
  173. "#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  174. (let ((file (buffer-file-name)))
  175. (org-test-with-temp-text
  176. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  177. (file-name-nondirectory file))
  178. (org-babel-tangle-jump-to-org)
  179. (buffer-string))))))))
  180. (ert-deftest ob-tangle/nested-block ()
  181. "Test tangling of org file with nested block."
  182. (should
  183. (string=
  184. "#+begin_src org
  185. ,#+begin_src emacs-lisp
  186. 1
  187. ,#+end_src
  188. #+end_src
  189. "
  190. (org-test-with-temp-text-in-file
  191. "#+header: :tangle \"test-ob-tangle.org\"
  192. #+begin_src org
  193. ,#+begin_src org
  194. ,,#+begin_src emacs-lisp
  195. 1
  196. ,,#+end_src
  197. ,#+end_src
  198. #+end_src"
  199. (unwind-protect
  200. (progn (org-babel-tangle)
  201. (with-temp-buffer (insert-file-contents "test-ob-tangle.org")
  202. (buffer-string)))
  203. (delete-file "test-ob-tangle.org"))))))
  204. (ert-deftest ob-tangle/block-order ()
  205. "Test order of tangled blocks."
  206. ;; Order per language.
  207. (should
  208. (equal '("1" "2")
  209. (let ((file (make-temp-file "org-tangle-")))
  210. (unwind-protect
  211. (progn
  212. (org-test-with-temp-text-in-file
  213. (format "#+property: header-args :tangle %S
  214. #+begin_src emacs-lisp
  215. 1
  216. #+end_src
  217. #+begin_src emacs-lisp
  218. 2
  219. #+end_src"
  220. file)
  221. (org-babel-tangle))
  222. (with-temp-buffer
  223. (insert-file-contents file)
  224. (org-split-string (buffer-string))))
  225. (delete-file file)))))
  226. ;; Order per source block.
  227. (should
  228. (equal '("1" "2")
  229. (let ((file (make-temp-file "org-tangle-")))
  230. (unwind-protect
  231. (progn
  232. (org-test-with-temp-text-in-file
  233. (format "#+property: header-args :tangle %S
  234. #+begin_src foo
  235. 1
  236. #+end_src
  237. #+begin_src bar
  238. 2
  239. #+end_src"
  240. file)
  241. (org-babel-tangle))
  242. (with-temp-buffer
  243. (insert-file-contents file)
  244. (org-split-string (buffer-string))))
  245. (delete-file file)))))
  246. ;; Preserve order with mixed languages.
  247. (should
  248. (equal '("1" "3" "2" "4")
  249. (let ((file (make-temp-file "org-tangle-")))
  250. (unwind-protect
  251. (progn
  252. (org-test-with-temp-text-in-file
  253. (format "#+property: header-args :tangle %S
  254. #+begin_src foo
  255. 1
  256. #+end_src
  257. #+begin_src bar
  258. 2
  259. #+end_src
  260. #+begin_src foo
  261. 3
  262. #+end_src
  263. #+begin_src bar
  264. 4
  265. #+end_src"
  266. file)
  267. (org-babel-tangle))
  268. (with-temp-buffer
  269. (insert-file-contents file)
  270. (org-split-string (buffer-string))))
  271. (delete-file file))))))
  272. (ert-deftest ob-tangle/commented-src-blocks ()
  273. "Test omission of commented src blocks."
  274. (should
  275. (equal '("A")
  276. (let ((file (make-temp-file "org-tangle-")))
  277. (unwind-protect
  278. (progn
  279. (org-test-with-temp-text-in-file
  280. (format "#+property: header-args :tangle %S
  281. * A
  282. #+begin_src emacs-lisp
  283. A
  284. #+end_src
  285. * COMMENT B
  286. #+begin_src emacs-lisp
  287. B
  288. #+end_src
  289. * C
  290. # #+begin_src emacs-lisp
  291. # C
  292. # #+end_src
  293. * D
  294. #+begin_comment
  295. #+begin_src emacs-lisp
  296. D
  297. #+end_src
  298. #+end_comment"
  299. file)
  300. (org-babel-tangle))
  301. (with-temp-buffer
  302. (insert-file-contents file)
  303. (org-split-string (buffer-string))))
  304. (delete-file file)))))
  305. (should
  306. (equal '("A")
  307. (let ((file (make-temp-file "org-tangle-")))
  308. (unwind-protect
  309. (progn
  310. (org-test-with-temp-text-in-file
  311. (format "#+property: header-args :tangle %S
  312. * A
  313. #+begin_src elisp :noweb yes
  314. A
  315. <<B>>
  316. <<C>>
  317. <<D>>
  318. #+end_src
  319. * COMMENT B
  320. #+begin_src elisp :noweb-ref B
  321. B
  322. #+end_src
  323. * C
  324. # #+begin_src elisp :noweb-ref C
  325. # C
  326. # #+end_src
  327. * D
  328. #+begin_comment
  329. #+begin_src elisp :noweb-ref D
  330. D
  331. #+end_src
  332. #+end_comment"
  333. file)
  334. (let (org-babel-noweb-error-all-langs
  335. org-babel-noweb-error-langs)
  336. (org-babel-tangle)))
  337. (with-temp-buffer
  338. (insert-file-contents file)
  339. (org-split-string (buffer-string))))
  340. (delete-file file))))))
  341. (ert-deftest ob-tangle/detangle-false-positive ()
  342. "Test handling of false positive link during detangle."
  343. (let (buffer)
  344. (unwind-protect
  345. (org-test-in-example-file (expand-file-name "babel.el" org-test-example-dir)
  346. (org-babel-detangle)
  347. (org-test-at-id "73115FB0-6565-442B-BB95-50195A499EF4"
  348. (setq buffer (current-buffer))
  349. (org-babel-next-src-block)
  350. (should (equal (string-trim (org-element-property
  351. :value (org-element-at-point)))
  352. ";; detangle changes"))))
  353. (kill-buffer buffer))))
  354. (provide 'test-ob-tangle)
  355. ;;; test-ob-tangle.el ends here