test-ob-tangle.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. ;; TODO
  19. ;; (ert-deftest ob-tangle/noweb-on-tangle ()
  20. ;; "Noweb header arguments tangle correctly.
  21. ;; - yes expand on both export and tangle
  22. ;; - no expand on neither export or tangle
  23. ;; - tangle expand on only tangle not export"
  24. ;; (let ((target-file (make-temp-file "ob-tangle-test-")))
  25. ;; (org-test-at-id "eb1f6498-5bd9-45e0-9c56-50717053e7b7"
  26. ;; (org-narrow-to-subtree)
  27. ;; (org-babel-tangle target-file))
  28. ;; (let ((tang (with-temp-buffer
  29. ;; (insert-file-contents target-file)
  30. ;; (buffer-string))))
  31. ;; (flet ((exp-p (arg)
  32. ;; (and
  33. ;; (string-match
  34. ;; (format "noweb-%s-start\\([^\000]*\\)noweb-%s-end" arg arg)
  35. ;; tang)
  36. ;; (string-match "expanded" (match-string 1 tang)))))
  37. ;; (should (exp-p "yes"))
  38. ;; (should-not (exp-p "no"))
  39. ;; (should (exp-p "tangle"))))))
  40. (ert-deftest ob-tangle/no-excessive-id-insertion-on-tangle ()
  41. "Don't add IDs to headings without tangling code blocks."
  42. (org-test-at-id "ef06fd7f-012b-4fde-87a2-2ae91504ea7e"
  43. (org-babel-next-src-block)
  44. (org-narrow-to-subtree)
  45. (org-babel-tangle)
  46. (should (null (org-id-get)))))
  47. (ert-deftest ob-tangle/continued-code-blocks-w-noweb-ref ()
  48. "Test that the :noweb-ref header argument is used correctly."
  49. (org-test-at-id "54d68d4b-1544-4745-85ab-4f03b3cbd8a0"
  50. (let ((tangled
  51. "df|sed '1d'|awk '{print $5 \" \" $6}'|sort -n |tail -1|awk '{print $2}'"))
  52. (org-narrow-to-subtree)
  53. (org-babel-tangle)
  54. (should (unwind-protect
  55. (with-temp-buffer
  56. (insert-file-contents "babel.sh")
  57. (goto-char (point-min))
  58. (re-search-forward (regexp-quote tangled) nil t))
  59. (when (file-exists-p "babel.sh") (delete-file "babel.sh")))))))
  60. (ert-deftest ob-tangle/expand-headers-as-noweb-references ()
  61. "Test that references to headers are expanded during noweb expansion."
  62. (org-test-at-id "2409e8ba-7b5f-4678-8888-e48aa02d8cb4"
  63. (org-babel-next-src-block 2)
  64. (let ((expanded (org-babel-expand-noweb-references)))
  65. (should (string-match (regexp-quote "simple") expanded))
  66. (should (string-match (regexp-quote "length 14") expanded)))))
  67. (ert-deftest ob-tangle/comment-links-at-left-margin ()
  68. "Test commenting of links at left margin."
  69. (should
  70. (string-match
  71. (regexp-quote "# [[https://orgmode.org][Org mode]]")
  72. (org-test-with-temp-text-in-file
  73. "[[https://orgmode.org][Org mode]]
  74. #+header: :comments org :tangle \"test-ob-tangle.sh\"
  75. #+begin_src sh
  76. echo 1
  77. #+end_src"
  78. (unwind-protect
  79. (progn (org-babel-tangle)
  80. (with-temp-buffer (insert-file-contents "test-ob-tangle.sh")
  81. (buffer-string)))
  82. (delete-file "test-ob-tangle.sh"))))))
  83. (ert-deftest ob-tangle/comment-links-numbering ()
  84. "Test numbering of source blocks when commenting with links."
  85. (should
  86. (org-test-with-temp-text-in-file
  87. "* H
  88. #+header: :tangle \"test-ob-tangle.el\" :comments link
  89. #+begin_src emacs-lisp
  90. 1
  91. #+end_src
  92. #+header: :tangle \"test-ob-tangle.el\" :comments link
  93. #+begin_src emacs-lisp
  94. 2
  95. #+end_src"
  96. (unwind-protect
  97. (progn
  98. (org-babel-tangle)
  99. (with-temp-buffer
  100. (insert-file-contents "test-ob-tangle.el")
  101. (buffer-string)
  102. (goto-char (point-min))
  103. (and (search-forward "[H:1]]" nil t)
  104. (search-forward "[H:2]]" nil t))))
  105. (delete-file "test-ob-tangle.el")))))
  106. (ert-deftest ob-tangle/comment-links-relative-file ()
  107. "Test relative file name handling when commenting with links."
  108. (should
  109. (org-test-with-temp-text-in-file
  110. "* H
  111. #+header: :tangle \"test-ob-tangle.el\" :comments link
  112. #+begin_src emacs-lisp
  113. 1
  114. #+end_src"
  115. (unwind-protect
  116. (let ((org-babel-tangle-use-relative-file-links t))
  117. (org-babel-tangle)
  118. (with-temp-buffer
  119. (insert-file-contents "test-ob-tangle.el")
  120. (buffer-string)
  121. (goto-char (point-min))
  122. (search-forward
  123. (concat "[file:" (file-name-nondirectory file))
  124. nil t)))
  125. (delete-file "test-ob-tangle.el"))))
  126. (should
  127. (org-test-with-temp-text-in-file
  128. "* H
  129. #+header: :tangle \"test-ob-tangle.el\" :comments link
  130. #+begin_src emacs-lisp
  131. 1
  132. #+end_src"
  133. (unwind-protect
  134. (let ((org-babel-tangle-use-relative-file-links nil))
  135. (org-babel-tangle)
  136. (with-temp-buffer
  137. (insert-file-contents "test-ob-tangle.el")
  138. (buffer-string)
  139. (goto-char (point-min))
  140. (search-forward (concat "[file:" file) nil t)))
  141. (delete-file "test-ob-tangle.el")))))
  142. (ert-deftest ob-tangle/jump-to-org ()
  143. "Test `org-babel-tangle-jump-to-org' specifications."
  144. ;; Standard test.
  145. (should
  146. (equal
  147. "* H\n#+begin_src emacs-lisp\n1\n#+end_src"
  148. (org-test-with-temp-text-in-file
  149. "* H\n#+begin_src emacs-lisp\n1\n#+end_src"
  150. (let ((file (buffer-file-name)))
  151. (org-test-with-temp-text
  152. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  153. (file-name-nondirectory file))
  154. (org-babel-tangle-jump-to-org)
  155. (buffer-string))))))
  156. ;; Multiple blocks in the same section.
  157. (should
  158. (equal
  159. "2"
  160. (org-test-with-temp-text-in-file
  161. "* H
  162. first block
  163. #+begin_src emacs-lisp
  164. 1
  165. #+end_src
  166. another block
  167. #+begin_src emacs-lisp
  168. 2
  169. #+end_src
  170. "
  171. (let ((file (buffer-file-name)))
  172. (org-test-with-temp-text
  173. (format ";; [[file:%s][H:2]]\n<point>2\n;; H:2 ends here\n"
  174. (file-name-nondirectory file))
  175. (org-babel-tangle-jump-to-org)
  176. (buffer-substring (line-beginning-position)
  177. (line-end-position)))))))
  178. ;; Preserve position within the source code.
  179. (should
  180. (equal
  181. "1)"
  182. (org-test-with-temp-text-in-file
  183. "* H\n#+begin_src emacs-lisp\n(+ 1 1)\n#+end_src"
  184. (let ((file (buffer-file-name)))
  185. (org-test-with-temp-text
  186. (format ";; [[file:%s][H:1]]\n(+ 1 <point>1)\n;; H:1 ends here\n"
  187. (file-name-nondirectory file))
  188. (org-babel-tangle-jump-to-org)
  189. (buffer-substring-no-properties (point) (line-end-position)))))))
  190. ;; Blocks before first heading.
  191. (should
  192. (equal
  193. "Buffer start\n#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  194. (org-test-with-temp-text-in-file
  195. "Buffer start\n#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  196. (let ((file (buffer-file-name)))
  197. (org-test-with-temp-text
  198. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  199. (file-name-nondirectory file))
  200. (org-babel-tangle-jump-to-org)
  201. (buffer-string))))))
  202. ;; Special case: buffer starts with a source block.
  203. (should
  204. (equal
  205. "#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  206. (org-test-with-temp-text-in-file
  207. "#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  208. (let ((file (buffer-file-name)))
  209. (org-test-with-temp-text
  210. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  211. (file-name-nondirectory file))
  212. (org-babel-tangle-jump-to-org)
  213. (buffer-string)))))))
  214. (ert-deftest ob-tangle/nested-block ()
  215. "Test tangling of org file with nested block."
  216. (should
  217. (string=
  218. "#+begin_src org
  219. ,#+begin_src emacs-lisp
  220. 1
  221. ,#+end_src
  222. #+end_src
  223. "
  224. (org-test-with-temp-text-in-file
  225. "#+header: :tangle \"test-ob-tangle.org\"
  226. #+begin_src org
  227. ,#+begin_src org
  228. ,,#+begin_src emacs-lisp
  229. 1
  230. ,,#+end_src
  231. ,#+end_src
  232. #+end_src"
  233. (unwind-protect
  234. (progn (org-babel-tangle)
  235. (with-temp-buffer (insert-file-contents "test-ob-tangle.org")
  236. (buffer-string)))
  237. (delete-file "test-ob-tangle.org"))))))
  238. (ert-deftest ob-tangle/block-order ()
  239. "Test order of tangled blocks."
  240. ;; Order per language.
  241. (should
  242. (equal '("1" "2")
  243. (let ((file (make-temp-file "org-tangle-")))
  244. (unwind-protect
  245. (progn
  246. (org-test-with-temp-text-in-file
  247. (format "#+property: header-args :tangle %S
  248. #+begin_src emacs-lisp
  249. 1
  250. #+end_src
  251. #+begin_src emacs-lisp
  252. 2
  253. #+end_src"
  254. file)
  255. (org-babel-tangle))
  256. (with-temp-buffer
  257. (insert-file-contents file)
  258. (org-split-string (buffer-string))))
  259. (delete-file file)))))
  260. ;; Order per source block.
  261. (should
  262. (equal '("1" "2")
  263. (let ((file (make-temp-file "org-tangle-")))
  264. (unwind-protect
  265. (progn
  266. (org-test-with-temp-text-in-file
  267. (format "#+property: header-args :tangle %S
  268. #+begin_src foo
  269. 1
  270. #+end_src
  271. #+begin_src bar
  272. 2
  273. #+end_src"
  274. file)
  275. (org-babel-tangle))
  276. (with-temp-buffer
  277. (insert-file-contents file)
  278. (org-split-string (buffer-string))))
  279. (delete-file file)))))
  280. ;; Preserve order with mixed languages.
  281. (should
  282. (equal '("1" "3" "2" "4")
  283. (let ((file (make-temp-file "org-tangle-")))
  284. (unwind-protect
  285. (progn
  286. (org-test-with-temp-text-in-file
  287. (format "#+property: header-args :tangle %S
  288. #+begin_src foo
  289. 1
  290. #+end_src
  291. #+begin_src bar
  292. 2
  293. #+end_src
  294. #+begin_src foo
  295. 3
  296. #+end_src
  297. #+begin_src bar
  298. 4
  299. #+end_src"
  300. file)
  301. (org-babel-tangle))
  302. (with-temp-buffer
  303. (insert-file-contents file)
  304. (org-split-string (buffer-string))))
  305. (delete-file file))))))
  306. (ert-deftest ob-tangle/commented-src-blocks ()
  307. "Test omission of commented src blocks."
  308. (should
  309. (equal '("A")
  310. (let ((file (make-temp-file "org-tangle-")))
  311. (unwind-protect
  312. (progn
  313. (org-test-with-temp-text-in-file
  314. (format "#+property: header-args :tangle %S
  315. * A
  316. #+begin_src emacs-lisp
  317. A
  318. #+end_src
  319. * COMMENT B
  320. #+begin_src emacs-lisp
  321. B
  322. #+end_src
  323. * C
  324. # #+begin_src emacs-lisp
  325. # C
  326. # #+end_src
  327. * D
  328. #+begin_comment
  329. #+begin_src emacs-lisp
  330. D
  331. #+end_src
  332. #+end_comment"
  333. file)
  334. (org-babel-tangle))
  335. (with-temp-buffer
  336. (insert-file-contents file)
  337. (org-split-string (buffer-string))))
  338. (delete-file file)))))
  339. (should
  340. (equal '("A")
  341. (let ((file (make-temp-file "org-tangle-")))
  342. (unwind-protect
  343. (progn
  344. (org-test-with-temp-text-in-file
  345. (format "#+property: header-args :tangle %S
  346. * A
  347. #+begin_src elisp :noweb yes
  348. A
  349. <<B>>
  350. <<C>>
  351. <<D>>
  352. #+end_src
  353. * COMMENT B
  354. #+begin_src elisp :noweb-ref B
  355. B
  356. #+end_src
  357. * C
  358. # #+begin_src elisp :noweb-ref C
  359. # C
  360. # #+end_src
  361. * D
  362. #+begin_comment
  363. #+begin_src elisp :noweb-ref D
  364. D
  365. #+end_src
  366. #+end_comment"
  367. file)
  368. (let (org-babel-noweb-error-all-langs
  369. org-babel-noweb-error-langs)
  370. (org-babel-tangle)))
  371. (with-temp-buffer
  372. (insert-file-contents file)
  373. (org-split-string (buffer-string))))
  374. (delete-file file))))))
  375. (provide 'test-ob-tangle)
  376. ;;; test-ob-tangle.el ends here