From 09e7c78d2278aeb8fec05d7a979980a0d3176add Mon Sep 17 00:00:00 2001 From: anatoly Date: Sun, 15 Nov 2015 22:28:34 -0500 Subject: [PATCH] adding m/start-with --- dev/dev.clj | 5 ++- src/mount/mount.clj | 41 ++++++++++++------- test/check/start_with_test.clj | 28 +++++++++++++ ...ithout_test.clj => start_without_test.clj} | 2 +- 4 files changed, 60 insertions(+), 16 deletions(-) create mode 100644 test/check/start_with_test.clj rename test/check/{without_test.clj => start_without_test.clj} (93%) diff --git a/dev/dev.clj b/dev/dev.clj index 63e82db..7988ca9 100644 --- a/dev/dev.clj +++ b/dev/dev.clj @@ -14,11 +14,14 @@ ;; [clojure.core.async :refer [>!! ! > states (sort-by (comp :order meta) order) (map #(fun % (meta %))) doall)) +(defn- rollback! [state] + (let [{:keys [origin start stop sub?]} (meta state)] + (when origin + (alter-meta! state assoc :origin nil + :start (or (:start origin) start) + :stop (or (:stop origin) stop))))) + +(defn- unsub [state] + (when (-> (meta state) :sub?) + (alter-meta! state assoc :sub? nil + :started false))) + +(defn- substitute! [state with] + (let [{:keys [start stop] :as origin} (meta state) + m-with (meta with)] + (alter-meta! with assoc :sub? true :started? true) ;; pre: called by "start-with" + (alter-meta! state assoc :origin {:start start + :stop stop} + :start (:start m-with) + :stop (:stop m-with)))) + (defn start [& states] (let [states (or (seq states) (find-all-states))] (bring states up <) @@ -93,7 +106,9 @@ (defn stop [& states] (let [states (or states (find-all-states))] + (doall (map unsub states)) ;; unmark substitutions marked by "start-with" (bring states down >) + (doall (map rollback! states)) ;; restore to origin from "start-with" :stopped)) (defn start-with-args [xs & states] @@ -103,12 +118,10 @@ (start))) (defn start-with [with] - (if (seq with) - (let [app (find-all-states)] - ;; needs more thinking on merging, since the ns should not change - ;; because it could be used in other states, so only start/stop need to be merged - (warn "substituting states is not _yet_ implemented") - (start)) + (let [app (find-all-states)] + (doall + (for [[from to] with] + (substitute! from to))) (start))) (defn start-without [& states] diff --git a/test/check/start_with_test.clj b/test/check/start_with_test.clj new file mode 100644 index 0000000..7fa8b85 --- /dev/null +++ b/test/check/start_with_test.clj @@ -0,0 +1,28 @@ +(ns check.start-with-test + (:require [mount :refer [defstate] :as m] + [app.config :refer [app-config]] + [app.nyse :refer [conn]] + [app :refer [nrepl]] + [clojure.test :refer :all])) + +(defstate test-conn :start (long 42) + :stop (constantly 0)) + +(defstate test-nrepl :start (vector)) + +(deftest start-with + + (testing "should start with substitutes" + (let [_ (m/start-with {#'app.nyse/conn #'check.start-with-test/test-conn + #'app/nrepl #'check.start-with-test/test-nrepl})] + (is (map? app-config)) + (is (vector? nrepl)) + (is (= conn 42)) + (mount/stop))) + + (testing "should start normally after start-with" + (let [_ (m/start)] + (is (map? app-config)) + (is (instance? clojure.tools.nrepl.server.Server nrepl)) + (is (instance? datomic.peer.LocalConnection conn)) + (mount/stop)))) diff --git a/test/check/without_test.clj b/test/check/start_without_test.clj similarity index 93% rename from test/check/without_test.clj rename to test/check/start_without_test.clj index fea64d4..f99264c 100644 --- a/test/check/without_test.clj +++ b/test/check/start_without_test.clj @@ -1,4 +1,4 @@ -(ns check.without-test +(ns check.start-without-test (:require [mount :as m] [app.config :refer [app-config]] [app.nyse :refer [conn]]