test-ox-publish.el 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. ;;; test-ox-publish.el --- Tests for "ox-publish.el" -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2016 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
  4. ;; Keywords: local
  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. ;;; Code:
  16. (defun org-test-publish (properties handler)
  17. "Publish a project defined by PROPERTIES.
  18. Call HANDLER with the publishing directory as its sole argument.
  19. Unless set otherwise in PROPERTIES, `:base-directory' is set to
  20. \"examples/pub/\" sub-directory from test directory and
  21. `:publishing-function' is set to `org-publish-attachment'."
  22. (let* ((org-publish-use-timestamps-flag nil)
  23. (org-publish-cache nil)
  24. (base-dir (expand-file-name "examples/pub/" org-test-dir))
  25. (pub-dir (make-temp-file "org-test" t))
  26. (org-publish-timestamp-directory
  27. (expand-file-name ".org-timestamps/" pub-dir))
  28. (project
  29. `("test" ,@(org-combine-plists
  30. `(:base-directory
  31. ,base-dir
  32. :publishing-function org-publish-attachment)
  33. properties
  34. `(:publishing-directory ,pub-dir)))))
  35. (unwind-protect
  36. (progn
  37. (org-publish-projects (list project))
  38. (funcall handler pub-dir))
  39. ;; Clear published data.
  40. (delete-directory pub-dir t)
  41. ;; Delete auto-generated site-map file, if applicable.
  42. (let ((site-map (and (plist-get properties :auto-sitemap)
  43. (expand-file-name
  44. (or (plist-get properties :sitemap-filename)
  45. "sitemap.org")
  46. base-dir))))
  47. (when (and site-map (file-exists-p site-map))
  48. (delete-file site-map))))))
  49. ;;; Site-map
  50. (ert-deftest test-org-publish/sitemap ()
  51. "Test site-map specifications."
  52. ;; Site-map creation is controlled with `:auto-sitemap'. It
  53. ;; defaults to "sitemap.org".
  54. (should
  55. (org-test-publish
  56. '(:auto-sitemap t)
  57. (lambda (dir) (file-exists-p (expand-file-name "sitemap.org" dir)))))
  58. (should-not
  59. (org-test-publish
  60. '(:auto-sitemap nil)
  61. (lambda (dir) (file-exists-p (expand-file-name "sitemap.org" dir)))))
  62. ;; Site-map file name is controlled with `:sitemap-filename'.
  63. (should
  64. (org-test-publish
  65. '(:auto-sitemap t :sitemap-filename "mysitemap.org")
  66. (lambda (dir) (file-exists-p (expand-file-name "mysitemap.org" dir)))))
  67. ;; Site-map title is controlled with `:sitemap-title'. It defaults
  68. ;; to the project name.
  69. (should
  70. (equal "#+TITLE: Sitemap for project test"
  71. (org-test-publish
  72. '(:auto-sitemap t)
  73. (lambda (dir)
  74. (with-temp-buffer
  75. (insert-file-contents (expand-file-name "sitemap.org" dir))
  76. (buffer-substring (point) (line-end-position)))))))
  77. (should
  78. (equal "#+TITLE: My title"
  79. (org-test-publish
  80. '(:auto-sitemap t :sitemap-title "My title")
  81. (lambda (dir)
  82. (with-temp-buffer
  83. (insert-file-contents (expand-file-name "sitemap.org" dir))
  84. (buffer-substring (point) (line-end-position)))))))
  85. ;; Allowed site-map styles: `list' and `tree'.
  86. (should
  87. (equal "
  88. - [[file:a.org][A]]
  89. - [[file:b.org][b]]
  90. - [[file:sub/c.org][C]]"
  91. (org-test-publish
  92. '(:auto-sitemap t
  93. :sitemap-sort-folders ignore
  94. :sitemap-style list
  95. :exclude "."
  96. :include ("a.org" "b.org" "sub/c.org"))
  97. (lambda (dir)
  98. (with-temp-buffer
  99. (insert-file-contents (expand-file-name "sitemap.org" dir))
  100. (buffer-substring (line-beginning-position 2) (point-max)))))))
  101. (should
  102. (equal "
  103. - [[file:a.org][A]]
  104. - [[file:b.org][b]]
  105. - sub
  106. - [[file:sub/c.org][C]]"
  107. (org-test-publish
  108. '(:auto-sitemap t
  109. :sitemap-style tree
  110. :exclude "."
  111. :include ("a.org" "b.org" "sub/c.org"))
  112. (lambda (dir)
  113. (with-temp-buffer
  114. (insert-file-contents (expand-file-name "sitemap.org" dir))
  115. (buffer-substring (line-beginning-position 2) (point-max)))))))
  116. ;; When style is `list', `:sitemap-sort-folders' controls the order
  117. ;; of appearance of directories among published files.
  118. (should
  119. (equal
  120. "
  121. - sub/
  122. - [[file:a.org][A]]
  123. - [[file:sub/c.org][C]]"
  124. (org-test-publish
  125. '(:auto-sitemap t
  126. :recursive t
  127. :sitemap-style list
  128. :sitemap-sort-folders first
  129. :exclude "."
  130. :include ("a.org" "sub/c.org"))
  131. (lambda (dir)
  132. (with-temp-buffer
  133. (insert-file-contents (expand-file-name "sitemap.org" dir))
  134. (buffer-substring (line-beginning-position 2) (point-max)))))))
  135. (should
  136. (equal
  137. "
  138. - [[file:a.org][A]]
  139. - [[file:sub/c.org][C]]
  140. - sub/"
  141. (org-test-publish
  142. '(:auto-sitemap t
  143. :recursive t
  144. :sitemap-style list
  145. :sitemap-sort-folders last
  146. :exclude "."
  147. :include ("a.org" "sub/c.org"))
  148. (lambda (dir)
  149. (with-temp-buffer
  150. (insert-file-contents (expand-file-name "sitemap.org" dir))
  151. (buffer-substring (line-beginning-position 2) (point-max)))))))
  152. ;; When style is `list', `:sitemap-sort-folders' can be used to
  153. ;; toggle visibility of directories in the site-map.
  154. (should
  155. (let ((case-fold-search t))
  156. (string-match-p
  157. "- sub/$"
  158. (org-test-publish
  159. '(:auto-sitemap t
  160. :recursive t
  161. :sitemap-style list
  162. :sitemap-sort-folders t
  163. :exclude "."
  164. :include ("a.org" "sub/c.org"))
  165. (lambda (dir)
  166. (with-temp-buffer
  167. (insert-file-contents (expand-file-name "sitemap.org" dir))
  168. (buffer-substring (line-beginning-position 2) (point-max))))))))
  169. (should-not
  170. (string-match-p
  171. "- sub/$"
  172. (org-test-publish
  173. '(:auto-sitemap t
  174. :recursive t
  175. :sitemap-style list
  176. :sitemap-sort-folders ignore
  177. :exclude "."
  178. :include ("a.org" "sub/c.org"))
  179. (lambda (dir)
  180. (with-temp-buffer
  181. (insert-file-contents (expand-file-name "sitemap.org" dir))
  182. (buffer-substring (line-beginning-position 2) (point-max)))))))
  183. ;; Using `:sitemap-sort-files', files can be sorted alphabetically
  184. ;; (according to their title, or file name when there is none),
  185. ;; chronologically a anti-chronologically.
  186. (should
  187. (equal
  188. "
  189. - [[file:a.org][A]]
  190. - [[file:b.org][b]]
  191. - [[file:sub/c.org][C]]"
  192. (org-test-publish
  193. '(:auto-sitemap t
  194. :recursive t
  195. :sitemap-style list
  196. :sitemap-sort-folders ignore
  197. :sitemap-sort-files alphabetically
  198. :exclude "."
  199. :include ("a.org" "b.org" "sub/c.org"))
  200. (lambda (dir)
  201. (with-temp-buffer
  202. (insert-file-contents (expand-file-name "sitemap.org" dir))
  203. (buffer-substring (line-beginning-position 2) (point-max)))))))
  204. (should
  205. (equal
  206. "
  207. - [[file:b.org][b]]
  208. - [[file:sub/c.org][C]]
  209. - [[file:a.org][A]]"
  210. (org-test-publish
  211. '(:auto-sitemap t
  212. :recursive t
  213. :sitemap-style list
  214. :sitemap-sort-folders ignore
  215. :sitemap-sort-files chronologically
  216. :exclude "."
  217. :include ("a.org" "b.org" "sub/c.org"))
  218. (lambda (dir)
  219. (with-temp-buffer
  220. (insert-file-contents (expand-file-name "sitemap.org" dir))
  221. (buffer-substring (line-beginning-position 2) (point-max)))))))
  222. (should
  223. (equal
  224. "
  225. - [[file:a.org][A]]
  226. - [[file:sub/c.org][C]]
  227. - [[file:b.org][b]]"
  228. (org-test-publish
  229. '(:auto-sitemap t
  230. :recursive t
  231. :sitemap-style list
  232. :sitemap-sort-folders ignore
  233. :sitemap-sort-files anti-chronologically
  234. :exclude "."
  235. :include ("a.org" "b.org" "sub/c.org"))
  236. (lambda (dir)
  237. (with-temp-buffer
  238. (insert-file-contents (expand-file-name "sitemap.org" dir))
  239. (buffer-substring (line-beginning-position 2) (point-max)))))))
  240. ;; `:sitemap-format-entry' formats entries in the site-map whereas
  241. ;; `:sitemap-function' controls the full site-map.
  242. (should
  243. (equal "
  244. - a.org"
  245. (org-test-publish
  246. '(:auto-sitemap t
  247. :exclude "."
  248. :include ("a.org")
  249. :sitemap-format-entry
  250. (lambda (f d _s) (file-relative-name f d)))
  251. (lambda (dir)
  252. (with-temp-buffer
  253. (insert-file-contents (expand-file-name "sitemap.org" dir))
  254. (buffer-substring (line-beginning-position 2) (point-max)))))))
  255. (should
  256. (equal "Custom!"
  257. (org-test-publish
  258. '(:auto-sitemap t
  259. :exclude "."
  260. :include ("a.org")
  261. :sitemap-function
  262. (lambda (title _files) "Custom!"))
  263. (lambda (dir)
  264. (with-temp-buffer
  265. (insert-file-contents (expand-file-name "sitemap.org" dir))
  266. (buffer-string))))))
  267. (should
  268. (equal "[[file:a.org][A]]"
  269. (org-test-publish
  270. '(:auto-sitemap t
  271. :exclude "."
  272. :include ("a.org")
  273. :sitemap-function
  274. (lambda (title files) (org-list-to-generic files nil)))
  275. (lambda (dir)
  276. (with-temp-buffer
  277. (insert-file-contents (expand-file-name "sitemap.org" dir))
  278. (buffer-string)))))))
  279. (provide 'test-ox-publish)
  280. ;;; test-ox-publish.el ends here