Skip to content

Commit 5b26e27

Browse files
committed
Use session exec in test middleware
1 parent af84959 commit 5b26e27

File tree

2 files changed

+64
-78
lines changed

2 files changed

+64
-78
lines changed

src/cider/nrepl/middleware/test.clj

Lines changed: 63 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -263,47 +263,35 @@
263263
the nREPL session."
264264
(atom {}))
265265

266-
(defmacro with-interruptible-eval
267-
"Run body mimicking interruptible-eval."
268-
[msg & body]
269-
`(let [session# (:session ~msg)]
270-
;; Before tools.nrepl-0.2.10, `queue-eval` was private.
271-
(@#'ie/queue-eval session# (:executor ~msg)
272-
(fn []
273-
(alter-meta! session# assoc
274-
:thread (Thread/currentThread)
275-
:eval-msg ~msg)
276-
(binding [ie/*msg* ~msg]
277-
(with-bindings @session#
278-
~@body)
279-
(alter-meta! session# dissoc :thread :eval-msg))))))
280-
281266
(defn handle-test-var-query-op
282-
[{:keys [var-query transport] :as msg}]
283-
(with-interruptible-eval
284-
msg
285-
(try
286-
(let [stringify-msg (fn [report]
287-
(walk/postwalk (fn [x] (if (and (map? x)
288-
(contains? x :message))
289-
(update x :message str)
290-
x))
291-
report))
292-
report (-> var-query
293-
(assoc-in [:ns-query :has-tests?] true)
294-
(assoc :test? true)
295-
(util.coerce/var-query)
296-
test-var-query
297-
stringify-msg)]
298-
(reset! results (:results report))
299-
(t/send transport (response-for msg (u/transform-value report))))
300-
(catch clojure.lang.ExceptionInfo e
301-
(let [d (ex-data e)]
302-
(if (::util.coerce/id d)
303-
(case (::util.coerce/id d)
304-
:namespace-not-found (t/send transport (response-for msg :status :namespace-not-found)))
305-
(throw e)))))
306-
(t/send transport (response-for msg :status :done))))
267+
[{:keys [var-query transport session id] :as msg}]
268+
(let [{:keys [exec]} (meta session)]
269+
(exec id
270+
(fn []
271+
(with-bindings (assoc @session #'ie/*msg* msg)
272+
(try
273+
(let [stringify-msg (fn [report]
274+
(walk/postwalk (fn [x] (if (and (map? x)
275+
(contains? x :message))
276+
(update x :message str)
277+
x))
278+
report))
279+
report (-> var-query
280+
(assoc-in [:ns-query :has-tests?] true)
281+
(assoc :test? true)
282+
(util.coerce/var-query)
283+
test-var-query
284+
stringify-msg)]
285+
(reset! results (:results report))
286+
(t/send transport (response-for msg (u/transform-value report))))
287+
(catch clojure.lang.ExceptionInfo e
288+
(let [d (ex-data e)]
289+
(if (::util.coerce/id d)
290+
(case (::util.coerce/id d)
291+
:namespace-not-found (t/send transport (response-for msg :status :namespace-not-found)))
292+
(throw e)))))))
293+
(fn []
294+
(t/send transport (response-for msg {:status :done}))))))
307295

308296
(defn handle-test-op
309297
[{:keys [ns tests include exclude] :as msg}]
@@ -322,41 +310,42 @@
322310
:exclude-meta-key exclude}})))
323311

324312
(defn handle-retest-op
325-
[{:keys [session transport] :as msg}]
326-
(with-interruptible-eval msg
327-
(let [nss (reduce (fn [ret [ns tests]]
328-
(let [problems (filter (comp #{:fail :error} :type)
329-
(mapcat val tests))
330-
vars (distinct (map :var problems))]
331-
(if (seq vars) (assoc ret ns vars) ret)))
332-
{} @results)
333-
report (test-nss nss)]
334-
(reset! results (:results report))
335-
(t/send transport (response-for msg (u/transform-value report))))
336-
(t/send transport (response-for msg :status :done))))
313+
[{:keys [transport session id] :as msg}]
314+
(let [{:keys [exec]} (meta session)]
315+
(exec id
316+
(fn []
317+
(with-bindings (assoc @session #'ie/*msg* msg)
318+
(let [nss (reduce (fn [ret [ns tests]]
319+
(let [problems (filter (comp #{:fail :error} :type)
320+
(mapcat val tests))
321+
vars (distinct (map :var problems))]
322+
(if (seq vars) (assoc ret ns vars) ret)))
323+
{} @results)
324+
report (test-nss nss)]
325+
(reset! results (:results report))
326+
(t/send transport (response-for msg (u/transform-value report))))))
327+
(fn []
328+
(t/send transport (response-for msg :status :done))))))
337329

338330
(defn handle-stacktrace-op
339-
[{:keys [ns var index session transport pprint-fn print-options] :as msg}]
340-
(with-interruptible-eval msg
341-
(let [[ns var] (map u/as-sym [ns var])]
342-
(if-let [e (get-in @results [ns var index :error])]
343-
(doseq [cause (st/analyze-causes e pprint-fn print-options)]
344-
(t/send transport (response-for msg cause)))
345-
(t/send transport (response-for msg :status :no-error)))
346-
(t/send transport (response-for msg :status :done)))))
347-
348-
;; Before tools.nrepl-0.2.10, `default-executor` was private and
349-
;; before 0.2.9 it didn't even exist.
350-
(def default-executor (delay (if-let [def (resolve 'ie/default-executor)]
351-
@@def
352-
(@#'ie/configure-executor))))
331+
[{:keys [ns var index transport session id pprint-fn print-options] :as msg}]
332+
(let [{:keys [exec]} (meta session)]
333+
(exec id
334+
(fn []
335+
(with-bindings (assoc @session #'ie/*msg* msg)
336+
(let [[ns var] (map u/as-sym [ns var])]
337+
(if-let [e (get-in @results [ns var index :error])]
338+
(doseq [cause (st/analyze-causes e pprint-fn print-options)]
339+
(t/send transport (response-for msg cause)))
340+
(t/send transport (response-for msg :status :no-error))))))
341+
(fn []
342+
(t/send transport (response-for msg :status :done))))))
353343

354344
(defn handle-test [handler msg & configuration]
355-
(let [executor (:executor configuration @default-executor)]
356-
(case (:op msg)
357-
"test-var-query" (handle-test-var-query-op (assoc msg :executor executor))
358-
"test" (handle-test-op (assoc msg :executor executor))
359-
"test-all" (handle-test-all-op (assoc msg :executor executor))
360-
"test-stacktrace" (handle-stacktrace-op (assoc msg :executor executor))
361-
"retest" (handle-retest-op (assoc msg :executor executor))
362-
(handler msg))))
345+
(case (:op msg)
346+
"test-var-query" (handle-test-var-query-op msg)
347+
"test" (handle-test-op msg)
348+
"test-all" (handle-test-all-op msg)
349+
"test-stacktrace" (handle-stacktrace-op msg)
350+
"retest" (handle-retest-op msg)
351+
(handler msg)))

test/clj/cider/nrepl/middleware/test_test.clj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
(deftest basic-sanity-test
1212
;; Just make sure that the namespace loads properly and the macro
1313
;; expands without errors. (See #264)
14-
(is (seq (macroexpand '(test/with-interruptible-eval {}
15-
10))))
16-
(is (= (class @test/default-executor)
17-
java.util.concurrent.ThreadPoolExecutor)))
14+
(is (seq (meta #'test/handle-test))))
1815

1916
(deftest only-selected-tests
2017
(testing "only single test is run with test"

0 commit comments

Comments
 (0)