diff --git a/src/leiningen/clean.clj b/src/leiningen/clean.clj index c9477d1af..9fd6f18c7 100644 --- a/src/leiningen/clean.clj +++ b/src/leiningen/clean.clj @@ -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? @@ -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 @@ -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 diff --git a/test/leiningen/test/clean.clj b/test/leiningen/test/clean.clj index aa8c2a16d..4eab5c782 100644 --- a/test/leiningen/test/clean.clj +++ b/test/leiningen/test/clean.clj @@ -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)) @@ -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." @@ -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]] @@ -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 diff --git a/test/leiningen/test/helper.clj b/test/leiningen/test/helper.clj index bbf4966a3..11b444477 100644 --- a/test/leiningen/test/helper.clj +++ b/test/leiningen/test/helper.clj @@ -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)))