| 1 |
;; |
| 2 |
;; testing gauche.fileutil and file.* modules |
| 3 |
;; |
| 4 |
|
| 5 |
(use gauche.test) |
| 6 |
(use gauche.generator) |
| 7 |
(test-start "file utilities") |
| 8 |
(use scheme.list) |
| 9 |
(use srfi.13) |
| 10 |
|
| 11 |
;; shorthand of normalizing pathname. this doesn't do anything on |
| 12 |
;; unix, but on Windows the separator in PATHNAME is replaced. |
| 13 |
(define (n . pathnames) (map sys-normalize-pathname pathnames)) |
| 14 |
|
| 15 |
;;------------------------------------------------------------------ |
| 16 |
(test-section "built-in gauche.fileutil") |
| 17 |
|
| 18 |
(define (md p) (sys-mkdir p #o777)) |
| 19 |
(define (mf p) (with-output-to-file p (cut display "z"))) |
| 20 |
|
| 21 |
(and sys-tm->alist (test-module 'gauche.fileutil)) ;; trigger autoload |
| 22 |
|
| 23 |
(test-remove-files "tmp1.o") |
| 24 |
|
| 25 |
(define (file-pred-tests path expected) |
| 26 |
(test* #"file-exists? (~path)" (car expected) (file-exists? path)) |
| 27 |
(test* #"file-is-regular? (~path)" (cadr expected) (file-is-regular? path)) |
| 28 |
(test* #"file-is-directory? (~path)" (caddr expected) (file-is-directory? path))) |
| 29 |
|
| 30 |
(file-pred-tests "tmp1.o" '(#f #f #f)) |
| 31 |
|
| 32 |
(with-output-to-file "tmp1.o" (cut display "Z")) |
| 33 |
|
| 34 |
(file-pred-tests "tmp1.o" '(#t #t #f)) |
| 35 |
|
| 36 |
(sys-unlink "tmp1.o") |
| 37 |
(sys-mkdir "tmp1.o" #o777) |
| 38 |
|
| 39 |
(file-pred-tests "tmp1.o" '(#t #f #t)) |
| 40 |
|
| 41 |
(sys-rmdir "tmp1.o") |
| 42 |
|
| 43 |
;; |
| 44 |
;; glob test. |
| 45 |
;; Note: on Windows the file/directory name can't end with a period. |
| 46 |
;; |
| 47 |
($ test-with-temporary-directory "tmp1.o" |
| 48 |
(^[] |
| 49 |
(md "tmp1.o/a") |
| 50 |
(mf "tmp1.o/a/b") |
| 51 |
(md "tmp1.o/a/cc") |
| 52 |
(mf "tmp1.o/a/cc/a") |
| 53 |
(mf "tmp1.o/a/.d") |
| 54 |
(md "tmp1.o/.a") |
| 55 |
(mf "tmp1.o/.a/b") |
| 56 |
(md "tmp1.o/.a/.d") |
| 57 |
(md "tmp1.o/aa") |
| 58 |
(mf "tmp1.o/aa/b") |
| 59 |
(mf "tmp1.o/aa/.d") |
| 60 |
(mf "tmp1.o/a.a") |
| 61 |
(mf "tmp1.o/a.b") |
| 62 |
(mf "tmp1.o/a.a.a") |
| 63 |
|
| 64 |
;; literal |
| 65 |
(test* "glob a.a" (n "tmp1.o/a.a") |
| 66 |
(glob "tmp1.o/a.a") |
| 67 |
(pa$ lset= equal?)) |
| 68 |
|
| 69 |
;; nomatch |
| 70 |
(test* "glob z" '() |
| 71 |
(glob "tmp1.o/z") |
| 72 |
(pa$ lset= equal?)) |
| 73 |
|
| 74 |
;; wildcard |
| 75 |
(test* "glob *" (n "tmp1.o/a" "tmp1.o/aa" "tmp1.o/a.a" |
| 76 |
"tmp1.o/a.b" "tmp1.o/a.a.a") |
| 77 |
(glob "tmp1.o/*") |
| 78 |
(pa$ lset= equal?)) |
| 79 |
|
| 80 |
(test* "glob a.*" (n "tmp1.o/a.a" "tmp1.o/a.b" "tmp1.o/a.a.a") |
| 81 |
(glob "tmp1.o/a.*") |
| 82 |
(pa$ lset= equal?)) |
| 83 |
|
| 84 |
(test* "glob .*" (n "tmp1.o/.a" "tmp1.o/." "tmp1.o/..") |
| 85 |
(glob "tmp1.o/.*") |
| 86 |
(pa$ lset= equal?)) |
| 87 |
|
| 88 |
(test* "glob ?" (n "tmp1.o/a") |
| 89 |
(glob "tmp1.o/?") |
| 90 |
(pa$ lset= equal?)) |
| 91 |
|
| 92 |
(test* "glob *?" (n "tmp1.o/a" "tmp1.o/aa" "tmp1.o/a.a" |
| 93 |
"tmp1.o/a.b" "tmp1.o/a.a.a") |
| 94 |
(glob "tmp1.o/*?") |
| 95 |
(pa$ lset= equal?)) |
| 96 |
|
| 97 |
(test* "glob ??" (n "tmp1.o/aa") |
| 98 |
(glob "tmp1.o/??") |
| 99 |
(pa$ lset= equal?)) |
| 100 |
|
| 101 |
(test* "glob *.*" (n "tmp1.o/a.a" "tmp1.o/a.b" "tmp1.o/a.a.a") |
| 102 |
(glob "tmp1.o/*.*") |
| 103 |
(pa$ lset= equal?)) |
| 104 |
|
| 105 |
(test* "glob */*" (n "tmp1.o/a/b" "tmp1.o/a/cc" "tmp1.o/aa/b") |
| 106 |
(glob "tmp1.o/*/*") |
| 107 |
(pa$ lset= equal?)) |
| 108 |
|
| 109 |
(test* "glob */?" (n "tmp1.o/a/b" "tmp1.o/aa/b") |
| 110 |
(glob "tmp1.o/*/?") |
| 111 |
(pa$ lset= equal?)) |
| 112 |
|
| 113 |
(test* "glob * (chdir)" (n "a" "aa" "a.a" "a.b" "a.a.a") |
| 114 |
(begin (sys-chdir "tmp1.o") (begin0 (glob "*") (sys-chdir ".."))) |
| 115 |
(pa$ lset= equal?)) |
| 116 |
|
| 117 |
(test* "glob */" (n "tmp1.o/a/" "tmp1.o/aa/") |
| 118 |
(glob "tmp1.o/*/") |
| 119 |
(pa$ lset= equal?)) |
| 120 |
|
| 121 |
;; ** |
| 122 |
(test* "glob tmp1.o/**/?" (n "tmp1.o/a" "tmp1.o/a/b" "tmp1.o/a/cc/a" |
| 123 |
"tmp1.o/aa/b") |
| 124 |
(glob "tmp1.o/**/?") |
| 125 |
(pa$ lset= equal?)) |
| 126 |
|
| 127 |
;; multi |
| 128 |
(test* "glob * .* (multi)" (n "tmp1.o/." "tmp1.o/.." "tmp1.o/.a" "tmp1.o/a" |
| 129 |
"tmp1.o/aa" "tmp1.o/a.a" "tmp1.o/a.b" |
| 130 |
"tmp1.o/a.a.a") |
| 131 |
(glob '("tmp1.o/*" "tmp1.o/.*")) |
| 132 |
(pa$ lset= equal?)) |
| 133 |
|
| 134 |
;; braces |
| 135 |
(test* "glob {a,aa}/{b,cc}" (n "tmp1.o/a/b" "tmp1.o/a/cc" "tmp1.o/aa/b") |
| 136 |
(glob '("tmp1.o/{a,aa}/{b,cc}")) |
| 137 |
(pa$ lset= equal?)) |
| 138 |
(test* "glob {a{,a,.{a,b}}}" (n "tmp1.o/a" "tmp1.o/aa" |
| 139 |
"tmp1.o/a.a" "tmp1.o/a.b") |
| 140 |
(glob '("tmp1.o/{a{,a,.{a,b}}}")) |
| 141 |
(pa$ lset= equal?)) |
| 142 |
(test* "glob {a/*,aa/*}" (n "tmp1.o/a/b" "tmp1.o/a/cc" "tmp1.o/aa/b") |
| 143 |
(glob '("tmp1.o/{a/*,aa/*}")) |
| 144 |
(pa$ lset= equal?)) |
| 145 |
(test* "glob {,?/}*" (n "tmp1.o/a/b" "tmp1.o/a/cc" "tmp1.o/a" "tmp1.o/aa" |
| 146 |
"tmp1.o/a.a" "tmp1.o/a.b" "tmp1.o/a.a.a") |
| 147 |
(glob '("tmp1.o/{,?/}*")) |
| 148 |
(pa$ lset= equal?)) |
| 149 |
(test* "glob {,.}*" (n "tmp1.o/a" "tmp1.o/aa" "tmp1.o/.a" "tmp1.o/." |
| 150 |
"tmp1.o/.." "tmp1.o/a.a" "tmp1.o/a.b" "tmp1.o/a.a.a") |
| 151 |
(glob '("tmp1.o/{,.}*")) |
| 152 |
(pa$ lset= equal?)) |
| 153 |
|
| 154 |
;; charset |
| 155 |
(test* "glob a.[ab]" (n "tmp1.o/a.a" "tmp1.o/a.b") |
| 156 |
(glob "tmp1.o/a.[ab]") |
| 157 |
(pa$ lset= equal?)) |
| 158 |
(test* "glob a.[[:alpha:]]" (n "tmp1.o/a.a" "tmp1.o/a.b") |
| 159 |
(glob "tmp1.o/a.[[:alpha:]]") |
| 160 |
(pa$ lset= equal?)) |
| 161 |
(test* "glob *.[[:alpha:]]" (n "tmp1.o/a.a" "tmp1.o/a.b" "tmp1.o/a.a.a") |
| 162 |
(glob "tmp1.o/*.[[:alpha:]]") |
| 163 |
(pa$ lset= equal?)) |
| 164 |
(test* "glob *.[![:alpha:]]" '() |
| 165 |
(glob "tmp1.o/*.[![:alpha:]]") |
| 166 |
(pa$ lset= equal?)) |
| 167 |
(test* "glob *.[^[:alpha:]]" '() |
| 168 |
(glob "tmp1.o/*.[^[:alpha:]]") |
| 169 |
(pa$ lset= equal?)) |
| 170 |
(test* "glob *.[^A-Z]" (n "tmp1.o/a.a" "tmp1.o/a.b" "tmp1.o/a.a.a") |
| 171 |
(glob "tmp1.o/*.[^A-Z]") |
| 172 |
(pa$ lset= equal?)) |
| 173 |
|
| 174 |
;; specifying current/root dir |
| 175 |
(test* "glob w/alt root dir" (n "tmp1.o/a.a" "tmp1.o/a.b" "tmp1.o/a.a.a") |
| 176 |
(glob "/*.*" :folder (make-glob-fs-fold :root-path "tmp1.o")) |
| 177 |
(pa$ lset= equal?)) |
| 178 |
(test* "glob w/alt current dir" (n "tmp1.o/a.a" "tmp1.o/a.b" "tmp1.o/a.a.a") |
| 179 |
(glob "*.*" :folder (make-glob-fs-fold :current-path "tmp1.o")) |
| 180 |
(pa$ lset= equal?)) |
| 181 |
)) |
| 182 |
|
| 183 |
;;------------------------------------------------------------------ |
| 184 |
(test-section "file.filter") |
| 185 |
(use file.filter) |
| 186 |
(test-module 'file.filter) |
| 187 |
|
| 188 |
(test-remove-files "tmp1.o" "tmp2.o") |
| 189 |
|
| 190 |
(with-output-to-file "tmp1.o" |
| 191 |
(cut display "aaa bbb ccc ddd\neee fff ggg hhh\n")) |
| 192 |
|
| 193 |
(test* "file.filter tmp1.o -> string" |
| 194 |
"AAA BBB CCC DDDEEE FFF GGG HHH" |
| 195 |
(with-output-to-string |
| 196 |
(cut file-filter (^[in out] |
| 197 |
(generator-for-each |
| 198 |
(^[line] (display (string-upcase line) out)) |
| 199 |
(cut read-line in))) |
| 200 |
:input "tmp1.o"))) |
| 201 |
|
| 202 |
(test* "file.filter string -> tmp2.o" |
| 203 |
"AAA BBB CCC DDDEEE FFF GGG HHH" |
| 204 |
(begin |
| 205 |
(with-input-from-string "aaa bbb ccc ddd\neee fff ggg hhh\n" |
| 206 |
(cut file-filter (^[in out] |
| 207 |
(generator-for-each |
| 208 |
(^[line] (display (string-upcase line) out)) |
| 209 |
(cut read-line in))) |
| 210 |
:output "tmp2.o")) |
| 211 |
(call-with-input-file "tmp2.o" port->string))) |
| 212 |
|
| 213 |
(test-remove-files "tmp2.o") |
| 214 |
|
| 215 |
(test* "file.filter cleanup" #f |
| 216 |
(with-error-handler (^e (file-exists? "tmp2.o")) |
| 217 |
(cut with-input-from-string "zzz" |
| 218 |
(cut file-filter (^[in out] (error "yyy")) |
| 219 |
:output "tmp2.o")))) |
| 220 |
|
| 221 |
(test-remove-files "tmp2.o") |
| 222 |
|
| 223 |
(test* "file.filter cleanup" #t |
| 224 |
(with-error-handler (^e (file-exists? "tmp2.o")) |
| 225 |
(cut with-input-from-string "zzz" |
| 226 |
(cut file-filter (^[in out] (error "yyy")) |
| 227 |
:output "tmp2.o" |
| 228 |
:keep-output? #t)))) |
| 229 |
|
| 230 |
(test-remove-files "tmp2.o") |
| 231 |
|
| 232 |
(let () |
| 233 |
(define (t tempfile) |
| 234 |
(test* "file.filter temporary" |
| 235 |
'(#f "AAA BBB CCC DDDEEE FFF GGG HHH") |
| 236 |
(let* ([r1 |
| 237 |
(with-input-from-string "aaa bbb ccc ddd\neee fff ggg hhh\n" |
| 238 |
(cut file-filter |
| 239 |
(^[in out] |
| 240 |
(do-generator [line (cut read-line in)] |
| 241 |
(display (string-upcase line) out)) |
| 242 |
(file-exists? "tmp2.o")) |
| 243 |
:output "tmp2.o" |
| 244 |
:temporary-file tempfile))] |
| 245 |
[r2 (call-with-input-file "tmp2.o" port->string)]) |
| 246 |
(sys-unlink "tmp2.o") |
| 247 |
(list r1 r2)))) |
| 248 |
(t "foo") |
| 249 |
(t #t)) |
| 250 |
|
| 251 |
(test-remove-files "tmp1.o" "tmp2.o") |
| 252 |
|
| 253 |
(test* "file.filter rename-hook" #t |
| 254 |
(begin |
| 255 |
(with-output-to-file "tmp1.o" (cut display "foo")) |
| 256 |
(let1 i1 (~ (sys-stat "tmp1.o")'ino) |
| 257 |
(file-filter (^[in out] (copy-port in out)) |
| 258 |
:input (open-input-string "foo") |
| 259 |
:output "tmp1.o" |
| 260 |
:temporary-file #t |
| 261 |
:leave-unchanged #t) |
| 262 |
(eqv? i1 (~ (sys-stat "tmp1.o")'ino))))) |
| 263 |
|
| 264 |
(test-remove-files "tmp1.o") |
| 265 |
|
| 266 |
(let () |
| 267 |
(define (f2s f) (call-with-input-file f |
| 268 |
(^i (call-with-output-string (^o (copy-port i o)))))) |
| 269 |
(define (t reader expect-result expect-output) |
| 270 |
(test* (format "file-filter-fold") (list expect-result expect-output) |
| 271 |
(let1 r (file-filter-fold |
| 272 |
(^[elt seed out] (write elt out) (+ seed 1)) |
| 273 |
0 |
| 274 |
:input "test1.o" |
| 275 |
:output "test2.o" |
| 276 |
:reader reader) |
| 277 |
(list r (f2s "test2.o")))) |
| 278 |
(test* (format "file-filter-map") (list expect-result expect-output) |
| 279 |
(let1 r (file-filter-map |
| 280 |
(^[elt out] (write elt out) #t) |
| 281 |
:input "test1.o" |
| 282 |
:output "test2.o" |
| 283 |
:reader reader) |
| 284 |
(list (length r) (f2s "test2.o")))) |
| 285 |
(test* (format "file-filter-for-each") (list expect-result expect-output) |
| 286 |
(let1 r 0 |
| 287 |
(file-filter-for-each |
| 288 |
(^[elt out] (write elt out) (inc! r)) |
| 289 |
:input "test1.o" |
| 290 |
:output "test2.o" |
| 291 |
:reader reader) |
| 292 |
(list r (f2s "test2.o"))))) |
| 293 |
|
| 294 |
(with-output-to-file "test1.o" (cut display "abc\n(def\nghi)")) |
| 295 |
(unwind-protect |
| 296 |
(begin |
| 297 |
(t read-line 3 "\"abc\"\"(def\"\"ghi)\"") |
| 298 |
(t read-char 13 "#\\a#\\b#\\c#\\newline#\\(#\\d#\\e#\\f#\\newline#\\g#\\h#\\i#\\)") |
| 299 |
(t read 2 "abc(def ghi)")) |
| 300 |
(test-remove-files "test1.o" "test2.o"))) |
| 301 |
|
| 302 |
(test* "file-filter-fold example" |
| 303 |
" 1: abc\n 3: def\n" |
| 304 |
(unwind-protect |
| 305 |
(begin |
| 306 |
(with-output-to-file "test1.o" |
| 307 |
(^[] |
| 308 |
(print "abc") |
| 309 |
(print "123") |
| 310 |
(print "def"))) |
| 311 |
(file-filter-fold (^[line nc out] |
| 312 |
(when (#/[a-z]/ line) |
| 313 |
(format out "~3d: ~a\n" nc line)) |
| 314 |
(+ nc 1)) |
| 315 |
1 :input "test1.o" :output "test2.o") |
| 316 |
(call-with-input-file "test2.o" |
| 317 |
(^i (call-with-output-string (^o (copy-port i o)))))) |
| 318 |
(test-remove-files "test1.o" "test2.o"))) |
| 319 |
|
| 320 |
(test-end) |