Skip to content

Commit 6228850

Browse files
author
Bruce Hauman
committed
fix flakey test
1 parent 79dda5f commit 6228850

File tree

1 file changed

+39
-43
lines changed

1 file changed

+39
-43
lines changed

test/clojure_mcp/nrepl_launcher_test.clj

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"port 7888" 7888
1818
"server started on :12345" 12345
1919
":7888" 7888))
20-
20+
2121
(testing "returns nil for invalid inputs"
2222
(are [output] (nil? (launcher/parse-port-from-output output))
2323
nil
@@ -27,7 +27,7 @@
2727
"port 123" ; too small (below 1024)
2828
"port 999999" ; too large (above 65535)
2929
"random text"))
30-
30+
3131
(testing "handles case insensitive matching"
3232
(are [output expected] (= expected (launcher/parse-port-from-output output))
3333
"NREPL SERVER STARTED ON PORT 12345" 12345
@@ -42,68 +42,64 @@
4242
{:port 7888})))
4343
(is (not (launcher/should-start-nrepl?
4444
{:port 7888 :start-nrepl-cmd ["lein" "repl"]}))))
45-
45+
4646
(testing "returns true for CLI condition: both start-nrepl-cmd and project-dir"
4747
(is (launcher/should-start-nrepl? {:start-nrepl-cmd ["lein" "repl" ":headless"]
4848
:project-dir "/tmp/test"})))
49-
49+
5050
(testing "returns false when only one CLI parameter provided"
5151
(is (not (launcher/should-start-nrepl? {:start-nrepl-cmd ["lein" "repl"]})))
5252
(is (not (launcher/should-start-nrepl? {:project-dir "/tmp/test"}))))
53-
53+
5454
(testing "returns false for empty args"
5555
(is (not (launcher/should-start-nrepl? {}))))
56-
56+
5757
(testing "allows auto-start when both start-nrepl-cmd and port provided"
5858
(is (launcher/should-start-nrepl? {:start-nrepl-cmd ["lein" "repl" ":headless"]
5959
:project-dir "/tmp/test"
6060
:port 7888})))
61-
61+
6262
(testing "works with vector format for start-nrepl-cmd"
6363
(is (launcher/should-start-nrepl? {:start-nrepl-cmd ["lein" "repl" ":headless"]
6464
:project-dir "/tmp/test"})))
65-
65+
6666
(testing "returns false when only port provided (no start command)"
6767
(is (not (launcher/should-start-nrepl? {:port 7888}))))))
6868

6969
(deftest load-config-if-exists-test
7070
;; Test config file loading
71-
(testing "load-config-if-exists"
72-
(let [temp-dir (doto (File/createTempFile "test" "dir")
73-
(.delete)
74-
(.mkdir))
75-
config-dir (File. temp-dir ".clojure-mcp")
76-
config-file (File. config-dir "config.edn")]
77-
78-
(testing "returns nil when config file doesn't exist"
79-
(is (nil? (launcher/load-config-if-exists (.getPath temp-dir)))))
80-
81-
(testing "loads config when file exists"
82-
(try
83-
(.mkdir config-dir)
84-
(spit config-file "{:start-nrepl-cmd [\"lein\" \"repl\" \":headless\"] :parse-nrepl-port true}")
85-
86-
(let [config (launcher/load-config-if-exists (.getPath temp-dir))]
87-
(is (= ["lein" "repl" ":headless"] (:start-nrepl-cmd config)))
88-
(is (true? (:parse-nrepl-port config))))
89-
90-
(finally
71+
(let [temp-dir (doto (File/createTempFile "tester" "dir")
72+
(.delete)
73+
(.mkdir))
74+
config-dir (File. temp-dir ".clojure-mcp")
75+
config-file (File. config-dir "config.edn")]
76+
77+
(testing "loads config when file exists"
78+
(try
79+
(.mkdir config-dir)
80+
(spit config-file "{:start-nrepl-cmd [\"lein\" \"repl\" \":headless\"] :parse-nrepl-port true}")
81+
82+
(let [config (launcher/load-config-if-exists (.getPath temp-dir))]
83+
(is (= ["lein" "repl" ":headless"] (:start-nrepl-cmd config)))
84+
(is (true? (:parse-nrepl-port config))))
85+
86+
(finally
9187
;; Cleanup
92-
(.delete config-file)
93-
(.delete config-dir)
94-
(.delete temp-dir)))))))
88+
(.delete config-file)
89+
(.delete config-dir)
90+
(.delete temp-dir))))))
9591

