From 96c73033bbd759054d394172dfad6971816c3dc3 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Wed, 22 Apr 2026 06:57:00 -0500 Subject: feat(hugo): draft picker, preview toggle, publish command Put the full Hugo workflow inside Emacs. All of it lives in modules/hugo-config.el. New functions: - cj/hugo-open-draft reads all .org files under content-org/log, finds those with #+hugo_draft: true, and offers a completing-read picker. - cj/hugo-preview toggles a local hugo server subprocess and opens the preview URL in the browser. A second press stops the server. - cj/hugo-publish opens magit-status on the website repo. The server-side post-receive hook on cjennings.net already rebuilds and deploys on push, so committing and pushing is the deploy. Two pure helpers support the picker: cj/hugo--post-metadata parses the front matter region of a post, and cj/hugo--collect-drafts walks a directory and filters to drafts. Seven ERT tests cover both helpers across normal, boundary, and error cases. Keybinding note: C-; h d and C-; h D have swapped roles. Lowercase d now opens the draft picker. Uppercase D toggles the draft flag in the current buffer. The previous lowercase-d binding was toggle. --- tests/test-hugo-config--collect-drafts.el | 81 +++++++++++++++++++++++++++++++ tests/test-hugo-config--post-metadata.el | 64 ++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 tests/test-hugo-config--collect-drafts.el create mode 100644 tests/test-hugo-config--post-metadata.el (limited to 'tests') diff --git a/tests/test-hugo-config--collect-drafts.el b/tests/test-hugo-config--collect-drafts.el new file mode 100644 index 00000000..8076b3d3 --- /dev/null +++ b/tests/test-hugo-config--collect-drafts.el @@ -0,0 +1,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 diff --git a/tests/test-hugo-config--post-metadata.el b/tests/test-hugo-config--post-metadata.el new file mode 100644 index 00000000..d0719f6f --- /dev/null +++ b/tests/test-hugo-config--post-metadata.el @@ -0,0 +1,64 @@ +;;; test-hugo-config--post-metadata.el --- Tests for hugo post metadata parser -*- lexical-binding: t; -*- + +;;; Commentary: +;; Unit tests for cj/hugo--post-metadata. Reads the Hugo front matter region of +;; an Org file and returns (:title TITLE :draft BOOL) or nil for non-Hugo posts. +;; Covers Normal, Boundary, and Error cases. + +;;; Code: + +(require 'ert) +(require 'hugo-config) + +(defun test-hugo-config--post-metadata--write-fixture (contents) + "Write CONTENTS to a unique temp .org file and return its path." + (let ((file (make-temp-file "hugo-test-" nil ".org"))) + (with-temp-file file (insert contents)) + file)) + +;;; Normal Cases + +(ert-deftest test-hugo-config--post-metadata-normal-draft-with-title () + "Normal: draft post with title returns plist with title and :draft t." + (let ((file (test-hugo-config--post-metadata--write-fixture + "#+title: My First Post +#+date: 2026-01-01 +#+hugo_draft: true + +Body text."))) + (unwind-protect + (let ((meta (cj/hugo--post-metadata file))) + (should (equal (plist-get meta :title) "My First Post")) + (should (eq (plist-get meta :draft) t))) + (delete-file file)))) + +;;; Boundary Cases + +(ert-deftest test-hugo-config--post-metadata-boundary-published-no-title () + "Boundary: published post without #+title: falls back to file basename." + (let ((file (test-hugo-config--post-metadata--write-fixture + "#+hugo_draft: false + +Body only."))) + (unwind-protect + (let ((meta (cj/hugo--post-metadata file))) + (should (stringp (plist-get meta :title))) + (should (string= (plist-get meta :title) + (file-name-base file))) + (should (eq (plist-get meta :draft) nil))) + (delete-file file)))) + +;;; Error Cases + +(ert-deftest test-hugo-config--post-metadata-error-missing-hugo-draft () + "Error: Org file without #+hugo_draft: keyword is not a Hugo post; returns nil." + (let ((file (test-hugo-config--post-metadata--write-fixture + "#+title: Just an Org file + +No Hugo front matter here."))) + (unwind-protect + (should (null (cj/hugo--post-metadata file))) + (delete-file file)))) + +(provide 'test-hugo-config--post-metadata) +;;; test-hugo-config--post-metadata.el ends here -- cgit v1.2.3