@@ -33,17 +33,18 @@ import kotlin.test.assertTrue
33
33
34
34
internal class CoderCLIManagerTest {
35
35
private fun mkbin (version : String ): String {
36
- return listOf (" #!/bin/sh" , """ echo '{"version": "$version "}'""" )
37
- .joinToString(" \n " )
36
+ return if (getOS() == OS .WINDOWS ) {
37
+ // Must use a .bat extension for this to work.
38
+ listOf (" @echo off" , """ echo {"version": "$version "}""" )
39
+ } else {
40
+ listOf (" #!/bin/sh" , """ echo '{"version": "$version "}'""" )
41
+ }.joinToString(System .lineSeparator())
38
42
}
39
43
40
44
private fun mockServer (errorCode : Int = 0, version : String? = null): Pair <HttpServer , URL > {
41
45
val srv = HttpServer .create(InetSocketAddress (0 ), 0 )
42
46
srv.createContext(" /" ) {exchange ->
43
47
var code = HttpURLConnection .HTTP_OK
44
- // TODO: Is there some simple way to create an executable file on
45
- // Windows without having to execute something to generate said
46
- // executable or having to commit one to the repo?
47
48
var response = mkbin(version ? : " ${srv.address.port} .0.0" )
48
49
val eTags = exchange.requestHeaders[" If-None-Match" ]
49
50
if (exchange.requestURI.path == " /bin/override" ) {
@@ -109,8 +110,7 @@ internal class CoderCLIManagerTest {
109
110
110
111
val (srv, url) = mockServer()
111
112
val ccm = CoderCLIManager (url, CoderSettings (CoderSettingsState (
112
- dataDirectory = tmpdir.resolve(" cli-dir-fail-to-write" ).toString()))
113
- )
113
+ dataDirectory = tmpdir.resolve(" cli-dir-fail-to-write" ).toString())))
114
114
115
115
ccm.localBinaryPath.parent.toFile().mkdirs()
116
116
ccm.localBinaryPath.parent.toFile().setWritable(false )
@@ -135,8 +135,7 @@ internal class CoderCLIManagerTest {
135
135
}
136
136
137
137
val ccm = CoderCLIManager (url.toURL(), CoderSettings (CoderSettingsState (
138
- dataDirectory = tmpdir.resolve(" real-cli" ).toString()))
139
- )
138
+ dataDirectory = tmpdir.resolve(" real-cli" ).toString())))
140
139
141
140
assertTrue(ccm.download())
142
141
assertDoesNotThrow { ccm.version() }
@@ -154,27 +153,19 @@ internal class CoderCLIManagerTest {
154
153
fun testDownloadMockCLI () {
155
154
val (srv, url) = mockServer()
156
155
var ccm = CoderCLIManager (url, CoderSettings (CoderSettingsState (
157
- dataDirectory = tmpdir.resolve(" mock-cli" ).toString()))
158
- )
156
+ dataDirectory = tmpdir.resolve(" mock-cli" ).toString()),
157
+ binaryName = " coder-windows.bat " ) )
159
158
160
159
assertEquals(true , ccm.download())
161
-
162
- // The mock does not serve a binary that works on Windows so do not
163
- // actually execute. Checking the contents works just as well as proof
164
- // that the binary was correctly downloaded anyway.
165
- assertContains(ccm.localBinaryPath.toFile().readText(), url.port.toString())
166
- if (getOS() != OS .WINDOWS ) {
167
- assertEquals(SemVer (url.port.toLong(), 0 , 0 ), ccm.version())
168
- }
160
+ assertEquals(SemVer (url.port.toLong(), 0 , 0 ), ccm.version())
169
161
170
162
// It should skip the second attempt.
171
163
assertEquals(false , ccm.download())
172
164
173
165
// Should use the source override.
174
166
ccm = CoderCLIManager (url, CoderSettings (CoderSettingsState (
175
167
binarySource = " /bin/override" ,
176
- dataDirectory = tmpdir.resolve(" mock-cli" ).toString()))
177
- )
168
+ dataDirectory = tmpdir.resolve(" mock-cli" ).toString())))
178
169
179
170
assertEquals(true , ccm.download())
180
171
assertContains(ccm.localBinaryPath.toFile().readText(), " 0.0.0" )
@@ -185,8 +176,7 @@ internal class CoderCLIManagerTest {
185
176
@Test
186
177
fun testRunNonExistentBinary () {
187
178
val ccm = CoderCLIManager (URL (" https://foo" ), CoderSettings (CoderSettingsState (
188
- dataDirectory = tmpdir.resolve(" does-not-exist" ).toString()))
189
- )
179
+ dataDirectory = tmpdir.resolve(" does-not-exist" ).toString())))
190
180
191
181
assertFailsWith(
192
182
exceptionClass = ProcessInitException ::class ,
@@ -197,8 +187,7 @@ internal class CoderCLIManagerTest {
197
187
fun testOverwitesWrongVersion () {
198
188
val (srv, url) = mockServer()
199
189
val ccm = CoderCLIManager (url, CoderSettings (CoderSettingsState (
200
- dataDirectory = tmpdir.resolve(" overwrite-cli" ).toString()))
201
- )
190
+ dataDirectory = tmpdir.resolve(" overwrite-cli" ).toString())))
202
191
203
192
ccm.localBinaryPath.parent.toFile().mkdirs()
204
193
ccm.localBinaryPath.toFile().writeText(" cli" )
@@ -325,9 +314,7 @@ internal class CoderCLIManagerTest {
325
314
sshConfigPath = tmpdir.resolve(" configured$it .conf" ))
326
315
settings.sshConfigPath.parent.toFile().mkdirs()
327
316
Path .of(" src/test/fixtures/inputs" ).resolve(" $it .conf" ).toFile().copyTo(
328
- settings.sshConfigPath.toFile(),
329
- true ,
330
- )
317
+ settings.sshConfigPath.toFile(), true )
331
318
332
319
val ccm = CoderCLIManager (URL (" https://test.coder.invalid" ), settings)
333
320
@@ -345,8 +332,7 @@ internal class CoderCLIManagerTest {
345
332
346
333
tests.forEach {
347
334
val ccm = CoderCLIManager (URL (" https://test.coder.invalid" ), CoderSettings (CoderSettingsState (
348
- headerCommand = it))
349
- )
335
+ headerCommand = it)))
350
336
351
337
assertFailsWith(
352
338
exceptionClass = Exception ::class ,
@@ -370,8 +356,7 @@ internal class CoderCLIManagerTest {
370
356
)
371
357
372
358
val ccm = CoderCLIManager (URL (" https://test.coder.parse-fail.invalid" ), CoderSettings (CoderSettingsState (
373
- binaryDirectory = tmpdir.resolve(" bad-version" ).toString()))
374
- )
359
+ binaryDirectory = tmpdir.resolve(" bad-version" ).toString())))
375
360
ccm.localBinaryPath.parent.toFile().mkdirs()
376
361
377
362
tests.forEach {
@@ -412,8 +397,7 @@ internal class CoderCLIManagerTest {
412
397
Triple (""" exit 1""" , " v1.0.0" , null ))
413
398
414
399
val ccm = CoderCLIManager (URL (" https://test.coder.matches-version.invalid" ), CoderSettings (CoderSettingsState (
415
- binaryDirectory = tmpdir.resolve(" matches-version" ).toString()))
416
- )
400
+ binaryDirectory = tmpdir.resolve(" matches-version" ).toString())))
417
401
ccm.localBinaryPath.parent.toFile().mkdirs()
418
402
419
403
test.forEach {
@@ -567,8 +551,7 @@ internal class CoderCLIManagerTest {
567
551
tests.forEach {
568
552
val (srv, url) = mockServer(version = it.first)
569
553
val ccm = CoderCLIManager (url, CoderSettings (CoderSettingsState (
570
- dataDirectory = tmpdir.resolve(" features" ).toString()))
571
- )
554
+ dataDirectory = tmpdir.resolve(" features" ).toString())))
572
555
assertEquals(true , ccm.download())
573
556
assertEquals(it.second, ccm.features, " version: ${it.first} " )
574
557
0 commit comments