Skip to content

Commit

Permalink
Make version parsing more strict
Browse files Browse the repository at this point in the history
  • Loading branch information
unexge committed Aug 11, 2022
1 parent 2fbbd78 commit fb56429
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -80,5 +80,3 @@ class CargoTomlGenerator(
writer.writeWithNoFormatting(TomlWriter().write(cargoToml))
}
}

fun smithyCodegenVersion(): String = Version.fullVersion()
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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", "")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()}");
""",
)
}
Expand Down

0 comments on commit fb56429

Please sign in to comment.