test-ob-tangle.el 14 KB

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