;;; A small self-contained test runner, so the project needs nothing ;;; beyond ASDF/UIOP. (defpackage #:wallpaper-switcher/tests (:use #:cl #:wallpaper-switcher) (:export #:run-tests)) (in-package #:wallpaper-switcher/tests) (defvar *tests* '()) (defvar *failures* 0) (defmacro deftest (name &body body) `(progn (defun ,name () ,@body) (pushnew ',name *tests*) ',name)) (defmacro check (form) `(unless ,form (incf *failures*) (format t "~& FAIL: ~S~%" ',form))) (defmacro check-equal (expected form) (let ((e (gensym)) (v (gensym))) `(let ((,e ,expected) (,v ,form)) (unless (equal ,e ,v) (incf *failures*) (format t "~& FAIL: ~S~% expected ~S~% got ~S~%" ',form ,e ,v))))) (defun run-tests () (let ((*failures* 0)) (dolist (test (reverse *tests*)) (format t "~&~A~%" test) (handler-case (funcall test) (error (e) (incf *failures*) (format t " ERROR: ~A~%" e)))) (format t "~&~D test~:P, ~D failure~:P~%" (length *tests*) *failures*) (zerop *failures*))) (defun call-with-temp-dir (fn) (let ((dir (uiop:ensure-directory-pathname (format nil "~Awallpaper-switcher-test-~36R/" (namestring (uiop:temporary-directory)) (random (expt 36 8)))))) (ensure-directories-exist dir) (unwind-protect (funcall fn dir) (uiop:delete-directory-tree dir :validate t)))) (defmacro with-temp-dir ((var) &body body) `(call-with-temp-dir (lambda (,var) ,@body))) (defun touch (dir name) (with-open-file (s (merge-pathnames (uiop:parse-native-namestring name) dir) :direction :output :if-does-not-exist :create) (declare (ignorable s)))) ;;; Tests (deftest test-shell-quote (check-equal "'plain'" (shell-quote "plain")) (check-equal "'with space'" (shell-quote "with space")) (check-equal "'it'\\''s'" (shell-quote "it's")) (check-equal "''" (shell-quote ""))) (deftest test-xml-escape (check-equal "a & b <c> "d" 'e'" (xml-escape "a & b \"d\" 'e'"))) (deftest test-menu-label (check-equal "sunset" (menu-label #p"/x/sunset.png")) (check-equal "my__wall" (menu-label #p"/x/my_wall.webp"))) (deftest test-expand-path (let ((home (string-right-trim "/" (namestring (user-homedir-pathname))))) (check-equal (concatenate 'string home "/.wallpapers") (expand-path "~/.wallpapers")) (check-equal (concatenate 'string home "/pics") (expand-path "$HOME/pics")) (check-equal (concatenate 'string home "/pics") (expand-path "${HOME}/pics")) (check-equal "/abs/path" (expand-path "/abs/path")))) (deftest test-parse-config (let ((c (parse-config-string "# comment directory = /srv/walls command = feh --bg-fill %f extensions = .PNG, jpg webp"))) (check-equal "/srv/walls" (config-directory c)) (check-equal "feh --bg-fill %f" (config-command c)) (check-equal '("png" "jpg" "webp") (config-extensions c)))) (deftest test-parse-config-warns-on-unknown-key (let ((warned nil)) (handler-bind ((warning (lambda (w) (setf warned t) (muffle-warning w)))) (parse-config-string "colour = blue")) (check warned))) (deftest test-config-defaults (let ((c (default-config))) (check-equal "~/.wallpapers" (config-directory c)) (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 (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) (dolist (n '("b.webp" "A.png" "c.PNG" "notes.txt" "noext")) (touch dir n)) (ensure-directories-exist (merge-pathnames "sub/" dir)) (touch (merge-pathnames "sub/" dir) "nested.png") (check-equal '("A.png" "b.webp" "c.PNG") (mapcar #'file-namestring (find-wallpapers (namestring dir) '("png" "webp")))))) (deftest test-find-wallpapers-missing-dir (check-equal nil (find-wallpapers "/nonexistent/wallpaper-switcher/" '("png")))) (deftest test-render-menu (with-temp-dir (dir) (touch dir "tom & jerry.png") (let ((xml (with-output-to-string (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 "'/bin/ws' '--set' '" xml)) (check (search "tom & jerry.png'" xml)) (check (search "" xml))))) (deftest test-render-menu-empty (with-temp-dir (dir) (let ((xml (with-output-to-string (s) (render-menu (make-config :directory (namestring dir)) s)))) (check (search "" xml :from-end t))) (check (not (search "No wallpapers" xml)))))) (deftest test-render-empty-subdirectory (with-temp-dir (dir) (touch dir "top.png") (let ((empty (subdir dir "empty"))) (touch empty "readme.txt") (let ((xml (render-to-string dir))) (check (search "label=\"empty\"" xml)) (check (search (format nil "" (uiop:native-namestring empty)) xml)))))) (deftest test-render-root-with-only-subdirectories (with-temp-dir (dir) (touch (subdir dir "only") "a.png") (let ((xml (render-to-string dir))) (check (search "label=\"a\"" xml)) (check (not (search "No wallpapers" xml)))))) (deftest test-render-skips-hidden-directories (with-temp-dir (dir) (touch (subdir dir ".thumbnails") "a.png") (let ((xml (render-to-string dir))) (check (not (search "thumbnails" xml))) (check (search "No wallpapers in" xml))))) (deftest test-render-symlink-loop (with-temp-dir (dir) (let ((sub (subdir dir "sub"))) (touch sub "a.png") (uiop:run-program (list "ln" "-s" (uiop:native-namestring dir) (uiop:native-namestring (merge-pathnames "loop" sub)))) (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))))))