From ce5f5ed90a22623fa2d97d10ae7583f6e682169f Mon Sep 17 00:00:00 2001 From: Chris Perkins Date: Sat, 21 Jan 2017 14:33:35 -0700 Subject: [PATCH] Do not start a DerefableState as a side-effect of printing See: https://github.com/clojure/clojure/blob/b80e1fe4b14654d943e2f8b060b0bc56e18b4757/src/clj/clojure/core_print.clj#L422-L446 --- src/mount/core.cljc | 14 ++++++++++---- test/core/mount/test.cljc | 2 ++ test/core/mount/test/printing.cljc | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 test/core/mount/test/printing.cljc diff --git a/src/mount/core.cljc b/src/mount/core.cljc index e1d57d4..af94f4e 100644 --- a/src/mount/core.cljc +++ b/src/mount/core.cljc @@ -104,6 +104,9 @@ (swap! running dissoc state) (update-meta! [state :status] #{:stopped}))) +(defn running-states [] + (set (keys @running))) + (deftype DerefableState [name] #?(:clj clojure.lang.IDeref :cljs IDeref) @@ -113,7 +116,13 @@ (let [{:keys [status inst] :as state} (@meta-state name)] (when-not (:started status) (up name state (atom #{}))) - @inst))) + @inst)) + #?(:clj clojure.lang.IPending + :cljs IPending) + (#?(:clj isRealized + :cljs -realized?) + [_] + (boolean ((running-states) name)))) (defn on-reload-meta [s-var] (or (-> s-var meta :on-reload) @@ -181,9 +190,6 @@ (with-ns ns name)) v))) -(defn running-states [] - (set (keys @running))) - (defn- unvar-state [s] (->> s (drop 2) (apply str))) ;; magic 2 is removing "#'" in state name diff --git a/test/core/mount/test.cljc b/test/core/mount/test.cljc index 1377e3a..04a29fe 100644 --- a/test/core/mount/test.cljc +++ b/test/core/mount/test.cljc @@ -13,6 +13,7 @@ mount.test.start-without mount.test.start-with mount.test.start-with-states + mount.test.printing )) #?(:clj (alter-meta! *ns* assoc ::load false)) @@ -31,6 +32,7 @@ 'mount.test.start-without 'mount.test.start-with 'mount.test.start-with-states + 'mount.test.printing )) (defn run-tests [] diff --git a/test/core/mount/test/printing.cljc b/test/core/mount/test/printing.cljc new file mode 100644 index 0000000..ea1a4b7 --- /dev/null +++ b/test/core/mount/test/printing.cljc @@ -0,0 +1,17 @@ +(ns mount.test.printing + (:require + #?@(:cljs [[cljs.test :as t :refer-macros [is are deftest testing use-fixtures]] + [mount.core :as mount :refer-macros [defstate]]] + :clj [[clojure.test :as t :refer [is are deftest testing use-fixtures]] + [mount.core :as mount :refer [defstate]]]))) + +#?(:clj (alter-meta! *ns* assoc ::load false)) + +(defstate foo + :start (do (println "Starting!") 42)) + +(deftest test-printing-has-no-side-effects + ;; Test that printing an unstarted DerefableState does not have the + ;; side-effect of starting it + (println foo) + (is (not= 42 foo)))