Permalink
Browse files

Merge pull request #100 from mjg123/master

Adds `ephemeral-{file,dir}` fns
  • Loading branch information...
2 parents 567dc11 + 3a947d4 commit 147f7b30d2c3c7e773a3650bf16a7b5720acfde8 @Raynes committed Oct 26, 2016
Showing with 28 additions and 0 deletions.
  1. +18 −0 src/me/raynes/fs.clj
  2. +10 −0 test/me/raynes/core_test.clj
View
@@ -344,6 +344,24 @@
([prefix suffix] (temp-dir prefix suffix 10))
([prefix suffix tries] (temp-create prefix suffix tries mkdirs)))
+(defn ephemeral-file
+ "Create an ephemeral file (will be deleted on JVM exit).
+ Returns nil if file could not be created even after n tries
+ (default 10)."
+ ([prefix] (ephemeral-file prefix "" 10))
+ ([prefix suffix] (ephemeral-file prefix suffix 10))
+ ([prefix suffix tries] (when-let [created (temp-create prefix suffix tries create)]
+ (doto created .deleteOnExit))))
+
+(defn ephemeral-dir
+ "Create an ephemeral directory (will be deleted on JVM exit).
+ Returns nil if dir could not be created even after n tries
+ (default 10)."
+ ([prefix] (ephemeral-dir prefix "" 10))
+ ([prefix suffix] (ephemeral-dir prefix suffix 10))
+ ([prefix suffix tries] (when-let [created (temp-create prefix suffix tries mkdirs)]
+ (doto created .deleteOnExit))))
+
; Taken from https://github.com/jkk/clj-glob. (thanks Justin!)
(defn- glob->regex
"Takes a glob-format string and returns a regex."
@@ -97,6 +97,16 @@
(directory? tmp) => true
(delete tmp)))
+(fact
+ (let [tmp (ephemeral-file "fs-")]
+ (exists? tmp) => true
+ (file? tmp) => true)) ;; is deleted on JVM exit
+
+(fact
+ (let [tmp (ephemeral-dir "fs-")]
+ (exists? tmp) => true
+ (directory? tmp) => true)) ;; is deleted on JVM exit
+
(fact
(absolute "foo") => (io/file *cwd* "foo"))

0 comments on commit 147f7b3

Please sign in to comment.