]> gitweb.factorcode.org Git - factor.git/commitdiff
io.files: adding check-file-exists
authorJohn Benediktsson <mrjbq7@gmail.com>
Mon, 18 Sep 2023 21:12:16 +0000 (14:12 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 18 Sep 2023 21:12:16 +0000 (14:12 -0700)
core/io/files/files-docs.factor
core/io/files/files-tests.factor
core/io/files/files.factor

index 774c1b784dccdaefcd5ea77a6ade91a2005cd97f..fd4268974ba52f806db5a8c90728b2cca2a69f4e 100644 (file)
@@ -115,6 +115,11 @@ HELP: file-exists?
 { $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." }
index 2bcb67a977337f96108947dbaad8351c45ba22e3..ddd6b8ccb648c9dea6b19852816369ec2608c5ec 100644 (file)
@@ -162,7 +162,11 @@ CONSTANT: pt-array-1
 
     { 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
index bb612e6d1b4910f8c40922aa626bee6e566814fa..c67c8e2e65c78d9c07562ad7d11fd47ad37e059c 100644 (file)
@@ -71,6 +71,11 @@ HOOK: (file-appender) io-backend ( path -- stream )
 : 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