emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob 0c3aaedcac9bb45137afde0cb919e1a1871ef286 17301 bytes (raw)
name: testing/lisp/test-ox-publish.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
 
;;; test-ox-publish.el --- Tests for "ox-publish.el" -*- lexical-binding: t; -*-

;; Copyright (C) 2016  Nicolas Goaziou

;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Code:

\f
;;; Helper functions

(defun org-test-publish (properties handler)
  "Publish a project defined by PROPERTIES.
Call HANDLER with the publishing directory as its sole argument.
Unless set otherwise in PROPERTIES, `:base-directory' is set to
\"examples/pub/\" sub-directory from test directory and
`:publishing-function' is set to `org-publish-attachment'."
  (declare (indent 1))
  (let* ((org-publish-use-timestamps-flag nil)
	 (org-publish-cache nil)
	 (base-dir (expand-file-name "examples/pub/" org-test-dir))
	 (pub-dir (make-temp-file "org-test" t))
	 (org-publish-timestamp-directory
	  (expand-file-name ".org-timestamps/" pub-dir))
	 (project
	  `("test" ,@(org-combine-plists
		      `(:base-directory
			,base-dir
			:publishing-function org-publish-attachment)
		      properties
		      `(:publishing-directory ,pub-dir)))))
    (unwind-protect
	(progn
	  (org-publish-projects (list project))
	  (funcall handler pub-dir))
      ;; Clear published data.
      (delete-directory pub-dir t)
      ;; Delete auto-generated site-map file, if applicable.
      (let ((site-map (and (plist-get properties :auto-sitemap)
			   (expand-file-name
			    (or (plist-get properties :sitemap-filename)
				"sitemap.org")
			    base-dir))))
	(when (and site-map (file-exists-p site-map))
	  (delete-file site-map))))))

\f
;;; Mandatory properties

(ert-deftest test-org-publish/base-extension ()
  "Test `:base-extension' specifications"
  ;; Regular tests.
  (should
   (equal '("a.org" "b.org")
	  (org-test-publish '(:base-extension "org")
	    (lambda (dir)
	      (remove ".org-timestamps"
		      (cl-remove-if #'file-directory-p
				    (directory-files dir)))))))
  (should
   (equal '("file.txt")
	  (org-test-publish '(:base-extension "txt")
	    (lambda (dir)
	      (remove ".org-timestamps"
		      (cl-remove-if #'file-directory-p
				    (directory-files dir)))))))
  ;; A nil value is equivalent to ".org".
  (should
   (equal '("a.org" "b.org")
	  (org-test-publish '(:base-extension nil)
	    (lambda (dir)
	      (remove ".org-timestamps"
		      (cl-remove-if #'file-directory-p
				    (directory-files dir)))))))
  ;; Symbol `any' includes all files, even those without extension.
  (should
   (equal '("a.org" "b.org" "file.txt" "noextension")
	  (org-test-publish '(:base-extension any)
	    (lambda (dir)
	      (remove ".org-timestamps"
		      (cl-remove-if #'file-directory-p
				    (directory-files dir))))))))

\f
;;; Site-map

(ert-deftest test-org-publish/sitemap ()
  "Test site-map specifications."
  ;; Site-map creation is controlled with `:auto-sitemap'.  It
  ;; defaults to "sitemap.org".
  (should
   (org-test-publish
       '(:auto-sitemap t)
     (lambda (dir) (file-exists-p (expand-file-name "sitemap.org" dir)))))
  (should-not
   (org-test-publish
       '(:auto-sitemap nil)
     (lambda (dir) (file-exists-p (expand-file-name "sitemap.org" dir)))))
  ;; Site-map file name is controlled with `:sitemap-filename'.
  (should
   (org-test-publish
       '(:auto-sitemap t :sitemap-filename "mysitemap.org")
     (lambda (dir) (file-exists-p (expand-file-name "mysitemap.org" dir)))))
  ;; Site-map title is controlled with `:sitemap-title'.  It defaults
  ;; to the project name.
  (should
   (equal "#+TITLE: Sitemap for project test"
	  (org-test-publish
	      '(:auto-sitemap t)
	    (lambda (dir)
	      (with-temp-buffer
		(insert-file-contents (expand-file-name "sitemap.org" dir))
		(buffer-substring (point) (line-end-position)))))))
  (should
   (equal "#+TITLE: My title"
	  (org-test-publish
	      '(:auto-sitemap t :sitemap-title "My title")
	    (lambda (dir)
	      (with-temp-buffer
		(insert-file-contents (expand-file-name "sitemap.org" dir))
		(buffer-substring (point) (line-end-position)))))))
  ;; Allowed site-map styles: `list' and `tree'.
  (should
   (equal "
- [[file:a.org][A]]
- [[file:b.org][b]]
- [[file:sub/c.org][C]]"
	  (org-test-publish
	      '(:auto-sitemap t
			      :sitemap-sort-folders ignore
			      :sitemap-style list
			      :exclude "."
			      :include ("a.org" "b.org" "sub/c.org"))
	    (lambda (dir)
	      (with-temp-buffer
		(insert-file-contents (expand-file-name "sitemap.org" dir))
		(buffer-substring (line-beginning-position 2) (point-max)))))))
  (should
   (equal "
- [[file:a.org][A]]
- [[file:b.org][b]]
- sub
  - [[file:sub/c.org][C]]"
	  (org-test-publish
	      '(:auto-sitemap t
			      :sitemap-style tree
			      :exclude "."
			      :include ("a.org" "b.org" "sub/c.org"))
	    (lambda (dir)
	      (with-temp-buffer
		(insert-file-contents (expand-file-name "sitemap.org" dir))
		(buffer-substring (line-beginning-position 2) (point-max)))))))
  ;; When style is `list', `:sitemap-sort-folders' controls the order
  ;; of appearance of directories among published files.
  (should
   (equal
    "
- sub/
- [[file:a.org][A]]
- [[file:sub/c.org][C]]"
    (org-test-publish
	'(:auto-sitemap t
			:recursive t
			:sitemap-style list
			:sitemap-sort-folders first
			:exclude "."
			:include ("a.org" "sub/c.org"))
      (lambda (dir)
	(with-temp-buffer
	  (insert-file-contents (expand-file-name "sitemap.org" dir))
	  (buffer-substring (line-beginning-position 2) (point-max)))))))
  (should
   (equal
    "
- [[file:a.org][A]]
- [[file:sub/c.org][C]]
- sub/"
    (org-test-publish
	'(:auto-sitemap t
			:recursive t
			:sitemap-style list
			:sitemap-sort-folders last
			:exclude "."
			:include ("a.org" "sub/c.org"))
      (lambda (dir)
	(with-temp-buffer
	  (insert-file-contents (expand-file-name "sitemap.org" dir))
	  (buffer-substring (line-beginning-position 2) (point-max)))))))
  ;; When style is `list', `:sitemap-sort-folders' can be used to
  ;; toggle visibility of directories in the site-map.
  (should
   (let ((case-fold-search t))
     (string-match-p
      "- sub/$"
      (org-test-publish
	  '(:auto-sitemap t
			  :recursive t
			  :sitemap-style list
			  :sitemap-sort-folders t
			  :exclude "."
			  :include ("a.org" "sub/c.org"))
	(lambda (dir)
	  (with-temp-buffer
	    (insert-file-contents (expand-file-name "sitemap.org" dir))
	    (buffer-substring (line-beginning-position 2) (point-max))))))))
  (should-not
   (string-match-p
    "- sub/$"
    (org-test-publish
	'(:auto-sitemap t
			:recursive t
			:sitemap-style list
			:sitemap-sort-folders ignore
			:exclude "."
			:include ("a.org" "sub/c.org"))
      (lambda (dir)
	(with-temp-buffer
	  (insert-file-contents (expand-file-name "sitemap.org" dir))
	  (buffer-substring (line-beginning-position 2) (point-max)))))))
  ;; Using `:sitemap-sort-files', files can be sorted alphabetically
  ;; (according to their title, or file name when there is none),
  ;; chronologically a anti-chronologically.
  (should
   (equal
    "
- [[file:a.org][A]]
- [[file:b.org][b]]
- [[file:sub/c.org][C]]"
    (org-test-publish
	'(:auto-sitemap t
			:recursive t
			:sitemap-style list
			:sitemap-sort-folders ignore
			:sitemap-sort-files alphabetically
			:exclude "."
			:include ("a.org" "b.org" "sub/c.org"))
      (lambda (dir)
	(with-temp-buffer
	  (insert-file-contents (expand-file-name "sitemap.org" dir))
	  (buffer-substring (line-beginning-position 2) (point-max)))))))
  (should
   (equal
    "
- [[file:b.org][b]]
- [[file:sub/c.org][C]]
- [[file:a.org][A]]"
    (org-test-publish
	'(:auto-sitemap t
			:recursive t
			:sitemap-style list
			:sitemap-sort-folders ignore
			:sitemap-sort-files chronologically
			:exclude "."
			:include ("a.org" "b.org" "sub/c.org"))
      (lambda (dir)
	(with-temp-buffer
	  (insert-file-contents (expand-file-name "sitemap.org" dir))
	  (buffer-substring (line-beginning-position 2) (point-max)))))))
  (should
   (equal
    "
- [[file:a.org][A]]
- [[file:sub/c.org][C]]
- [[file:b.org][b]]"
    (org-test-publish
	'(:auto-sitemap t
			:recursive t
			:sitemap-style list
			:sitemap-sort-folders ignore
			:sitemap-sort-files anti-chronologically
			:exclude "."
			:include ("a.org" "b.org" "sub/c.org"))
      (lambda (dir)
	(with-temp-buffer
	  (insert-file-contents (expand-file-name "sitemap.org" dir))
	  (buffer-substring (line-beginning-position 2) (point-max)))))))
  ;; `:sitemap-format-entry' formats entries in the site-map whereas
  ;; `:sitemap-function' controls the full site-map.
  (should
   (equal "
- a.org"
	  (org-test-publish
	      '(:auto-sitemap t
			      :exclude "."
			      :include ("a.org")
			      :sitemap-format-entry
			      (lambda (f _s _p) f))
	    (lambda (dir)
	      (with-temp-buffer
		(insert-file-contents (expand-file-name "sitemap.org" dir))
		(buffer-substring (line-beginning-position 2) (point-max)))))))
  (should
   (equal "Custom!"
	  (org-test-publish
	      '(:auto-sitemap t
			      :exclude "."
			      :include ("a.org")
			      :sitemap-function (lambda (_title _f) "Custom!"))
	    (lambda (dir)
	      (with-temp-buffer
		(insert-file-contents (expand-file-name "sitemap.org" dir))
		(buffer-string))))))
  (should
   (equal "[[file:a.org][A]]"
	  (org-test-publish
	      '(:auto-sitemap t
			      :exclude "."
			      :include ("a.org")
			      :sitemap-function
			      (lambda (_title f) (org-list-to-generic f nil)))
	    (lambda (dir)
	      (with-temp-buffer
		(insert-file-contents (expand-file-name "sitemap.org" dir))
		(buffer-string)))))))

\f
;;; Cross references

(ert-deftest test-org-publish/resolve-external-link ()
  "Test `org-publish-resolve-external-link' specifications."
  ;; Function should preserve internal reference when used between
  ;; published files.
  (should
   (apply
    #'equal
    (let* ((ids nil)
	   (backend
	    (org-export-create-backend
	     :transcoders
	     '((headline . (lambda (h c i)
			     (concat (org-export-get-reference h i) " " c)))
	       (paragraph . (lambda (p c i) c))
	       (section . (lambda (s c i) c))
	       (link . (lambda (l c i)
			 (let ((option (org-element-property :search-option l))
			       (path (org-element-property :path l)))
			   (and option
				(org-publish-resolve-external-link
				 option path))))))))
	   (publish
	    (lambda (plist filename pub-dir)
	      (org-publish-org-to backend filename ".test" plist pub-dir))))
      (org-test-publish
	  (list :publishing-function (list publish))
	(lambda (dir)
	  (cl-subseq
	   (split-string
	    (mapconcat (lambda (f) (org-file-contents (expand-file-name f dir)))
		       (directory-files dir nil "\\.test\\'")
		       " "))
	   1 3))))))
  ;; When optional argument PREFER-CUSTOM is non-nil, use custom ID
  ;; instead of internal reference, whenever possible.
  (should
   (equal
    '("a1" "b1")
    (let* ((ids nil)
	   (link-transcoder
	    (lambda (l c i)
	      (let ((option (org-element-property :search-option l))
		    (path (org-element-property :path l)))
		(push (org-publish-resolve-external-link option path t)
		      ids)
		"")))
	   (backend
	    (org-export-create-backend
	     :transcoders `((headline . (lambda (h c i) c))
			    (paragraph . (lambda (p c i) c))
			    (section . (lambda (s c i) c))
			    (link . ,link-transcoder))))
	   (publish
	    (lambda (plist filename pub-dir)
	      (org-publish-org-to backend filename ".test" plist pub-dir))))
      (org-test-publish (list :publishing-function (list publish)
			      :exclude "."
			      :include '("a.org" "b.org"))
	#'ignore)
      (sort ids #'string<)))))

\f
;;; Tools

(ert-deftest test-org-publish/get-project-from-filename ()
  "Test `org-publish-get-project-from-filename' specifications."
  ;; Check base directory.
  (should
   (let* ((base (expand-file-name "examples/pub/" org-test-dir))
	  (file (expand-file-name "a.org" base))
	  (org-publish-project-alist `(("p" :base-directory ,base))))
     (org-publish-get-project-from-filename file)))
  ;; Return nil if no appropriate project is found.
  (should-not
   (let* ((base (expand-file-name "examples/pub/" org-test-dir))
	  (file (expand-file-name "a.org" base))
	  (org-publish-project-alist `(("p" :base-directory ,base))))
     (org-publish-get-project-from-filename "/other/file.org")))
  ;; Return the first project effectively publishing the provided
  ;; file.
  (should
   (equal "p2"
	  (let* ((base (expand-file-name "examples/pub/" org-test-dir))
		 (file (expand-file-name "a.org" base))
		 (org-publish-project-alist
		  `(("p1" :base-directory "/other/")
		    ("p2" :base-directory ,base))))
	    (car (org-publish-get-project-from-filename file)))))
  ;; When :recursive in non-nil, allow files in sub-directories.
  (should
   (let* ((base (expand-file-name "examples/pub/" org-test-dir))
	  (file (expand-file-name "sub/c.org" base))
	  (org-publish-project-alist
	   `(("p" :base-directory ,base :recursive t))))
     (org-publish-get-project-from-filename file)))
  (should-not
   (let* ((base (expand-file-name "examples/pub/" org-test-dir))
	  (file (expand-file-name "sub/c.org" base))
	  (org-publish-project-alist
	   `(("p" :base-directory ,base :recursive nil))))
     (org-publish-get-project-from-filename file)))
  ;; Check :base-extension.
  (should
   (let* ((base (expand-file-name "examples/pub/" org-test-dir))
	  (file (expand-file-name "file.txt" base))
	  (org-publish-project-alist
	   `(("p" :base-directory ,base :base-extension "txt"))))
     (org-publish-get-project-from-filename file)))
  (should-not
   (let* ((base (expand-file-name "examples/pub/" org-test-dir))
	  (file (expand-file-name "file.txt" base))
	  (org-publish-project-alist
	   `(("p" :base-directory ,base :base-extension "org"))))
     (org-publish-get-project-from-filename file)))
  ;; When :base-extension has the special value `any', allow any
  ;; extension, including none.
  (should
   (let* ((base (expand-file-name "examples/pub/" org-test-dir))
	  (file (expand-file-name "file.txt" base))
	  (org-publish-project-alist
	   `(("p" :base-directory ,base :base-extension any))))
     (org-publish-get-project-from-filename file)))
  (should
   (let* ((base (expand-file-name "examples/pub/" org-test-dir))
	  (file (expand-file-name "noextension" base))
	  (org-publish-project-alist
	   `(("p" :base-directory ,base :base-extension any))))
     (org-publish-get-project-from-filename file)))
  ;; Pathological case: Handle both :extension any and :recursive t.
  (should
   (let* ((base (expand-file-name "examples/pub/" org-test-dir))
	  (file (expand-file-name "sub/c.org" base))
	  (org-publish-project-alist
	   `(("p" :base-directory ,base :recursive t :base-extension any))))
     (org-publish-get-base-files (org-publish-get-project-from-filename file))))

  ;; Check :exclude property.
  (should-not
   (let* ((base (expand-file-name "examples/pub/" org-test-dir))
	  (file (expand-file-name "a.org" base))
	  (org-publish-project-alist
	   `(("p" :base-directory ,base :exclude "a"))))
     (org-publish-get-project-from-filename file)))
  (should
   (let* ((base (expand-file-name "examples/pub/" org-test-dir))
	  (file (expand-file-name "a.org" base))
	  (org-publish-project-alist
	   `(("p" :base-directory ,base :exclude "other"))))
     (org-publish-get-project-from-filename file)))
  ;; The regexp matches against relative file name, not absolute one.
  (should
   (let* ((base (expand-file-name "examples/pub/" org-test-dir))
	  (file (expand-file-name "a.org" base))
	  (org-publish-project-alist
	   `(("p" :base-directory ,base :exclude "examples/pub"))))
     (org-publish-get-project-from-filename file)))
  ;; Check :include property.
  (should
   (let* ((base (expand-file-name "examples/pub/" org-test-dir))
	  (file (expand-file-name "file.txt" base))
	  (org-publish-project-alist
	   `(("p" :base-directory ,base :include (,file)))))
     (org-publish-get-project-from-filename file)))
  ;; :include property has precedence over :exclude one.
  (should
   (let* ((base (expand-file-name "examples/pub/" org-test-dir))
	  (file (expand-file-name "a.org" base))
	  (org-publish-project-alist
	   `(("p"
	      :base-directory ,base
	      :include (,(file-name-nondirectory file))
	      :exclude "a"))))
     (org-publish-get-project-from-filename file)))
  ;; With optional argument, return a meta-project publishing provided
  ;; file.
  (should
   (equal "meta"
	  (let* ((base (expand-file-name "examples/pub/" org-test-dir))
		 (file (expand-file-name "a.org" base))
		 (org-publish-project-alist
		  `(("meta" :components ("p"))
		    ("p" :base-directory ,base))))
	    (car (org-publish-get-project-from-filename file t))))))


(provide 'test-ox-publish)
;;; test-ox-publish.el ends here

debug log:

solving 0c3aaedca ...
found 0c3aaedca in https://git.savannah.gnu.org/cgit/emacs/org-mode.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).