ftw.test 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 (make-file-tree dir tree)
  75. "Make file system TREE at DIR."
  76. (define (touch file)
  77. (call-with-output-file file
  78. (cut display "" <>)))
  79. (let loop ((dir dir)
  80. (tree tree))
  81. (define (scope file)
  82. (string-append dir "/" file))
  83. (match tree
  84. (('directory name (body ...))
  85. (mkdir (scope name))
  86. (for-each (cute loop (scope name) <>) body))
  87. (('directory name (? integer? mode) (body ...))
  88. (mkdir (scope name))
  89. (for-each (cute loop (scope name) <>) body)
  90. (chmod (scope name) mode))
  91. ((file)
  92. (touch (scope file)))
  93. ((file (? integer? mode))
  94. (touch (scope file))
  95. (chmod (scope file) mode))
  96. ((from '-> to)
  97. (symlink to (scope from))))))
  98. (define (delete-file-tree dir tree)
  99. "Delete file TREE from DIR."
  100. (let loop ((dir dir)
  101. (tree tree))
  102. (define (scope file)
  103. (string-append dir "/" file))
  104. (match tree
  105. (('directory name (body ...))
  106. (for-each (cute loop (scope name) <>) body)
  107. (rmdir (scope name)))
  108. (('directory name (? integer? mode) (body ...))
  109. (chmod (scope name) #o755) ; make sure it can be entered
  110. (for-each (cute loop (scope name) <>) body)
  111. (rmdir (scope name)))
  112. ((from '-> _)
  113. (delete-file (scope from)))
  114. ((file _ ...)
  115. (delete-file (scope file))))))
  116. (define-syntax-rule (with-file-tree dir tree body ...)
  117. (dynamic-wind
  118. (lambda ()
  119. (make-file-tree dir tree))
  120. (lambda ()
  121. body ...)
  122. (lambda ()
  123. (delete-file-tree dir tree))))
  124. (with-test-prefix "file-system-fold"
  125. (pass-if "test-suite"
  126. (let ((enter? (lambda (n s r)
  127. ;; Enter only `test-suite/tests/'.
  128. (if (member `(down ,%test-dir) r)
  129. (string=? (basename n) "tests")
  130. (string=? (basename n) "test-suite"))))
  131. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  132. (down (lambda (n s r) (cons `(down ,n) r)))
  133. (up (lambda (n s r) (cons `(up ,n) r)))
  134. (skip (lambda (n s r) (cons `(skip ,n) r)))
  135. (error (lambda (n s e r) (cons `(error ,n) r))))
  136. (define seq
  137. (reverse
  138. (file-system-fold enter? leaf down up skip error '() %test-dir)))
  139. (match seq
  140. ((('down (? (cut string=? <> %test-dir)))
  141. between ...
  142. ('up (? (cut string=? <> %test-dir))))
  143. (and (any (match-lambda (('leaf (= basename "lib.scm")) #t) (_ #f))
  144. between)
  145. (any (match-lambda (('down (= basename "tests")) #t) (_ #f))
  146. between)
  147. (any (match-lambda (('leaf (= basename "alist.test")) #t) (_ #f))
  148. between)
  149. (any (match-lambda (('up (= basename "tests")) #t) (_ #f))
  150. between)
  151. (any (match-lambda (('skip (= basename "vm")) #t) (_ #f))
  152. between))))))
  153. (pass-if "test-suite (never enter)"
  154. (let ((enter? (lambda (n s r) #f))
  155. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  156. (down (lambda (n s r) (cons `(down ,n) r)))
  157. (up (lambda (n s r) (cons `(up ,n) r)))
  158. (skip (lambda (n s r) (cons `(skip ,n) r)))
  159. (error (lambda (n s e r) (cons `(error ,n) r))))
  160. (equal? (file-system-fold enter? leaf down up skip error '() %test-dir)
  161. `((skip , %test-dir)))))
  162. (pass-if "test-suite/lib.scm (flat file)"
  163. (let ((enter? (lambda (n s r) #t))
  164. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  165. (down (lambda (n s r) (cons `(down ,n) r)))
  166. (up (lambda (n s r) (cons `(up ,n) r)))
  167. (skip (lambda (n s r) (cons `(skip ,n) r)))
  168. (error (lambda (n s e r) (cons `(error ,n) r)))
  169. (name (string-append %test-dir "/lib.scm")))
  170. (equal? (file-system-fold enter? leaf down up skip error '() name)
  171. `((leaf ,name)))))
  172. (pass-if "ENOENT"
  173. (let ((enter? (lambda (n s r) #t))
  174. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  175. (down (lambda (n s r) (cons `(down ,n) r)))
  176. (up (lambda (n s r) (cons `(up ,n) r)))
  177. (skip (lambda (n s r) (cons `(skip ,n) r)))
  178. (error (lambda (n s e r) (cons `(error ,n ,e) r)))
  179. (name "/.does-not-exist."))
  180. (equal? (file-system-fold enter? leaf down up skip error '() name)
  181. `((error ,name ,ENOENT)))))
  182. (pass-if "EACCES"
  183. (with-file-tree %top-builddir '(directory "test-EACCES" #o000
  184. (("a") ("b")))
  185. (let ((enter? (lambda (n s r) #t))
  186. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  187. (down (lambda (n s r) (cons `(down ,n) r)))
  188. (up (lambda (n s r) (cons `(up ,n) r)))
  189. (skip (lambda (n s r) (cons `(skip ,n) r)))
  190. (error (lambda (n s e r) (cons `(error ,n ,e) r)))
  191. (name (string-append %top-builddir "/test-EACCES")))
  192. (equal? (file-system-fold enter? leaf down up skip error '() name)
  193. `((error ,name ,EACCES))))))
  194. (pass-if "dangling symlink and lstat"
  195. (with-file-tree %top-builddir '(directory "test-dangling"
  196. (("dangling" -> "xxx")))
  197. (let ((enter? (lambda (n s r) #t))
  198. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  199. (down (lambda (n s r) (cons `(down ,n) r)))
  200. (up (lambda (n s r) (cons `(up ,n) r)))
  201. (skip (lambda (n s r) (cons `(skip ,n) r)))
  202. (error (lambda (n s e r) (cons `(error ,n ,e) r)))
  203. (name (string-append %top-builddir "/test-dangling")))
  204. (equal? (file-system-fold enter? leaf down up skip error '()
  205. name)
  206. `((up ,name)
  207. (leaf ,(string-append name "/dangling"))
  208. (down ,name))))))
  209. (pass-if "dangling symlink and stat"
  210. ;; Same as above, but using `stat' instead of `lstat'.
  211. (with-file-tree %top-builddir '(directory "test-dangling"
  212. (("dangling" -> "xxx")))
  213. (let ((enter? (lambda (n s r) #t))
  214. (leaf (lambda (n s r) (cons `(leaf ,n) r)))
  215. (down (lambda (n s r) (cons `(down ,n) r)))
  216. (up (lambda (n s r) (cons `(up ,n) r)))
  217. (skip (lambda (n s r) (cons `(skip ,n) r)))
  218. (error (lambda (n s e r) (cons `(error ,n ,e) r)))
  219. (name (string-append %top-builddir "/test-dangling")))
  220. (equal? (file-system-fold enter? leaf down up skip error '()
  221. name stat)
  222. `((up ,name)
  223. (error ,(string-append name "/dangling") ,ENOENT)
  224. (down ,name)))))))
  225. (with-test-prefix "file-system-tree"
  226. (pass-if "test-suite (never enter)"
  227. (match (file-system-tree %test-dir (lambda (n s) #f))
  228. (("test-suite" (= stat:type 'directory)) ; no children
  229. #t)))
  230. (pass-if "test-suite/*"
  231. (match (file-system-tree %test-dir (lambda (n s)
  232. (string=? n %test-dir)))
  233. (("test-suite" (= stat:type 'directory) children ...)
  234. (any (match-lambda
  235. (("tests" (= stat:type 'directory)) ; no children
  236. #t)
  237. (_ #f))
  238. children))))
  239. (pass-if "test-suite (recursive)"
  240. (match (file-system-tree %test-dir)
  241. (("test-suite" (= stat:type 'directory) children ...)
  242. (any (match-lambda
  243. (("tests" (= stat:type 'directory) (= car files) ...)
  244. (let ((expected '("alist.test" "bytevectors.test"
  245. "ftw.test" "gc.test" "vlist.test")))
  246. (lset= string=?
  247. (lset-intersection string=? files expected)
  248. expected)))
  249. (_ #f))
  250. children))))
  251. (pass-if "ENOENT"
  252. (not (file-system-tree "/.does-not-exist."))))
  253. (with-test-prefix "scandir"
  254. (pass-if "top-srcdir"
  255. (let ((valid? (negate (cut string-any #\/ <>))))
  256. (match (scandir %top-srcdir)
  257. (((? valid? files) ...)
  258. ;; Both subdirs and files must be included.
  259. (let ((expected '("libguile" "README" "COPYING"
  260. "test-suite" "Makefile.am"
  261. "." "..")))
  262. (lset= string=?
  263. (lset-intersection string=? files expected)
  264. expected))))))
  265. (pass-if "test-suite"
  266. (let ((select? (cut string-suffix? ".test" <>)))
  267. (match (scandir (string-append %test-dir "/tests") select?)
  268. (("." ".." "00-initial-env.test" (? select?) ...)
  269. #t))))
  270. (pass-if "flat file"
  271. (not (scandir (string-append %test-dir "/Makefile.am"))))
  272. (pass-if "EACCES"
  273. (not (scandir "/.does-not-exist."))))
  274. ;;; Local Variables:
  275. ;;; eval: (put 'with-file-tree 'scheme-indent-function 2)
  276. ;;; End: