1 ;;; test-ox-publish.el --- Tests for "ox-publish.el" -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2016 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 (defun org-test-publish (properties handler
)
26 "Publish a project defined by PROPERTIES.
27 Call HANDLER with the publishing directory as its sole argument.
28 Unless set otherwise in PROPERTIES, `:base-directory' is set to
29 \"examples/pub/\" sub-directory from test directory and
30 `:publishing-function' is set to `org-publish-attachment'."
32 (let* ((org-publish-use-timestamps-flag nil
)
33 (org-publish-cache nil
)
34 (base-dir (expand-file-name "examples/pub/" org-test-dir
))
35 (pub-dir (make-temp-file "org-test" t
))
36 (org-publish-timestamp-directory
37 (expand-file-name ".org-timestamps/" pub-dir
))
39 `("test" ,@(org-combine-plists
42 :publishing-function org-publish-attachment
)
44 `(:publishing-directory
,pub-dir
)))))
47 (org-publish-projects (list project
))
48 (funcall handler pub-dir
))
49 ;; Clear published data.
50 (delete-directory pub-dir t
)
51 ;; Delete auto-generated site-map file, if applicable.
52 (let ((site-map (and (plist-get properties
:auto-sitemap
)
54 (or (plist-get properties
:sitemap-filename
)
57 (when (and site-map
(file-exists-p site-map
))
58 (delete-file site-map
))))))
61 ;;; Mandatory properties
63 (ert-deftest test-org-publish
/base-extension
()
64 "Test `:base-extension' specifications"
67 (equal '("a.org" "b.org")
68 (org-test-publish '(:base-extension
"org")
70 (remove ".org-timestamps"
71 (cl-remove-if #'file-directory-p
72 (directory-files dir
)))))))
75 (org-test-publish '(:base-extension
"txt")
77 (remove ".org-timestamps"
78 (cl-remove-if #'file-directory-p
79 (directory-files dir
)))))))
80 ;; A nil value is equivalent to ".org".
82 (equal '("a.org" "b.org")
83 (org-test-publish '(:base-extension nil
)
85 (remove ".org-timestamps"
86 (cl-remove-if #'file-directory-p
87 (directory-files dir
)))))))
88 ;; Symbol `any' includes all files, even those without extension.
90 (equal '("a.org" "b.org" "file.txt" "noextension")
91 (org-test-publish '(:base-extension any
)
93 (remove ".org-timestamps"
94 (cl-remove-if #'file-directory-p
95 (directory-files dir
))))))))
101 (ert-deftest test-org-publish
/sitemap
()
102 "Test site-map specifications."
103 ;; Site-map creation is controlled with `:auto-sitemap'. It
104 ;; defaults to "sitemap.org".
108 (lambda (dir) (file-exists-p (expand-file-name "sitemap.org" dir
)))))
112 (lambda (dir) (file-exists-p (expand-file-name "sitemap.org" dir
)))))
113 ;; Site-map file name is controlled with `:sitemap-filename'.
116 '(:auto-sitemap t
:sitemap-filename
"mysitemap.org")
117 (lambda (dir) (file-exists-p (expand-file-name "mysitemap.org" dir
)))))
118 ;; Site-map title is controlled with `:sitemap-title'. It defaults
119 ;; to the project name.
121 (equal "#+TITLE: Sitemap for project test"
126 (insert-file-contents (expand-file-name "sitemap.org" dir
))
127 (buffer-substring (point) (line-end-position)))))))
129 (equal "#+TITLE: My title"
131 '(:auto-sitemap t
:sitemap-title
"My title")
134 (insert-file-contents (expand-file-name "sitemap.org" dir
))
135 (buffer-substring (point) (line-end-position)))))))
136 ;; Allowed site-map styles: `list' and `tree'.
141 - [[file:sub/c.org][C]]"
144 :sitemap-sort-folders ignore
147 :include
("a.org" "b.org" "sub/c.org"))
150 (insert-file-contents (expand-file-name "sitemap.org" dir
))
151 (buffer-substring (line-beginning-position 2) (point-max)))))))
157 - [[file:sub/c.org][C]]"
162 :include
("a.org" "b.org" "sub/c.org"))
165 (insert-file-contents (expand-file-name "sitemap.org" dir
))
166 (buffer-substring (line-beginning-position 2) (point-max)))))))
167 ;; When style is `list', `:sitemap-sort-folders' controls the order
168 ;; of appearance of directories among published files.
174 - [[file:sub/c.org][C]]"
179 :sitemap-sort-folders first
181 :include
("a.org" "sub/c.org"))
184 (insert-file-contents (expand-file-name "sitemap.org" dir
))
185 (buffer-substring (line-beginning-position 2) (point-max)))))))
190 - [[file:sub/c.org][C]]
196 :sitemap-sort-folders last
198 :include
("a.org" "sub/c.org"))
201 (insert-file-contents (expand-file-name "sitemap.org" dir
))
202 (buffer-substring (line-beginning-position 2) (point-max)))))))
203 ;; When style is `list', `:sitemap-sort-folders' can be used to
204 ;; toggle visibility of directories in the site-map.
206 (let ((case-fold-search t
))
213 :sitemap-sort-folders t
215 :include
("a.org" "sub/c.org"))
218 (insert-file-contents (expand-file-name "sitemap.org" dir
))
219 (buffer-substring (line-beginning-position 2) (point-max))))))))
227 :sitemap-sort-folders ignore
229 :include
("a.org" "sub/c.org"))
232 (insert-file-contents (expand-file-name "sitemap.org" dir
))
233 (buffer-substring (line-beginning-position 2) (point-max)))))))
234 ;; Using `:sitemap-sort-files', files can be sorted alphabetically
235 ;; (according to their title, or file name when there is none),
236 ;; chronologically a anti-chronologically.
242 - [[file:sub/c.org][C]]"
247 :sitemap-sort-folders ignore
248 :sitemap-sort-files alphabetically
250 :include
("a.org" "b.org" "sub/c.org"))
253 (insert-file-contents (expand-file-name "sitemap.org" dir
))
254 (buffer-substring (line-beginning-position 2) (point-max)))))))
259 - [[file:sub/c.org][C]]
265 :sitemap-sort-folders ignore
266 :sitemap-sort-files chronologically
268 :include
("a.org" "b.org" "sub/c.org"))
271 (insert-file-contents (expand-file-name "sitemap.org" dir
))
272 (buffer-substring (line-beginning-position 2) (point-max)))))))
277 - [[file:sub/c.org][C]]
283 :sitemap-sort-folders ignore
284 :sitemap-sort-files anti-chronologically
286 :include
("a.org" "b.org" "sub/c.org"))
289 (insert-file-contents (expand-file-name "sitemap.org" dir
))
290 (buffer-substring (line-beginning-position 2) (point-max)))))))
291 ;; `:sitemap-format-entry' formats entries in the site-map whereas
292 ;; `:sitemap-function' controls the full site-map.
300 :sitemap-format-entry
301 (lambda (f _s _p
) f
))
304 (insert-file-contents (expand-file-name "sitemap.org" dir
))
305 (buffer-substring (line-beginning-position 2) (point-max)))))))
312 :sitemap-function
(lambda (_title _f
) "Custom!"))
315 (insert-file-contents (expand-file-name "sitemap.org" dir
))
318 (equal "[[file:a.org][A]]"
324 (lambda (_title f
) (org-list-to-generic f nil
)))
327 (insert-file-contents (expand-file-name "sitemap.org" dir
))
328 (buffer-string)))))))
333 (ert-deftest test-org-publish
/get-project-from-filename
()
334 "Test `org-publish-get-project-from-filename' specifications."
335 ;; Check base directory.
337 (let* ((base (expand-file-name "examples/pub/" org-test-dir
))
338 (file (expand-file-name "a.org" base
))
339 (org-publish-project-alist `(("p" :base-directory
,base
))))
340 (org-publish-get-project-from-filename file
)))
341 ;; Return nil if no appropriate project is found.
343 (let* ((base (expand-file-name "examples/pub/" org-test-dir
))
344 (file (expand-file-name "a.org" base
))
345 (org-publish-project-alist `(("p" :base-directory
,base
))))
346 (org-publish-get-project-from-filename "/other/file.org")))
347 ;; Return the first project effectively publishing the provided
351 (let* ((base (expand-file-name "examples/pub/" org-test-dir
))
352 (file (expand-file-name "a.org" base
))
353 (org-publish-project-alist
354 `(("p1" :base-directory
"/other/")
355 ("p2" :base-directory
,base
))))
356 (car (org-publish-get-project-from-filename file
)))))
357 ;; When :recursive in non-nil, allow files in sub-directories.
359 (let* ((base (expand-file-name "examples/pub/" org-test-dir
))
360 (file (expand-file-name "sub/c.org" base
))
361 (org-publish-project-alist
362 `(("p" :base-directory
,base
:recursive t
))))
363 (org-publish-get-project-from-filename file
)))
365 (let* ((base (expand-file-name "examples/pub/" org-test-dir
))
366 (file (expand-file-name "sub/c.org" base
))
367 (org-publish-project-alist
368 `(("p" :base-directory
,base
:recursive nil
))))
369 (org-publish-get-project-from-filename file
)))
370 ;; Check :base-extension.
372 (let* ((base (expand-file-name "examples/pub/" org-test-dir
))
373 (file (expand-file-name "file.txt" base
))
374 (org-publish-project-alist
375 `(("p" :base-directory
,base
:base-extension
"txt"))))
376 (org-publish-get-project-from-filename file
)))
378 (let* ((base (expand-file-name "examples/pub/" org-test-dir
))
379 (file (expand-file-name "file.txt" base
))
380 (org-publish-project-alist
381 `(("p" :base-directory
,base
:base-extension
"org"))))
382 (org-publish-get-project-from-filename file
)))
383 ;; When :base-extension has the special value `any', allow any
384 ;; extension, including none.
386 (let* ((base (expand-file-name "examples/pub/" org-test-dir
))
387 (file (expand-file-name "file.txt" base
))
388 (org-publish-project-alist
389 `(("p" :base-directory
,base
:base-extension any
))))
390 (org-publish-get-project-from-filename file
)))
392 (let* ((base (expand-file-name "examples/pub/" org-test-dir
))
393 (file (expand-file-name "noextension" base
))
394 (org-publish-project-alist
395 `(("p" :base-directory
,base
:base-extension any
))))
396 (org-publish-get-project-from-filename file
)))
397 ;; Pathological case: Handle both :extension any and :recursive t.
399 (let* ((base (expand-file-name "examples/pub/" org-test-dir
))
400 (file (expand-file-name "sub/c.org" base
))
401 (org-publish-project-alist
402 `(("p" :base-directory
,base
:recursive t
:base-extension any
))))
403 (org-publish-get-base-files (org-publish-get-project-from-filename file
))))
405 ;; Check :exclude property.
407 (let* ((base (expand-file-name "examples/pub/" org-test-dir
))
408 (file (expand-file-name "a.org" base
))
409 (org-publish-project-alist
410 `(("p" :base-directory
,base
:exclude
"a"))))
411 (org-publish-get-project-from-filename file
)))
413 (let* ((base (expand-file-name "examples/pub/" org-test-dir
))
414 (file (expand-file-name "a.org" base
))
415 (org-publish-project-alist
416 `(("p" :base-directory
,base
:exclude
"other"))))
417 (org-publish-get-project-from-filename file
)))
418 ;; The regexp matches against relative file name, not absolute one.
420 (let* ((base (expand-file-name "examples/pub/" org-test-dir
))
421 (file (expand-file-name "a.org" base
))
422 (org-publish-project-alist
423 `(("p" :base-directory
,base
:exclude
"examples/pub"))))
424 (org-publish-get-project-from-filename file
)))
425 ;; Check :include property.
427 (let* ((base (expand-file-name "examples/pub/" org-test-dir
))
428 (file (expand-file-name "file.txt" base
))
429 (org-publish-project-alist
430 `(("p" :base-directory
,base
:include
(,file
)))))
431 (org-publish-get-project-from-filename file
)))
432 ;; :include property has precedence over :exclude one.
434 (let* ((base (expand-file-name "examples/pub/" org-test-dir
))
435 (file (expand-file-name "a.org" base
))
436 (org-publish-project-alist
438 :base-directory
,base
439 :include
(,(file-name-nondirectory file
))
441 (org-publish-get-project-from-filename file
)))
442 ;; With optional argument, return a meta-project publishing provided
446 (let* ((base (expand-file-name "examples/pub/" org-test-dir
))
447 (file (expand-file-name "a.org" base
))
448 (org-publish-project-alist
449 `(("meta" :components
("p"))
450 ("p" :base-directory
,base
))))
451 (car (org-publish-get-project-from-filename file t
))))))
454 (provide 'test-ox-publish
)
455 ;;; test-ox-publish.el ends here