diff options
Diffstat (limited to 'src/clojure')
-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)))) |