Skip to content

Commit 9eb9084

Browse files
committed
Use .bat for Windows version test
Mostly a proof of concept, should expand to other tests at some point.
1 parent 23ebb16 commit 9eb9084

File tree

2 files changed

+24
-39
lines changed

2 files changed

+24
-39
lines changed

src/main/kotlin/com/coder/gateway/settings/CoderSettings.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ open class CoderSettings(
2222
private val state: CoderSettingsState,
2323
// The location of the SSH config. Defaults to ~/.ssh/config.
2424
val sshConfigPath: Path = Path.of(System.getProperty("user.home")).resolve(".ssh/config"),
25-
// Env allows overriding the default environment.
25+
// Overrides the default environment (for tests).
2626
private val env: Environment = Environment(),
27+
// Overrides the default binary name (for tests).
28+
private val binaryName: String? = null,
2729
) {
2830
val tls = CoderTLSSettings(state)
2931
val enableDownloads: Boolean
@@ -68,10 +70,10 @@ open class CoderSettings(
6870
* To where the specified deployment should download the binary.
6971
*/
7072
fun binPath(url: URL, forceDownloadToData: Boolean = false): Path {
71-
val binaryName = getCoderCLIForOS(getOS(), getArch())
73+
val name = binaryName ?: getCoderCLIForOS(getOS(), getArch())
7274
val dir = if (forceDownloadToData || state.binaryDirectory.isBlank()) dataDir(url)
7375
else withHost(Path.of(expand(state.binaryDirectory)), url)
74-
return dir.resolve(binaryName).toAbsolutePath()
76+
return dir.resolve(name).toAbsolutePath()
7577
}
7678

7779
/**

src/test/kotlin/com/coder/gateway/cli/CoderCLIManagerTest.kt

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,18 @@ import kotlin.test.assertTrue
3333

3434
internal class CoderCLIManagerTest {
3535
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())
3842
}
3943

4044
private fun mockServer(errorCode: Int = 0, version: String? = null): Pair<HttpServer, URL> {
4145
val srv = HttpServer.create(InetSocketAddress(0), 0)
4246
srv.createContext("/") {exchange ->
4347
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?
4748
var response = mkbin(version ?: "${srv.address.port}.0.0")
4849
val eTags = exchange.requestHeaders["If-None-Match"]
4950
if (exchange.requestURI.path == "/bin/override") {
@@ -109,8 +110,7 @@ internal class CoderCLIManagerTest {
109110

110111
val (srv, url) = mockServer()
111112
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())))
114114

115115
ccm.localBinaryPath.parent.toFile().mkdirs()
116116
ccm.localBinaryPath.parent.toFile().setWritable(false)
@@ -135,8 +135,7 @@ internal class CoderCLIManagerTest {
135135
}
136136

137137
val ccm = CoderCLIManager(url.toURL(), CoderSettings(CoderSettingsState(
138-
dataDirectory = tmpdir.resolve("real-cli").toString()))
139-
)
138+
dataDirectory = tmpdir.resolve("real-cli").toString())))
140139

141140
assertTrue(ccm.download())
142141
assertDoesNotThrow { ccm.version() }
@@ -154,27 +153,19 @@ internal class CoderCLIManagerTest {
154153
fun testDownloadMockCLI() {
155154
val (srv, url) = mockServer()
156155
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"))
159158

160159
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())
169161

170162
// It should skip the second attempt.
171163
assertEquals(false, ccm.download())
172164

173165
// Should use the source override.
174166
ccm = CoderCLIManager(url, CoderSettings(CoderSettingsState(
175167
binarySource = "/bin/override",
176-
dataDirectory = tmpdir.resolve("mock-cli").toString()))
177-
)
168+
dataDirectory = tmpdir.resolve("mock-cli").toString())))
178169

179170
assertEquals(true, ccm.download())
180171
assertContains(ccm.localBinaryPath.toFile().readText(), "0.0.0")
@@ -185,8 +176,7 @@ internal class CoderCLIManagerTest {
185176
@Test
186177
fun testRunNonExistentBinary() {
187178
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())))
190180

191181
assertFailsWith(
192182
exceptionClass = ProcessInitException::class,
@@ -197,8 +187,7 @@ internal class CoderCLIManagerTest {
197187
fun testOverwitesWrongVersion() {
198188
val (srv, url) = mockServer()
199189
val ccm = CoderCLIManager(url, CoderSettings(CoderSettingsState(
200-
dataDirectory = tmpdir.resolve("overwrite-cli").toString()))
201-
)
190+
dataDirectory = tmpdir.resolve("overwrite-cli").toString())))
202191

203192
ccm.localBinaryPath.parent.toFile().mkdirs()
204193
ccm.localBinaryPath.toFile().writeText("cli")
@@ -325,9 +314,7 @@ internal class CoderCLIManagerTest {
325314
sshConfigPath = tmpdir.resolve("configured$it.conf"))
326315
settings.sshConfigPath.parent.toFile().mkdirs()
327316
Path.of("src/test/fixtures/inputs").resolve("$it.conf").toFile().copyTo(
328-
settings.sshConfigPath.toFile(),
329-
true,
330-
)
317+
settings.sshConfigPath.toFile(), true)
331318

332319
val ccm = CoderCLIManager(URL("https://test.coder.invalid"), settings)
333320

@@ -345,8 +332,7 @@ internal class CoderCLIManagerTest {
345332

346333
tests.forEach {
347334
val ccm = CoderCLIManager(URL("https://test.coder.invalid"), CoderSettings(CoderSettingsState(
348-
headerCommand = it))
349-
)
335+
headerCommand = it)))
350336

351337
assertFailsWith(
352338
exceptionClass = Exception::class,
@@ -370,8 +356,7 @@ internal class CoderCLIManagerTest {
370356
)
371357

372358
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())))
375360
ccm.localBinaryPath.parent.toFile().mkdirs()
376361

377362
tests.forEach {
@@ -412,8 +397,7 @@ internal class CoderCLIManagerTest {
412397
Triple("""exit 1""", "v1.0.0", null))
413398

414399
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())))
417401
ccm.localBinaryPath.parent.toFile().mkdirs()
418402

419403
test.forEach {
@@ -567,8 +551,7 @@ internal class CoderCLIManagerTest {
567551
tests.forEach {
568552
val (srv, url) = mockServer(version = it.first)
569553
val ccm = CoderCLIManager(url, CoderSettings(CoderSettingsState(
570-
dataDirectory = tmpdir.resolve("features").toString()))
571-
)
554+
dataDirectory = tmpdir.resolve("features").toString())))
572555
assertEquals(true, ccm.download())
573556
assertEquals(it.second, ccm.features, "version: ${it.first}")
574557

0 commit comments

Comments
 (0)