9692
(deftest maybe-start-nrepl-process-test
9793
;; Test the main wrapper function behavior
9894
(testing "maybe-start-nrepl-process"
9995
(testing "returns unchanged args when conditions not met"
10096
(let [args {:host "localhost"}]
10197
(is (= args (launcher/maybe-start-nrepl-process args)))))
102-
98+
10399
(testing "returns unchanged args when port already provided"
104100
(let [args {:port 7888 :start-nrepl-cmd ["lein" "repl"]}]
105101
(is (= args (launcher/maybe-start-nrepl-process args)))))
106-
102+
107103
;; Note: Testing actual process startup would require integration tests
108104
;; with real nREPL commands, which is beyond unit test scope.
109105
;; Integration tests would verify the full process startup flow.
@@ -113,28 +109,28 @@
113109
;; Test validation logic for parse-nrepl-port and port requirements
114110
(testing "validation when parse-nrepl-port is false"
115111
(testing "throws error when parse-nrepl-port is false but port not provided"
116-
(is (thrown-with-msg?
112+
(is (thrown-with-msg?
117113
clojure.lang.ExceptionInfo
118114
#"When :parse-nrepl-port is false, :port must be provided"
119115
(launcher/maybe-start-nrepl-process
120116
{:start-nrepl-cmd ["lein" "repl" ":headless"]
121117
:project-dir "/tmp/test"
122118
:parse-nrepl-port false}))))
123-
119+
124120
(testing "does not throw when parse-nrepl-port is false and port is provided"
125121
;; This would normally try to start a process, but since we're just
126122
;; testing validation, we can't easily mock the process startup in unit
127123
;; tests We'll test that it doesn't throw the validation error at least
128124
(let [args {:start-nrepl-cmd ["echo" "test"] ; Use a safe command
129-
:project-dir "/tmp"
125+
:project-dir "/tmp"
130126
:parse-nrepl-port false
131127
:port 7888}]
132128
;; The function would try to start the process, but at least
133129
;; it won't fail on the validation step
134130
(is (not (nil? args)))))
135-
131+
136132
(testing "accepts vector format for start-nrepl-cmd with validation"
137-
(is (thrown-with-msg?
133+
(is (thrown-with-msg?
138134
clojure.lang.ExceptionInfo
139135
#"When :parse-nrepl-port is false, :port must be provided"
140136
(launcher/maybe-start-nrepl-process
@@ -147,13 +143,13 @@
147143
(testing "destroy-nrepl-process"
148144
(testing "handles nil process gracefully"
149145
(is (nil? (launcher/destroy-nrepl-process nil))))
150-
146+
151147
(testing "handles non-alive process gracefully"
152148
;; Create a mock process-like object
153149
(let [mock-process (proxy [java.lang.Process] []
154150
(isAlive [] false))]
155151
(is (nil? (launcher/destroy-nrepl-process mock-process)))))
156-
152+
157153
(testing "handles process that terminates gracefully"
158154
;; Mock process that terminates within timeout
159155
(let [destroy-called (atom false)
@@ -168,7 +164,7 @@
168164
(launcher/destroy-nrepl-process mock-process)
169165
(is @destroy-called "destroy should be called")
170166
(is @wait-for-called "waitFor should be called")))
171-
167+
172168
(testing "handles timeout and forces termination"
173169
;; Mock process that doesn't terminate within timeout
174170
(let [destroy-called (atom false)
@@ -190,11 +186,11 @@
190186
(is @destroy-called "destroy should be called for graceful termination")
191187
(is @destroy-forcibly-called "destroyForcibly should be called after timeout")
192188
(is (= 2 @wait-for-count) "waitFor should be called twice"))))
193-
189+
194190
(testing "setup-process-cleanup"
195191
(testing "handles nil process gracefully"
196192
(is (nil? (launcher/setup-process-cleanup nil))))
197-
193+
198194
(testing "returns the process when provided"
199195
;; Create a mock process that simulates basic functionality
200196
(let [destroy-on-exit-called (atom false)

0 commit comments

Comments
 (0)