1
1
package com.coder.gateway.sdk
2
2
3
+ import com.google.gson.JsonSyntaxException
3
4
import com.sun.net.httpserver.HttpExchange
4
5
import com.sun.net.httpserver.HttpHandler
5
6
import com.sun.net.httpserver.HttpServer
6
7
import org.zeroturnaround.exec.InvalidExitValueException
7
8
import 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.*
12
10
13
11
import java.nio.file.Files
14
12
import java.nio.file.Path
@@ -134,10 +132,11 @@ class CoderCLIManagerTest extends Specification {
134
132
135
133
when :
136
134
def downloaded = ccm. downloadCLI()
135
+ ccm. version()
137
136
138
137
then :
139
138
downloaded
140
- ccm . version() . contains( " Coder " )
139
+ noExceptionThrown( )
141
140
142
141
// Make sure login failures propagate correctly.
143
142
when :
@@ -161,7 +160,7 @@ class CoderCLIManagerTest extends Specification {
161
160
// The mock does not serve a binary that works on Windows so do not
162
161
// actually execute. Checking the contents works just as well as proof
163
162
// that the binary was correctly downloaded anyway.
164
- ccm. localBinaryPath. toFile(). text == " #!/bin/sh \n echo ' $ url ' "
163
+ ccm. localBinaryPath. toFile(). text. contains( url)
165
164
166
165
cleanup :
167
166
srv. stop(0 )
@@ -172,7 +171,7 @@ class CoderCLIManagerTest extends Specification {
172
171
def ccm = new CoderCLIManager (new URL (" https://foo" ), tmpdir. resolve(" does-not-exist" ))
173
172
174
173
when :
175
- ccm. version( )
174
+ ccm. login( " token " )
176
175
177
176
then :
178
177
thrown(ProcessInitException )
@@ -193,7 +192,7 @@ class CoderCLIManagerTest extends Specification {
193
192
downloaded
194
193
ccm. localBinaryPath. toFile(). readBytes() != " cli" . getBytes()
195
194
ccm. localBinaryPath. toFile(). lastModified() > 0
196
- ccm. localBinaryPath. toFile(). text == " #!/bin/sh \n echo ' $ url ' "
195
+ ccm. localBinaryPath. toFile(). text. contains( url)
197
196
198
197
cleanup :
199
198
srv. stop(0 )
@@ -207,6 +206,7 @@ class CoderCLIManagerTest extends Specification {
207
206
when :
208
207
def downloaded1 = ccm. downloadCLI()
209
208
ccm. localBinaryPath. toFile(). setLastModified(0 )
209
+ // Download will be skipped due to a 304.
210
210
def downloaded2 = ccm. downloadCLI()
211
211
212
212
then :
@@ -231,8 +231,8 @@ class CoderCLIManagerTest extends Specification {
231
231
232
232
then :
233
233
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)
236
236
237
237
cleanup :
238
238
srv1. stop(0 )
@@ -249,7 +249,7 @@ class CoderCLIManagerTest extends Specification {
249
249
250
250
then :
251
251
downloaded
252
- ccm. localBinaryPath. toFile(). text == " #!/bin/sh \n echo ' ${ expected.replace("{{url}}", url)} ' "
252
+ ccm. localBinaryPath. toFile(). text. contains( expected. replace(" {{url}}" , url))
253
253
254
254
cleanup :
255
255
srv. stop(0 )
@@ -429,4 +429,83 @@ class CoderCLIManagerTest extends Specification {
429
429
" malformed-start-after-end" ,
430
430
]
431
431
}
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
+ }
432
511
}
0 commit comments