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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
;;; 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))))))
(defun subdir (dir name)
(ensure-directories-exist (merge-pathnames (format nil "~A/" name) dir)))
(defun render-to-string (dir)
(with-output-to-string (s)
(render-menu (make-config :directory (namestring dir)) s)))
(deftest test-find-subdirectories
(with-temp-dir (dir)
(dolist (n '("zeta" "Alpha" ".hidden" "beta"))
(subdir dir n))
(touch dir "file.png")
(check-equal '("Alpha" "beta" "zeta")
(mapcar (lambda (d) (car (last (pathname-directory d))))
(find-subdirectories (namestring dir))))))
(deftest test-render-submenus
(with-temp-dir (dir)
(touch dir "top.png")
(touch (subdir dir "nature") "forest.webp")
(touch (subdir dir "nature/deep_sea") "reef.png")
(let ((xml (render-to-string dir)))
(check (search "<menu id=\"wallpaper-switcher:" xml))
(check (search "label=\"nature\"" xml))
(check (search "label=\"deep__sea\"" xml))
(check (search "label=\"forest\"" xml))
(check (search "label=\"reef\"" xml))
;; Directories come before files, and nesting is preserved.
(check (< (search "label=\"nature\"" xml) (search "label=\"top\"" xml)))
(check (< (search "label=\"deep__sea\"" xml)
(search "label=\"forest\"" xml)
(search "</menu>" 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 "<separator label=\"No wallpapers in ~A\"/>"
(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)))))))
|