test-ob-tangle.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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/comment-noweb-relative ()
  144. "Test :comments noweb tangling with relative file paths."
  145. (should
  146. (org-test-with-temp-text-in-file
  147. "* Inner
  148. #+name: inner
  149. #+begin_src emacs-lisp
  150. 2
  151. #+end_src
  152. * Main
  153. #+header: :tangle \"test-ob-tangle.el\" :comments noweb :noweb yes
  154. #+begin_src emacs-lisp
  155. 1
  156. <<inner>>
  157. #+end_src"
  158. (unwind-protect
  159. (let ((org-babel-tangle-use-relative-file-links t))
  160. (org-babel-tangle)
  161. (with-temp-buffer
  162. (insert-file-contents "test-ob-tangle.el")
  163. (buffer-string)
  164. (goto-char (point-min))
  165. (and
  166. (search-forward (concat ";; [[file:" (file-name-nondirectory file) "::inner") nil t)
  167. (search-forward ";; inner ends here" nil t))))
  168. (delete-file "test-ob-tangle.el")))))
  169. (ert-deftest ob-tangle/comment-noweb-absolute ()
  170. "Test :comments noweb tangling with absolute file path."
  171. (should
  172. (org-test-with-temp-text-in-file
  173. "* Inner
  174. #+name: inner
  175. #+begin_src emacs-lisp
  176. 2
  177. #+end_src
  178. * Main
  179. #+header: :tangle \"test-ob-tangle.el\" :comments noweb :noweb yes
  180. #+begin_src emacs-lisp
  181. 1
  182. <<inner>>
  183. #+end_src"
  184. (unwind-protect
  185. (let ((org-babel-tangle-use-relative-file-links nil))
  186. (org-babel-tangle)
  187. (with-temp-buffer
  188. (insert-file-contents "test-ob-tangle.el")
  189. (buffer-string)
  190. (goto-char (point-min))
  191. (and
  192. (search-forward (concat ";; [[file:" file "::inner") nil t)
  193. (search-forward ";; inner ends here" nil t))))
  194. (delete-file "test-ob-tangle.el")))))
  195. (ert-deftest ob-tangle/jump-to-org ()
  196. "Test `org-babel-tangle-jump-to-org' specifications."
  197. ;; Standard test.
  198. (let ((org-file-apps '((t . emacs))))
  199. (should
  200. (equal
  201. "* H\n#+begin_src emacs-lisp\n1\n#+end_src"
  202. (org-test-with-temp-text-in-file
  203. "* H\n#+begin_src emacs-lisp\n1\n#+end_src"
  204. (let ((file (buffer-file-name)))
  205. (org-test-with-temp-text
  206. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  207. (file-name-nondirectory file))
  208. (org-babel-tangle-jump-to-org)
  209. (buffer-string))))))
  210. ;; Multiple blocks in the same section.
  211. (should
  212. (equal
  213. "2"
  214. (org-test-with-temp-text-in-file
  215. "* H
  216. first block
  217. #+begin_src emacs-lisp
  218. 1
  219. #+end_src
  220. another block
  221. #+begin_src emacs-lisp
  222. 2
  223. #+end_src
  224. "
  225. (let ((file (buffer-file-name)))
  226. (org-test-with-temp-text
  227. (format ";; [[file:%s][H:2]]\n<point>2\n;; H:2 ends here\n"
  228. (file-name-nondirectory file))
  229. (org-babel-tangle-jump-to-org)
  230. (buffer-substring (line-beginning-position)
  231. (line-end-position)))))))
  232. ;; Preserve position within the source code.
  233. (should
  234. (equal
  235. "1)"
  236. (org-test-with-temp-text-in-file
  237. "* H\n#+begin_src emacs-lisp\n(+ 1 1)\n#+end_src"
  238. (let ((file (buffer-file-name)))
  239. (org-test-with-temp-text
  240. (format ";; [[file:%s][H:1]]\n(+ 1 <point>1)\n;; H:1 ends here\n"
  241. (file-name-nondirectory file))
  242. (org-babel-tangle-jump-to-org)
  243. (buffer-substring-no-properties (point) (line-end-position)))))))
  244. ;; Blocks before first heading.
  245. (should
  246. (equal
  247. "Buffer start\n#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  248. (org-test-with-temp-text-in-file
  249. "Buffer start\n#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  250. (let ((file (buffer-file-name)))
  251. (org-test-with-temp-text
  252. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  253. (file-name-nondirectory file))
  254. (org-babel-tangle-jump-to-org)
  255. (buffer-string))))))
  256. ;; Special case: buffer starts with a source block.
  257. (should
  258. (equal
  259. "#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  260. (org-test-with-temp-text-in-file
  261. "#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  262. (let ((file (buffer-file-name)))
  263. (org-test-with-temp-text
  264. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  265. (file-name-nondirectory file))
  266. (org-babel-tangle-jump-to-org)
  267. (buffer-string))))))))
  268. (ert-deftest ob-tangle/nested-block ()
  269. "Test tangling of org file with nested block."
  270. (should
  271. (string=
  272. "#+begin_src org
  273. ,#+begin_src emacs-lisp
  274. 1
  275. ,#+end_src
  276. #+end_src
  277. "
  278. (org-test-with-temp-text-in-file
  279. "#+header: :tangle \"test-ob-tangle.org\"
  280. #+begin_src org
  281. ,#+begin_src org
  282. ,,#+begin_src emacs-lisp
  283. 1
  284. ,,#+end_src
  285. ,#+end_src
  286. #+end_src"
  287. (unwind-protect
  288. (progn (org-babel-tangle)
  289. (with-temp-buffer (insert-file-contents "test-ob-tangle.org")
  290. (buffer-string)))
  291. (delete-file "test-ob-tangle.org"))))))
  292. (ert-deftest ob-tangle/block-order ()
  293. "Test order of tangled blocks."
  294. ;; Order per language.
  295. (should
  296. (equal '("1" "2")
  297. (let ((file (make-temp-file "org-tangle-")))
  298. (unwind-protect
  299. (progn
  300. (org-test-with-temp-text-in-file
  301. (format "#+property: header-args :tangle %S
  302. #+begin_src emacs-lisp
  303. 1
  304. #+end_src
  305. #+begin_src emacs-lisp
  306. 2
  307. #+end_src"
  308. file)
  309. (org-babel-tangle))
  310. (with-temp-buffer
  311. (insert-file-contents file)
  312. (org-split-string (buffer-string))))
  313. (delete-file file)))))
  314. ;; Order per source block.
  315. (should
  316. (equal '("1" "2")
  317. (let ((file (make-temp-file "org-tangle-")))
  318. (unwind-protect
  319. (progn
  320. (org-test-with-temp-text-in-file
  321. (format "#+property: header-args :tangle %S
  322. #+begin_src foo
  323. 1
  324. #+end_src
  325. #+begin_src bar
  326. 2
  327. #+end_src"
  328. file)
  329. (org-babel-tangle))
  330. (with-temp-buffer
  331. (insert-file-contents file)
  332. (org-split-string (buffer-string))))
  333. (delete-file file)))))
  334. ;; Preserve order with mixed languages.
  335. (should
  336. (equal '("1" "2" "3" "4")
  337. (let ((file (make-temp-file "org-tangle-")))
  338. (unwind-protect
  339. (progn
  340. (org-test-with-temp-text-in-file
  341. (format "#+property: header-args :tangle %S
  342. #+begin_src foo
  343. 1
  344. #+end_src
  345. #+begin_src bar
  346. 2
  347. #+end_src
  348. #+begin_src foo
  349. 3
  350. #+end_src
  351. #+begin_src bar
  352. 4
  353. #+end_src"
  354. file)
  355. (org-babel-tangle))
  356. (with-temp-buffer
  357. (insert-file-contents file)
  358. (org-split-string (buffer-string))))
  359. (delete-file file))))))
  360. (ert-deftest ob-tangle/commented-src-blocks ()
  361. "Test omission of commented src blocks."
  362. (should
  363. (equal '("A")
  364. (let ((file (make-temp-file "org-tangle-")))
  365. (unwind-protect
  366. (progn
  367. (org-test-with-temp-text-in-file
  368. (format "#+property: header-args :tangle %S
  369. * A
  370. #+begin_src emacs-lisp
  371. A
  372. #+end_src
  373. * COMMENT B
  374. #+begin_src emacs-lisp
  375. B
  376. #+end_src
  377. * C
  378. # #+begin_src emacs-lisp
  379. # C
  380. # #+end_src
  381. * D
  382. #+begin_comment
  383. #+begin_src emacs-lisp
  384. D
  385. #+end_src
  386. #+end_comment"
  387. file)
  388. (org-babel-tangle))
  389. (with-temp-buffer
  390. (insert-file-contents file)
  391. (org-split-string (buffer-string))))
  392. (delete-file file)))))
  393. (should
  394. (equal '("A")
  395. (let ((file (make-temp-file "org-tangle-")))
  396. (unwind-protect
  397. (progn
  398. (org-test-with-temp-text-in-file
  399. (format "#+property: header-args :tangle %S
  400. * A
  401. #+begin_src elisp :noweb yes
  402. A
  403. <<B>>
  404. <<C>>
  405. <<D>>
  406. #+end_src
  407. * COMMENT B
  408. #+begin_src elisp :noweb-ref B
  409. B
  410. #+end_src
  411. * C
  412. # #+begin_src elisp :noweb-ref C
  413. # C
  414. # #+end_src
  415. * D
  416. #+begin_comment
  417. #+begin_src elisp :noweb-ref D
  418. D
  419. #+end_src
  420. #+end_comment"
  421. file)
  422. (let (org-babel-noweb-error-all-langs
  423. org-babel-noweb-error-langs)
  424. (org-babel-tangle)))
  425. (with-temp-buffer
  426. (insert-file-contents file)
  427. (org-split-string (buffer-string))))
  428. (delete-file file))))))
  429. (ert-deftest ob-tangle/multiple-noweb-in-line ()
  430. "Test handling of multiple noweb references in a single line."
  431. (should
  432. (equal '("1" "2" "1")
  433. (let ((file (make-temp-file "org-tangle-")))
  434. (unwind-protect
  435. (progn
  436. (org-test-with-temp-text-in-file
  437. (format "
  438. #+name: block1
  439. #+begin_src elisp
  440. 1
  441. #+end_src
  442. #+name: block2
  443. #+begin_src elisp
  444. 2
  445. #+end_src
  446. #+name: block3
  447. #+begin_src elisp :noweb yes :tangle %s
  448. <<block1>> <<block2>> <<block1>>
  449. #+end_src"
  450. file)
  451. (let ((org-babel-noweb-error-all-langs nil)
  452. (org-babel-noweb-error-langs nil))
  453. (org-babel-tangle)))
  454. (with-temp-buffer
  455. (insert-file-contents file)
  456. (org-split-string (buffer-string))))
  457. (delete-file file))))))
  458. (ert-deftest ob-tangle/detangle-false-positive ()
  459. "Test handling of false positive link during detangle."
  460. (let (buffer)
  461. (unwind-protect
  462. (org-test-in-example-file (expand-file-name "babel.el" org-test-example-dir)
  463. (org-babel-detangle)
  464. (org-test-at-id "73115FB0-6565-442B-BB95-50195A499EF4"
  465. (setq buffer (current-buffer))
  466. (org-babel-next-src-block)
  467. (should (equal (string-trim (org-element-property
  468. :value (org-element-at-point)))
  469. ";; detangle changes"))))
  470. (kill-buffer buffer))))
  471. (provide 'test-ob-tangle)
  472. ;;; test-ob-tangle.el ends here