Skip to content

Commit bdb714a

Browse files
author
Yogthos
committed
improve deprecation warning tests
1 parent e289ca1 commit bdb714a

2 files changed

Lines changed: 28 additions & 17 deletions

File tree

src/selmer/util.clj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,8 @@ so it can access vectors as well as maps."
300300

301301
(defonce ^:private warned-keys (atom #{}))
302302

303-
(defn- log-deprecation-warning
304-
"Emit a deprecation warning. Tries clojure.tools.logging first, falls back to stderr."
303+
(defn- default-deprecation-warning-handler
304+
"Default handler that tries clojure.tools.logging first, falls back to stderr."
305305
[message]
306306
(try
307307
(require 'clojure.tools.logging)
@@ -315,6 +315,8 @@ so it can access vectors as well as maps."
315315
(binding [*out* *err*]
316316
(println "DEPRECATION WARNING:" message)))))
317317

318+
(def ^:dynamic *deprecation-warning-handler* default-deprecation-warning-handler)
319+
318320
(defn deprecated-key-lookup
319321
"Looks up a key in context-map, preferring the namespaced version (e.g. :selmer/async)
320322
but falling back to the non-namespaced version (e.g. :async) with a deprecation warning.
@@ -326,7 +328,7 @@ so it can access vectors as well as maps."
326328
(when (and *warn-on-deprecated-keys*
327329
(not (contains? @warned-keys non-namespaced-key)))
328330
(swap! warned-keys conj non-namespaced-key)
329-
(log-deprecation-warning
331+
(*deprecation-warning-handler*
330332
(str "Using " non-namespaced-key " in context is deprecated. "
331333
"Please use " namespaced-key " instead.")))
332334
(get context-map non-namespaced-key))))

test/selmer/core_test.clj

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -523,30 +523,39 @@
523523
(deftest script-deprecated-key-warning
524524
;; Reset warned-keys so warnings are emitted fresh
525525
(reset! @#'selmer.util/warned-keys #{})
526-
(let [sw (java.io.StringWriter.)]
527-
(binding [*err* sw]
526+
(let [warnings (atom [])]
527+
(binding [*deprecation-warning-handler* #(swap! warnings conj %)]
528528
(render "{% script \"/js/site.js\" %}" {:async true}))
529-
(is (str/includes? (str sw) "DEPRECATION WARNING"))
530-
(is (str/includes? (str sw) ":selmer/async")))
529+
(is (= 1 (count @warnings)))
530+
(is (str/includes? (first @warnings) ":selmer/async")))
531531
;; Second call should NOT warn again (once per key per session)
532-
(let [sw (java.io.StringWriter.)]
533-
(binding [*err* sw]
532+
(let [warnings (atom [])]
533+
(binding [*deprecation-warning-handler* #(swap! warnings conj %)]
534534
(render "{% script \"/js/site.js\" %}" {:async true}))
535-
(is (= "" (str sw))))
535+
(is (= 0 (count @warnings))))
536536
;; Reset and test defer warning
537537
(reset! @#'selmer.util/warned-keys #{})
538-
(let [sw (java.io.StringWriter.)]
539-
(binding [*err* sw]
538+
(let [warnings (atom [])]
539+
(binding [*deprecation-warning-handler* #(swap! warnings conj %)]
540540
(render "{% script \"/js/site.js\" %}" {:defer true}))
541-
(is (str/includes? (str sw) "DEPRECATION WARNING"))
542-
(is (str/includes? (str sw) ":selmer/defer")))
541+
(is (= 1 (count @warnings)))
542+
(is (str/includes? (first @warnings) ":selmer/defer")))
543543
;; No warning when *warn-on-deprecated-keys* is false
544544
(reset! @#'selmer.util/warned-keys #{})
545-
(let [sw (java.io.StringWriter.)]
546-
(binding [*err* sw
545+
(let [warnings (atom [])]
546+
(binding [*deprecation-warning-handler* #(swap! warnings conj %)
547547
*warn-on-deprecated-keys* false]
548548
(render "{% script \"/js/site.js\" %}" {:async true}))
549-
(is (= "" (str sw)))))
549+
(is (= 0 (count @warnings)))))
550+
551+
(deftest script-deprecated-key-warning-default-handler
552+
;; Verify the default handler writes to stderr (the println fallback path)
553+
(reset! @#'selmer.util/warned-keys #{})
554+
(let [sw (java.io.StringWriter.)]
555+
(binding [*err* sw]
556+
(render "{% script \"/js/site.js\" %}" {:async true}))
557+
(is (str/includes? (str sw) "DEPRECATION WARNING"))
558+
(is (str/includes? (str sw) ":selmer/async"))))
550559

551560
(deftest cycle-test
552561
(is

0 commit comments

Comments
 (0)