Skip to content

Commit df6b5c6

Browse files
authored
Remove the possible duplicate output introduced in a052dd8 (#737)
Output to the original output outside of the with-out-binding loop.
1 parent 338023f commit df6b5c6

File tree

2 files changed

+90
-54
lines changed

2 files changed

+90
-54
lines changed

src/cider/nrepl/middleware/out.clj

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,32 @@ Please do not inline; they must not be recomputed at runtime."}
4040
(catch Exception ~'e
4141
(unsubscribe-session ~'session))))))
4242

43+
(defn- dispatch-string
44+
([messages type ^String x]
45+
(.write ^Writer (original-output type) x)
46+
(with-out-binding [printer messages type]
47+
(.write printer x)))
48+
([messages type ^String x ^Integer off ^Integer len]
49+
(.write ^Writer (original-output type) x off len)
50+
(with-out-binding [printer messages type]
51+
(.write printer x off len))))
52+
53+
(defn- dispatch-int
54+
([messages type ^Integer x]
55+
(.write ^Writer (original-output type) x)
56+
(with-out-binding [printer messages type]
57+
(.write printer x))))
58+
59+
(defn- dispatch-chars
60+
([messages type ^{:tag "[C"} x]
61+
(.write ^Writer (original-output type) x)
62+
(with-out-binding [printer messages type]
63+
(.write printer x)))
64+
([messages type ^{:tag "[C"} x ^Integer off ^Integer len]
65+
(.write ^Writer (original-output type) x off len)
66+
(with-out-binding [printer messages type]
67+
(.write printer x off len))))
68+
4369
(defn forking-printer
4470
"Returns a PrintWriter suitable for binding as *out* or *err*. All
4571
operations are forwarded to all output bindings in the sessions of
@@ -54,36 +80,14 @@ Please do not inline; they must not be recomputed at runtime."}
5480
;; as `int` and `char[]` aren't supported by proxy.
5581
(write
5682
([x]
57-
(with-out-binding [printer messages type]
58-
(cond
59-
(string? x)
60-
(do
61-
(.write ^Writer (original-output type) ^String x)
62-
(.write printer ^String x))
63-
64-
(integer? x)
65-
(do
66-
(.write ^Writer (original-output type) ^int x)
67-
(.write printer ^int x))
68-
:else
69-
(do
70-
(.write ^Writer (original-output type)
71-
^{:tag "[C"} x)
72-
(.write printer ^{:tag "[C"} x)))))
83+
(cond
84+
(string? x) (dispatch-string messages type x)
85+
(integer? x) (dispatch-int messages type x)
86+
:else (dispatch-chars messages type x)))
7387
([x ^Integer off ^Integer len]
74-
(with-out-binding [printer messages type]
75-
(cond
76-
(string? x)
77-
(do
78-
(.write ^Writer (original-output type)
79-
^String x off len)
80-
(.write printer ^String x off len))
81-
82-
:else
83-
(do
84-
(.write ^Writer (original-output type)
85-
^{:tag "[C"} x off len)
86-
(.write printer ^{:tag "[C"} x off len))))))
88+
(if (string? x)
89+
(dispatch-string messages type x off len)
90+
(dispatch-chars messages type x off len))))
8791
(flush []
8892
(.flush ^Writer (original-output type))
8993
(with-out-binding [printer messages type]

test/clj/cider/nrepl/middleware/out_test.clj

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,58 +47,90 @@
4747
(defn- forking-printer-test-streams
4848
[]
4949
(let [out-writer (StringWriter.)
50-
message-writer (StringWriter.)
51-
message {:session (atom {#'*out* message-writer})}
52-
printer (o/forking-printer [message] :out)]
50+
message-writers [(StringWriter.) (StringWriter.)]
51+
messages [{:session (atom {#'*out* (message-writers 0)})}
52+
{:session (atom {#'*err* (message-writers 1)})}]
53+
printers [(o/forking-printer [(messages 0) (messages 1)] :out)
54+
(o/forking-printer [(messages 0) (messages 1)] :err)]]
5355
{:out-writer out-writer
54-
:message-writer message-writer
55-
:printer printer}))
56+
:message-writers message-writers
57+
:printers printers}))
5658

5759
(deftest forking-printer-test
5860
(testing "forking-printer prints to all message streams and original stream"
5961
(testing "with String argument "
6062
(let [{:keys [^StringWriter out-writer
61-
^StringWriter message-writer
62-
^PrintWriter printer]}
63+
message-writers
64+
printers]}
6365
(forking-printer-test-streams)]
6466
(with-original-output [{:out out-writer}]
65-
(.write printer "Hello")
67+
(.write ^PrintWriter (printers 0) "Hello")
6668
(is (= "Hello" (.toString out-writer)))
67-
(is (= "Hello" (.toString message-writer))))))
69+
(is (= "Hello" (.toString ^StringWriter (message-writers 0))))
70+
(is (= "" (.toString ^StringWriter (message-writers 1)))))
71+
(with-original-output [{:err out-writer}]
72+
(.write ^PrintWriter (printers 1) "Hello")
73+
(is (= "HelloHello" (.toString out-writer)))
74+
(is (= "Hello" (.toString ^StringWriter (message-writers 0))))
75+
(is (= "Hello" (.toString ^StringWriter (message-writers 1)))))))
6876
(testing "with int"
6977
(let [{:keys [^StringWriter out-writer
70-
^StringWriter message-writer
71-
^PrintWriter printer]}
78+
message-writers
79+
printers]}
7280
(forking-printer-test-streams)
7381
an-int (int 32)]
7482
(with-original-output [{:out out-writer}]
75-
(.write printer an-int)
83+
(.write ^PrintWriter (printers 0) an-int)
7684
(is (= " " (.toString out-writer)))
77-
(is (= " " (.toString message-writer))))))
85+
(is (= " " (.toString ^StringWriter (message-writers 0))))
86+
(is (= "" (.toString ^StringWriter (message-writers 1)))))
87+
(with-original-output [{:err out-writer}]
88+
(.write ^PrintWriter (printers 1) an-int)
89+
(is (= " " (.toString out-writer)))
90+
(is (= " " (.toString ^StringWriter (message-writers 0))))
91+
(is (= " " (.toString ^StringWriter (message-writers 1)))))))
7892
(testing "with char array"
7993
(let [{:keys [^StringWriter out-writer
80-
^StringWriter message-writer
81-
^PrintWriter printer]}
94+
message-writers
95+
printers]}
8296
(forking-printer-test-streams)]
8397
(with-original-output [{:out out-writer}]
84-
(.write printer (char-array "and"))
98+
(.write ^PrintWriter (printers 0) (char-array "and"))
8599
(is (= "and" (.toString out-writer)))
86-
(is (= "and" (.toString message-writer))))))
100+
(is (= "and" (.toString ^StringWriter (message-writers 0))))
101+
(is (= "" (.toString ^StringWriter (message-writers 1)))))
102+
(with-original-output [{:err out-writer}]
103+
(.write ^PrintWriter (printers 1) (char-array "and"))
104+
(is (= "andand" (.toString out-writer)))
105+
(is (= "and" (.toString ^StringWriter (message-writers 0))))
106+
(is (= "and" (.toString ^StringWriter (message-writers 1)))))))
87107
(testing "with String with offsets"
88108
(let [{:keys [^StringWriter out-writer
89-
^StringWriter message-writer
90-
^PrintWriter printer]}
109+
message-writers
110+
printers]}
91111
(forking-printer-test-streams)]
92112
(with-original-output [{:out out-writer}]
93-
(.write printer "12 good34" 3 4)
113+
(.write ^PrintWriter (printers 0) "12 good34" 3 4)
94114
(is (= "good" (.toString out-writer)))
95-
(is (= "good" (.toString message-writer))))))
115+
(is (= "good" (.toString ^StringWriter (message-writers 0))))
116+
(is (= "" (.toString ^StringWriter (message-writers 1)))))
117+
(with-original-output [{:err out-writer}]
118+
(.write ^PrintWriter (printers 1) "12 good34" 3 4)
119+
(is (= "goodgood" (.toString out-writer)))
120+
(is (= "good" (.toString ^StringWriter (message-writers 0))))
121+
(is (= "good" (.toString ^StringWriter (message-writers 1)))))))
96122
(testing "with char array with offsets"
97123
(let [{:keys [^StringWriter out-writer
98-
^StringWriter message-writer
99-
^PrintWriter printer]}
124+
message-writers
125+
printers]}
100126
(forking-printer-test-streams)]
101127
(with-original-output [{:out out-writer}]
102-
(.write printer (char-array " bye67") 1 3)
128+
(.write ^PrintWriter (printers 0) (char-array " bye67") 1 3)
103129
(is (= "bye" (.toString out-writer)))
104-
(is (= "bye" (.toString message-writer))))))))
130+
(is (= "bye" (.toString ^StringWriter (message-writers 0))))
131+
(is (= "" (.toString ^StringWriter (message-writers 1)))))
132+
(with-original-output [{:err out-writer}]
133+
(.write ^PrintWriter (printers 1) (char-array " bye67") 1 3)
134+
(is (= "byebye" (.toString out-writer)))
135+
(is (= "bye" (.toString ^StringWriter (message-writers 0))))
136+
(is (= "bye" (.toString ^StringWriter (message-writers 1)))))))))

0 commit comments

Comments
 (0)