filesys.test 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. ;;;; filesys.test --- test file system functions -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2004, 2006 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 2.1 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-filesys)
  19. #:use-module (test-suite lib)
  20. #:use-module (test-suite guile-test))
  21. (define (test-file)
  22. (data-file-name "filesys-test.tmp"))
  23. (define (test-symlink)
  24. (data-file-name "filesys-test-link.tmp"))
  25. ;;;
  26. ;;; copy-file
  27. ;;;
  28. (with-test-prefix "copy-file"
  29. ;; return next prospective file descriptor number
  30. (define (next-fd)
  31. (let ((fd (dup 0)))
  32. (close fd)
  33. fd))
  34. ;; in guile 1.6.4 and earlier, copy-file didn't close the input fd when
  35. ;; the output could not be opened
  36. (pass-if "fd leak when dest unwritable"
  37. (let ((old-next (next-fd)))
  38. (false-if-exception (copy-file "/dev/null" "no/such/dir/foo"))
  39. (= old-next (next-fd)))))
  40. ;;;
  41. ;;; lstat
  42. ;;;
  43. (with-test-prefix "lstat"
  44. (pass-if "normal file"
  45. (call-with-output-file (test-file)
  46. (lambda (port)
  47. (display "hello" port)))
  48. (eqv? 5 (stat:size (lstat (test-file)))))
  49. (call-with-output-file (test-file)
  50. (lambda (port)
  51. (display "hello" port)))
  52. (false-if-exception (delete-file (test-symlink)))
  53. (if (not (false-if-exception
  54. (begin (symlink (test-file) (test-symlink)) #t)))
  55. (display "cannot create symlink, lstat test skipped\n")
  56. (pass-if "symlink"
  57. ;; not much to test, except that it works
  58. (->bool (lstat (test-symlink))))))
  59. ;;;
  60. ;;; opendir and friends
  61. ;;;
  62. (with-test-prefix "opendir"
  63. (with-test-prefix "root directory"
  64. (let ((d (opendir "/")))
  65. (pass-if "not empty"
  66. (string? (readdir d)))
  67. (pass-if "all entries are strings"
  68. (let more ()
  69. (let ((f (readdir d)))
  70. (cond ((string? f)
  71. (more))
  72. ((eof-object? f)
  73. #t)
  74. (else
  75. #f)))))
  76. (closedir d))))
  77. ;;;
  78. ;;; stat
  79. ;;;
  80. (with-test-prefix "stat"
  81. (with-test-prefix "filename"
  82. (pass-if "size"
  83. (call-with-output-file (test-file)
  84. (lambda (port)
  85. (display "hello" port)))
  86. (eqv? 5 (stat:size (stat (test-file))))))
  87. (with-test-prefix "file descriptor"
  88. (pass-if "size"
  89. (call-with-output-file (test-file)
  90. (lambda (port)
  91. (display "hello" port)))
  92. (let* ((fd (open-fdes (test-file) O_RDONLY))
  93. (st (stat fd)))
  94. (close-fdes fd)
  95. (eqv? 5 (stat:size st)))))
  96. (with-test-prefix "port"
  97. (pass-if "size"
  98. (call-with-output-file (test-file)
  99. (lambda (port)
  100. (display "hello" port)))
  101. (let* ((port (open-file (test-file) "r+"))
  102. (st (stat port)))
  103. (close-port port)
  104. (eqv? 5 (stat:size st))))))
  105. (delete-file (test-file))
  106. (delete-file (test-symlink))