Skip to content

Commit

Permalink
Use main/abort instead of throwing exception in clean.
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy committed Dec 5, 2014
1 parent 26dff19 commit bebce21
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
22 changes: 11 additions & 11 deletions src/leiningen/clean.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns leiningen.clean
"Remove all files from project's target-path."
(:require [clojure.java.io :as io]
[leiningen.core.utils :as utils])
[leiningen.core.utils :as utils]
[leiningen.core.main :as main])
(:import [java.io IOException]))

(defn real-directory?
Expand Down Expand Up @@ -59,10 +60,11 @@
[project]
(-> project :clean-targets meta (get :protect true)))

(defn- error-msg [pre]
(str pre " "
"Check :clean-targets or override this behavior by adding metadata -> "
":clean-targets ^{:protect false} [...targets...]"))
(defn- error-msg [& args]
(apply str (concat args
"\nCheck :clean-targets"
" or override this behavior by adding metadata ->"
"\n :clean-targets ^{:protect false} [...targets...]")))

(defn- sanity-check
"Ensure that a clean-target string refers to a directory that is sensible to
Expand All @@ -71,13 +73,11 @@
(when (and (string? clean-target)
(protect-clean-targets? project))
(cond (not (ancestor? (:root project) clean-target))
(throw (IOException.
(error-msg
(format "Deleting a path outside of the project root [\"%s\"] is not allowed." clean-target))))
(main/abort (error-msg "Deleting path outside of the project root [\""
clean-target "\"] is not allowed."))
(protected-path? project clean-target)
(throw (IOException.
(error-msg
(format "Deleting non-target project paths [\"%s\"] is not allowed." clean-target)))))))
(main/abort (error-msg "Deleting non-target project paths [\""
clean-target "%s\"] is not allowed.")))))

(defn- with-parent-target-path
"Assoc the :target-path sans the profile suffix, if any format
Expand Down
15 changes: 8 additions & 7 deletions test/leiningen/test/clean.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:use [clojure.test]
[clojure.java.io :only [file make-parents writer]]
[leiningen.clean :only [clean]]
[leiningen.test.helper :only [sample-project]])
[leiningen.test.helper :only [sample-project noerr-fixture]])
(:require [leiningen.core.project :as project]))

(def target-1 (:target-path sample-project))
Expand All @@ -25,7 +25,8 @@

;; The original delete-file-recursively is potentially destructive, so let's mock it.
(with-redefs [leiningen.clean/delete-file-recursively mock-delete-files]
(f))))
(f)))
noerr-fixture)

(defn assert-cleaned
"Asserts that the mock was called for the given target path."
Expand Down Expand Up @@ -77,8 +78,8 @@
(let [modified-project
(assoc sample-project
:clean-targets [test-dir])]
(is (thrown-with-msg? java.io.IOException #"project root"
(clean modified-project))))))
(is (thrown-with-msg? clojure.lang.ExceptionInfo #"project root"
(clean modified-project))))))

(testing "should not delete protected project paths"
(doseq [path-key [:test-paths :resource-paths :source-paths :java-source-paths]]
Expand All @@ -87,21 +88,21 @@
(assoc sample-project
path-key [test-path]
:clean-targets [test-path])]
(is (thrown-with-msg? java.io.IOException #"non-target"
(is (thrown-with-msg? clojure.lang.ExceptionInfo #"non-target"
(clean modified-project))))))

(testing "should not delete project.clj"
(let [modified-project
(assoc sample-project
:clean-targets [(relative-to-absolute-project-path "project.clj")])]
(is (thrown-with-msg? java.io.IOException #"non-target"
(is (thrown-with-msg? clojure.lang.ExceptionInfo #"non-target"
(clean modified-project)))))

(testing "should not delete docs"
(let [modified-project
(assoc sample-project
:clean-targets [(relative-to-absolute-project-path "doc/stuff.doc")])]
(is (thrown-with-msg? java.io.IOException #"non-target"
(is (thrown-with-msg? clojure.lang.ExceptionInfo #"non-target"
(clean modified-project))))))

(deftest test-protect-metadata-override
Expand Down
4 changes: 4 additions & 0 deletions test/leiningen/test/helper.clj
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,7 @@
[filename f]
(with-open [z (java.util.zip.ZipFile. filename)]
(reduce #(conj %1 (f %2)) [] (entries z))))

(defn noerr-fixture [f]
(binding [*err* (java.io.StringWriter.)]
(f)))

0 comments on commit bebce21

Please sign in to comment.