Skip to content

Commit b920e66

Browse files
plexusbbatsov
authored andcommitted
Add REPL helpers: repl-session! / close-session!
Many tests make use of `session-fixture`, which sets up an nREPL session, and tears it down again. When working on tests this makes it tedious to quickly evaluate test forms. These helper functions set up the nREPL session similar to `session-fixture` but allow you to manage the start/stop lifecycle yourself, so you can start the session once and get to work.
1 parent 7b646fb commit b920e66

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

test/common/cider/nrepl/test_session.clj

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,44 @@
88
(def ^:dynamic *handler* cider-nrepl-handler)
99
(def ^:dynamic *session* nil)
1010

11+
(def ^:dynamic *server* nil)
12+
(def ^:dynamic *transport* nil)
13+
14+
(defn repl-session!
15+
"Start an nREPL session and set *session* accordingly.
16+
17+
Eval'ing this function in the REPL will allow you to test out messages
18+
with [[message]].
19+
20+
When dealing with tests that use [[session-fixture]], this can help you to be
21+
able to evaluate test forms in the REPL. Call [[close-session!]] when you're
22+
done."
23+
[]
24+
(let [server (start-server :handler *handler*)
25+
transport (nrepl/connect :port (:port server))
26+
client (nrepl/client transport Long/MAX_VALUE)]
27+
(alter-var-root #'*server* (constantly server))
28+
(alter-var-root #'*transport* (constantly transport))
29+
(alter-var-root #'*session* (constantly (nrepl/client-session client)))))
30+
31+
(defn close-session!
32+
"Stop the server/session created by [[repl-session!]], and reset the vars."
33+
[]
34+
(.close *server*)
35+
(.close *transport*)
36+
(alter-var-root #'*server* (constantly nil))
37+
(alter-var-root #'*transport* (constantly nil))
38+
(alter-var-root #'*session* (constantly nil)))
39+
1140
(defn session-fixture
1241
[f]
13-
(with-open [server (start-server :handler *handler*)
42+
(with-open [server (start-server :handler *handler*)
1443
transport (nrepl/connect :port (:port server))]
15-
(let [client (nrepl/client transport Long/MAX_VALUE)]
16-
(binding [*session* (nrepl/client-session client)]
44+
(let [client (nrepl/client transport Long/MAX_VALUE)
45+
session (nrepl/client-session client)]
46+
(binding [*server* server
47+
*transport* transport
48+
*session* session]
1749
(f)))))
1850

1951
(defn message

0 commit comments

Comments
 (0)