Fix #1688: use-fixtures should add metadata to *ns* (#1689)

* Fix #1688: use-fixtures should add metadata to *ns*

* fix test
This commit is contained in:
Michiel Borkent 2024-04-28 16:22:47 +02:00 committed by GitHub
parent ba35032253
commit b47a5cc21d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 12 deletions

View file

@ -7,6 +7,10 @@ A preview of the next release can be installed from
[Babashka](https://github.com/babashka/babashka): Native, fast starting Clojure interpreter for scripting
## Unreleased
- Fix [#1688](https://github.com/babashka/babashka/issues/1688): use-fixtures should add metadata to `*ns*`
## 1.3.190 (2024-04-17)
- Fix [#1679](https://github.com/babashka/babashka/issues/1679): bump timbre and fix wrapping `timbre/log!`

View file

@ -660,14 +660,12 @@
;;; DEFINING FIXTURES
(def ^:private ns->fixtures (atom {}))
(defn- add-ns-meta
"Adds elements in coll to the current namespace metadata as the
value of key."
{:added "1.1"}
[key coll]
(swap! ns->fixtures assoc-in [(sci-namespaces/sci-ns-name @sci/ns) key] coll))
(alter-meta! @sci/ns assoc key coll))
(defmulti use-fixtures
"Wrap test runs in a fixture function to perform setup and
@ -677,10 +675,10 @@
(fn [fixture-type & args] fixture-type))
(defmethod use-fixtures :each [fixture-type & args]
(add-ns-meta ::each-fixtures args))
(add-ns-meta :clojure.test/each-fixtures args))
(defmethod use-fixtures :once [fixture-type & args]
(add-ns-meta ::once-fixtures args))
(add-ns-meta :clojure.test/once-fixtures args))
(defn- default-fixture
"The default, empty, fixture function. Just calls its argument."
@ -731,10 +729,8 @@
[vars]
(doseq [[ns vars] (group-by (comp :ns meta) vars)
:when ns]
(let [ns-name (sci-namespaces/sci-ns-name ns)
fixtures (get @ns->fixtures ns-name)
once-fixture-fn (join-fixtures (::once-fixtures fixtures))
each-fixture-fn (join-fixtures (::each-fixtures fixtures))]
(let [once-fixture-fn (join-fixtures (:clojure.test/once-fixtures (meta ns)))
each-fixture-fn (join-fixtures (:clojure.test/each-fixtures (meta ns)))]
(once-fixture-fn
(fn []
(doseq [v vars]

View file

@ -37,8 +37,12 @@
(deftest fixtures-test
(let [output (bb "
(require '[clojure.test :as t])
(defn once [f] (prn :once-before) (f) (prn :once-after))
(defn each [f] (prn :each-before) (f) (prn :each-after))
(defn once [f] (prn :once-before) (f)
(prn :once-after)
(prn (some? (::t/once-fixtures (meta *ns*)))))
(defn each [f] (prn :each-before) (f) (prn :each-after)
(prn (some? (::t/each-fixtures (meta *ns*)))))
(t/use-fixtures :once once)
(t/use-fixtures :each each)
(t/deftest foo)
@ -48,9 +52,12 @@
:once-before
:each-before
:each-after
true
:each-before
:each-after
:once-after")))))
true
:once-after
true")))))
(deftest with-test
(let [output (bb "