forked from clojure-emacs/cider-nrepl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3824d72
commit 5971cbf
Showing
5 changed files
with
141 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
(ns cider.nrepl.middleware.reload | ||
(:require | ||
[clj-reload.core :as reload] | ||
[clojure.main :refer [repl-caught]] | ||
[clojure.string :as str] | ||
[haystack.analyzer :as analyzer] | ||
[nrepl.middleware.interruptible-eval :refer [*msg*]] | ||
[nrepl.middleware.print :as print] | ||
[nrepl.misc :refer [response-for]] | ||
[nrepl.transport :as transport] | ||
[orchard.misc :as misc])) | ||
|
||
(defn- user-reload | ||
"clj-reload.core/reload from the user project. | ||
Must be configured via clj-reload.core/init before being called." | ||
[] | ||
(some-> (symbol "clj-reload.core" "reload") ;; Don't use mrandorsenized version | ||
resolve)) | ||
|
||
(defn respond | ||
[{:keys [transport] :as msg} response] | ||
(transport/send transport (response-for msg response))) | ||
|
||
(defn- refresh-reply | ||
[{:keys [::print/print-fn transport session id] :as msg}] | ||
(let [{:keys [exec]} (meta session)] | ||
(exec id | ||
(fn [] | ||
(try | ||
(let [reload (or (user-reload) reload/reload)] | ||
(reload (cond-> {:log-fn (fn [& args] | ||
(respond msg {:progress (str/join " " args)}))} | ||
(:all msg) (assoc :only :all))) | ||
(respond msg {:status :ok})) | ||
(catch Throwable error | ||
(respond msg {:status :error | ||
;; TODO assoc :file, :line info if available | ||
:error (analyzer/analyze error print-fn)}) | ||
(binding [*msg* msg | ||
*err* (print/replying-PrintWriter :err msg {})] | ||
(repl-caught error))))) | ||
|
||
(fn [] (respond msg {:status :done}))))) | ||
|
||
(defn handle-reload [handler msg] | ||
(case (:op msg) | ||
"cider.clj-reload/reload" (refresh-reply msg) | ||
"cider.clj-reload/reload-all" (refresh-reply (assoc msg :all true)) | ||
(handler msg))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
(ns cider.nrepl.middleware.reload-test | ||
(:require | ||
[cider.nrepl.middleware.reload :as rl] | ||
[cider.nrepl.test-session :as session] | ||
[clj-reload.core :as reload] | ||
[clojure.test :refer :all])) | ||
|
||
(use-fixtures :each session/session-fixture) | ||
|
||
(def ^:private dirs-to-reload | ||
;; Limit the scope of what we reload, because (for example) reloading the | ||
;; cider.nrepl.middleware.test-session ns causes *session* in that ns to be | ||
;; unloaded, which breaks session-fixture, and hence all of the below tests. | ||
["test/clj/cider/nrepl/middleware/util"]) | ||
|
||
(reload/init {:dirs dirs-to-reload}) | ||
|
||
(deftest user-reload | ||
(testing "returns nil if clojure.tools.namespace isn't loaded" | ||
(with-redefs [resolve (constantly nil)] | ||
(is (nil? (#'rl/user-reload)))))) | ||
|
||
(deftest reload-op-test | ||
(testing "reload op works" | ||
(let [response (session/message {:op "cider.clj-reload/reload"})] | ||
;; There is nothing to reload since the files did not change, | ||
;; but the message does come from clj-reload.core/reload. | ||
(is (= "Nothing to reload" (:progress response))) | ||
(is (= #{"done" "ok"} (:status response)))))) | ||
|
||
(deftest reload-all-op-test | ||
(testing "reload-all op works" | ||
(let [response (session/message {:op "cider.clj-reload/reload-all"})] | ||
(is (seq (:progress response))) | ||
(is (= #{"done" "ok"} (:status response)))))) | ||
|