ftw.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. ;;;; ftw.test --- exercise ice-9/ftw.scm -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright 2006, 2011, 2012 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)
  48. (stat:dev! s dev)
  49. (stat:ino! s ino)
  50. (visited? s))
  51. (pass-if "0 0 - 1st" (eq? #f (try-visited? 0 0)))
  52. (pass-if "0 0 - 2nd" (eq? #t (try-visited? 0 0)))
  53. (pass-if "0 0 - 3rd" (eq? #t (try-visited? 0 0)))
  54. (pass-if "0 1" (eq? #f (try-visited? 0 1)))
  55. (pass-if "0 2" (eq? #f (try-visited? 0 2)))
  56. (pass-if "0 3" (eq? #f (try-visited? 0 3)))
  57. (pass-if "5 5" (eq? #f (try-visited? 5 5)))
  58. (pass-if "5 7" (eq? #f (try-visited? 5 7)))
  59. (pass-if "7 5" (eq? #f (try-visited? 7 5)))
  60. (pass-if "7 7" (eq? #f (try-visited? 7 7)))
  61. (pass-if "5 5 - 2nd" (eq? #t (try-visited? 5 5)))
  62. (pass-if "5 7 - 2nd" (eq? #t (try-visited? 5 7)))
  63. (pass-if "7 5 - 2nd" (eq? #t (try-visited? 7 5)))
  64. (pass-if "7 7 - 2nd" (eq? #t (try-visited? 7 7)))))
  65. ;;;
  66. ;;; `file-system-fold' & co.
  67. ;;;
  68. (define %top-builddir
  69. (canonicalize-path (getcwd)))
  70. (define %top-srcdir
  71. (assq-ref %guile-build-info 'top_srcdir))
  72. (define %test-dir
  73. (string-append %top-srcdir "/test-suite"))
  74. (define %test-suite-lib-dir
  75. (string-append %top-srcdir "/test-suite/test-suite"))
  76. (define (make-file-tree dir tree)
  77. "Make file system TREE at DIR."
  78. (define (touch file)
  79. (call-with-output-file file
  80. (cut display "" <>)))
  81. (let loop ((dir dir)
  82. (tree tree))
  83. (define (scope file)
  84. (string-append dir "/" file))
  85. (match tree
  86. (('directory name (body ...))
  87. (mkdir (scope name))
  88. (for-each (cute loop (scope name) <>) body))
  89. (('directory name (? integer? mode) (body ...))
  90. (mkdir (scope name))
  91. (for-each (cute loop (scope name) <>) body)
  92. (chmod (scope name) mode))
  93. ((file)
  94. (touch (scope file)))
  95. ((file (? integer? mode))
  96. (touch (scope file))
  97. (chmod (scope file) mode))
  98. ((from '-> to)
  99. (symlink to (scope from))))))
  100. (define (delete-file-tree dir tree)
  101. "Delete file TREE from DIR."
  102. (let loop ((dir dir)
  103. (tree tree))
  104. (define (scope file)
  105. (string-append dir "/" file))
  106. (match tree
  107. (('directory name (body ...))
  108. (for-each (cute loop (scope name) <>) body)
  109. (rmdir (scope name)))
  110. (('directory name (? integer? mode) (body ...))
  111. (chmod (scope name) #o755) ; make sure it can be entered
  112. (for-each (cute loop (scope name) <>) body)
  113. (rmdir (scope name)))
  114. ((from '-> _)
  115. (delete-file (scope from)))
  116. ((file _ ...)
  117. (delete-file (scope file))))))
  118. (define-syntax-rule (with-file-tree dir tree body ...)
  119. (dynamic-wind
  120. (lambda ()
  121. (make-file-tree dir tree))
  122. (lambda ()
  123. body ...)
  124. (lambda ()
  125. (delete-file-tree dir tree))))
  126. (with-test-prefix "file-system-fold"
  127. (pass-if "test-suite"
  128. (let ((enter? (lambda (n s r)
  129. ;; Enter only `test-suite/tests/'.
  130. (if (member `(down ,%test-dir) r)
  131. (or (string=? (basename n) "tests")
  132. (string=? (basename n) "test-suite"))
  133. (string=? (basename n) "test-suite"))))
  134. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  135. (down (lambda (n s r) (cons `(down ,n) r)))
  136. (up (lambda (n s r) (cons `(up ,n) r)))
  137. (skip (lambda (n s r) (cons `(skip ,n) r)))
  138. (error (lambda (n s e r) (cons `(error ,n) r))))
  139. (define seq
  140. (reverse
  141. (file-system-fold enter? leaf down up skip error '() %test-dir)))
  142. (match seq
  143. ((('down (? (cut string=? <> %test-dir)))
  144. between ...
  145. ('up (? (cut string=? <> %test-dir))))
  146. (and (any (match-lambda (('down (= basename "test-suite")) #t) (_ #f))
  147. between)
  148. (any (match-lambda (('down (= basename "tests")) #t) (_ #f))
  149. between)
  150. (any (match-lambda (('leaf (= basename "alist.test")) #t) (_ #f))
  151. between)
  152. (any (match-lambda (('up (= basename "tests")) #t) (_ #f))
  153. between)
  154. (any (match-lambda (('skip (= basename "vm")) #t) (_ #f))
  155. between))))))
  156. (pass-if-equal "test-suite (never enter)"
  157. `((skip ,%test-dir))
  158. (let ((enter? (lambda (n s r) #f))
  159. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  160. (down (lambda (n s r) (cons `(down ,n) r)))
  161. (up (lambda (n s r) (cons `(up ,n) r)))
  162. (skip (lambda (n s r) (cons `(skip ,n) r)))
  163. (error (lambda (n s e r) (cons `(error ,n) r))))
  164. (file-system-fold enter? leaf down up skip error '() %test-dir)))
  165. (let ((name (string-append %test-suite-lib-dir "/lib.scm")))
  166. (pass-if-equal "test-suite/lib.scm (flat file)"
  167. `((leaf ,name))
  168. (let ((enter? (lambda (n s r) #t))
  169. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  170. (down (lambda (n s r) (cons `(down ,n) r)))
  171. (up (lambda (n s r) (cons `(up ,n) r)))
  172. (skip (lambda (n s r) (cons `(skip ,n) r)))
  173. (error (lambda (n s e r) (cons `(error ,n) r))))
  174. (file-system-fold enter? leaf down up skip error '() name))))
  175. (pass-if "ENOENT"
  176. (let ((enter? (lambda (n s r) #t))
  177. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  178. (down (lambda (n s r) (cons `(down ,n) r)))
  179. (up (lambda (n s r) (cons `(up ,n) r)))
  180. (skip (lambda (n s r) (cons `(skip ,n) r)))
  181. (error (lambda (n s e r) (cons `(error ,n ,e) r)))
  182. (name "/.does-not-exist."))
  183. (equal? (file-system-fold enter? leaf down up skip error '() name)
  184. `((error ,name ,ENOENT)))))
  185. (let ((name (string-append %top-builddir "/test-EACCES")))
  186. (pass-if-equal "EACCES"
  187. `((error ,name ,EACCES))
  188. (if (zero? (getuid))
  189. ;; When run as root, this test would fail because root can
  190. ;; list the contents of #o000 directories.
  191. (throw 'unresolved)
  192. (with-file-tree %top-builddir '(directory "test-EACCES" #o000
  193. (("a") ("b")))
  194. (let ((enter? (lambda (n s r) #t))
  195. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  196. (down (lambda (n s r) (cons `(down ,n) r)))
  197. (up (lambda (n s r) (cons `(up ,n) r)))
  198. (skip (lambda (n s r) (cons `(skip ,n) r)))
  199. (error (lambda (n s e r) (cons `(error ,n ,e) r))))
  200. (file-system-fold enter? leaf down up skip error '() name))))))
  201. (pass-if "dangling symlink and lstat"
  202. (with-file-tree %top-builddir '(directory "test-dangling"
  203. (("dangling" -> "xxx")))
  204. (let ((enter? (lambda (n s r) #t))
  205. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  206. (down (lambda (n s r) (cons `(down ,n) r)))
  207. (up (lambda (n s r) (cons `(up ,n) r)))
  208. (skip (lambda (n s r) (cons `(skip ,n) r)))
  209. (error (lambda (n s e r) (cons `(error ,n ,e) r)))
  210. (name (string-append %top-builddir "/test-dangling")))
  211. (equal? (file-system-fold enter? leaf down up skip error '()
  212. name)
  213. `((up ,name)
  214. (leaf ,(string-append name "/dangling"))
  215. (down ,name))))))
  216. (pass-if "dangling symlink and stat"
  217. ;; Same as above, but using `stat' instead of `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 stat)
  229. `((up ,name)
  230. (error ,(string-append name "/dangling") ,ENOENT)
  231. (down ,name)))))))
  232. (with-test-prefix "file-system-tree"
  233. (pass-if "test-suite (never enter)"
  234. (match (file-system-tree %test-dir (lambda (n s) #f))
  235. (("test-suite" (= stat:type 'directory)) ; no children
  236. #t)))
  237. (pass-if "test-suite/*"
  238. (match (file-system-tree %test-dir (lambda (n s)
  239. (string=? n %test-dir)))
  240. (("test-suite" (= stat:type 'directory) children ...)
  241. (any (match-lambda
  242. (("tests" (= stat:type 'directory)) ; no children
  243. #t)
  244. (_ #f))
  245. children))))
  246. (pass-if "test-suite (recursive)"
  247. (match (file-system-tree %test-dir)
  248. (("test-suite" (= stat:type 'directory) children ...)
  249. (any (match-lambda
  250. (("tests" (= stat:type 'directory) (= car files) ...)
  251. (let ((expected '("alist.test" "bytevectors.test"
  252. "ftw.test" "gc.test" "vlist.test")))
  253. (lset= string=?
  254. (lset-intersection string=? files expected)
  255. expected)))
  256. (_ #f))
  257. children))))
  258. (pass-if "ENOENT"
  259. (not (file-system-tree "/.does-not-exist."))))
  260. (with-test-prefix "scandir"
  261. (pass-if "top-srcdir"
  262. (let ((valid? (negate (cut string-any #\/ <>))))
  263. (match (scandir %top-srcdir)
  264. (((? valid? files) ...)
  265. ;; Both subdirs and files must be included.
  266. (let ((expected '("libguile" "README" "COPYING"
  267. "test-suite" "Makefile.am"
  268. "." "..")))
  269. (lset= string=?
  270. (lset-intersection string=? files expected)
  271. expected))))))
  272. (pass-if "test-suite"
  273. (let ((select? (cut string-suffix? ".test" <>)))
  274. (match (scandir (string-append %test-dir "/tests") select?)
  275. (("00-initial-env.test" (? select?) ...)
  276. #t))))
  277. (pass-if "flat file"
  278. (not (scandir (string-append %test-dir "/Makefile.am"))))
  279. (pass-if "EACCES"
  280. (not (scandir "/.does-not-exist.")))
  281. (pass-if "no select"
  282. (null? (scandir %test-dir (lambda (_) #f))))
  283. ;; In Guile up to 2.0.6, this would return ("." ".." "link-to-dir").
  284. (pass-if-equal "symlink to directory"
  285. '("." ".." "link-to-dir" "subdir")
  286. (with-file-tree %top-builddir '(directory "test-scandir-symlink"
  287. (("link-to-dir" -> "subdir")
  288. (directory "subdir"
  289. (("a")))))
  290. (let ((name (string-append %top-builddir "/test-scandir-symlink")))
  291. (scandir name)))))
  292. ;;; Local Variables:
  293. ;;; eval: (put 'with-file-tree 'scheme-indent-function 2)
  294. ;;; End: