test-ob-tangle.el 11 KB

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