| 1 |
;; |
| 2 |
;; testing file.util |
| 3 |
;; |
| 4 |
|
| 5 |
(use gauche.test) |
| 6 |
(use scheme.list) |
| 7 |
(use srfi.13) |
| 8 |
(use gauche.uvector) |
| 9 |
|
| 10 |
(test-start "file.*") |
| 11 |
|
| 12 |
(test-section "file.util") |
| 13 |
(use file.util) |
| 14 |
(cond-expand |
| 15 |
[gauche.os.windows |
| 16 |
(test-module 'file.util :allow-undefined '(sys-symlink))] |
| 17 |
[else |
| 18 |
(test-module 'file.util)]) |
| 19 |
|
| 20 |
;; shorthand of normalizing pathname. this doesn't do anything on |
| 21 |
;; unix, but on Windows the separator in PATHNAME is replaced. |
| 22 |
;; NB: we remove the drive letter from DOS path to accommodate |
| 23 |
;; different installations. |
| 24 |
(define (n pathname . pathnames) |
| 25 |
(define (normalize s) |
| 26 |
(let1 s (sys-normalize-pathname s) |
| 27 |
(if (#/^[a-zA-Z]:/ s) (string-drop s 2) s))) |
| 28 |
(if (null? pathnames) |
| 29 |
(normalize pathname) |
| 30 |
(map normalize (cons pathname pathnames)))) |
| 31 |
|
| 32 |
(define (na pathname . pathnames) ;make it absolute |
| 33 |
(sys-normalize-pathname (apply n pathname pathnames) :absolute #t)) |
| 34 |
|
| 35 |
;; mingw doesn't have fully compatible permissions as unix. |
| 36 |
;; this procedure compensates it. |
| 37 |
(define (P perm) |
| 38 |
(cond-expand |
| 39 |
[gauche.os.windows (let1 p (ash perm -6) |
| 40 |
(logior p (ash p 3) (ash p 6)))] |
| 41 |
[else perm])) |
| 42 |
|
| 43 |
;;===================================================================== |
| 44 |
(test-section "pathname utils") |
| 45 |
|
| 46 |
(test* "build-path" "" (build-path "")) |
| 47 |
(test* "build-path" "." (build-path ".")) |
| 48 |
(test* "build-path" (n "/") (build-path "/")) |
| 49 |
(test* "build-path" (n "a/b/c") (build-path "a" "b" "c")) |
| 50 |
(test* "build-path" (n "a/b/c") (build-path "a/" "b/" "c")) |
| 51 |
(test* "build-path" (n "/a/b/c") (build-path "/" "a/b" "c")) |
| 52 |
(test* "build-path" (n "./a/b/c") (build-path "." "a/b" "c")) |
| 53 |
(test* "build-path" (test-error) (build-path "." "/a/b")) |
| 54 |
(test* "build-path" (n "foo") (build-path "" "foo")) |
| 55 |
(test* "build-path" (n "foo/bar") (build-path "" "foo" "" "bar")) |
| 56 |
(test* "build-path" (n "foo") (build-path "" "foo" "")) |
| 57 |
(test* "build-path" (n "./a/./b") (build-path #f "a" #f "b")) |
| 58 |
|
| 59 |
(test* "decompose-path" '("/a/b/c" "d" "e") |
| 60 |
(values->list (decompose-path "/a/b/c/d.e"))) |
| 61 |
(test* "decompose-path" '("." "d" "e") |
| 62 |
(values->list (decompose-path "d.e"))) |
| 63 |
(test* "decompose-path" '("." "d" "") |
| 64 |
(values->list (decompose-path "d."))) |
| 65 |
(test* "decompose-path" '("." "d" #f) |
| 66 |
(values->list (decompose-path "d"))) |
| 67 |
(test* "decompose-path" '("/a.b" "c" #f) |
| 68 |
(values->list (decompose-path "/a.b/c"))) |
| 69 |
(test* "decompose-path" '("/a.b" #f #f) |
| 70 |
(values->list (decompose-path "/a.b/"))) |
| 71 |
(test* "decompose-path" '("/a.b" "c.c" "d") |
| 72 |
(values->list (decompose-path "/a.b/c.c.d"))) |
| 73 |
(test* "decompose-path" '("/a.b" ".d" #f) |
| 74 |
(values->list (decompose-path "/a.b/.d"))) |
| 75 |
|
| 76 |
(test* "path-extension" "c" (path-extension "/a.b/c.d/e.c")) |
| 77 |
(test* "path-extension" "" (path-extension "/a.b/c.d/e.c.")) |
| 78 |
(test* "path-extension" #f (path-extension "/a.b/c.d/.e")) |
| 79 |
(test* "path-extension" #f (path-extension "/a.b/c.d/e")) |
| 80 |
|
| 81 |
(test* "path-sans-extension" "/a.b/c.d/e" |
| 82 |
(path-sans-extension "/a.b/c.d/e.c")) |
| 83 |
(test* "path-sans-extension" "/a.b/c.d/e.c" |
| 84 |
(path-sans-extension "/a.b/c.d/e.c.")) |
| 85 |
(test* "path-sans-extension" "/a.b/c.d/.e" |
| 86 |
(path-sans-extension "/a.b/c.d/.e")) |
| 87 |
(test* "path-sans-extension" "/a.b/c.d/e" |
| 88 |
(path-sans-extension "/a.b/c.d/e")) |
| 89 |
(test* "path-sans-extension" "a" (path-sans-extension "a.c")) |
| 90 |
(test* "path-sans-extension" "a" (path-sans-extension "a.")) |
| 91 |
(test* "path-sans-extension" "a" (path-sans-extension "a")) |
| 92 |
(test* "path-sans-extension" ".a" (path-sans-extension ".a")) |
| 93 |
(test* "path-sans-extension" ".a" (path-sans-extension ".a.c")) |
| 94 |
|
| 95 |
(test* "path-swap-extension" "/a.b/c.d/e.o" |
| 96 |
(path-swap-extension "/a.b/c.d/e.c" "o")) |
| 97 |
(test* "path-swap-extension" "/a.b/c.d/e.c.o" |
| 98 |
(path-swap-extension "/a.b/c.d/e.c." "o")) |
| 99 |
(test* "path-swap-extension" "/a.b/c.d/.e.o" |
| 100 |
(path-swap-extension "/a.b/c.d/.e" "o")) |
| 101 |
(test* "path-swap-extension" "/a.b/c.d/e.o" |
| 102 |
(path-swap-extension "/a.b/c.d/e" "o")) |
| 103 |
(test* "path-swap-extension" "/a.b/c.d/e" |
| 104 |
(path-swap-extension "/a.b/c.d/e.c" #f)) |
| 105 |
(test* "path-swap-extension" "a.o" (path-swap-extension "a.c" "o")) |
| 106 |
(test* "path-swap-extension" "a.o" (path-swap-extension "a." "o")) |
| 107 |
(test* "path-swap-extension" "a.o" (path-swap-extension "a" "o")) |
| 108 |
(test* "path-swap-extension" ".a.o" (path-swap-extension ".a" "o")) |
| 109 |
(test* "path-swap-extension" ".a.o" (path-swap-extension ".a.c" "o")) |
| 110 |
|
| 111 |
;; find-file-in-paths is tested after diretory util tests |
| 112 |
|
| 113 |
;;===================================================================== |
| 114 |
(test-section "directories") |
| 115 |
|
| 116 |
(test* "current-directory" (list (n "/") (n "/") #t #t) |
| 117 |
(let* ([cur (sys-getcwd)] |
| 118 |
[root (begin (current-directory "/") |
| 119 |
(n (sys-getcwd)))] |
| 120 |
[root* (n (current-directory))] |
| 121 |
[cur* (begin (current-directory cur) |
| 122 |
(sys-getcwd))] |
| 123 |
[cur** (current-directory)]) |
| 124 |
(list root root* (string=? cur cur*) (string=? cur* cur**)))) |
| 125 |
|
| 126 |
;; prepare test data set |
| 127 |
(define *test-tree* |
| 128 |
`(test.out |
| 129 |
((test1.o ,(make-string 100 #\o)) |
| 130 |
(test2.o ,(make-string 100 #\o)) |
| 131 |
(test3.o ,(make-string 100 #\i)) |
| 132 |
(test4.o ,(make-string 20000 #\i)) |
| 133 |
(test5.o ,(make-string 20000 #\i)) |
| 134 |
,@(cond-expand |
| 135 |
[gauche.sys.symlink |
| 136 |
'((test6.o :symlink "test1.o") |
| 137 |
(test7.o :symlink "test6.o") |
| 138 |
(test2.d :symlink "test.d"))] |
| 139 |
[else |
| 140 |
`((test6.o ,(make-string 100 #\o)) |
| 141 |
(test7.o ,(make-string 100 #\o)) |
| 142 |
(test2.d :mode #o777 |
| 143 |
((test10.o ,(make-string 100 #\o)) |
| 144 |
(test11.o ,(make-string 100 #\o)) |
| 145 |
test12.o)))]) |
| 146 |
(test.d ((test10.o ,(make-string 100 #\o)) |
| 147 |
,(cond-expand |
| 148 |
[gauche.sys.symlink |
| 149 |
'(test11.o :symlink "../test1.o")] |
| 150 |
[else |
| 151 |
`(test11.o ,(make-string 100 #\o))]) |
| 152 |
test12.o)) |
| 153 |
))) |
| 154 |
|
| 155 |
(test-remove-files "test.out") |
| 156 |
|
| 157 |
(test* "create-directory-tree" #t |
| 158 |
(begin (create-directory-tree "." *test-tree*) |
| 159 |
(and (file-is-directory? "test.out") |
| 160 |
(file-is-regular? "test.out/test1.o") |
| 161 |
(file-is-regular? "test.out/test2.o") |
| 162 |
(file-is-regular? "test.out/test3.o") |
| 163 |
(file-is-regular? "test.out/test4.o") |
| 164 |
(file-is-regular? "test.out/test5.o") |
| 165 |
(file-is-directory? "test.out/test.d") |
| 166 |
(file-is-regular? "test.out/test.d/test10.o") |
| 167 |
(file-is-regular? "test.out/test.d/test12.o")))) |
| 168 |
|
| 169 |
(test* "check-directory-tree" #t |
| 170 |
(check-directory-tree "." *test-tree*)) |
| 171 |
|
| 172 |
(test* "directory-list" |
| 173 |
'("." ".." "test.d" "test1.o" "test2.d" "test2.o" |
| 174 |
"test3.o" "test4.o" "test5.o" "test6.o" "test7.o" ) |
| 175 |
(directory-list "test.out")) |
| 176 |
|
| 177 |
(test* "directory-list :children?" |
| 178 |
'("test.d" "test1.o" "test2.d" "test2.o" |
| 179 |
"test3.o" "test4.o" "test5.o" "test6.o" "test7.o" ) |
| 180 |
(directory-list "test.out" :children? #t)) |
| 181 |
|
| 182 |
(test* "directory-list :add-path?" |
| 183 |
(n "test.out/." "test.out/.." "test.out/test.d" "test.out/test1.o" |
| 184 |
"test.out/test2.d" "test.out/test2.o" "test.out/test3.o" |
| 185 |
"test.out/test4.o" "test.out/test5.o" "test.out/test6.o" |
| 186 |
"test.out/test7.o" ) |
| 187 |
(directory-list "test.out/" :add-path? #t)) |
| 188 |
|
| 189 |
(test* "directory-list :filter" |
| 190 |
(n "test.out/test1.o" |
| 191 |
"test.out/test2.o" "test.out/test3.o" "test.out/test4.o" |
| 192 |
"test.out/test5.o" "test.out/test6.o" "test.out/test7.o" ) |
| 193 |
(directory-list "test.out" :add-path? #t |
| 194 |
:filter (^p (string-suffix? "o" p)))) |
| 195 |
|
| 196 |
(test* "directory-list :filter" |
| 197 |
(n "test.out/test1.o" |
| 198 |
"test.out/test2.o" "test.out/test3.o" "test.out/test4.o" |
| 199 |
"test.out/test5.o" "test.out/test6.o" "test.out/test7.o" ) |
| 200 |
(directory-list "test.out" :add-path? #t :filter-add-path? #t |
| 201 |
:filter file-is-regular?)) |
| 202 |
|
| 203 |
(test* "directory-list2" |
| 204 |
'(("." ".." "test.d" "test2.d") |
| 205 |
("test1.o" "test2.o" "test3.o" "test4.o" |
| 206 |
"test5.o" "test6.o" "test7.o" )) |
| 207 |
(values->list (directory-list2 "test.out"))) |
| 208 |
|
| 209 |
(test* "directory-list2 :add-path" |
| 210 |
`(,(n "test.out/." "test.out/.." "test.out/test.d" "test.out/test2.d") |
| 211 |
,(n "test.out/test1.o" "test.out/test2.o" "test.out/test3.o" |
| 212 |
"test.out/test4.o" "test.out/test5.o" "test.out/test6.o" |
| 213 |
"test.out/test7.o")) |
| 214 |
(values->list (directory-list2 "test.out" :add-path? #t))) |
| 215 |
|
| 216 |
(test* "directory-list2 :children" |
| 217 |
`(,(n "test.out/test.d" "test.out/test2.d") |
| 218 |
,(n "test.out/test1.o" "test.out/test2.o" "test.out/test3.o" |
| 219 |
"test.out/test4.o" "test.out/test5.o" "test.out/test6.o" |
| 220 |
"test.out/test7.o")) |
| 221 |
(values->list (directory-list2 "test.out" :add-path? #t :children? #t))) |
| 222 |
|
| 223 |
(test* "directory-list2 :filter" |
| 224 |
'(("test.d" "test2.d") |
| 225 |
("test1.o" "test2.o" "test3.o" "test4.o" |
| 226 |
"test5.o" "test6.o" "test7.o" )) |
| 227 |
(values->list |
| 228 |
(directory-list2 "test.out" |
| 229 |
:filter (^p (string-contains p "test"))))) |
| 230 |
|
| 231 |
(cond-expand |
| 232 |
[gauche.sys.symlink |
| 233 |
(test* "directory-list2 :follow-link? #f" |
| 234 |
'(("test.d") |
| 235 |
("test1.o" "test2.d" "test2.o" "test3.o" "test4.o" |
| 236 |
"test5.o" "test6.o" "test7.o" )) |
| 237 |
(values->list |
| 238 |
(directory-list2 "test.out" :follow-link? #f :children? #t)))] |
| 239 |
[else]) |
| 240 |
|
| 241 |
(test* "directory-fold" |
| 242 |
(n "test.out/test.d/test10.o" "test.out/test.d/test11.o" |
| 243 |
"test.out/test1.o" |
| 244 |
"test.out/test2.d/test10.o" "test.out/test2.d/test11.o" |
| 245 |
"test.out/test2.o" "test.out/test3.o" |
| 246 |
"test.out/test6.o" "test.out/test7.o") |
| 247 |
(reverse |
| 248 |
(directory-fold "test.out" |
| 249 |
(^[path result] |
| 250 |
(if (= (file-size path) 100) |
| 251 |
(cons path result) |
| 252 |
result)) |
| 253 |
'())) |
| 254 |
) |
| 255 |
|
| 256 |
(test* "directory-fold" |
| 257 |
(n "test.out" |
| 258 |
"test.out/test.d" |
| 259 |
"test.out/test.d/test10.o" |
| 260 |
"test.out/test.d/test11.o" |
| 261 |
"test.out/test.d/test12.o" |
| 262 |
"test.out/test1.o" |
| 263 |
"test.out/test2.d" |
| 264 |
"test.out/test2.d/test10.o" |
| 265 |
"test.out/test2.d/test11.o" |
| 266 |
"test.out/test2.d/test12.o" |
| 267 |
"test.out/test2.o" "test.out/test3.o" |
| 268 |
"test.out/test4.o" "test.out/test5.o" |
| 269 |
"test.out/test6.o" "test.out/test7.o") |
| 270 |
(reverse |
| 271 |
(directory-fold "test.out" cons '() |
| 272 |
:lister (^[path seed] |
| 273 |
(values |
| 274 |
(directory-list path |
| 275 |
:add-path? #t |
| 276 |
:children? #t) |
| 277 |
(cons path seed))))) |
| 278 |
) |
| 279 |
|
| 280 |
(cond-expand |
| 281 |
[gauche.sys.symlink |
| 282 |
(test* "directory-fold :follow-link? #f" |
| 283 |
(n "test.out/test.d/test10.o" |
| 284 |
"test.out/test.d/test11.o" |
| 285 |
"test.out/test1.o" |
| 286 |
"test.out/test2.o" "test.out/test3.o" |
| 287 |
"test.out/test6.o" "test.out/test7.o") |
| 288 |
(reverse |
| 289 |
(directory-fold "test.out" |
| 290 |
(^[path result] |
| 291 |
(if (and (file-is-regular? path) |
| 292 |
(= (file-size path) 100)) |
| 293 |
(cons path result) |
| 294 |
result)) |
| 295 |
'() |
| 296 |
:follow-link? #f))) |
| 297 |
;; this tests dangling symlink |
| 298 |
(sys-symlink "foo" "test.out/test.dangling") |
| 299 |
(test* "directory-fold :follow-link? #t; dangling link" |
| 300 |
(n "test.out/test.d/test10.o" "test.out/test.d/test11.o" |
| 301 |
"test.out/test.d/test12.o" |
| 302 |
"test.out/test.dangling" "test.out/test1.o" |
| 303 |
"test.out/test2.d/test10.o" "test.out/test2.d/test11.o" |
| 304 |
"test.out/test2.d/test12.o" |
| 305 |
"test.out/test2.o" "test.out/test3.o" "test.out/test4.o" |
| 306 |
"test.out/test5.o" "test.out/test6.o" "test.out/test7.o") |
| 307 |
(sort (directory-fold "test.out" cons '()))) |
| 308 |
(sys-unlink "test.out/test.dangling")] |
| 309 |
[else]) |
| 310 |
|
| 311 |
(test* "directory-fold :lister" |
| 312 |
(cond-expand |
| 313 |
[gauche.sys.symlink |
| 314 |
(n "test.out/test.d/test10.o" "test.out/test.d/test11.o" |
| 315 |
"test.out/test.d/test12.o" |
| 316 |
"test.out/test1.o")] |
| 317 |
[else |
| 318 |
(n "test.out/test.d/test10.o" "test.out/test.d/test11.o" |
| 319 |
"test.out/test.d/test12.o" |
| 320 |
"test.out/test2.d/test10.o" "test.out/test2.d/test11.o" |
| 321 |
"test.out/test2.d/test12.o" |
| 322 |
"test.out/test1.o")]) |
| 323 |
(reverse |
| 324 |
(directory-fold "test.out" cons '() |
| 325 |
:follow-link? #f |
| 326 |
:lister (^[dir knil] |
| 327 |
(receive (dirs files) |
| 328 |
(directory-list2 dir :add-path? #t :children? #t :follow-link? #f) |
| 329 |
(append dirs |
| 330 |
(filter (cut string-contains <> "test1") |
| 331 |
files)))))) |
| 332 |
) |
| 333 |
|
| 334 |
(test-remove-files "test.out") |
| 335 |
|
| 336 |
;;===================================================================== |
| 337 |
(test-section "file utils") |
| 338 |
|
| 339 |
(create-directory-tree "." *test-tree*) |
| 340 |
|
| 341 |
;; resolve-path should be here for it uses test.out hierarchy created above. |
| 342 |
'(test* "resolve-path" "/" (resolve-path "/")) |
| 343 |
(test* "resolve-path" "." (resolve-path ".")) |
| 344 |
(test* "resolve-path" "test.out" (resolve-path "test.out")) |
| 345 |
(cond-expand |
| 346 |
[gauche.sys.symlink |
| 347 |
(test* "resolve-path" "test.out/test1.o" |
| 348 |
(resolve-path "test.out/test6.o")) |
| 349 |
(test* "resolve-path" "test.out/test1.o" |
| 350 |
(resolve-path "test.out/test7.o")) |
| 351 |
(test* "resolve-path" "test.out/test1.o" |
| 352 |
(resolve-path "test.out/test2.d/test11.o")) |
| 353 |
(test* "resolve-path" "test.out/test1.o" |
| 354 |
(resolve-path "test.out/test2.d/../test.d/test11.o")) |
| 355 |
(test* "resolve-path" "test.out/test1.o" |
| 356 |
(resolve-path "test.out/test.d/../test2.d/test11.o")) |
| 357 |
] |
| 358 |
[else]) |
| 359 |
|
| 360 |
(test* "file-type" #f |
| 361 |
(file-type "nonexistent/file")) |
| 362 |
(test* "file-type" '(directory directory regular) |
| 363 |
(map file-type |
| 364 |
'("test.out/test.d" "test.out/test2.d" "test.out/test1.o"))) |
| 365 |
(cond-expand |
| 366 |
[gauche.sys.symlink |
| 367 |
(test* "file-type :follow-link? #f" '(directory symlink regular) |
| 368 |
(map (cut file-type <> :follow-link? #f) |
| 369 |
'("test.out/test.d" "test.out/test2.d" "test.out/test1.o")))] |
| 370 |
[else]) |
| 371 |
|
| 372 |
(test* "file-eq?" #t |
| 373 |
(file-eq? "test.out/test1.o" "test.out/test1.o")) |
| 374 |
(cond-expand |
| 375 |
[gauche.sys.symlink |
| 376 |
(test* "file-eq? (symlink)" #f |
| 377 |
(file-eq? "test.out/test1.o" "test.out/test7.o"))] |
| 378 |
[else]) |
| 379 |
(test* "file-eq?" #f |
| 380 |
(file-eq? "test.out/test1.o" "test.out/test2.o")) |
| 381 |
(test* "file-eqv?" #t |
| 382 |
(file-eqv? "test.out/test1.o" "test.out/test1.o")) |
| 383 |
(cond-expand |
| 384 |
[gauche.sys.symlink |
| 385 |
(test* "file-eqv? (symlink)" #t |
| 386 |
(file-eqv? "test.out/test1.o" "test.out/test7.o")) |
| 387 |
(test* "file-eqv? (symlink)" #t |
| 388 |
(file-eqv? "test.out/test1.o" "test.out/test.d/test11.o"))] |
| 389 |
[else]) |
| 390 |
(test* "file-eqv?" #f |
| 391 |
(file-eqv? "test.out/test1.o" "test.out/test2.o")) |
| 392 |
(test* "file-equal?" #t |
| 393 |
(file-equal? "test.out/test1.o" "test.out/test1.o")) |
| 394 |
(test* "file-equal?" #t |
| 395 |
(file-equal? "test.out/test1.o" "test.out/test2.o")) |
| 396 |
(test* "file-equal?" #f |
| 397 |
(file-equal? "test.out/test1.o" "test.out/test4.o")) |
| 398 |
(test* "file-equal?" #t |
| 399 |
(file-equal? "test.out/test4.o" "test.out/test5.o")) |
| 400 |
|
| 401 |
(let1 touched "test.out/touched" |
| 402 |
(define (times file) |
| 403 |
(let1 s (sys-stat file) |
| 404 |
(list (~ s'atime) (~ s'mtime)))) |
| 405 |
(test* "touch-file :create #f" #t |
| 406 |
(and (not (file-exists? touched)) |
| 407 |
(begin (touch-file touched :create #f) |
| 408 |
(not (file-exists? touched))))) |
| 409 |
(test* "touch-file" #t |
| 410 |
(and (not (file-exists? touched)) |
| 411 |
(begin (touch-file touched) |
| 412 |
(file-exists? touched)))) |
| 413 |
;; NB: win32 has a problem of reading file's timestamp which can be |
| 414 |
;; before 1970/1/1 _on_local_time_. For example, if it is 100 in UTC |
| 415 |
;; and we call _stat() in the timezone UTC-8, we get UINT_MAX. Here |
| 416 |
;; we use a timestamp value large enough to avoid the problem. |
| 417 |
(test* "touch-file :time" '(50001 50001) |
| 418 |
(begin (touch-file touched :time 50001) (times touched))) |
| 419 |
(test* "touch-file :time :type" '(60000 50001) |
| 420 |
(begin (touch-file touched :time 60000 :type 'atime) (times touched))) |
| 421 |
(test* "touch-file :time :type" '(60000 70000) |
| 422 |
(begin (touch-file touched :time 70000 :type 'mtime) (times touched))) |
| 423 |
|
| 424 |
(test* "touch-files" (n "test.out/touched" "test.out/touched1" |
| 425 |
"test.out/touched2" "test.out/touched3") |
| 426 |
(begin (touch-files (n "test.out/touched" "test.out/touched1" |
| 427 |
"test.out/touched2" "test.out/touched3")) |
| 428 |
(sort (glob "test.out/touched*")))) |
| 429 |
) |
| 430 |
|
| 431 |
(test* "copy-file (normal)" #t |
| 432 |
(and (copy-file "test.out/test5.o" "test.out/test.copy") |
| 433 |
(not (file-eq? "test.out/test5.o" "test.out/test.copy")) |
| 434 |
(file-equal? "test.out/test5.o" "test.out/test.copy"))) |
| 435 |
|
| 436 |
(test* "copy-file (normal; permission check)" |
| 437 |
(number->string (logand #o666 (lognot (sys-umask))) 8) |
| 438 |
(number->string (file-perm "test.out/test.copy") 8)) |
| 439 |
|
| 440 |
(test* "copy-file (:if-exists :error)" (test-error) |
| 441 |
(copy-file "test.out/test5.o" "test.out/test.copy")) |
| 442 |
|
| 443 |
(test* "copy-file (:if-exists #f)" #f |
| 444 |
(copy-file "test.out/test5.o" "test.out/test.copy" :if-exists #f)) |
| 445 |
|
| 446 |
(test* "copy-file (:if-exists :supersede)" #t |
| 447 |
(and (copy-file "test.out/test1.o" "test.out/test.copy" |
| 448 |
:if-exists :supersede) |
| 449 |
(file-equal? "test.out/test1.o" "test.out/test.copy") |
| 450 |
(not (file-exists? "test.out/test.copy.orig")))) |
| 451 |
|
| 452 |
(test* "copy-file (:if-exists :backup)" #t |
| 453 |
(and (copy-file "test.out/test5.o" "test.out/test.copy" |
| 454 |
:if-exists :backup) |
| 455 |
(file-equal? "test.out/test5.o" "test.out/test.copy") |
| 456 |
(file-equal? "test.out/test1.o" "test.out/test.copy.orig"))) |
| 457 |
|
| 458 |
(test* "copy-file (:if-exists :backup)" #t |
| 459 |
(and (copy-file "test.out/test1.o" "test.out/test.copy" |
| 460 |
:if-exists :backup :backup-suffix "~") |
| 461 |
(file-equal? "test.out/test1.o" "test.out/test.copy") |
| 462 |
(file-equal? "test.out/test5.o" "test.out/test.copy~"))) |
| 463 |
|
| 464 |
(let () |
| 465 |
(define (check-copy-file-append . args) |
| 466 |
(let1 preamble "********************************\n" |
| 467 |
(sys-unlink "test.out/test.copy") |
| 468 |
(sys-unlink "test.out/test.copy2") |
| 469 |
(with-output-to-file "test.out/test.copy" |
| 470 |
(^[] (display preamble))) |
| 471 |
(apply copy-file "test.out/test1.o" "test.out/test.copy" |
| 472 |
:if-exists :append args) |
| 473 |
(with-output-to-file "test.out/test.copy2" |
| 474 |
(^[] (display preamble) |
| 475 |
(call-with-input-file "test.out/test1.o" |
| 476 |
(^[in] (copy-port in (current-output-port)))))) |
| 477 |
(file-equal? "test.out/test.copy" "test.out/test.copy2"))) |
| 478 |
(test* "copy-file (:if-exists :append)" #t |
| 479 |
(check-copy-file-append)) |
| 480 |
(test* "copy-file (:if-exists :append :safe #t)" #t |
| 481 |
(check-copy-file-append :safe #t))) |
| 482 |
|
| 483 |
(sys-unlink "test.out/test.copy") |
| 484 |
(sys-unlink "test.out/test.copy2") |
| 485 |
(sys-unlink "test.out/test.copy~") |
| 486 |
(sys-unlink "test.out/test.copy.orig") |
| 487 |
|
| 488 |
(cond-expand |
| 489 |
[gauche.sys.symlink |
| 490 |
(test* "copy-file (:follow-link? #f)" 'symlink |
| 491 |
(begin |
| 492 |
(copy-file "test.out/test6.o" "test.out/test.copy" :follow-link? #f) |
| 493 |
(sys-system "ls -l test.out") |
| 494 |
(file-type "test.out/test.copy" :follow-link? #f))) |
| 495 |
(test* "copy-file (:follow-link? #f :if-exists :backup)" |
| 496 |
'(symlink symlink) |
| 497 |
(begin |
| 498 |
(copy-file "test.out/test6.o" "test.out/test.copy" |
| 499 |
:follow-link? #f :if-exists :backup :backup-suffix "~") |
| 500 |
(list (file-type "test.out/test.copy" :follow-link? #f) |
| 501 |
(file-type "test.out/test.copy~" :follow-link? #f)))) |
| 502 |
(sys-unlink "test.out/test.copy") |
| 503 |
(sys-unlink "test.out/test.copy~")] |
| 504 |
[else]) |
| 505 |
|
| 506 |
(test* "copy-file (normal, safe)" #t |
| 507 |
(and (copy-file "test.out/test5.o" "test.out/test.copy" :safe #t) |
| 508 |
(not (file-eq? "test.out/test5.o" "test.out/test.copy")) |
| 509 |
(file-equal? "test.out/test5.o" "test.out/test.copy"))) |
| 510 |
|
| 511 |
(test* "copy-file (:if-exists :error, safe)" (test-error) |
| 512 |
(copy-file "test.out/test5.o" "test.out/test.copy" :safe #t)) |
| 513 |
|
| 514 |
(test* "copy-file (:if-exists #f, safe)" #f |
| 515 |
(copy-file "test.out/test5.o" "test.out/test.copy" :if-exists #f :safe #t)) |
| 516 |
|
| 517 |
(test* "copy-file (:if-exists :supersede, safe)" #t |
| 518 |
(and (copy-file "test.out/test1.o" "test.out/test.copy" |
| 519 |
:if-exists :supersede :safe #t) |
| 520 |
(file-equal? "test.out/test1.o" "test.out/test.copy") |
| 521 |
(not (file-exists? "test.out/test.copy.orig")))) |
| 522 |
|
| 523 |
(test* "copy-file (:if-exists :backup, safe)" #t |
| 524 |
(and (copy-file "test.out/test5.o" "test.out/test.copy" |
| 525 |
:if-exists :backup :safe #t) |
| 526 |
(file-equal? "test.out/test5.o" "test.out/test.copy") |
| 527 |
(file-equal? "test.out/test1.o" "test.out/test.copy.orig"))) |
| 528 |
|
| 529 |
(test* "copy-file (:if-exists :backup, safe)" #t |
| 530 |
(and (copy-file "test.out/test1.o" "test.out/test.copy" |
| 531 |
:if-exists :backup :backup-suffix "~" :safe #t) |
| 532 |
(file-equal? "test.out/test1.o" "test.out/test.copy") |
| 533 |
(file-equal? "test.out/test5.o" "test.out/test.copy~"))) |
| 534 |
|
| 535 |
(test* "copy-file (same file)" (test-error) |
| 536 |
(copy-file "test.out/test.copy" "test.out/test.copy" |
| 537 |
:if-exists :supersede)) |
| 538 |
|
| 539 |
(sys-unlink "test.out/test.copy") |
| 540 |
|
| 541 |
(test* "copy-file (keep-mode)" (P #o642) |
| 542 |
(begin (sys-chmod "test.out/test1.o" #o642) |
| 543 |
(copy-file "test.out/test1.o" "test.out/test.copy" |
| 544 |
:keep-mode #t) |
| 545 |
(file-perm "test.out/test.copy"))) |
| 546 |
|
| 547 |
(sys-unlink "test.out/test.copy") |
| 548 |
|
| 549 |
(test* "copy-file (keep-mode w/safe)" (P #o624) |
| 550 |
(begin (sys-chmod "test.out/test1.o" #o624) |
| 551 |
(copy-file "test.out/test1.o" "test.out/test.copy" |
| 552 |
:keep-mode #t) |
| 553 |
(file-perm "test.out/test.copy"))) |
| 554 |
|
| 555 |
(sys-chmod "test.out/test1.o" #o644) |
| 556 |
(sys-chmod "test.out/test.copy" #o644) |
| 557 |
|
| 558 |
(test* "move-file (normal)" #t |
| 559 |
(and (move-file "test.out/test.copy" "test.out/test.move") |
| 560 |
(not (file-exists? "test.out/test.copy")) |
| 561 |
(file-equal? "test.out/test1.o" "test.out/test.move"))) |
| 562 |
|
| 563 |
(test* "move-file (:if-exists :error)" (test-error) |
| 564 |
(move-file "test.out/test5.o" "test.out/test.move")) |
| 565 |
|
| 566 |
(test* "move-file (:if-exists :supersede)" #t |
| 567 |
(and (move-file "test.out/test5.o" "test.out/test.move" |
| 568 |
:if-exists :supersede) |
| 569 |
(not (file-exists? "test.out/test5.o")) |
| 570 |
(not (file-equal? "test.out/test1.o" "test.out/test.move")))) |
| 571 |
|
| 572 |
(test* "move-file (:if-exists :backup)" #t |
| 573 |
(and (move-file "test.out/test1.o" "test.out/test.move" |
| 574 |
:if-exists :backup) |
| 575 |
(not (file-exists? "test.out/test1.o")) |
| 576 |
(file-equal? "test.out/test2.o" "test.out/test.move") |
| 577 |
(file-equal? "test.out/test4.o" "test.out/test.move.orig"))) |
| 578 |
|
| 579 |
(test* "move-file (same file)" (test-error) |
| 580 |
(move-file "test.out/test.move" "test.out/test.move" |
| 581 |
:if-exists :supersede)) |
| 582 |
|
| 583 |
(let () |
| 584 |
(define (listdir d) |
| 585 |
(map (cut regexp-replace #/^test2?\.out[\/\\]/ <> "") |
| 586 |
(directory-fold d cons '()))) |
| 587 |
|
| 588 |
(test* "copy-directory*" (listdir "test.out") |
| 589 |
(begin |
| 590 |
(copy-directory* "test.out" "test2.out") |
| 591 |
(listdir "test2.out")) |
| 592 |
(cut lset= string=? <> <>)) |
| 593 |
|
| 594 |
(test* "remove-directory*" '(#f #f) |
| 595 |
(begin |
| 596 |
(remove-directory* "test.out") |
| 597 |
(remove-directory* "test2.out") |
| 598 |
(list (file-exists? "test.out") (file-exists? "test2.out")))) |
| 599 |
|
| 600 |
;; check dangling link behavior |
| 601 |
(cond-expand |
| 602 |
[gauche.sys.symlink |
| 603 |
(let () |
| 604 |
(define (tester desc fn expect . opts) |
| 605 |
(test* #"dangling symlink - ~desc" expect |
| 606 |
(begin |
| 607 |
(sys-symlink "no such file" "test.out") |
| 608 |
(apply fn "test.out" "test2.out" opts) |
| 609 |
(and (file-is-symlink? "test2.out") |
| 610 |
(string=? (sys-readlink "test.out") |
| 611 |
(sys-readlink "test2.out"))))) |
| 612 |
(remove-files "test.out" "test2.out")) |
| 613 |
|
| 614 |
(tester "copy-file" copy-file (test-error)) |
| 615 |
(tester "copy-file" copy-file #t :follow-link? #f) |
| 616 |
(tester "copy-directory*" copy-directory* #t) |
| 617 |
(tester "copy-directory*" copy-directory* (test-error) :follow-link? #t) |
| 618 |
)] |
| 619 |
[else]) |
| 620 |
) |
| 621 |
|
| 622 |
;; |
| 623 |
;; temporary file/dir |
| 624 |
;; |
| 625 |
|
| 626 |
(test* "call-with-temporary-file" '(#t #t #f) |
| 627 |
(unwind-protect |
| 628 |
(begin |
| 629 |
(remove-files '("test.o")) |
| 630 |
(make-directory* "test.o") |
| 631 |
(receive (exists name) |
| 632 |
(call-with-temporary-file |
| 633 |
(^[oport name] (values (file-exists? name) name)) |
| 634 |
:directory "test.o" |
| 635 |
:prefix "ooo") |
| 636 |
(list exists |
| 637 |
(boolean (#/test.o[\/\\]ooo\w+$/ name)) |
| 638 |
(file-exists? name)))) |
| 639 |
(remove-files '("test.o")))) |
| 640 |
|
| 641 |
(test* "call-with-temporary-directory" '(#t #t #f) |
| 642 |
(unwind-protect |
| 643 |
(begin |
| 644 |
(remove-files '("test.o")) |
| 645 |
(make-directory* "test.o") |
| 646 |
(receive (exists name) |
| 647 |
(call-with-temporary-directory |
| 648 |
(^[name] (values (file-is-directory? name) name)) |
| 649 |
:directory "test.o" |
| 650 |
:prefix "ooo") |
| 651 |
(list exists |
| 652 |
(boolean (#/test.o[\/\\]ooo\w+$/ name)) |
| 653 |
(file-exists? name)))) |
| 654 |
(remove-files '("test.o")))) |
| 655 |
|
| 656 |
;; |
| 657 |
;; file->* |
| 658 |
;; |
| 659 |
|
| 660 |
(test* "file->string, if-does-not-exist #f" #f |
| 661 |
(file->string "test.out" :if-does-not-exist #f)) |
| 662 |
(test* "file->string, if-does-not-exist :error" (test-error) |
| 663 |
(file->string "test.out" :if-does-not-exist :error)) |
| 664 |
|
| 665 |
(with-output-to-file "test.out" |
| 666 |
(cut display "humuhumu\nnukunuku\napua`a\n\n")) |
| 667 |
|
| 668 |
(test* "file->string" "humuhumu\nnukunuku\napua`a\n\n" |
| 669 |
(file->string "test.out")) |
| 670 |
(test* "file->string-list" '("humuhumu" "nukunuku" "apua`a" "") |
| 671 |
(file->string-list "test.out")) |
| 672 |
|
| 673 |
(remove-files "test.out" "test2.out") |
| 674 |
|
| 675 |
;; |
| 676 |
;; with-lock-file |
| 677 |
;; |
| 678 |
|
| 679 |
(define (test-lock-file type) |
| 680 |
(remove-files "test.out" "test.out.2") |
| 681 |
|
| 682 |
(test* #"with-lock-file (~type) just lock&release" '(#t #f) |
| 683 |
(let1 r (with-lock-file "test.out" |
| 684 |
(^[] (file-exists? "test.out")) |
| 685 |
:type type) |
| 686 |
(list r (file-exists? "test.out")))) |
| 687 |
|
| 688 |
(remove-files "test.out" "test.out.2") |
| 689 |
|
| 690 |
(test* #"with-lock-file (~type) lock giveup" `((,<lock-file-failure> #t) #f) |
| 691 |
(let1 r |
| 692 |
(with-lock-file "test.out" |
| 693 |
(^[] |
| 694 |
(let1 r2 |
| 695 |
(guard (e (else (class-of e))) |
| 696 |
(with-lock-file "test.out" |
| 697 |
(^[] 'boo!) |
| 698 |
:type type |
| 699 |
:abandon-timeout #f |
| 700 |
:retry-interval 0.1 |
| 701 |
:retry-limit 0.5)) |
| 702 |
(list r2 (file-exists? "test.out")))) |
| 703 |
:type type) |
| 704 |
(list r (file-exists? "test.out")))) |
| 705 |
|
| 706 |
(remove-files "test.out" "test.out.2") |
| 707 |
|
| 708 |
(test* #"with-lock-file (~type) stealing" 'got |
| 709 |
(begin |
| 710 |
(case type |
| 711 |
[(file) (touch-file "test.out")] |
| 712 |
[(directory) (make-directory* "test.out")]) |
| 713 |
(with-lock-file "test.out" |
| 714 |
(^[] 'got) |
| 715 |
:type type |
| 716 |
:retry-interval 0.1 |
| 717 |
:retry-limit 5 |
| 718 |
:abandon-timeout 1))) |
| 719 |
(remove-files "test.out" "test.out.2") |
| 720 |
|
| 721 |
(test* #"with-lock-file (~type) stealing failure" |
| 722 |
(test-error <lock-file-failure>) |
| 723 |
(begin |
| 724 |
(case type |
| 725 |
[(file) (make-directory* "test.out")] |
| 726 |
[(directory) (touch-file "test.out")]) |
| 727 |
(with-lock-file "test.out" |
| 728 |
(^[] 'got) |
| 729 |
:type type |
| 730 |
:retry-interval 0.1 |
| 731 |
:retry-limit 0.5 |
| 732 |
:abandon-timeout 1))) |
| 733 |
(remove-files "test.out" "test.out.2") |
| 734 |
) |
| 735 |
|
| 736 |
(test-lock-file 'file) |
| 737 |
(test-lock-file 'directory) |
| 738 |
|
| 739 |
;; |
| 740 |
;; find-file-in-paths |
| 741 |
;; |
| 742 |
|
| 743 |
(let () |
| 744 |
(define (cleanup) |
| 745 |
(when (file-exists? "test.out") |
| 746 |
(remove-directory* "test.out"))) |
| 747 |
(define paths '("test.out/bin" |
| 748 |
"test.out/usr/bin" |
| 749 |
"test.out/usr/sbin")) |
| 750 |
(cleanup) |
| 751 |
(dolist [p paths] (make-directory* p)) |
| 752 |
(print (directory-list ".")) |
| 753 |
(touch-files '("test.out/bin/foo" |
| 754 |
"test.out/bin/bar" |
| 755 |
"test.out/bin/ban.com" |
| 756 |
"test.out/usr/bin/foo" |
| 757 |
"test.out/usr/bin/bar" |
| 758 |
"test.out/usr/bin/baz" |
| 759 |
"test.out/usr/bin/ban.exe" |
| 760 |
"test.out/usr/sbin/foo" |
| 761 |
"test.out/usr/sbin/bar" |
| 762 |
"test.out/usr/sbin/ban")) |
| 763 |
|
| 764 |
(unwind-protect |
| 765 |
(begin |
| 766 |
(test* "find-file-in-paths" (na "test.out/bin/foo") |
| 767 |
(find-file-in-paths "foo" |
| 768 |
:paths paths |
| 769 |
:pred file-exists?)) |
| 770 |
(test* "find-file-in-paths" (na "test.out/usr/bin/foo") |
| 771 |
(find-file-in-paths "foo" |
| 772 |
:paths (cdr paths) |
| 773 |
:pred file-exists?)) |
| 774 |
(test* "find-file-in-paths" (na "test.out/usr/sbin/ban") |
| 775 |
(find-file-in-paths "ban" |
| 776 |
:paths paths |
| 777 |
:pred file-exists?)) |
| 778 |
(test* "find-file-in-paths" (na "test.out/usr/bin/ban.exe") |
| 779 |
(find-file-in-paths "ban" |
| 780 |
:paths paths |
| 781 |
:pred file-exists? |
| 782 |
:extensions '("exe"))) |
| 783 |
(test* "find-file-in-paths" (na "test.out/bin/ban.com") |
| 784 |
(find-file-in-paths "ban" |
| 785 |
:paths paths |
| 786 |
:pred file-exists? |
| 787 |
:extensions '("exe" "com"))) |
| 788 |
) |
| 789 |
(cleanup)) |
| 790 |
) |
| 791 |
|
| 792 |
;;===================================================================== |
| 793 |
(test-section "file.event") |
| 794 |
(use file.event) |
| 795 |
(test-module 'file.event) |
| 796 |
(test-module 'file.event.common) ;loaded via file.event |
| 797 |
|
| 798 |
(define-module file-event-generic-test |
| 799 |
(use gauche.test) |
| 800 |
(use file.event.generic) |
| 801 |
(test-module 'file.event.generic)) |
| 802 |
|
| 803 |
(define-module file-event-specific-test |
| 804 |
(use gauche.test) |
| 805 |
;; Those submodules are already loaded via file.event |
| 806 |
(cond-expand |
| 807 |
[gauche.sys.inotify (test-module 'file.event.inotify)] |
| 808 |
[gauche.sys.kqueue (test-module 'file.event.kqueue)] |
| 809 |
[else])) |
| 810 |
|
| 811 |
(test-end) |