aboutsummaryrefslogtreecommitdiff
path: root/t/tests.lisp
blob: 4fc0665b8fa2ae0bfbf59566dbb3821dc9c8f20a (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
;;; 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 <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-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 "<openbox_pipe_menu>" xml))
      (check (search "label=\"tom &amp; jerry\"" xml))
      (check (search "<action name=\"Execute\"><command>&apos;/bin/ws&apos; &apos;--set&apos; &apos;"
                     xml))
      (check (search "tom &amp; jerry.png&apos;</command>" 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)))))))

;;; 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))))))