-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add codegen version to generated package (#1621)
* Add codegen version to generated package metadata * Update `CHANGELOG.next.toml` * Remove unnecessary try-catch block from `smithyCodegenVersion` * Add git commit hash to version * Fix version filename * Add tests for `Version` * Store version in "$smithyRsVersion\n$gitCommitHash" format * Make version parsing more strict
- Loading branch information
Showing
7 changed files
with
181 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/Version.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.rust.codegen.smithy | ||
|
||
import software.amazon.smithy.codegen.core.CodegenException | ||
|
||
// generated as part of the build, see codegen/build.gradle.kts | ||
private const val VERSION_FILENAME = "runtime-crate-version.txt" | ||
|
||
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 | ||
|
||
fun crateVersion(): String = | ||
fromDefaultResource().crateVersion | ||
|
||
private fun fromDefaultResource(): Version = | ||
parse( | ||
object {}.javaClass.getResource(VERSION_FILENAME)?.readText() | ||
?: throw CodegenException("$VERSION_FILENAME does not exist"), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/VersionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
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 | ||
import org.junit.jupiter.params.provider.MethodSource | ||
|
||
class VersionTest { | ||
@ParameterizedTest() | ||
@MethodSource("versionProvider") | ||
fun `parses version`( | ||
content: String, | ||
fullVersion: String, | ||
crateVersion: String, | ||
) { | ||
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 { | ||
@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", | ||
), | ||
) | ||
|
||
@JvmStatic | ||
fun invalidVersionProvider() = listOf("0.0.0", "") | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...st/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CargoTomlGeneratorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
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 | ||
|
||
class CargoTomlGeneratorTest { | ||
private val CargoMetadata: CargoDependency = CargoDependency("cargo_metadata", CratesIo("0.15.0")) | ||
|
||
@Test | ||
fun `adds codegen version to package metadata`() { | ||
val project = TestWorkspace.testProject() | ||
project.lib { writer -> | ||
writer.addDependency(CargoMetadata) | ||
writer.unitTest( | ||
"smithy_codegen_version_in_package_metadata", | ||
""" | ||
let metadata = cargo_metadata::MetadataCommand::new() | ||
.exec() | ||
.expect("could not run `cargo metadata`"); | ||
let pgk_metadata = &metadata.root_package().expect("missing root package").metadata; | ||
let codegen_version = pgk_metadata | ||
.get("smithy") | ||
.and_then(|s| s.get("codegen-version")) | ||
.expect("missing `smithy.codegen-version` field") | ||
.as_str() | ||
.expect("`smithy.codegen-version` is not str"); | ||
assert_eq!(codegen_version, "${Version.fullVersion()}"); | ||
""", | ||
) | ||
} | ||
project.compileAndTest() | ||
} | ||
} |