diff options
author | Phil Hagelberg <technomancy@gmail.com> | 2009-10-08 21:29:25 -0700 |
---|---|---|
committer | Chouser <chouser@n01se.net> | 2009-10-28 21:50:46 -0400 |
commit | 8eef617aac7b077421d4aa9c84a2df189af6ee17 (patch) | |
tree | 469cf322f331efe7faf45f8535361845fe455148 | |
parent | b9c7820e64d4e5b86c837b25800cf61094ad2517 (diff) |
java-utils: Add delete-file and delete-file-recursively functions
Also added test for delete-file. Refs #33
Signed-off-by: Chouser <chouser@n01se.net>
-rw-r--r-- | src/clojure/contrib/java_utils.clj | 17 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib/test_java_utils.clj | 11 |
2 files changed, 26 insertions, 2 deletions
diff --git a/src/clojure/contrib/java_utils.clj b/src/clojure/contrib/java_utils.clj index 579d1128..7af4bc93 100644 --- a/src/clojure/contrib/java_utils.clj +++ b/src/clojure/contrib/java_utils.clj @@ -175,6 +175,23 @@ (doto (as-properties m) (.store f #^String comments))))) +(defn delete-file + "Delete file f. Raise an exception if it fails unless silently is true." + [f & [silently]] + (or (.delete (file f)) + silently + (throw (java.io.IOException. (str "Couldn't delete " f))))) + +(defn delete-file-recursively + "Delete file f. If it's a directory, recursively delete all its contents. +Raise an exception if any deletion fails unless silently is true." + [f & [silently]] + (let [f (file f)] + (if (.isDirectory f) + (doseq [child (.listFiles f)] + (delete-file-recursively child silently))) + (delete-file f silently))) + (defmulti #^{:doc "Coerces argument (URL, URI, or String) to a java.net.URL." :arglists '([arg])} diff --git a/src/clojure/contrib/test_contrib/test_java_utils.clj b/src/clojure/contrib/test_contrib/test_java_utils.clj index abf1b9a6..8e0f67c4 100644 --- a/src/clojure/contrib/test_contrib/test_java_utils.clj +++ b/src/clojure/contrib/test_contrib/test_java_utils.clj @@ -88,7 +88,7 @@ (is (= (inc propcount) (count (System/getProperties))))) (is (= propcount (count (System/getProperties)))))) ) - + (deftest test-as-properties (let [expected (doto (Properties.) (.setProperty "a" "b") @@ -113,4 +113,11 @@ (read-properties f))))) - +(deftest test-delete-file + (let [file (File/createTempFile "test" "deletion") + not-file (File. (str (java.util.UUID/randomUUID)))] + (delete-file (.getAbsolutePath file)) + (is (not (.exists file))) + (is (thrown? ArithmeticException (/ 1 0))) + (is (thrown? java.io.IOException (delete-file not-file))) + (is (delete-file not-file :silently)))) |