diff options
| author | Craig Jennings <c@cjennings.net> | 2026-07-11 12:24:01 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-07-11 12:24:01 -0500 |
| commit | 5bee76514def9ca54c1bd52fb1a9c89ddfdab2ac (patch) | |
| tree | 7a5ba0da9af3c70a1709361e8da5880ff768a760 | |
| parent | c4b5fefa6712649c637a286886bd18ac412eba8d (diff) | |
| download | dotemacs-5bee76514def9ca54c1bd52fb1a9c89ddfdab2ac.tar.gz dotemacs-5bee76514def9ca54c1bd52fb1a9c89ddfdab2ac.zip | |
fix(ui-theme): create persist/ before writing the saved theme
cj/theme-write-file-contents guarded on file-writable-p, which returns nil when the target's directory doesn't exist. On a fresh machine persist/ isn't there yet, so the theme write failed silently and the choice never persisted. Create the parent directory first, and still return nil if the path isn't writable.
| -rw-r--r-- | modules/ui-theme.el | 10 | ||||
| -rw-r--r-- | tests/test-ui-theme-persistence.el | 13 |
2 files changed, 21 insertions, 2 deletions
diff --git a/modules/ui-theme.el b/modules/ui-theme.el index 499e71a4..b3cdc634 100644 --- a/modules/ui-theme.el +++ b/modules/ui-theme.el @@ -88,8 +88,14 @@ If FILENAME isn't readable, return nil." (string-trim (buffer-string))))) (defun cj/theme-write-file-contents (content filename) - "Write CONTENT to FILENAME. -If FILENAME isn't writeable, return nil. If successful, return t." + "Write CONTENT to FILENAME, creating its parent directory if absent. +On a fresh machine the `persist/' directory doesn't exist yet, and +`file-writable-p' returns nil for a file inside a missing directory, so the +write would silently fail. If FILENAME still isn't writeable, return nil. +If successful, return t." + (let ((dir (file-name-directory filename))) + (when (and dir (not (file-directory-p dir))) + (ignore-errors (make-directory dir t)))) (when (file-writable-p filename) (condition-case err (progn diff --git a/tests/test-ui-theme-persistence.el b/tests/test-ui-theme-persistence.el index 02bb105a..250b606b 100644 --- a/tests/test-ui-theme-persistence.el +++ b/tests/test-ui-theme-persistence.el @@ -32,6 +32,19 @@ "modus-vivendi"))) (delete-file file)))) +(ert-deftest test-ui-theme-write-file-contents-creates-missing-parent-dir () + "Boundary: writing into a not-yet-existing directory creates it first. +On a fresh machine `persist/' does not exist, and `file-writable-p' returns nil +for a file inside a missing directory, so the write must create the parent." + (let* ((sandbox (make-temp-file "ui-theme-sandbox-" t)) + (file (expand-file-name "persist/emacs-theme" sandbox))) + (unwind-protect + (progn + (should-not (file-directory-p (file-name-directory file))) + (should (cj/theme-write-file-contents "modus-vivendi" file)) + (should (equal (cj/theme-read-file-contents file) "modus-vivendi"))) + (delete-directory sandbox t)))) + (ert-deftest test-ui-theme-write-file-contents-uses-write-region () "Theme persistence should write directly instead of visiting the file." (let ((file (make-temp-file "ui-theme-write-region-")) |
