From f1933ef4b012de715e637f489bbcb89d944496f8 Mon Sep 17 00:00:00 2001 From: "Christopher R. Nelson" Date: Sun, 1 Nov 2026 00:27:57 -0400 Subject: Save the chosen wallpaper and restore it at login Menu items now call the program back with --set FILE, which runs the configured command and, if it exits 0, saves the path to $XDG_STATE_HOME/wallpaper-switcher/current. --restore sets the saved wallpaper again, for use in labwc/openbox autostart. Items use the binary's absolute path so the menu works when it is not on PATH. Paths from the command line are parsed as native namestrings so file names containing * or [ are not treated as wildcards. Co-Authored-By: Claude Opus 5.5 --- t/tests.lisp | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 82 insertions(+), 7 deletions(-) (limited to 't/tests.lisp') diff --git a/t/tests.lisp b/t/tests.lisp index bdb109f..4fc0665 100644 --- a/t/tests.lisp +++ b/t/tests.lisp @@ -52,7 +52,7 @@ `(call-with-temp-dir (lambda (,var) ,@body))) (defun touch (dir name) - (with-open-file (s (merge-pathnames name dir) :direction :output + (with-open-file (s (merge-pathnames (uiop:parse-native-namestring name) dir) :direction :output :if-does-not-exist :create) (declare (ignorable s)))) @@ -102,9 +102,14 @@ extensions = .PNG, jpg webp"))) (check-equal '("png" "webp") (config-extensions c)) (check (search "swaybg" (config-command c))))) +(deftest test-expand-template + (check-equal "swaybg -i '/w/it'\\''s.png' 100%" + (expand-template "swaybg -i %f 100%%" "/w/it's.png"))) + (deftest test-build-command - (check-equal "sh -c 'swaybg -i '\\''/w/a b.png'\\'' 100%'" - (build-command "swaybg -i %f 100%%" #p"/w/a b.png"))) + (let ((*self-command* '("/opt/wall switcher" "--command" "feh %f"))) + (check-equal "'/opt/wall switcher' '--command' 'feh %f' '--set' '/w/a b.png'" + (build-command #p"/w/a b.png")))) (deftest test-find-wallpapers (with-temp-dir (dir) @@ -123,13 +128,13 @@ extensions = .PNG, jpg webp"))) (with-temp-dir (dir) (touch dir "tom & jerry.png") (let ((xml (with-output-to-string (s) - (render-menu (make-config :directory (namestring dir) - :command "swaybg -i %f") - s)))) + (let ((*self-command* '("/bin/ws"))) + (render-menu (make-config :directory (namestring dir)) s))))) (check (search "" xml)) (check (search "label=\"tom & jerry\"" xml)) - (check (search "sh -c 'swaybg -i '\\''" + (check (search "'/bin/ws' '--set' '" xml)) + (check (search "tom & jerry.png'" xml)) (check (search "" xml))))) (deftest test-render-menu-empty @@ -207,3 +212,73 @@ extensions = .PNG, jpg webp"))) (let ((xml (render-to-string dir))) (check (search "label=\"a\"" xml)) (check (not (search "label=\"loop\"" xml))))))) + +;;; Saving and restoring + +(defmacro with-state-in ((dir) &body body) + `(let ((*state-file* (merge-pathnames "state/current" ,dir))) + ,@body)) + +(defun recording-config (dir) + "A config whose command appends the chosen file to DIR/log." + (make-config :command (format nil "printf '%s\\n' %f >> ~A" + (shell-quote (uiop:native-namestring + (merge-pathnames "log" dir)))))) + +(defun log-lines (dir) + (let ((log (merge-pathnames "log" dir))) + (when (probe-file log) (uiop:read-file-lines log)))) + +(deftest test-state-round-trip + (with-temp-dir (dir) + (with-state-in (dir) + (check-equal nil (read-state)) + (write-state #p"/w/first.png") + (write-state #p"/w/second one.png") + (check-equal "/w/second one.png" (read-state)) + (check (not (probe-file (merge-pathnames "state/current-tmp" dir))))))) + +(deftest test-state-file-default-location + (let ((*state-file* nil)) + (check (search "wallpaper-switcher/current" + (uiop:native-namestring (state-file-path)))))) + +(deftest test-set-wallpaper-runs-command-and-saves + (with-temp-dir (dir) + (with-state-in (dir) + (touch dir "it's [odd]*.png") + (let ((file (uiop:native-namestring (merge-pathnames (uiop:parse-native-namestring "it's [odd]*.png") dir)))) + (wallpaper-switcher::set-wallpaper (recording-config dir) file) + (check-equal (list file) (log-lines dir)) + (check-equal file (read-state)))))) + +(deftest test-set-wallpaper-failure-does-not-save + (with-temp-dir (dir) + (with-state-in (dir) + (touch dir "a.png") + (let ((errored nil)) + (handler-case + (wallpaper-switcher::set-wallpaper (make-config :command "exit 3") + (namestring (merge-pathnames "a.png" dir))) + (error () (setf errored t))) + (check errored) + (check-equal nil (read-state)))))) + +(deftest test-set-wallpaper-missing-file + (with-temp-dir (dir) + (with-state-in (dir) + (check (nth-value 1 (ignore-errors + (wallpaper-switcher::set-wallpaper + (recording-config dir) "/nonexistent/x.png")))) + (check-equal nil (log-lines dir))))) + +(deftest test-restore-wallpaper + (with-temp-dir (dir) + (with-state-in (dir) + (check (nth-value 1 (ignore-errors + (wallpaper-switcher::restore-wallpaper (recording-config dir))))) + (touch dir "b.webp") + (let ((file (uiop:native-namestring (merge-pathnames "b.webp" dir)))) + (write-state (merge-pathnames "b.webp" dir)) + (wallpaper-switcher::restore-wallpaper (recording-config dir)) + (check-equal (list file) (log-lines dir)))))) -- cgit v1.2.3