From 376a275cea9d5c00d7564df13d01bfcf9170128c Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Thu, 1 Aug 2024 14:23:41 -0500 Subject: [PATCH 1/5] unstrument before instrumenting --- src/malli/dev.clj | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/malli/dev.clj b/src/malli/dev.clj index 557610b64..2bd125633 100644 --- a/src/malli/dev.clj +++ b/src/malli/dev.clj @@ -47,15 +47,16 @@ (-capture-fail! options) (mi/collect! {:ns (all-ns)}) (let [watch (bound-fn [_ _ old new] - (->> (for [[n d] (:clj new) - :let [no (get-in old [:clj n])] - [s d] d - :when (not= d (get no s))] - [[n s] d]) - (into {}) - (reduce-kv assoc-in {}) - (assoc options :data) - (mi/instrument!)) + (let [options (->> (for [[n d] (:clj new) + :let [no (get-in old [:clj n])] + [s d] d + :when (not= d (get no s))] + [[n s] d]) + (into {}) + (reduce-kv assoc-in {}) + (assoc options :data))] + (mi/unstrument! options) + (mi/instrument! options)) (clj-kondo/emit! options))] (add-watch @#'m/-function-schemas* ::watch watch)) (let [count (->> (mi/instrument! options) (count))] From bff45711be88c930241f15edc23907cb6d1c04e3 Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Thu, 1 Aug 2024 14:28:43 -0500 Subject: [PATCH 2/5] try --- src/malli/dev.clj | 19 +++++++++---------- src/malli/instrument.clj | 9 ++++++--- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/malli/dev.clj b/src/malli/dev.clj index 2bd125633..557610b64 100644 --- a/src/malli/dev.clj +++ b/src/malli/dev.clj @@ -47,16 +47,15 @@ (-capture-fail! options) (mi/collect! {:ns (all-ns)}) (let [watch (bound-fn [_ _ old new] - (let [options (->> (for [[n d] (:clj new) - :let [no (get-in old [:clj n])] - [s d] d - :when (not= d (get no s))] - [[n s] d]) - (into {}) - (reduce-kv assoc-in {}) - (assoc options :data))] - (mi/unstrument! options) - (mi/instrument! options)) + (->> (for [[n d] (:clj new) + :let [no (get-in old [:clj n])] + [s d] d + :when (not= d (get no s))] + [[n s] d]) + (into {}) + (reduce-kv assoc-in {}) + (assoc options :data) + (mi/instrument!)) (clj-kondo/emit! options))] (add-watch @#'m/-function-schemas* ::watch watch)) (let [count (->> (mi/instrument! options) (count))] diff --git a/src/malli/instrument.clj b/src/malli/instrument.clj index 0eb3e80ae..8dd7694f0 100644 --- a/src/malli/instrument.clj +++ b/src/malli/instrument.clj @@ -5,7 +5,8 @@ (defn -find-var [n s] (find-var (symbol (str n "/" s)))) (defn -sequential [x] (cond (set? x) x (sequential? x) x :else [x])) -(defn -original [v] (let [f (deref v)] (-> f meta ::original (or f)))) +(defn -f->original [f] (-> f meta ::original (or f))) +(defn -original [v] (let [f (deref v)] (-f->original f))) (defn -filter-ns [& ns] (fn [n _ _] ((set ns) n))) (defn -filter-var [f] (fn [n s _] (f (-find-var n s)))) @@ -24,8 +25,10 @@ (cond (and gen (true? (:gen d))) (assoc $ :gen gen) (true? (:gen d)) (dissoc $ :gen) :else $))] - (alter-var-root v (fn [f] (-> (m/-instrument dgen f) (with-meta {::original f}))))) - :unstrument (alter-var-root v (fn [f] (-> f meta ::original (or f)))) + (alter-var-root v (fn [f] + (let [f (-f->original f)] + (-> (m/-instrument dgen f) (with-meta {::original f})))))) + :unstrument (alter-var-root v -f->original) (mode v d)) v)))))) From fda9a1626d9f0e1e80216d0a8eda898d1a6f70ba Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Thu, 1 Aug 2024 14:31:03 -0500 Subject: [PATCH 3/5] wip --- test/malli/instrument_test.clj | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/malli/instrument_test.clj b/test/malli/instrument_test.clj index e440ca416..56da73781 100644 --- a/test/malli/instrument_test.clj +++ b/test/malli/instrument_test.clj @@ -104,6 +104,9 @@ (select-keys {:a 1} :a)))) (testing "With instrumentation" + ;; intentionally bad annotation ... + (m/=> clojure.core/select-keys [:=> [:cat] string?]) + ;; that is automatically unstrumented (m/=> clojure.core/select-keys [:=> [:cat map? sequential?] map?]) (with-out-str (mi/instrument! {:filters [(mi/-filter-ns 'clojure.core)]})) (is (thrown-with-msg? From 19caf12c8636e6db675ff49f74b8fd748368ff8c Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Thu, 1 Aug 2024 14:31:32 -0500 Subject: [PATCH 4/5] test --- test/malli/instrument_test.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/malli/instrument_test.clj b/test/malli/instrument_test.clj index 56da73781..c115357ab 100644 --- a/test/malli/instrument_test.clj +++ b/test/malli/instrument_test.clj @@ -106,7 +106,7 @@ (testing "With instrumentation" ;; intentionally bad annotation ... (m/=> clojure.core/select-keys [:=> [:cat] string?]) - ;; that is automatically unstrumented + ;; that is automatically unstrumented before we instrument with the correct annotation (m/=> clojure.core/select-keys [:=> [:cat map? sequential?] map?]) (with-out-str (mi/instrument! {:filters [(mi/-filter-ns 'clojure.core)]})) (is (thrown-with-msg? From 134e0f450caba4e7395a8efa9952ef88ec57aa6c Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Thu, 1 Aug 2024 15:47:41 -0500 Subject: [PATCH 5/5] fix test --- test/malli/instrument_test.clj | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/test/malli/instrument_test.clj b/test/malli/instrument_test.clj index c115357ab..87be0f3e0 100644 --- a/test/malli/instrument_test.clj +++ b/test/malli/instrument_test.clj @@ -104,9 +104,6 @@ (select-keys {:a 1} :a)))) (testing "With instrumentation" - ;; intentionally bad annotation ... - (m/=> clojure.core/select-keys [:=> [:cat] string?]) - ;; that is automatically unstrumented before we instrument with the correct annotation (m/=> clojure.core/select-keys [:=> [:cat map? sequential?] map?]) (with-out-str (mi/instrument! {:filters [(mi/-filter-ns 'clojure.core)]})) (is (thrown-with-msg? @@ -116,3 +113,16 @@ (select-keys {:a 1} :a))) (is (= {:a 1} (select-keys {:a 1} [:a]))) (with-out-str (mi/unstrument! {:filters [(mi/-filter-ns 'clojure.core)]})))) + +(defn reinstrumented [] 1) + +(deftest reinstrument-test + (m/=> reinstrumented [:-> [:= 2]]) + (instrument!) + (is (thrown-with-msg? + Exception + #":malli\.core/invalid-output" + (reinstrumented))) + (m/=> reinstrumented [:-> [:= 1]]) + (instrument!) + (is (= 1 (reinstrumented))))