11package com.coder.gateway.sdk
22
3+ import com.google.gson.JsonSyntaxException
34import com.sun.net.httpserver.HttpExchange
45import com.sun.net.httpserver.HttpHandler
56import com.sun.net.httpserver.HttpServer
67import org.zeroturnaround.exec.InvalidExitValueException
78import org.zeroturnaround.exec.ProcessInitException
8- import spock.lang.Requires
9- import spock.lang.Shared
10- import spock.lang.Specification
11- import spock.lang.Unroll
9+ import spock.lang.*
1210
1311import java.nio.file.Files
1412import java.nio.file.Path
@@ -134,10 +132,11 @@ class CoderCLIManagerTest extends Specification {
134132
135133 when :
136134 def downloaded = ccm. downloadCLI()
135+ ccm. version()
137136
138137 then :
139138 downloaded
140- ccm . version() . contains( " Coder " )
139+ noExceptionThrown( )
141140
142141 // Make sure login failures propagate correctly.
143142 when :
@@ -161,7 +160,7 @@ class CoderCLIManagerTest extends Specification {
161160 // The mock does not serve a binary that works on Windows so do not
162161 // actually execute. Checking the contents works just as well as proof
163162 // that the binary was correctly downloaded anyway.
164- ccm. localBinaryPath. toFile(). text == " #!/bin/sh \n echo ' $ url ' "
163+ ccm. localBinaryPath. toFile(). text. contains( url)
165164
166165 cleanup :
167166 srv. stop(0 )
@@ -172,7 +171,7 @@ class CoderCLIManagerTest extends Specification {
172171 def ccm = new CoderCLIManager (new URL (" https://foo" ), tmpdir. resolve(" does-not-exist" ))
173172
174173 when :
175- ccm. version( )
174+ ccm. login( " token " )
176175
177176 then :
178177 thrown(ProcessInitException )
@@ -193,7 +192,7 @@ class CoderCLIManagerTest extends Specification {
193192 downloaded
194193 ccm. localBinaryPath. toFile(). readBytes() != " cli" . getBytes()
195194 ccm. localBinaryPath. toFile(). lastModified() > 0
196- ccm. localBinaryPath. toFile(). text == " #!/bin/sh \n echo ' $ url ' "
195+ ccm. localBinaryPath. toFile(). text. contains( url)
197196
198197 cleanup :
199198 srv. stop(0 )
@@ -207,6 +206,7 @@ class CoderCLIManagerTest extends Specification {
207206 when :
208207 def downloaded1 = ccm. downloadCLI()
209208 ccm. localBinaryPath. toFile(). setLastModified(0 )
209+ // Download will be skipped due to a 304.
210210 def downloaded2 = ccm. downloadCLI()
211211
212212 then :
@@ -231,8 +231,8 @@ class CoderCLIManagerTest extends Specification {
231231
232232 then :
233233 ccm1. localBinaryPath != ccm2. localBinaryPath
234- ccm1. localBinaryPath. toFile(). text == " #!/bin/sh \n echo ' $ url1 ' "
235- ccm2. localBinaryPath. toFile(). text == " #!/bin/sh \n echo ' $ url2 ' "
234+ ccm1. localBinaryPath. toFile(). text. contains( url1)
235+ ccm2. localBinaryPath. toFile(). text. contains( url2)
236236
237237 cleanup :
238238 srv1. stop(0 )
@@ -249,7 +249,7 @@ class CoderCLIManagerTest extends Specification {
249249
250250 then :
251251 downloaded
252- ccm. localBinaryPath. toFile(). text == " #!/bin/sh \n echo ' ${ expected.replace("{{url}}", url)} ' "
252+ ccm. localBinaryPath. toFile(). text. contains( expected. replace(" {{url}}" , url))
253253
254254 cleanup :
255255 srv. stop(0 )
@@ -429,4 +429,83 @@ class CoderCLIManagerTest extends Specification {
429429 " malformed-start-after-end" ,
430430 ]
431431 }
432+
433+ @IgnoreIf ({ os.windows })
434+ def " parses version" () {
435+ given :
436+ def ccm = new CoderCLIManager (new URL (" https://test.coder.invalid" ), tmpdir)
437+ Files . createDirectories(ccm. localBinaryPath. parent)
438+
439+ when :
440+ ccm. localBinaryPath. toFile(). text = " #!/bin/sh\n $contents "
441+ ccm. localBinaryPath. toFile(). setExecutable(true )
442+
443+ then :
444+ ccm. version() == expected
445+
446+ where :
447+ contents | expected
448+ """ echo '{"version": "1.0.0"}'""" | CoderSemVer . parse(" 1.0.0" )
449+ """ echo '{"version": "1.0.0", "foo": true, "baz": 1}'""" | CoderSemVer . parse(" 1.0.0" )
450+ }
451+
452+ @IgnoreIf ({ os.windows })
453+ def " fails to parse version" () {
454+ given :
455+ def ccm = new CoderCLIManager (new URL (" https://test.coder.parse-fail.invalid" ), tmpdir)
456+ Files . createDirectories(ccm. localBinaryPath. parent)
457+
458+ when :
459+ if (contents != null ) {
460+ ccm. localBinaryPath. toFile(). text = " #!/bin/sh\n $contents "
461+ ccm. localBinaryPath. toFile(). setExecutable(true )
462+ }
463+ ccm. version()
464+
465+ then :
466+ thrown(expected)
467+
468+ where :
469+ contents | expected
470+ null | ProcessInitException
471+ """ echo '{"foo": true, "baz": 1}'""" | InvalidVersionException
472+ """ echo '{"version: '""" | JsonSyntaxException
473+ " exit 0" | InvalidVersionException
474+ " exit 1" | InvalidExitValueException
475+ }
476+
477+ @IgnoreIf ({ os.windows })
478+ def " checks if version matches" () {
479+ given :
480+ def ccm = new CoderCLIManager (new URL (" https://test.coder.version-matches.invalid" ), tmpdir)
481+ Files . createDirectories(ccm. localBinaryPath. parent)
482+
483+ when :
484+ if (contents != null ) {
485+ ccm. localBinaryPath. toFile(). text = " #!/bin/sh\n $contents "
486+ ccm. localBinaryPath. toFile(). setExecutable(true )
487+ }
488+
489+ then :
490+ ccm. matchesVersion(build) == matches
491+
492+ where :
493+ contents | build | matches
494+ null | " v1.0.0" | false
495+ """ echo '{"version": "v1.0.0"}'""" | " v1.0.0" | true
496+ """ echo '{"version": "v1.0.0"}'""" | " v1.0.0-devel+b5b5b5b5" | true
497+ """ echo '{"version": "v1.0.0-devel+b5b5b5b5"}'""" | " v1.0.0-devel+b5b5b5b5" | true
498+ """ echo '{"version": "v1.0.0-devel+b5b5b5b5"}'""" | " v1.0.0" | true
499+ """ echo '{"version": "v1.0.0-devel+b5b5b5b5"}'""" | " v1.0.0-devel+c6c6c6c6" | true
500+ """ echo '{"version": "v1.0.0-prod+b5b5b5b5"}'""" | " v1.0.0-devel+b5b5b5b5" | true
501+ """ echo '{"version": "v1.0.0"}'""" | " v1.0.1" | false
502+ """ echo '{"version": "v1.0.0"}'""" | " v1.1.0" | false
503+ """ echo '{"version": "v1.0.0"}'""" | " v2.0.0" | false
504+ """ echo '{"version": "v1.0.0"}'""" | " v0.0.0" | false
505+ """ echo '{"version": ""}'""" | " v1.0.0" | false
506+ """ echo '{"version": "v1.0.0"}'""" | " " | false
507+ """ echo '{"version'""" | " v1.0.0" | false
508+ """ exit 0""" | " v1.0.0" | false
509+ """ exit 1""" | " v1.0.0" | false
510+ }
432511}
0 commit comments