Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added with-temp-dir macro #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/me/raynes/fs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,35 @@
[[with-mutable-cwd]]"
[path]
(set! *cwd* (file path)))

(defmacro with-temp-dir
"Execute the `body` after binding a given vector of
symbols to temp directory instances.
will delete all temp directories after execution.

Example:
```
(with-temp-dir [temp-dir1 temp-dir2]
(println temp-dir1)
(let [temp-file1 (str temp-dir1 'tuki.txt')
temp-file2 (str temp-dir2 'shuki.txt')]
(spit temp-file1 'hi')
(spit temp-file2 'buddy')
[(slurp temp-file1) (slurp temp-file2)]))

#<File /var/folders/lp/2tg9rhtj28g0y60n8nt_wq_c0000gp/T/temp-dir11490694621530-86140538>
['hi' 'buddy']

(directory? '/var/folders/lp/2tg9rhtj28g0y60n8nt_wq_c0000gp/T/temp-dir11490694621530-8614053')
false"
[dirs & body]
(let [bindings (reduce
(fn [acc dir]
(conj acc dir `(temp-dir ~(str dir)))) [] dirs)]
`(let ~bindings
(try
(do ~@body)
(finally
(reduce (fn [_# x#]
(delete-dir x#)) ~dirs))))))

6 changes: 6 additions & 0 deletions test/me/raynes/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,12 @@
(absolute? "foo/bar") => false
(absolute? "foo/") => false))

(fact
(let [temp-dirs (with-temp-dir [source target]
(every? directory? [source target]) => true
[source target])]
(every? (comp not exists?) temp-dirs)))

(defmacro run-java-7-tests []
(when (try (import '[java.nio.file Files Path LinkOption StandardCopyOption FileAlreadyExistsException]
'[java.nio.file.attribute FileAttribute])
Expand Down