/test/clj/lambdacd/util/internal/temp_test.clj

https://github.com/flosell/lambdacd · Clojure · 48 lines · 43 code · 5 blank · 0 comment · 11 complexity · fd6753d18b38509224918467181739e8 MD5 · raw file

  1. (ns lambdacd.util.internal.temp-test
  2. (:require [clojure.test :refer :all]
  3. [lambdacd.util.internal.temp :refer :all]
  4. [clojure.java.io :as io]
  5. [me.raynes.fs :as fs]))
  6. (deftest create-temp-dir-test
  7. (testing "creating in default tmp folder"
  8. (testing "that we can create a temp-directory"
  9. (is (fs/exists? (io/file (create-temp-dir)))))
  10. (testing "that it is writable"
  11. (is (fs/mkdir (io/file (create-temp-dir) "hello")))))
  12. (testing "creating in a defined parent directory"
  13. (testing "that it is a child of the parent directory"
  14. (let [parent (create-temp-dir)]
  15. (is (= parent (.getParent (io/file (create-temp-dir parent)))))))))
  16. (defn- throw-if-not-exists [f]
  17. (if (not (fs/exists? f))
  18. (throw (IllegalStateException. (str f " does not exist")))
  19. "some-value-from-function"))
  20. (deftest with-temp-test
  21. (testing "that a tempfile is deleted after use"
  22. (let [f (create-temp-file)]
  23. (is (= "some-value-from-function" (with-temp f (throw-if-not-exists f))))
  24. (is (not (fs/exists? f)))))
  25. (testing "that a tempfile is deleted when body throws"
  26. (let [f (create-temp-file)]
  27. (is (thrown? Exception (with-temp f (throw (Exception. "oh no!")))))
  28. (is (not (fs/exists? f)))))
  29. (testing "that a temp-dir is deleted after use"
  30. (let [d (create-temp-dir)]
  31. (fs/touch (fs/file d "somefile"))
  32. (is (= "some-value-from-function" (with-temp d (throw-if-not-exists d))))
  33. (is (not (fs/exists? (fs/file d "somefile"))))
  34. (is (not (fs/exists? d)))))
  35. (testing "that it can deal with circular symlinks"
  36. (let [f (create-temp-dir)]
  37. (is (= "some-value-from-function"
  38. (with-temp f (let [link-parent (io/file f "foo" "bar")]
  39. (fs/mkdirs link-parent)
  40. (fs/sym-link (io/file link-parent "link-to-the-start") f)
  41. "some-value-from-function"
  42. ))))
  43. (is (not (fs/exists? f))))))