{ $values { "path" "a pathname string" } { "?" boolean } }
{ $description "Tests if the file named by " { $snippet "path" } " exists." } ;
+HELP: check-file-exists
+{ $values { "path" "a pathname string" } }
+{ $description "Check if a file exists, otherwise throw a " { $link no-such-file } " error." }
+{ $errors "Throws an error if the file does not exist." } ;
+
HELP: if-file-exists
{ $values { "path" "a pathname string" } { "true" { $quotation ( ..a path -- ..b ) } } { "false" { $quotation ( ..a path -- ..b ) } } }
{ $description "If " { $snippet "path" } " is a file that exists, calls the " { $snippet "true" } " quotation, otherwise calls the " { $snippet "false" } " quotation." }
{ t } [ "test2.txt" file-exists? ] unit-test
+ { "test2.txt" } [ "test2.txt" check-file-exists ] unit-test
+
{ } [ "test2.txt" delete-file ] unit-test
+
+ [ "test2.txt" check-file-exists ] [ no-such-file? ] must-fail-with
] with-test-directory
! File seeking tests
: file-exists? ( path -- ? )
normalize-path native-string>alien (file-exists?) ;
+ERROR: no-such-file path ;
+
+: check-file-exists ( path -- path )
+ dup file-exists? [ no-such-file ] unless ;
+
: if-file-exists ( ..a path true: ( ..a path -- ..b ) false: ( ..a path -- ..b ) -- ..b )
[ dup file-exists? ] 2dip if ; inline