From 5cd9d694337e4ff2ff1a90e0232c95cf7a9aa4fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Varl=C4=B1?= Date: Wed, 10 Aug 2022 12:12:14 +0100 Subject: [PATCH] Store version in "$smithyRsVersion\n$gitCommitHash" format --- codegen/build.gradle.kts | 2 +- .../smithy/rust/codegen/smithy/Version.kt | 21 +++---- .../smithy/rust/codegen/smithy/VersionTest.kt | 56 ++++++++++++++++--- 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/codegen/build.gradle.kts b/codegen/build.gradle.kts index ab4a337d015..b19e7fa0b82 100644 --- a/codegen/build.gradle.kts +++ b/codegen/build.gradle.kts @@ -61,7 +61,7 @@ val generateSmithyRuntimeCrateVersion by tasks.registering { val crateVersion = project.properties["smithy.rs.runtime.crate.version"].toString() inputs.property("crateVersion", crateVersion) // version format must be in sync with `software.amazon.smithy.rust.codegen.smithy.Version` - val version = "$crateVersion-${gitCommitHash()}" + val version = "$crateVersion\n${gitCommitHash()}" sourceSets.main.get().output.dir(resourcesDir) doLast { versionFile.writeText(version) 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 d606f16906d..e767fa87250 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,22 +7,17 @@ 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 +private const val VERSION_FILENAME = "runtime-crate-version.txt" + class Version(private val content: String) { - fun fullVersion(): String = content - - fun crateVersion(): String { - val parts = content.split("-") - if (parts.size < 2) { - return "" - } - return parts.first() - } + // Returns full version in the "{smithy_rs_version}-{git_commit_hash}" format + fun fullVersion(): String = content.lines().joinToString("-") - companion object { - // generated as part of the build in the "{smithy_rs_version}-{git_commit_hash}" format, - // see codegen/build.gradle.kts - private const val VERSION_FILENAME = "runtime-crate-version.txt" + fun crateVersion(): String = content.lines().first() + companion object { fun fullVersion(): String = fromDefaultResource().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 567ea9a4265..def4ba6bd7f 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,23 +5,61 @@ package software.amazon.smithy.rust.codegen.smithy -import org.junit.jupiter.api.Assertions +import io.kotest.matchers.shouldBe import org.junit.jupiter.params.ParameterizedTest -import org.junit.jupiter.params.provider.CsvSource +import org.junit.jupiter.params.provider.Arguments +import org.junit.jupiter.params.provider.MethodSource class VersionTest { @ParameterizedTest() - @CsvSource( - "'0.47.0-0198d26096eb1af510ce24766c921ffc5e4c191e','0.47.0-0198d26096eb1af510ce24766c921ffc5e4c191e','0.47.0'", - "'0.0.0','0.0.0',''", - "'','',''", - ) + @MethodSource("versionProvider") fun `parses version`( content: String, fullVersion: String, crateVersion: String, ) { - Assertions.assertEquals(fullVersion, Version(content).fullVersion()) - Assertions.assertEquals(crateVersion, Version(content).crateVersion()) + Version(content).fullVersion() shouldBe fullVersion + Version(content).crateVersion() shouldBe crateVersion + } + + companion object { + @JvmStatic + fun versionProvider() = listOf( + Arguments.of( + "0.47.0\n0198d26096eb1af510ce24766c921ffc5e4c191e", + "0.47.0-0198d26096eb1af510ce24766c921ffc5e4c191e", + "0.47.0", + ), + Arguments.of( + "release-2022-08-04\ndb48039065bec890ef387385773b37154b555b14", + "release-2022-08-04-db48039065bec890ef387385773b37154b555b14", + "release-2022-08-04", + ), + Arguments.of( + "0.30.0-alpha\na1dbbe2947de3c8bbbef9446eb442e298f83f200", + "0.30.0-alpha-a1dbbe2947de3c8bbbef9446eb442e298f83f200", + "0.30.0-alpha", + ), + Arguments.of( + "0.6-rc1.cargo\nc281800a185b34600b05f8b501a0322074184123", + "0.6-rc1.cargo-c281800a185b34600b05f8b501a0322074184123", + "0.6-rc1.cargo", + ), + Arguments.of( + "0.27.0-alpha.1\n643f2ee", + "0.27.0-alpha.1-643f2ee", + "0.27.0-alpha.1", + ), + Arguments.of( + "0.0.0", + "0.0.0", + "0.0.0", + ), + Arguments.of( + "", + "", + "", + ), + ) } }