From 61fd4de9db1364c6ceda8c5acbbaa874a1f34adb Mon Sep 17 00:00:00 2001 From: Alex Hall Date: Fri, 29 Apr 2016 21:56:23 +0000 Subject: [PATCH] Catch and handle fixture errors during tests --- src/leiningen/test.clj | 19 +++++++++++++++++++ test/leiningen/test/helper.clj | 2 ++ test/leiningen/test/test.clj | 5 +++++ .../sample-fixture-error/project.clj | 7 +++++++ .../sample-fixture-error/test/test_a.clj | 13 +++++++++++++ .../sample-fixture-error/test/test_b.clj | 11 +++++++++++ .../sample-fixture-error/test/test_c.clj | 7 +++++++ 7 files changed, 64 insertions(+) create mode 100644 test_projects/sample-fixture-error/project.clj create mode 100644 test_projects/sample-fixture-error/test/test_a.clj create mode 100644 test_projects/sample-fixture-error/test/test_b.clj create mode 100644 test_projects/sample-fixture-error/test/test_c.clj diff --git a/src/leiningen/test.clj b/src/leiningen/test.clj index 9f8120e8..f3062896 100644 --- a/src/leiningen/test.clj +++ b/src/leiningen/test.clj @@ -74,6 +74,25 @@ (let [failures# (atom {}) selected-namespaces# ~(form-for-nses-selectors-match selectors ns-sym) _# (when ~*monkeypatch?* + (leiningen.core.injected/add-hook + #'clojure.test/test-ns + (fn [test-ns# ns#] + (try + (test-ns# ns#) + (catch Throwable t# + (binding [clojure.test/*report-counters* + (ref clojure.test/*initial-report-counters*) + clojure.test/*testing-vars* + (list (with-meta 'test + {:name ns# + :ns ns#}))] + (clojure.test/do-report {:type :error + :message "Uncaught exception in test fixture" + :expected nil + :actual t#}) + (clojure.test/do-report {:type :end-test-ns + :ns (the-ns ns#)}) + @clojure.test/*report-counters*))))) (leiningen.core.injected/add-hook #'clojure.test/report (fn [report# m# & args#] diff --git a/test/leiningen/test/helper.clj b/test/leiningen/test/helper.clj index 4abaff6f..95403ab1 100644 --- a/test/leiningen/test/helper.clj +++ b/test/leiningen/test/helper.clj @@ -41,6 +41,8 @@ (def sample-reader-cond-project (read-test-project "sample-reader-cond")) +(def sample-fixture-error-project (read-test-project "sample-fixture-error")) + (def tricky-name-project (read-test-project "tricky-name")) (def native-project (read-test-project "native")) diff --git a/test/leiningen/test/test.clj b/test/leiningen/test/test.clj index ff2198a5..e7e278b3 100644 --- a/test/leiningen/test/test.clj +++ b/test/leiningen/test/test.clj @@ -5,6 +5,7 @@ [leiningen.test.helper :refer [tmp-dir sample-no-aot-project sample-reader-cond-project sample-failing-project + sample-fixture-error-project with-system-err-str]] [clojure.java.io :as io] [leiningen.core.main :as main] @@ -97,3 +98,7 @@ (is (= "EOF while reading" (try (test project) false (catch Exception e (.getMessage e)))))))) + +(deftest test-catch-fixture-errors + (test sample-fixture-error-project) + (is (= (ran?) #{:test-a :test-c}))) diff --git a/test_projects/sample-fixture-error/project.clj b/test_projects/sample-fixture-error/project.clj new file mode 100644 index 00000000..4dfc2e51 --- /dev/null +++ b/test_projects/sample-fixture-error/project.clj @@ -0,0 +1,7 @@ +;; This project is used for leiningen's test suite, so don't change +;; any of these values without updating the relevant tests. If you +;; just want a basic project to work from, generate a new one with +;; "lein new". + +(defproject sample-fixture-error "0.1.0-SNAPSHOT" + :dependencies [[org.clojure/clojure "1.8.0"]]) diff --git a/test_projects/sample-fixture-error/test/test_a.clj b/test_projects/sample-fixture-error/test/test_a.clj new file mode 100644 index 00000000..9a076465 --- /dev/null +++ b/test_projects/sample-fixture-error/test/test_a.clj @@ -0,0 +1,13 @@ +(ns test-a + (:require [clojure.test :refer :all] + [clojure.java.io :refer [writer]])) + +(defn record-ran [t] + (let [file-name (format "%s/lein-test-ran" + (System/getProperty "java.io.tmpdir"))] + (with-open [w (writer file-name :append true)] + (.write w (str t "\n"))))) + +(deftest test-a + (record-ran :test-a) + (is (= 1 1))) diff --git a/test_projects/sample-fixture-error/test/test_b.clj b/test_projects/sample-fixture-error/test/test_b.clj new file mode 100644 index 00000000..5b899de1 --- /dev/null +++ b/test_projects/sample-fixture-error/test/test_b.clj @@ -0,0 +1,11 @@ +(ns test-b + (:require [clojure.test :refer :all] + [test-a :refer [record-ran]])) + +(use-fixtures :once + (fn [& _] + (throw (Exception. "Don't panic. This is an expected exception.")))) + +(deftest test-b + (record-ran :test-b) + (is (= 1 1))) diff --git a/test_projects/sample-fixture-error/test/test_c.clj b/test_projects/sample-fixture-error/test/test_c.clj new file mode 100644 index 00000000..601f7de4 --- /dev/null +++ b/test_projects/sample-fixture-error/test/test_c.clj @@ -0,0 +1,7 @@ +(ns test-c + (:require [clojure.test :refer :all] + [test-a :refer (record-ran)])) + +(deftest test-c + (record-ran :test-c) + (is (= 1 1)))