Skip to content

Commit c0805a2

Browse files
committed
Split out version check for testing
1 parent edf950b commit c0805a2

File tree

3 files changed

+82
-15
lines changed

3 files changed

+82
-15
lines changed

src/main/kotlin/com/coder/gateway/sdk/CoderSemVer.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.coder.gateway.sdk
22

3+
import com.coder.gateway.CoderSupportedVersions
34

45
class CoderSemVer(private val major: Long = 0, private val minor: Long = 0, private val patch: Long = 0) : Comparable<CoderSemVer> {
56

@@ -62,5 +63,27 @@ class CoderSemVer(private val major: Long = 0, private val minor: Long = 0, priv
6263
if (matchResult.groupValues[3].isNotEmpty()) matchResult.groupValues[3].toLong() else 0,
6364
)
6465
}
66+
67+
/**
68+
* Check to see if the plugin is compatible with the provided version.
69+
* Throws if not valid.
70+
*/
71+
@JvmStatic
72+
fun checkVersionCompatibility(buildVersion: String) {
73+
if (!isValidVersion(buildVersion)) {
74+
throw InvalidVersionException("Invalid version $buildVersion")
75+
}
76+
77+
if (!parse(buildVersion).isInClosedRange(
78+
CoderSupportedVersions.minCompatibleCoderVersion,
79+
CoderSupportedVersions.maxCompatibleCoderVersion
80+
)
81+
) {
82+
throw IncompatibleVersionException("Incompatible version $buildVersion")
83+
}
84+
}
6585
}
66-
}
86+
}
87+
88+
class InvalidVersionException(message: String) : Exception(message)
89+
class IncompatibleVersionException(message: String) : Exception(message)

src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.coder.gateway.views.steps
22

33
import com.coder.gateway.CoderGatewayBundle
4-
import com.coder.gateway.CoderSupportedVersions
54
import com.coder.gateway.icons.CoderIcons
65
import com.coder.gateway.models.CoderWorkspacesWizardModel
76
import com.coder.gateway.models.WorkspaceAgentModel
@@ -14,6 +13,8 @@ import com.coder.gateway.sdk.Arch
1413
import com.coder.gateway.sdk.CoderCLIManager
1514
import com.coder.gateway.sdk.CoderRestClientService
1615
import com.coder.gateway.sdk.CoderSemVer
16+
import com.coder.gateway.sdk.IncompatibleVersionException
17+
import com.coder.gateway.sdk.InvalidVersionException
1718
import com.coder.gateway.sdk.OS
1819
import com.coder.gateway.sdk.TemplateIconDownloader
1920
import com.coder.gateway.sdk.ex.AuthenticationResponseException
@@ -521,23 +522,21 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
521522
logger.info("Authenticating to ${localWizardModel.coderURL}...")
522523
coderClient.initClientSession(localWizardModel.coderURL.toURL(), token)
523524

524-
logger.info("Checking Coder version...")
525-
if (!CoderSemVer.isValidVersion(coderClient.buildVersion)) {
526-
logger.warn("Got invalid version ${coderClient.buildVersion}")
525+
try {
526+
logger.info("Checking compatibility with Coder version ${coderClient.buildVersion}...")
527+
CoderSemVer.checkVersionCompatibility(coderClient.buildVersion)
528+
logger.info("${coderClient.buildVersion} is compatible")
529+
} catch (e: InvalidVersionException) {
530+
logger.warn(e)
527531
notificationBanner.apply {
528532
component.isVisible = true
529533
showWarning(CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.invalid.coder.version", coderClient.buildVersion))
530534
}
531-
} else {
532-
val coderVersion = CoderSemVer.parse(coderClient.buildVersion)
533-
if (!coderVersion.isInClosedRange(CoderSupportedVersions.minCompatibleCoderVersion, CoderSupportedVersions.maxCompatibleCoderVersion)) {
534-
logger.warn("Running with incompatible version ${coderClient.buildVersion}")
535-
notificationBanner.apply {
536-
component.isVisible = true
537-
showWarning(CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.unsupported.coder.version", coderClient.buildVersion))
538-
}
539-
} else {
540-
logger.info("Got version ${coderClient.buildVersion}...")
535+
} catch (e: IncompatibleVersionException) {
536+
logger.warn(e)
537+
notificationBanner.apply {
538+
component.isVisible = true
539+
showWarning(CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.unsupported.coder.version", coderClient.buildVersion))
541540
}
542541
}
543542

src/test/groovy/CoderSemVerTest.groovy

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,49 @@ class CoderSemVerTest extends spock.lang.Specification {
278278

279279
]
280280
}
281+
282+
def "should be invalid"() {
283+
when:
284+
CoderSemVer.checkVersionCompatibility(version)
285+
286+
then:
287+
thrown(InvalidVersionException)
288+
289+
where:
290+
version << [
291+
"",
292+
"foo",
293+
"1.foo.2",
294+
]
295+
}
296+
297+
def "should be incompatible"() {
298+
when:
299+
CoderSemVer.checkVersionCompatibility(version)
300+
301+
then:
302+
thrown(IncompatibleVersionException)
303+
304+
where:
305+
version << [
306+
"0.0.0",
307+
"0.12.8",
308+
"9999999999.99999.99",
309+
]
310+
}
311+
312+
def "should be compatible"() {
313+
when:
314+
CoderSemVer.checkVersionCompatibility(version)
315+
316+
then:
317+
noExceptionThrown()
318+
319+
where:
320+
version << [
321+
"0.12.9",
322+
"0.99.99",
323+
"1.0.0",
324+
]
325+
}
281326
}

0 commit comments

Comments
 (0)