From ee5f2a7a2ef0c715dbb01b8a87d9b3c439db335c Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 8 Nov 2022 12:35:21 +0100 Subject: [PATCH] Add run-test and run-test-var to clojure.test --- CHANGELOG.md | 2 +- src/babashka/impl/clojure/test.clj | 36 ++++++++++++++++++++++++++++++ src/babashka/impl/test.clj | 2 ++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0e73ac8..6c215b33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ A preview of the next release can be installed from ## Unreleased -... +- Add `run-test` and `run-test-var` to `clojure.test` ## 1.0.165 (2022-11-01) diff --git a/src/babashka/impl/clojure/test.clj b/src/babashka/impl/clojure/test.clj index 2810192e..bd2c98b0 100644 --- a/src/babashka/impl/clojure/test.clj +++ b/src/babashka/impl/clojure/test.clj @@ -804,3 +804,39 @@ [summary] (and (zero? (:fail summary 0)) (zero? (:error summary 0)))) + +(defn run-test-var + "Runs the tests for a single Var, with fixtures executed around the test, and summary output after." + {:added "1.11"} + [v] + (sci/binding [report-counters (atom @initial-report-counters)] + (let [ns-obj (-> v meta :ns) + summary (do + (do-report {:type :begin-test-ns + :ns ns-obj}) + (test-vars [v]) + (do-report {:type :end-test-ns + :ns ns-obj}) + (assoc @@report-counters :type :summary))] + (do-report summary) + summary))) + +(defmacro run-test + "Runs a single test. + Because the intent is to run a single test, there is no check for the namespace test-ns-hook." + {:added "1.11"} + [test-symbol] + (let [test-var (sci/resolve @ctx test-symbol)] + (cond + (nil? test-var) + (sci/binding [sci/out sci/err] + (binding [*out* sci/out] + (println "Unable to resolve" test-symbol "to a test function."))) + + (not (-> test-var meta :test)) + (sci/binding [sci/out sci/err] + (binding [*out* sci/out] + (println test-symbol "is not a test."))) + + :else + `(run-test-var ~test-var)))) diff --git a/src/babashka/impl/test.clj b/src/babashka/impl/test.clj index 62b1b494..2d3e95a5 100644 --- a/src/babashka/impl/test.clj +++ b/src/babashka/impl/test.clj @@ -55,6 +55,8 @@ 'test-ns (new-var 'test-ns (contextualize t/test-ns)) ;; running tests: high level 'run-tests (new-var 'run-tests (contextualize t/run-tests)) + 'run-test-var (sci/copy-var t/run-test-var tns) + 'run-test (sci/copy-var t/run-test tns) 'run-all-tests (new-var 'run-all-tests (contextualize t/run-all-tests)) 'successful? (sci/copy-var t/successful? tns) 'with-test-out (sci/copy-var t/with-test-out tns)})