Debian Codegraph @

test-suite/tests/ftw.test

raw
1 ;;;; ftw.test --- exercise ice-9/ftw.scm -*- scheme -*-
2 ;;;;
3 ;;;; Copyright 2006, 2011, 2012, 2018 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
9 ;;;;
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 (define-module (test-suite test-ice-9-ftw)
20 #:use-module (test-suite lib)
21 #:use-module (ice-9 ftw)
22 #:use-module (ice-9 match)
23 #:use-module (srfi srfi-1)
24 #:use-module (srfi srfi-26))
25
26 (define mingw?
27 (string-contains %host-type "-mingw32"))
28
29 ;; the procedure-source checks here ensure the vector indexes we write match
30 ;; what ice-9/posix.scm stat:dev and stat:ino do (which in turn match
31 ;; libguile/filesys.c of course)
32
33 (define (stat:dev! st dev)
34 (vector-set! st 0 dev))
35 (define (stat:ino! st ino)
36 (vector-set! st 1 ino))
37
38 (let* ((s (stat "/"))
39 (i (stat:ino s))
40 (d (stat:dev s)))
41 (stat:ino! s (1+ i))
42 (stat:dev! s (1+ d))
43 (if (not (and (= (stat:ino s) (1+ i))
44 (= (stat:dev s) (1+ d))))
45 (error "unexpected definitions of stat:dev and stat:ino")))
46
47 ;;
48 ;; visited?-proc
49 ;;
50
51 (with-test-prefix "visited?-proc"
52
53 ;; normally internal-only
54 (let* ((visited?-proc (@@ (ice-9 ftw) visited?-proc))
55 (visited? (visited?-proc 97))
56 (s (stat "/")))
57
58 (define (try-visited? dev ino fname)
59 (stat:dev! s dev)
60 (stat:ino! s ino)
61 (visited? s fname))
62
63 (with-test-prefix "valid inodes"
64
65 (pass-if "0 1 - 1st" (eq? #f (try-visited? 0 1 "0.1")))
66 (pass-if "0 1 - 2nd" (eq? #t (try-visited? 0 1 "0.1")))
67 (pass-if "0 1 - 3rd" (eq? #t (try-visited? 0 1 "0.1")))
68
69 (pass-if "0 2" (eq? #f (try-visited? 0 2 "0.2")))
70 (pass-if "0 3" (eq? #f (try-visited? 0 3 "0.3")))
71 (pass-if "0 4" (eq? #f (try-visited? 0 4 "0.4")))
72
73 (pass-if "5 5" (eq? #f (try-visited? 5 5 "5.5")))
74 (pass-if "5 7" (eq? #f (try-visited? 5 7 "5.7")))
75 (pass-if "7 5" (eq? #f (try-visited? 7 5 "7.5")))
76 (pass-if "7 7" (eq? #f (try-visited? 7 7 "7.7")))
77
78 (pass-if "5 5 - 2nd" (eq? #t (try-visited? 5 5 "5.5")))
79 (pass-if "5 7 - 2nd" (eq? #t (try-visited? 5 7 "5.7")))
80 (pass-if "7 5 - 2nd" (eq? #t (try-visited? 7 5 "7.5")))
81 (pass-if "7 7 - 2nd" (eq? #t (try-visited? 7 7 "7.7"))))
82
83 (with-test-prefix "broken inodes"
84 (pass-if "0 1 - 1st" (eq? #f (try-visited? 0 0 "0.1")))
85 (pass-if "0 1 - 2nd" (eq? #t (try-visited? 0 0 "0.1")))
86 (pass-if "0 1 - 3rd" (eq? #t (try-visited? 0 0 "0.1")))
87
88 (pass-if "0 2" (eq? #f (try-visited? 0 0 "0.2")))
89 (pass-if "0 3" (eq? #f (try-visited? 0 0 "0.3")))
90 (pass-if "0 4" (eq? #f (try-visited? 0 0 "0.4")))
91
92 (pass-if "5 5" (eq? #f (try-visited? 5 0 "5.5")))
93 (pass-if "5 7" (eq? #f (try-visited? 5 0 "5.7")))
94 (pass-if "7 5" (eq? #f (try-visited? 7 0 "7.5")))
95 (pass-if "7 7" (eq? #f (try-visited? 7 0 "7.7")))
96
97 (pass-if "5 5 - 2nd" (eq? #t (try-visited? 5 0 "5.5")))
98 (pass-if "5 7 - 2nd" (eq? #t (try-visited? 5 0 "5.7")))
99 (pass-if "7 5 - 2nd" (eq? #t (try-visited? 7 0 "7.5")))
100 (pass-if "7 7 - 2nd" (eq? #t (try-visited? 7 0 "7.7"))))))
101
102
103 ;;;
104 ;;; `file-system-fold' & co.
105 ;;;
106
107 (define %top-builddir
108 (canonicalize-path (getcwd)))
109
110 (define %top-srcdir
111 (canonicalize-path (assq-ref %guile-build-info 'top_srcdir)))
112
113 (define %test-dir
114 (string-append %top-srcdir "/test-suite"))
115
116 (define %test-suite-lib-dir
117 (string-append %top-srcdir "/test-suite/test-suite"))
118
119 (define (make-file-tree dir tree)
120 "Make file system TREE at DIR."
121 (define (touch file)
122 (call-with-output-file file
123 (cut display "" <>)))
124
125 (let loop ((dir dir)
126 (tree tree))
127 (define (scope file)
128 (string-append dir "/" file))
129
130 (match tree
131 (('directory name (body ...))
132 (mkdir (scope name))
133 (for-each (cute loop (scope name) <>) body))
134 (('directory name (? integer? mode) (body ...))
135 (mkdir (scope name))
136 (for-each (cute loop (scope name) <>) body)
137 (chmod (scope name) mode))
138 ((file)
139 (touch (scope file)))
140 ((file (? integer? mode))
141 (touch (scope file))
142 (chmod (scope file) mode))
143 ((from '-> to)
144 (symlink to (scope from))))))
145
146 (define (delete-file-tree dir tree)
147 "Delete file TREE from DIR."
148 (let loop ((dir dir)
149 (tree tree))
150 (define (scope file)
151 (string-append dir "/" file))
152
153 (match tree
154 (('directory name (body ...))
155 (for-each (cute loop (scope name) <>) body)
156 (rmdir (scope name)))
157 (('directory name (? integer? mode) (body ...))
158 (chmod (scope name) #o755) ; make sure it can be entered
159 (for-each (cute loop (scope name) <>) body)
160 (rmdir (scope name)))
161 ((from '-> _)
162 (delete-file (scope from)))
163 ((file _ ...)
164 (delete-file (scope file))))))
165
166 (define-syntax-rule (with-file-tree dir tree body ...)
167 (dynamic-wind
168 (lambda ()
169 (make-file-tree dir tree))
170 (lambda ()
171 body ...)
172 (lambda ()
173 (delete-file-tree dir tree))))
174
175 (with-test-prefix "file-system-fold"
176
177 (pass-if "test-suite"
178 (let ((enter? (lambda (n s r)
179 ;; Enter only `test-suite/tests/'.
180 (if (member `(down ,%test-dir) r)
181 (or (string=? (basename n) "tests")
182 (string=? (basename n) "test-suite"))
183 (string=? (basename n) "test-suite"))))
184 (leaf (lambda (n s r) (cons `(leaf ,n) r)))
185 (down (lambda (n s r) (cons `(down ,n) r)))
186 (up (lambda (n s r) (cons `(up ,n) r)))
187 (skip (lambda (n s r) (cons `(skip ,n) r)))
188 (error (lambda (n s e r) (cons `(error ,n) r))))
189 (define seq
190 (reverse
191 (file-system-fold enter? leaf down up skip error '() %test-dir)))
192
193 (match seq
194 ((('down (? (cut string=? <> %test-dir)))
195 between ...
196 ('up (? (cut string=? <> %test-dir))))
197 (and (any (match-lambda (('down (= basename "test-suite")) #t) (_ #f))
198 between)
199 (any (match-lambda (('down (= basename "tests")) #t) (_ #f))
200 between)
201 (any (match-lambda (('leaf (= basename "alist.test")) #t) (_ #f))
202 between)
203 (any (match-lambda (('up (= basename "tests")) #t) (_ #f))
204 between)
205 (any (match-lambda (('skip (= basename "standalone")) #t) (_ #f))
206 between))))))
207
208 (pass-if-equal "test-suite (never enter)"
209 `((skip ,%test-dir))
210 (let ((enter? (lambda (n s r) #f))
211 (leaf (lambda (n s r) (cons `(leaf ,n) r)))
212 (down (lambda (n s r) (cons `(down ,n) r)))
213 (up (lambda (n s r) (cons `(up ,n) r)))
214 (skip (lambda (n s r) (cons `(skip ,n) r)))
215 (error (lambda (n s e r) (cons `(error ,n) r))))
216 (file-system-fold enter? leaf down up skip error '() %test-dir)))
217
218 (let ((name (string-append %test-suite-lib-dir "/lib.scm")))
219 (pass-if-equal "test-suite/lib.scm (flat file)"
220 `((leaf ,name))
221 (let ((enter? (lambda (n s r) #t))
222 (leaf (lambda (n s r) (cons `(leaf ,n) r)))
223 (down (lambda (n s r) (cons `(down ,n) r)))
224 (up (lambda (n s r) (cons `(up ,n) r)))
225 (skip (lambda (n s r) (cons `(skip ,n) r)))
226 (error (lambda (n s e r) (cons `(error ,n) r))))
227 (file-system-fold enter? leaf down up skip error '() name))))
228
229 (pass-if "ENOENT"
230 (let ((enter? (lambda (n s r) #t))
231 (leaf (lambda (n s r) (cons `(leaf ,n) r)))
232 (down (lambda (n s r) (cons `(down ,n) r)))
233 (up (lambda (n s r) (cons `(up ,n) r)))
234 (skip (lambda (n s r) (cons `(skip ,n) r)))
235 (error (lambda (n s e r) (cons `(error ,n ,e) r)))
236 (name "/.does-not-exist."))
237 (equal? (file-system-fold enter? leaf down up skip error '() name)
238 `((error ,name ,ENOENT)))))
239
240 (let ((name (string-append %top-builddir "/test-EACCES")))
241 (pass-if-equal "EACCES"
242 `((error ,name ,EACCES))
243 (if (or (and (defined? 'getuid) (zero? (getuid)))
244 ;; When run as root, this test would fail because root can
245 ;; list the contents of #o000 directories.
246 mingw?
247 ;; MinGW uses ACLs for directory control, which
248 ;; chmod doesn't emulate.
249 )
250 (throw 'unresolved)
251 (with-file-tree %top-builddir '(directory "test-EACCES" #o000
252 (("a") ("b")))
253 (let ((enter? (lambda (n s r) #t))
254 (leaf (lambda (n s r) (cons `(leaf ,n) r)))
255 (down (lambda (n s r) (cons `(down ,n) r)))
256 (up (lambda (n s r) (cons `(up ,n) r)))
257 (skip (lambda (n s r) (cons `(skip ,n) r)))
258 (error (lambda (n s e r) (cons `(error ,n ,e) r))))
259 (file-system-fold enter? leaf down up skip error '() name))))))
260
261 (pass-if "dangling symlink and lstat"
262 (if (not (defined? 'symlink))
263 (throw 'unresolved)
264 (with-file-tree %top-builddir '(directory "test-dangling"
265 (("dangling" -> "xxx")))
266 (let ((enter? (lambda (n s r) #t))
267 (leaf (lambda (n s r) (cons `(leaf ,n) r)))
268 (down (lambda (n s r) (cons `(down ,n) r)))
269 (up (lambda (n s r) (cons `(up ,n) r)))
270 (skip (lambda (n s r) (cons `(skip ,n) r)))
271 (error (lambda (n s e r) (cons `(error ,n ,e) r)))
272 (name (string-append %top-builddir "/test-dangling")))
273 (equal? (file-system-fold enter? leaf down up skip error '()
274 name)
275 `((up ,name)
276 (leaf ,(string-append name "/dangling"))
277 (down ,name)))))))
278
279 (pass-if "dangling symlink and stat"
280 ;; Same as above, but using `stat' instead of `lstat'.
281 (if (not (defined? 'symlink))
282 (throw 'unresolved)
283 (with-file-tree %top-builddir '(directory "test-dangling"
284 (("dangling" -> "xxx")))
285 (let ((enter? (lambda (n s r) #t))
286 (leaf (lambda (n s r) (cons `(leaf ,n) r)))
287 (down (lambda (n s r) (cons `(down ,n) r)))
288 (up (lambda (n s r) (cons `(up ,n) r)))
289 (skip (lambda (n s r) (cons `(skip ,n) r)))
290 (error (lambda (n s e r) (cons `(error ,n ,e) r)))
291 (name (string-append %top-builddir "/test-dangling")))
292 (equal? (file-system-fold enter? leaf down up skip error '()
293 name stat)
294 `((up ,name)
295 (error ,(string-append name "/dangling") ,ENOENT)
296 (down ,name))))))))
297
298 (with-test-prefix "file-system-tree"
299
300 (pass-if "test-suite (never enter)"
301 (match (file-system-tree %test-dir (lambda (n s) #f))
302 (("test-suite" (= stat:type 'directory)) ; no children
303 #t)))
304
305 (pass-if "test-suite/*"
306 (match (file-system-tree %test-dir (lambda (n s)
307 (string=? n %test-dir)))
308 (("test-suite" (= stat:type 'directory) children ...)
309 (any (match-lambda
310 (("tests" (= stat:type 'directory)) ; no children
311 #t)
312 (_ #f))
313 children))))
314
315 (pass-if "test-suite (recursive)"
316 (match (file-system-tree %test-dir)
317 (("test-suite" (= stat:type 'directory) children ...)
318 (any (match-lambda
319 (("tests" (= stat:type 'directory) (= car files) ...)
320 (let ((expected '("alist.test" "bytevectors.test"
321 "ftw.test" "gc.test" "vlist.test")))
322 (lset= string=?
323 (lset-intersection string=? files expected)
324 expected)))
325 (_ #f))
326 children))))
327
328 (pass-if "ENOENT"
329 (not (file-system-tree "/.does-not-exist."))))
330
331 (with-test-prefix "scandir"
332
333 (pass-if "top-srcdir"
334 (let ((valid? (negate (cut string-any #\/ <>))))
335 (match (scandir %top-srcdir)
336 (((? valid? files) ...)
337 ;; Both subdirs and files must be included.
338 (let ((expected '("libguile" "README" "COPYING"
339 "test-suite" "Makefile.am"
340 "." "..")))
341 (lset= string=?
342 (lset-intersection string=? files expected)
343 expected))))))
344
345 (pass-if "test-suite"
346 (let ((select? (cut string-suffix? ".test" <>)))
347 (match (scandir (string-append %test-dir "/tests") select?)
348 (("00-initial-env.test" (? select?) ...)
349 #t))))
350
351 (pass-if "flat file"
352 (not (scandir (string-append %test-dir "/Makefile.am"))))
353
354 (pass-if "EACCES"
355 (not (scandir "/.does-not-exist.")))
356
357 (pass-if "no select"
358 (null? (scandir %test-dir (lambda (_) #f))))
359
360 ;; In Guile up to 2.0.6, this would return ("." ".." "link-to-dir").
361 (pass-if-equal "symlink to directory"
362 '("." ".." "link-to-dir" "subdir")
363 (if (not (defined? 'symlink))
364 (throw 'unresolved)
365 (with-file-tree %top-builddir '(directory "test-scandir-symlink"
366 (("link-to-dir" -> "subdir")
367 (directory "subdir"
368 (("a")))))
369 (let ((name (string-append %top-builddir "/test-scandir-symlink")))
370 (scandir name))))))
371
372 ;;; Local Variables:
373 ;;; eval: (put 'with-file-tree 'scheme-indent-function 2)
374 ;;; End: