aboutsummaryrefslogtreecommitdiff
path: root/tests/test-hugo-config--collect-drafts.el
blob: 8076b3d3119ffc5c2488e8ba7596ea8f9b9e91c9 (plain)
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
;;; test-hugo-config--collect-drafts.el --- Tests for draft collector  -*- lexical-binding: t; -*-

;;; Commentary:
;; Unit tests for cj/hugo--collect-drafts. Walks a directory of Org files
;; and returns an alist of (TITLE . FILEPATH) for posts where :draft is t.
;; Covers Normal, Boundary, and Error cases.

;;; Code:

(require 'ert)
(require 'hugo-config)

(defun test-hugo-config--collect-drafts--mkdir ()
  "Return a fresh temp directory for fixture files."
  (make-temp-file "hugo-test-drafts-" t))

(defun test-hugo-config--collect-drafts--write (dir name contents)
  "Write CONTENTS into NAME inside DIR. Return the absolute path."
  (let ((file (expand-file-name name dir)))
    (with-temp-file file (insert contents))
    file))

;;; Normal Cases

(ert-deftest test-hugo-config--collect-drafts-normal-mixed-dir ()
  "Normal: directory with a draft and a published post returns only the draft."
  (let ((dir (test-hugo-config--collect-drafts--mkdir)))
    (unwind-protect
        (progn
          (test-hugo-config--collect-drafts--write
           dir "draft.org"
           "#+title: Unfinished Thought
#+hugo_draft: true

Body.")
          (test-hugo-config--collect-drafts--write
           dir "published.org"
           "#+title: Shipped
#+hugo_draft: false

Body.")
          (let ((result (cj/hugo--collect-drafts dir)))
            (should (= 1 (length result)))
            (should (string= "Unfinished Thought" (car (car result))))))
      (delete-directory dir t))))

;;; Boundary Cases

(ert-deftest test-hugo-config--collect-drafts-boundary-empty-dir ()
  "Boundary: empty directory returns nil."
  (let ((dir (test-hugo-config--collect-drafts--mkdir)))
    (unwind-protect
        (should (null (cj/hugo--collect-drafts dir)))
      (delete-directory dir t))))

(ert-deftest test-hugo-config--collect-drafts-boundary-all-published ()
  "Boundary: directory with only published posts returns nil."
  (let ((dir (test-hugo-config--collect-drafts--mkdir)))
    (unwind-protect
        (progn
          (test-hugo-config--collect-drafts--write
           dir "one.org" "#+title: One\n#+hugo_draft: false\n")
          (test-hugo-config--collect-drafts--write
           dir "two.org" "#+title: Two\n#+hugo_draft: false\n")
          (should (null (cj/hugo--collect-drafts dir))))
      (delete-directory dir t))))

;;; Error Cases

(ert-deftest test-hugo-config--collect-drafts-error-non-hugo-org ()
  "Error: Org files without #+hugo_draft: are not posts; returns nil."
  (let ((dir (test-hugo-config--collect-drafts--mkdir)))
    (unwind-protect
        (progn
          (test-hugo-config--collect-drafts--write
           dir "random.org" "#+title: Not a post\n\nJust an org file.")
          (should (null (cj/hugo--collect-drafts dir))))
      (delete-directory dir t))))

(provide 'test-hugo-config--collect-drafts)
;;; test-hugo-config--collect-drafts.el ends here