diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/Version.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/Version.kt index e767fa8725..322b933f9c 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/Version.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/Version.kt @@ -7,25 +7,29 @@ package software.amazon.smithy.rust.codegen.smithy import software.amazon.smithy.codegen.core.CodegenException -// generated as part of the build in the "{smithy_rs_version}\n{git_commit_hash}" format, -// see codegen/build.gradle.kts +// generated as part of the build, see codegen/build.gradle.kts private const val VERSION_FILENAME = "runtime-crate-version.txt" -class Version(private val content: String) { - // Returns full version in the "{smithy_rs_version}-{git_commit_hash}" format - fun fullVersion(): String = content.lines().joinToString("-") - - fun crateVersion(): String = content.lines().first() - +data class Version(val fullVersion: String, val crateVersion: String) { companion object { + // Version must be in the "{smithy_rs_version}\n{git_commit_hash}" format + fun parse(content: String): Version { + val lines = content.lines() + if (lines.size != 2) { + throw IllegalArgumentException("Invalid version format, it should contain `2` lines but contains `${lines.size}` line(s)") + } + return Version(lines.joinToString("-"), lines.first()) + } + + // Returns full version in the "{smithy_rs_version}-{git_commit_hash}" format fun fullVersion(): String = - fromDefaultResource().fullVersion() + fromDefaultResource().fullVersion fun crateVersion(): String = - fromDefaultResource().crateVersion() + fromDefaultResource().crateVersion private fun fromDefaultResource(): Version = - Version( + parse( object {}.javaClass.getResource(VERSION_FILENAME)?.readText() ?: throw CodegenException("$VERSION_FILENAME does not exist"), ) diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CargoTomlGenerator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CargoTomlGenerator.kt index 975e1060c7..b932cac262 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CargoTomlGenerator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CargoTomlGenerator.kt @@ -66,7 +66,7 @@ class CargoTomlGenerator( "repository" to settings.moduleRepository, "metadata" to listOfNotNull( "smithy" to listOfNotNull( - "codegen-version" to smithyCodegenVersion(), + "codegen-version" to Version.fullVersion(), ).toMap(), ).toMap(), ).toMap(), @@ -80,5 +80,3 @@ class CargoTomlGenerator( writer.writeWithNoFormatting(TomlWriter().write(cargoToml)) } } - -fun smithyCodegenVersion(): String = Version.fullVersion() diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/VersionTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/VersionTest.kt index def4ba6bd7..56c254c2d6 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/VersionTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/VersionTest.kt @@ -5,6 +5,7 @@ package software.amazon.smithy.rust.codegen.smithy +import io.kotest.assertions.throwables.shouldThrowAny import io.kotest.matchers.shouldBe import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.Arguments @@ -18,8 +19,17 @@ class VersionTest { fullVersion: String, crateVersion: String, ) { - Version(content).fullVersion() shouldBe fullVersion - Version(content).crateVersion() shouldBe crateVersion + val version = Version.parse(content) + version.fullVersion shouldBe fullVersion + version.crateVersion shouldBe crateVersion + } + + @ParameterizedTest() + @MethodSource("invalidVersionProvider") + fun `fails to parse version`( + content: String, + ) { + shouldThrowAny { Version.parse(content) } } companion object { @@ -50,16 +60,9 @@ class VersionTest { "0.27.0-alpha.1-643f2ee", "0.27.0-alpha.1", ), - Arguments.of( - "0.0.0", - "0.0.0", - "0.0.0", - ), - Arguments.of( - "", - "", - "", - ), ) + + @JvmStatic + fun invalidVersionProvider() = listOf("0.0.0", "") } } diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CargoTomlGeneratorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CargoTomlGeneratorTest.kt index 26b06fbd1f..ecc20a7f02 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CargoTomlGeneratorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CargoTomlGeneratorTest.kt @@ -8,6 +8,7 @@ package software.amazon.smithy.rust.codegen.smithy.generators import org.junit.jupiter.api.Test import software.amazon.smithy.rust.codegen.rustlang.CargoDependency import software.amazon.smithy.rust.codegen.rustlang.CratesIo +import software.amazon.smithy.rust.codegen.smithy.Version import software.amazon.smithy.rust.codegen.testutil.TestWorkspace import software.amazon.smithy.rust.codegen.testutil.compileAndTest import software.amazon.smithy.rust.codegen.testutil.unitTest @@ -35,7 +36,7 @@ class CargoTomlGeneratorTest { .expect("missing `smithy.codegen-version` field") .as_str() .expect("`smithy.codegen-version` is not str"); - assert_eq!(codegen_version, "${smithyCodegenVersion()}"); + assert_eq!(codegen_version, "${Version.fullVersion()}"); """, ) }