ftw.test 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. (define-module (test-suite test-ice-9-ftw)
  19. #:use-module (test-suite lib)
  20. #:use-module (ice-9 ftw)
  21. #:use-module (ice-9 match)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-26))
  24. ;; the procedure-source checks here ensure the vector indexes we write match
  25. ;; what ice-9/posix.scm stat:dev and stat:ino do (which in turn match
  26. ;; libguile/filesys.c of course)
  27. (define (stat:dev! st dev)
  28. (vector-set! st 0 dev))
  29. (define (stat:ino! st ino)
  30. (vector-set! st 1 ino))
  31. (let* ((s (stat "/"))
  32. (i (stat:ino s))
  33. (d (stat:dev s)))
  34. (stat:ino! s (1+ i))
  35. (stat:dev! s (1+ d))
  36. (if (not (and (= (stat:ino s) (1+ i))
  37. (= (stat:dev s) (1+ d))))
  38. (error "unexpected definitions of stat:dev and stat:ino")))
  39. ;;
  40. ;; visited?-proc
  41. ;;
  42. (with-test-prefix "visited?-proc"
  43. ;; normally internal-only
  44. (let* ((visited?-proc (@@ (ice-9 ftw) visited?-proc))
  45. (visited? (visited?-proc 97))
  46. (s (stat "/")))
  47. (define (try-visited? dev ino fname)
  48. (stat:dev! s dev)
  49. (stat:ino! s ino)
  50. (visited? s fname))
  51. (with-test-prefix "valid inodes"
  52. (pass-if "0 1 - 1st" (eq? #f (try-visited? 0 1 "0.1")))
  53. (pass-if "0 1 - 2nd" (eq? #t (try-visited? 0 1 "0.1")))
  54. (pass-if "0 1 - 3rd" (eq? #t (try-visited? 0 1 "0.1")))
  55. (pass-if "0 2" (eq? #f (try-visited? 0 2 "0.2")))
  56. (pass-if "0 3" (eq? #f (try-visited? 0 3 "0.3")))
  57. (pass-if "0 4" (eq? #f (try-visited? 0 4 "0.4")))
  58. (pass-if "5 5" (eq? #f (try-visited? 5 5 "5.5")))
  59. (pass-if "5 7" (eq? #f (try-visited? 5 7 "5.7")))
  60. (pass-if "7 5" (eq? #f (try-visited? 7 5 "7.5")))
  61. (pass-if "7 7" (eq? #f (try-visited? 7 7 "7.7")))
  62. (pass-if "5 5 - 2nd" (eq? #t (try-visited? 5 5 "5.5")))
  63. (pass-if "5 7 - 2nd" (eq? #t (try-visited? 5 7 "5.7")))
  64. (pass-if "7 5 - 2nd" (eq? #t (try-visited? 7 5 "7.5")))
  65. (pass-if "7 7 - 2nd" (eq? #t (try-visited? 7 7 "7.7"))))
  66. (with-test-prefix "broken inodes"
  67. (pass-if "0 1 - 1st" (eq? #f (try-visited? 0 0 "0.1")))
  68. (pass-if "0 1 - 2nd" (eq? #t (try-visited? 0 0 "0.1")))
  69. (pass-if "0 1 - 3rd" (eq? #t (try-visited? 0 0 "0.1")))
  70. (pass-if "0 2" (eq? #f (try-visited? 0 0 "0.2")))
  71. (pass-if "0 3" (eq? #f (try-visited? 0 0 "0.3")))
  72. (pass-if "0 4" (eq? #f (try-visited? 0 0 "0.4")))
  73. (pass-if "5 5" (eq? #f (try-visited? 5 0 "5.5")))
  74. (pass-if "5 7" (eq? #f (try-visited? 5 0 "5.7")))
  75. (pass-if "7 5" (eq? #f (try-visited? 7 0 "7.5")))
  76. (pass-if "7 7" (eq? #f (try-visited? 7 0 "7.7")))
  77. (pass-if "5 5 - 2nd" (eq? #t (try-visited? 5 0 "5.5")))
  78. (pass-if "5 7 - 2nd" (eq? #t (try-visited? 5 0 "5.7")))
  79. (pass-if "7 5 - 2nd" (eq? #t (try-visited? 7 0 "7.5")))
  80. (pass-if "7 7 - 2nd" (eq? #t (try-visited? 7 0 "7.7"))))))
  81. ;;;
  82. ;;; `file-system-fold' & co.
  83. ;;;
  84. (define %top-builddir
  85. (canonicalize-path (getcwd)))
  86. (define %top-srcdir
  87. (canonicalize-path (assq-ref %guile-build-info 'top_srcdir)))
  88. (define %test-dir
  89. (string-append %top-srcdir "/test-suite"))
  90. (define %test-suite-lib-dir
  91. (string-append %top-srcdir "/test-suite/test-suite"))
  92. (define (make-file-tree dir tree)
  93. "Make file system TREE at DIR."
  94. (define (touch file)
  95. (call-with-output-file file
  96. (cut display "" <>)))
  97. (let loop ((dir dir)
  98. (tree tree))
  99. (define (scope file)
  100. (string-append dir "/" file))
  101. (match tree
  102. (('directory name (body ...))
  103. (mkdir (scope name))
  104. (for-each (cute loop (scope name) <>) body))
  105. (('directory name (? integer? mode) (body ...))
  106. (mkdir (scope name))
  107. (for-each (cute loop (scope name) <>) body)
  108. (chmod (scope name) mode))
  109. ((file)
  110. (touch (scope file)))
  111. ((file (? integer? mode))
  112. (touch (scope file))
  113. (chmod (scope file) mode))
  114. ((from '-> to)
  115. (symlink to (scope from))))))
  116. (define (delete-file-tree dir tree)
  117. "Delete file TREE from DIR."
  118. (let loop ((dir dir)
  119. (tree tree))
  120. (define (scope file)
  121. (string-append dir "/" file))
  122. (match tree
  123. (('directory name (body ...))
  124. (for-each (cute loop (scope name) <>) body)
  125. (rmdir (scope name)))
  126. (('directory name (? integer? mode) (body ...))
  127. (chmod (scope name) #o755) ; make sure it can be entered
  128. (for-each (cute loop (scope name) <>) body)
  129. (rmdir (scope name)))
  130. ((from '-> _)
  131. (delete-file (scope from)))
  132. ((file _ ...)
  133. (delete-file (scope file))))))
  134. (define-syntax-rule (with-file-tree dir tree body ...)
  135. (dynamic-wind
  136. (lambda ()
  137. (make-file-tree dir tree))
  138. (lambda ()
  139. body ...)
  140. (lambda ()
  141. (delete-file-tree dir tree))))
  142. (with-test-prefix "file-system-fold"
  143. (pass-if "test-suite"
  144. (let ((enter? (lambda (n s r)
  145. ;; Enter only `test-suite/tests/'.
  146. (if (member `(down ,%test-dir) r)
  147. (or (string=? (basename n) "tests")
  148. (string=? (basename n) "test-suite"))
  149. (string=? (basename n) "test-suite"))))
  150. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  151. (down (lambda (n s r) (cons `(down ,n) r)))
  152. (up (lambda (n s r) (cons `(up ,n) r)))
  153. (skip (lambda (n s r) (cons `(skip ,n) r)))
  154. (error (lambda (n s e r) (cons `(error ,n) r))))
  155. (define seq
  156. (reverse
  157. (file-system-fold enter? leaf down up skip error '() %test-dir)))
  158. (match seq
  159. ((('down (? (cut string=? <> %test-dir)))
  160. between ...
  161. ('up (? (cut string=? <> %test-dir))))
  162. (and (any (match-lambda (('down (= basename "test-suite")) #t) (_ #f))
  163. between)
  164. (any (match-lambda (('down (= basename "tests")) #t) (_ #f))
  165. between)
  166. (any (match-lambda (('leaf (= basename "alist.test")) #t) (_ #f))
  167. between)
  168. (any (match-lambda (('up (= basename "tests")) #t) (_ #f))
  169. between)
  170. (any (match-lambda (('skip (= basename "standalone")) #t) (_ #f))
  171. between))))))
  172. (pass-if-equal "test-suite (never enter)"
  173. `((skip ,%test-dir))
  174. (let ((enter? (lambda (n s r) #f))
  175. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  176. (down (lambda (n s r) (cons `(down ,n) r)))
  177. (up (lambda (n s r) (cons `(up ,n) r)))
  178. (skip (lambda (n s r) (cons `(skip ,n) r)))
  179. (error (lambda (n s e r) (cons `(error ,n) r))))
  180. (file-system-fold enter? leaf down up skip error '() %test-dir)))
  181. (let ((name (string-append %test-suite-lib-dir "/lib.scm")))
  182. (pass-if-equal "test-suite/lib.scm (flat file)"
  183. `((leaf ,name))
  184. (let ((enter? (lambda (n s r) #t))
  185. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  186. (down (lambda (n s r) (cons `(down ,n) r)))
  187. (up (lambda (n s r) (cons `(up ,n) r)))
  188. (skip (lambda (n s r) (cons `(skip ,n) r)))
  189. (error (lambda (n s e r) (cons `(error ,n) r))))
  190. (file-system-fold enter? leaf down up skip error '() name))))
  191. (pass-if "ENOENT"
  192. (let ((enter? (lambda (n s r) #t))
  193. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  194. (down (lambda (n s r) (cons `(down ,n) r)))
  195. (up (lambda (n s r) (cons `(up ,n) r)))
  196. (skip (lambda (n s r) (cons `(skip ,n) r)))
  197. (error (lambda (n s e r) (cons `(error ,n ,e) r)))
  198. (name "/.does-not-exist."))
  199. (equal? (file-system-fold enter? leaf down up skip error '() name)
  200. `((error ,name ,ENOENT)))))
  201. (let ((name (string-append %top-builddir "/test-EACCES")))
  202. (pass-if-equal "EACCES"
  203. `((error ,name ,EACCES))
  204. (if (and (defined? 'getuid) (zero? (getuid)))
  205. ;; When run as root, this test would fail because root can
  206. ;; list the contents of #o000 directories.
  207. (throw 'unresolved)
  208. (with-file-tree %top-builddir '(directory "test-EACCES" #o000
  209. (("a") ("b")))
  210. (let ((enter? (lambda (n s r) #t))
  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 ,e) r))))
  216. (file-system-fold enter? leaf down up skip error '() name))))))
  217. (pass-if "dangling symlink and lstat"
  218. (with-file-tree %top-builddir '(directory "test-dangling"
  219. (("dangling" -> "xxx")))
  220. (let ((enter? (lambda (n s r) #t))
  221. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  222. (down (lambda (n s r) (cons `(down ,n) r)))
  223. (up (lambda (n s r) (cons `(up ,n) r)))
  224. (skip (lambda (n s r) (cons `(skip ,n) r)))
  225. (error (lambda (n s e r) (cons `(error ,n ,e) r)))
  226. (name (string-append %top-builddir "/test-dangling")))
  227. (equal? (file-system-fold enter? leaf down up skip error '()
  228. name)
  229. `((up ,name)
  230. (leaf ,(string-append name "/dangling"))
  231. (down ,name))))))
  232. (pass-if "dangling symlink and stat"
  233. ;; Same as above, but using `stat' instead of `lstat'.
  234. (with-file-tree %top-builddir '(directory "test-dangling"
  235. (("dangling" -> "xxx")))
  236. (let ((enter? (lambda (n s r) #t))
  237. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  238. (down (lambda (n s r) (cons `(down ,n) r)))
  239. (up (lambda (n s r) (cons `(up ,n) r)))
  240. (skip (lambda (n s r) (cons `(skip ,n) r)))
  241. (error (lambda (n s e r) (cons `(error ,n ,e) r)))
  242. (name (string-append %top-builddir "/test-dangling")))
  243. (equal? (file-system-fold enter? leaf down up skip error '()
  244. name stat)
  245. `((up ,name)
  246. (error ,(string-append name "/dangling") ,ENOENT)
  247. (down ,name)))))))
  248. (with-test-prefix "file-system-tree"
  249. (pass-if "test-suite (never enter)"
  250. (match (file-system-tree %test-dir (lambda (n s) #f))
  251. (("test-suite" (= stat:type 'directory)) ; no children
  252. #t)))
  253. (pass-if "test-suite/*"
  254. (match (file-system-tree %test-dir (lambda (n s)
  255. (string=? n %test-dir)))
  256. (("test-suite" (= stat:type 'directory) children ...)
  257. (any (match-lambda
  258. (("tests" (= stat:type 'directory)) ; no children
  259. #t)
  260. (_ #f))
  261. children))))
  262. (pass-if "test-suite (recursive)"
  263. (match (file-system-tree %test-dir)
  264. (("test-suite" (= stat:type 'directory) children ...)
  265. (any (match-lambda
  266. (("tests" (= stat:type 'directory) (= car files) ...)
  267. (let ((expected '("alist.test" "bytevectors.test"
  268. "ftw.test" "gc.test" "vlist.test")))
  269. (lset= string=?
  270. (lset-intersection string=? files expected)
  271. expected)))
  272. (_ #f))
  273. children))))
  274. (pass-if "ENOENT"
  275. (not (file-system-tree "/.does-not-exist."))))
  276. (with-test-prefix "scandir"
  277. (pass-if "top-srcdir"
  278. (let ((valid? (negate (cut string-any #\/ <>))))
  279. (match (scandir %top-srcdir)
  280. (((? valid? files) ...)
  281. ;; Both subdirs and files must be included.
  282. (let ((expected '("libguile" "README" "COPYING"
  283. "test-suite" "Makefile.am"
  284. "." "..")))
  285. (lset= string=?
  286. (lset-intersection string=? files expected)
  287. expected))))))
  288. (pass-if "test-suite"
  289. (let ((select? (cut string-suffix? ".test" <>)))
  290. (match (scandir (string-append %test-dir "/tests") select?)
  291. (("00-initial-env.test" (? select?) ...)
  292. #t))))
  293. (pass-if "flat file"
  294. (not (scandir (string-append %test-dir "/Makefile.am"))))
  295. (pass-if "EACCES"
  296. (not (scandir "/.does-not-exist.")))
  297. (pass-if "no select"
  298. (null? (scandir %test-dir (lambda (_) #f))))
  299. ;; In Guile up to 2.0.6, this would return ("." ".." "link-to-dir").
  300. (pass-if-equal "symlink to directory"
  301. '("." ".." "link-to-dir" "subdir")
  302. (with-file-tree %top-builddir '(directory "test-scandir-symlink"
  303. (("link-to-dir" -> "subdir")
  304. (directory "subdir"
  305. (("a")))))
  306. (let ((name (string-append %top-builddir "/test-scandir-symlink")))
  307. (scandir name)))))
  308. ;;; Local Variables:
  309. ;;; eval: (put 'with-file-tree 'scheme-indent-function 2)
  310. ;;; End: