diff options
Diffstat (limited to 't')
| -rw-r--r-- | t/tests.lisp | 89 |
1 files changed, 82 insertions, 7 deletions
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 "<openbox_pipe_menu>" xml)) (check (search "label=\"tom & jerry\"" xml)) - (check (search "<action name=\"Execute\"><command>sh -c 'swaybg -i '\\''" + (check (search "<action name=\"Execute\"><command>'/bin/ws' '--set' '" xml)) + (check (search "tom & jerry.png'</command>" xml)) (check (search "</openbox_pipe_menu>" 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)))))) |
