1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
;;; 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 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 <c> \"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-build-command
(check-equal "sh -c 'swaybg -i '\\''/w/a b.png'\\'' 100%'"
(build-command "swaybg -i %f 100%%" #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)
(render-menu (make-config :directory (namestring dir)
:command "swaybg -i %f")
s))))
(check (search "<openbox_pipe_menu>" xml))
(check (search "label=\"tom & jerry\"" xml))
(check (search "<action name=\"Execute\"><command>sh -c 'swaybg -i '\\''"
xml))
(check (search "</openbox_pipe_menu>" 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 "<separator label=\"No wallpapers in" xml))
(check (not (search "<item" xml))))))
|