more tests for io.directories.search, fix docs, refactoring

db4
Doug Coleman 2009-04-24 15:22:12 -05:00
parent 33743c1a3d
commit c3c51e2c60
3 changed files with 35 additions and 13 deletions

View File

@ -15,13 +15,20 @@ HELP: each-file
} }
} ; } ;
HELP: recursive-directory HELP: recursive-directory-files
{ $values { $values
{ "path" "a pathname string" } { "bfs?" "a boolean, breadth-first or depth-first" } { "path" "a pathname string" } { "bfs?" "a boolean, breadth-first or depth-first" }
{ "paths" "a sequence of pathname strings" } { "paths" "a sequence of pathname strings" }
} }
{ $description "Traverses a directory path recursively and returns a sequence of files in a breadth-first or depth-first manner." } ; { $description "Traverses a directory path recursively and returns a sequence of files in a breadth-first or depth-first manner." } ;
HELP: recursive-directory-entries
{ $values
{ "path" "a pathname string" } { "bfs?" "a boolean, breadth-first or depth-first" }
{ "directory-entries" "a sequence of directory-entries" }
}
{ $description "Traverses a directory path recursively and returns a sequence of directory-entries in a breadth-first or depth-first manner." } ;
HELP: find-file HELP: find-file
{ $values { $values
{ "path" "a pathname string" } { "bfs?" "a boolean, breadth-first or depth-first" } { "quot" quotation } { "path" "a pathname string" } { "bfs?" "a boolean, breadth-first or depth-first" } { "quot" quotation }
@ -55,7 +62,8 @@ HELP: find-all-in-directories
ARTICLE: "io.directories.search" "Searching directories" ARTICLE: "io.directories.search" "Searching directories"
"The " { $vocab-link "io.directories.search" } " vocabulary contains words used for recursively iterating over a directory and for finding files in a directory tree." $nl "The " { $vocab-link "io.directories.search" } " vocabulary contains words used for recursively iterating over a directory and for finding files in a directory tree." $nl
"Traversing directories:" "Traversing directories:"
{ $subsection recursive-directory } { $subsection recursive-directory-files }
{ $subsection recursive-directory-entries }
{ $subsection each-file } { $subsection each-file }
"Finding files:" "Finding files:"
{ $subsection find-file } { $subsection find-file }

View File

@ -1,12 +1,14 @@
USING: io.directories.search io.files io.files.unique USING: combinators.smart io.directories
io.pathnames kernel namespaces sequences sorting tools.test ; io.directories.hierarchy io.directories.search io.files
io.files.unique io.pathnames kernel namespaces sequences
sorting strings tools.test ;
IN: io.directories.search.tests IN: io.directories.search.tests
[ t ] [ [ t ] [
[ [
10 [ "io.paths.test" "gogogo" make-unique-file ] replicate 10 [ "io.paths.test" "gogogo" make-unique-file ] replicate
current-temporary-directory get [ ] find-all-files current-temporary-directory get [ ] find-all-files
] with-unique-directory drop [ natural-sort ] bi@ = ] cleanup-unique-directory [ natural-sort ] bi@ =
] unit-test ] unit-test
[ f ] [ [ f ] [
@ -18,3 +20,18 @@ IN: io.directories.search.tests
[ f ] [ [ f ] [
{ } t [ "asdfasdfasdfasdfasdf" tail? ] find-in-directories { } t [ "asdfasdfasdfasdfasdf" tail? ] find-in-directories
] unit-test ] unit-test
[ t ] [
[
current-temporary-directory get
"the-head" unique-file drop t
[ file-name "the-head" head? ] find-file string?
] cleanup-unique-directory
] unit-test
[ t ] [
[ unique-directory unique-directory ] output>array
[ [ "abcd" append-path touch-file ] each ]
[ [ file-name "abcd" = ] find-all-in-directories length 2 = ]
[ [ delete-tree ] each ] tri
] unit-test

View File

@ -43,7 +43,8 @@ TUPLE: directory-iterator path bfs queue ;
:: iterate-directory-entries ( iter quot -- directory-entry/f ) :: iterate-directory-entries ( iter quot -- directory-entry/f )
iter next-directory-entry [ iter next-directory-entry [
quot call( obj -- obj ) [ iter quot iterate-directory-entries ] unless* quot call( obj -- obj )
[ iter quot iterate-directory-entries ] unless*
] [ ] [
f f
] if* ; inline recursive ] if* ; inline recursive
@ -57,8 +58,7 @@ TUPLE: directory-iterator path bfs queue ;
PRIVATE> PRIVATE>
: each-file ( path bfs? quot -- ) : each-file ( path bfs? quot -- )
setup-traversal [ name>> ] prepose setup-traversal iterate-directory drop ;
iterate-directory-entries drop ; inline
: each-directory-entry ( path bfs? quot -- ) : each-directory-entry ( path bfs? quot -- )
setup-traversal iterate-directory-entries drop ; setup-traversal iterate-directory-entries drop ;
@ -87,11 +87,8 @@ ERROR: file-not-found path bfs? quot ;
3dup find-file dup [ 2nip nip ] [ drop file-not-found ] if ; 3dup find-file dup [ 2nip nip ] [ drop file-not-found ] if ;
: find-in-directories ( directories bfs? quot -- path'/f ) : find-in-directories ( directories bfs? quot -- path'/f )
'[ '[ _ [ _ _ find-file-throws ] attempt-all ]
_ [ _ _ find-file-throws ] attempt-all [ drop f ] recover ;
] [
drop f
] recover ;
: find-all-in-directories ( directories quot -- paths/f ) : find-all-in-directories ( directories quot -- paths/f )
'[ _ find-all-files ] map concat ; '[ _ find-all-files ] map concat ;