|
17 | 17 | "port 7888" 7888 |
18 | 18 | "server started on :12345" 12345 |
19 | 19 | ":7888" 7888)) |
20 | | - |
| 20 | + |
21 | 21 | (testing "returns nil for invalid inputs" |
22 | 22 | (are [output] (nil? (launcher/parse-port-from-output output)) |
23 | 23 | nil |
|
27 | 27 | "port 123" ; too small (below 1024) |
28 | 28 | "port 999999" ; too large (above 65535) |
29 | 29 | "random text")) |
30 | | - |
| 30 | + |
31 | 31 | (testing "handles case insensitive matching" |
32 | 32 | (are [output expected] (= expected (launcher/parse-port-from-output output)) |
33 | 33 | "NREPL SERVER STARTED ON PORT 12345" 12345 |
|
42 | 42 | {:port 7888}))) |
43 | 43 | (is (not (launcher/should-start-nrepl? |
44 | 44 | {:port 7888 :start-nrepl-cmd ["lein" "repl"]})))) |
45 | | - |
| 45 | + |
46 | 46 | (testing "returns true for CLI condition: both start-nrepl-cmd and project-dir" |
47 | 47 | (is (launcher/should-start-nrepl? {:start-nrepl-cmd ["lein" "repl" ":headless"] |
48 | 48 | :project-dir "/tmp/test"}))) |
49 | | - |
| 49 | + |
50 | 50 | (testing "returns false when only one CLI parameter provided" |
51 | 51 | (is (not (launcher/should-start-nrepl? {:start-nrepl-cmd ["lein" "repl"]}))) |
52 | 52 | (is (not (launcher/should-start-nrepl? {:project-dir "/tmp/test"})))) |
53 | | - |
| 53 | + |
54 | 54 | (testing "returns false for empty args" |
55 | 55 | (is (not (launcher/should-start-nrepl? {})))) |
56 | | - |
| 56 | + |
57 | 57 | (testing "allows auto-start when both start-nrepl-cmd and port provided" |
58 | 58 | (is (launcher/should-start-nrepl? {:start-nrepl-cmd ["lein" "repl" ":headless"] |
59 | 59 | :project-dir "/tmp/test" |
60 | 60 | :port 7888}))) |
61 | | - |
| 61 | + |
62 | 62 | (testing "works with vector format for start-nrepl-cmd" |
63 | 63 | (is (launcher/should-start-nrepl? {:start-nrepl-cmd ["lein" "repl" ":headless"] |
64 | 64 | :project-dir "/tmp/test"}))) |
65 | | - |
| 65 | + |
66 | 66 | (testing "returns false when only port provided (no start command)" |
67 | 67 | (is (not (launcher/should-start-nrepl? {:port 7888})))))) |
68 | 68 |
|
69 | 69 | (deftest load-config-if-exists-test |
70 | 70 | ;; 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 |
91 | 87 | ;; 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)))))) |
95 | 91 |
|
96 | 92 | (deftest maybe-start-nrepl-process-test |
97 | 93 | ;; Test the main wrapper function behavior |
98 | 94 | (testing "maybe-start-nrepl-process" |
99 | 95 | (testing "returns unchanged args when conditions not met" |
100 | 96 | (let [args {:host "localhost"}] |
101 | 97 | (is (= args (launcher/maybe-start-nrepl-process args))))) |
102 | | - |
| 98 | + |
103 | 99 | (testing "returns unchanged args when port already provided" |
104 | 100 | (let [args {:port 7888 :start-nrepl-cmd ["lein" "repl"]}] |
105 | 101 | (is (= args (launcher/maybe-start-nrepl-process args))))) |
106 | | - |
| 102 | + |
107 | 103 | ;; Note: Testing actual process startup would require integration tests |
108 | 104 | ;; with real nREPL commands, which is beyond unit test scope. |
109 | 105 | ;; Integration tests would verify the full process startup flow. |
|
113 | 109 | ;; Test validation logic for parse-nrepl-port and port requirements |
114 | 110 | (testing "validation when parse-nrepl-port is false" |
115 | 111 | (testing "throws error when parse-nrepl-port is false but port not provided" |
116 | | - (is (thrown-with-msg? |
| 112 | + (is (thrown-with-msg? |
117 | 113 | clojure.lang.ExceptionInfo |
118 | 114 | #"When :parse-nrepl-port is false, :port must be provided" |
119 | 115 | (launcher/maybe-start-nrepl-process |
120 | 116 | {:start-nrepl-cmd ["lein" "repl" ":headless"] |
121 | 117 | :project-dir "/tmp/test" |
122 | 118 | :parse-nrepl-port false})))) |
123 | | - |
| 119 | + |
124 | 120 | (testing "does not throw when parse-nrepl-port is false and port is provided" |
125 | 121 | ;; This would normally try to start a process, but since we're just |
126 | 122 | ;; testing validation, we can't easily mock the process startup in unit |
127 | 123 | ;; tests We'll test that it doesn't throw the validation error at least |
128 | 124 | (let [args {:start-nrepl-cmd ["echo" "test"] ; Use a safe command |
129 | | - :project-dir "/tmp" |
| 125 | + :project-dir "/tmp" |
130 | 126 | :parse-nrepl-port false |
131 | 127 | :port 7888}] |
132 | 128 | ;; The function would try to start the process, but at least |
133 | 129 | ;; it won't fail on the validation step |
134 | 130 | (is (not (nil? args))))) |
135 | | - |
| 131 | + |
136 | 132 | (testing "accepts vector format for start-nrepl-cmd with validation" |
137 | | - (is (thrown-with-msg? |
| 133 | + (is (thrown-with-msg? |
138 | 134 | clojure.lang.ExceptionInfo |
139 | 135 | #"When :parse-nrepl-port is false, :port must be provided" |
140 | 136 | (launcher/maybe-start-nrepl-process |
|
147 | 143 | (testing "destroy-nrepl-process" |
148 | 144 | (testing "handles nil process gracefully" |
149 | 145 | (is (nil? (launcher/destroy-nrepl-process nil)))) |
150 | | - |
| 146 | + |
151 | 147 | (testing "handles non-alive process gracefully" |
152 | 148 | ;; Create a mock process-like object |
153 | 149 | (let [mock-process (proxy [java.lang.Process] [] |
154 | 150 | (isAlive [] false))] |
155 | 151 | (is (nil? (launcher/destroy-nrepl-process mock-process))))) |
156 | | - |
| 152 | + |
157 | 153 | (testing "handles process that terminates gracefully" |
158 | 154 | ;; Mock process that terminates within timeout |
159 | 155 | (let [destroy-called (atom false) |
|
168 | 164 | (launcher/destroy-nrepl-process mock-process) |
169 | 165 | (is @destroy-called "destroy should be called") |
170 | 166 | (is @wait-for-called "waitFor should be called"))) |
171 | | - |
| 167 | + |
172 | 168 | (testing "handles timeout and forces termination" |
173 | 169 | ;; Mock process that doesn't terminate within timeout |
174 | 170 | (let [destroy-called (atom false) |
|
190 | 186 | (is @destroy-called "destroy should be called for graceful termination") |
191 | 187 | (is @destroy-forcibly-called "destroyForcibly should be called after timeout") |
192 | 188 | (is (= 2 @wait-for-count) "waitFor should be called twice")))) |
193 | | - |
| 189 | + |
194 | 190 | (testing "setup-process-cleanup" |
195 | 191 | (testing "handles nil process gracefully" |
196 | 192 | (is (nil? (launcher/setup-process-cleanup nil)))) |
197 | | - |
| 193 | + |
198 | 194 | (testing "returns the process when provided" |
199 | 195 | ;; Create a mock process that simulates basic functionality |
200 | 196 | (let [destroy-on-exit-called (atom false) |
|
0 commit comments