From 5bb687078abfe44a495eb073f972ad01d2bbd013 Mon Sep 17 00:00:00 2001 From: pawelpasterz <32893017+pawelpasterz@users.noreply.github.com> Date: Fri, 18 Dec 2020 12:01:10 +0100 Subject: [PATCH] ci: 1411 implement version (#1412) Fixes #1411 Implement `Version` class (and related logic) to handle dependencies versioning ## Test Plan > How do we know the code works? 1. run ``` git checkout master &&\ ./gradlew clean assemble &&\ ./gradlew dependencyUpdates -DoutputFormatter=json -DoutputDir=. &&\ java -jar ./flank-scripts/build/libs/flank-scripts.jar dependencies update &&\ git diff >> dep_master ``` 1. run ``` git checkout . &&\ git checkout 1411-implement-version &&\ ./gradlew clean assemble &&\ ./gradlew dependencyUpdates -DoutputFormatter=json -DoutputDir=. &&\ java -jar ./flank-scripts/build/libs/flank-scripts.jar dependencies update &&\ git diff >> dep_version ``` 1. compare dep files ``` diff dep_master dep_version ``` 1. there should be no differences (nothing is printed to the console, exit code = 0) ## Checklist - [x] Unit tested --- flank-scripts/build.gradle.kts | 2 +- .../update/DependenciesResultCheck.kt | 9 +- .../update/DependencyExtensions.kt | 2 +- .../dependencies/update/DependencyUpdate.kt | 6 +- .../dependencies/update/GradleDependency.kt | 13 ++- .../dependencies/update/UpdateGradle.kt | 11 +- .../update/UpdateVersionsInFile.kt | 2 +- .../kotlin/flank/scripts/utils/Version.kt | 52 +++++++++ .../update/DependencyExtensionsTest.kt | 43 ++++---- .../dependencies/update/UpdateGradleTest.kt | 104 ++++++++++++------ .../update/UpdateVersionsInFileTest.kt | 11 +- ...expected_gradle-wrapper.properties_RC.test | 5 - .../kotlin/flank/scripts/utils/TestParsers.kt | 33 ++++++ .../flank/scripts/utils/VersionParseTest.kt | 53 +++++++++ .../scripts/utils/VersionToStringTest.kt | 33 ++++++ 15 files changed, 294 insertions(+), 85 deletions(-) create mode 100644 flank-scripts/src/main/kotlin/flank/scripts/utils/Version.kt delete mode 100644 flank-scripts/src/test/kotlin/flank/scripts/dependencies/update/testfiles/expected_gradle-wrapper.properties_RC.test create mode 100644 flank-scripts/src/test/kotlin/flank/scripts/utils/TestParsers.kt create mode 100644 flank-scripts/src/test/kotlin/flank/scripts/utils/VersionParseTest.kt create mode 100644 flank-scripts/src/test/kotlin/flank/scripts/utils/VersionToStringTest.kt diff --git a/flank-scripts/build.gradle.kts b/flank-scripts/build.gradle.kts index a469cf88db..26cbd3256f 100644 --- a/flank-scripts/build.gradle.kts +++ b/flank-scripts/build.gradle.kts @@ -28,7 +28,7 @@ shadowJar.apply { } } // .. -version = "1.2.1" +version = "1.2.2" group = "com.github.flank" application { diff --git a/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/DependenciesResultCheck.kt b/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/DependenciesResultCheck.kt index 23e206aab2..684b02c76e 100644 --- a/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/DependenciesResultCheck.kt +++ b/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/DependenciesResultCheck.kt @@ -1,5 +1,6 @@ package flank.scripts.dependencies.update +import flank.scripts.utils.Version import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -13,7 +14,7 @@ data class DependenciesResultCheck( @Serializable data class Dependency( val group: String, - val version: String, + val version: Version, val name: String? = null, @SerialName("available") val availableVersion: AvailableVersion? = null ) @@ -25,7 +26,7 @@ data class Dependencies( @Serializable data class AvailableVersion( - val release: String?, - val milestone: String?, - val integration: String? + val release: Version?, + val milestone: Version?, + val integration: Version? ) diff --git a/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/DependencyExtensions.kt b/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/DependencyExtensions.kt index e0d60217fa..904bdff207 100644 --- a/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/DependencyExtensions.kt +++ b/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/DependencyExtensions.kt @@ -8,4 +8,4 @@ val Dependency.versionToUpdate ?: availableVersion?.integration ?: version -fun GradleDependency.needsUpdate() = (running.version != current.version) || (running.version != releaseCandidate.version) +fun GradleDependency.needsUpdate() = running.version < current.version || running.version < releaseCandidate.version diff --git a/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/DependencyUpdate.kt b/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/DependencyUpdate.kt index f9c28e7046..164b659c06 100644 --- a/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/DependencyUpdate.kt +++ b/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/DependencyUpdate.kt @@ -1,8 +1,10 @@ package flank.scripts.dependencies.update +import flank.scripts.utils.Version + data class DependencyUpdate( val name: String, val valName: String, - val oldVersion: String, - val newVersion: String + val oldVersion: Version, + val newVersion: Version ) diff --git a/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/GradleDependency.kt b/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/GradleDependency.kt index b69d78b631..5a183fb30a 100644 --- a/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/GradleDependency.kt +++ b/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/GradleDependency.kt @@ -1,18 +1,19 @@ package flank.scripts.dependencies.update +import flank.scripts.utils.Version import kotlinx.serialization.Serializable @Serializable data class GradleDependency( - val current: GradleVersion, - val nightly: GradleVersion, - val releaseCandidate: GradleVersion, - val running: GradleVersion + val current: GradleReleaseChannel, + val nightly: GradleReleaseChannel, + val releaseCandidate: GradleReleaseChannel, + val running: GradleReleaseChannel ) @Serializable -data class GradleVersion( - val version: String, +data class GradleReleaseChannel( + val version: Version, val reason: String, val isUpdateAvailable: Boolean, val isFailure: Boolean diff --git a/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/UpdateGradle.kt b/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/UpdateGradle.kt index 01abe3d0c6..0ddb09556c 100644 --- a/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/UpdateGradle.kt +++ b/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/UpdateGradle.kt @@ -14,12 +14,9 @@ private fun updateGradleWrapper(gradleDependency: GradleDependency, gradleWrappe findAllGradleWrapperPropertiesFiles(gradleWrapperPropertiesPath) .forEach { val from = gradleDependency.running.version - val to = if (gradleDependency.releaseCandidate.isUpdateAvailable) - gradleDependency.releaseCandidate.version - else - gradleDependency.current.version + val to = maxOf(gradleDependency.releaseCandidate.version, gradleDependency.current.version) println("Update gradle wrapper $from to $to in file ${it.path}") - it.updateGradleWrapperPropertiesFile(from, to) + it.updateGradleWrapperPropertiesFile(from.toString(), to.toString()) } } @@ -28,8 +25,6 @@ private fun findAllGradleWrapperPropertiesFiles(gradleWrapperPropertiesPath: Str .filter { it.fileName.toString() == GRADLE_WRAPPER_PROPERTIES_FILE } .map { it.toFile() } -private fun File.updateGradleWrapperPropertiesFile(from: String, to: String) { - writeText(readText().replace(from, to)) -} +private fun File.updateGradleWrapperPropertiesFile(from: String, to: String) = writeText(readText().replace(from, to)) private const val GRADLE_WRAPPER_PROPERTIES_FILE = "gradle-wrapper.properties" diff --git a/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/UpdateVersionsInFile.kt b/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/UpdateVersionsInFile.kt index 424da03586..9ed758781b 100644 --- a/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/UpdateVersionsInFile.kt +++ b/flank-scripts/src/main/kotlin/flank/scripts/dependencies/update/UpdateVersionsInFile.kt @@ -17,7 +17,7 @@ private fun String.getInsertLine( .find { containsValDeclaration(it) } ?.let { println("Updated dependency ${it.name} from ${it.oldVersion} to ${it.newVersion}") - replaceFirst(it.oldVersion, it.newVersion) + replaceFirst(it.oldVersion.toString(), it.newVersion.toString()) } ?: this diff --git a/flank-scripts/src/main/kotlin/flank/scripts/utils/Version.kt b/flank-scripts/src/main/kotlin/flank/scripts/utils/Version.kt new file mode 100644 index 0000000000..48e6ad3977 --- /dev/null +++ b/flank-scripts/src/main/kotlin/flank/scripts/utils/Version.kt @@ -0,0 +1,52 @@ +package flank.scripts.utils + +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializable +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder + +object VersionSerializer : KSerializer { + override val descriptor = PrimitiveSerialDescriptor("Version", PrimitiveKind.STRING) + + override fun deserialize(decoder: Decoder): Version = parseToVersion(decoder.decodeString()) + + override fun serialize(encoder: Encoder, value: Version) = encoder.encodeString(value.toString()) +} + +@Serializable(with = VersionSerializer::class) +data class Version( + private val major: Int? = null, + private val minor: Int? = null, + private val micro: Int? = null, + private val patch: Int? = null, + private val qualifier: String? = null +) : Comparable { + private val hasSuffix = major != null && qualifier != null + + override operator fun compareTo(other: Version): Int = when { + major differs other.major -> compareValuesBy(major, other.major, { it ?: 0 }) + minor differs other.minor -> compareValuesBy(minor, other.minor, { it ?: 0 }) + micro differs other.micro -> compareValuesBy(micro, other.micro, { it ?: 0 }) + patch differs other.patch -> compareValuesBy(patch, other.patch, { it ?: 0 }) + else -> nullsLast().compare(qualifier, other.qualifier) + } + + private infix fun Int?.differs(other: Int?) = (this ?: 0) != (other ?: 0) + + override fun toString(): String = listOfNotNull(major, minor, micro, patch) + .joinToString(".") + "${if (hasSuffix) "-" else ""}${qualifier ?: ""}" +} + +fun parseToVersion(versionString: String): Version { + val groups = "(\\d*)\\.?(\\d*)\\.?(\\d*)\\.?(\\d*)[-.]?([a-zA-Z0-9_.-]*)".toRegex().find(versionString)?.groupValues + return if (groups == null) Version(qualifier = versionString) + else Version( + major = groups[1].toIntOrNull(), + minor = groups[2].toIntOrNull(), + micro = groups[3].toIntOrNull(), + patch = groups[4].toIntOrNull(), + qualifier = if (groups[5].isNotBlank()) groups[5] else null + ) +} diff --git a/flank-scripts/src/test/kotlin/flank/scripts/dependencies/update/DependencyExtensionsTest.kt b/flank-scripts/src/test/kotlin/flank/scripts/dependencies/update/DependencyExtensionsTest.kt index 2748b04d06..5abd0337dc 100644 --- a/flank-scripts/src/test/kotlin/flank/scripts/dependencies/update/DependencyExtensionsTest.kt +++ b/flank-scripts/src/test/kotlin/flank/scripts/dependencies/update/DependencyExtensionsTest.kt @@ -1,5 +1,8 @@ package flank.scripts.dependencies.update +import flank.scripts.utils.toAvailableVersion +import flank.scripts.utils.toDependency +import flank.scripts.utils.toGradleReleaseChannel import org.junit.Test import org.junit.Assert.assertEquals @@ -11,7 +14,7 @@ class DependencyExtensionsTest { @Test fun `should return group with name`() { // given - val dependency = Dependency( + val dependency = toDependency( "group", "1.0", "name" @@ -26,11 +29,11 @@ class DependencyExtensionsTest { @Test fun `should get version to update if release available`() { - val dependency = Dependency( + val dependency = toDependency( "group", "1.0", "name", - AvailableVersion( + toAvailableVersion( "1.1", null, null ) ) @@ -44,11 +47,11 @@ class DependencyExtensionsTest { @Test fun `should get version to update if milestone available`() { - val dependency = Dependency( + val dependency = toDependency( "group", "1.0", "name", - AvailableVersion(null, "1.1", null) + toAvailableVersion(null, "1.1", null) ) // when @@ -60,11 +63,11 @@ class DependencyExtensionsTest { @Test fun `should get version to update if integration available`() { - val dependency = Dependency( + val dependency = toDependency( "group", "1.0", "name", - AvailableVersion(null, null, "1.1") + toAvailableVersion(null, null, "1.1") ) // when @@ -76,7 +79,7 @@ class DependencyExtensionsTest { @Test fun `should get current version to update if no update`() { - val dependency = Dependency( + val dependency = toDependency( "group", "1.0", "name", @@ -94,22 +97,22 @@ class DependencyExtensionsTest { fun `should properly check if gradle needs update`() { // given val gradleWhichNeedsUpdate = GradleDependency( - current = GradleVersion("1.1", "test", false, false), - nightly = GradleVersion("1.3", "test", false, false), - releaseCandidate = GradleVersion("1.2rc", "test", false, false), - running = GradleVersion("1", "test", false, false), + current = toGradleReleaseChannel("1.1", "test", false, false), + nightly = toGradleReleaseChannel("1.3", "test", false, false), + releaseCandidate = toGradleReleaseChannel("1.2rc", "test", false, false), + running = toGradleReleaseChannel("1", "test", false, false), ) val gradleWhichNeedsUpdateRc = GradleDependency( - current = GradleVersion("1.1", "test", false, false), - nightly = GradleVersion("1.3", "test", false, false), - releaseCandidate = GradleVersion("1.2rc", "test", false, false), - running = GradleVersion("1.1", "test", false, false), + current = toGradleReleaseChannel("1.1", "test", false, false), + nightly = toGradleReleaseChannel("1.3", "test", false, false), + releaseCandidate = toGradleReleaseChannel("1.2rc", "test", false, false), + running = toGradleReleaseChannel("1.1", "test", false, false), ) val gradleWhichDoesNotNeedUpdate = GradleDependency( - current = GradleVersion("1.1", "test", false, false), - nightly = GradleVersion("1.3", "test", false, false), - releaseCandidate = GradleVersion("1.1", "test", false, false), - running = GradleVersion("1.1", "test", false, false), + current = toGradleReleaseChannel("1.1", "test", false, false), + nightly = toGradleReleaseChannel("1.3", "test", false, false), + releaseCandidate = toGradleReleaseChannel("1.1", "test", false, false), + running = toGradleReleaseChannel("1.1", "test", false, false), ) diff --git a/flank-scripts/src/test/kotlin/flank/scripts/dependencies/update/UpdateGradleTest.kt b/flank-scripts/src/test/kotlin/flank/scripts/dependencies/update/UpdateGradleTest.kt index b3b2979523..ec8bacf8a4 100644 --- a/flank-scripts/src/test/kotlin/flank/scripts/dependencies/update/UpdateGradleTest.kt +++ b/flank-scripts/src/test/kotlin/flank/scripts/dependencies/update/UpdateGradleTest.kt @@ -5,8 +5,11 @@ import org.junit.Rule import org.junit.Test import org.junit.rules.TemporaryFolder import java.io.File +import org.junit.runner.RunWith +import org.junit.runners.Parameterized -class UpdateGradleTest { +@RunWith(Parameterized::class) +class UpdateGradleTest(private val settings: List) { @get:Rule val tempFolder = TemporaryFolder() @@ -14,55 +17,92 @@ class UpdateGradleTest { private val testGradleVersionFile = File("src/test/kotlin/flank/scripts/dependencies/update/testfiles/test_gradle-wrapper.properties.test") + companion object { + @JvmStatic + @Parameterized.Parameters + fun params() = listOf( + asList( + ReleaseCandidate(version = "6.7-rc-1", isExpected = true), + Running(version = "6.5.2"), + Current(version = "6.6") + ), + asList( + ReleaseCandidate(version = "6.6-rc-1"), + Running(version = "6.5.3"), + Current(version = "6.8", isExpected = true) + ), + asList( + ReleaseCandidate(version = "6.7-rc-2", isExpected = true), + Running(version = "6.7-rc-1"), + Current(version = "6.6") + ), + asList( + ReleaseCandidate(version = "6.2-rc-2"), + Running(version = "6.5", isExpected = true), + Current(version = "6.4") + ) + ) + } + @Test fun `Should update gradle`() { // given - val expectedVersions = - File("src/test/kotlin/flank/scripts/dependencies/update/testfiles/expected_gradle-wrapper.properties.test") + val expectedVersions = testGradleVersionFile.replaceGradleVersion(settings.first { it.isExpected }.version) val copyOfTestVersions = tempFolder.newFile("gradle-wrapper.properties").apply { - writeText(testGradleVersionFile.readText()) + writeText(testGradleVersionFile.replaceGradleVersion(settings.first { it.name == "running" }.version)) + } + val preparedReport = tempFolder.newFile("report.json").apply { + writeText(testReport.replaceChannelTemplate(settings)) } // when - testReport.updateGradle(tempFolder.root.absolutePath) + preparedReport.updateGradle(tempFolder.root.absolutePath) // then - assertEquals(copyOfTestVersions.readText(), expectedVersions.readText()) + assertEquals(expectedVersions, copyOfTestVersions.readText()) } +} - @Test - fun `Should update RC version gradle`() { - val rcReportText = testReport.readText().replace("\r\n", "\n").replace( - """ - | "releaseCandidate": { - | "version": "6.7-rc-1", +private fun File.replaceChannelTemplate(settings: List): String { + var result = readText().replace("\r\n", "\n") + for (setting in settings) { + result = result.replace("\"${setting.name}\": \\{([^{}]+)}".toRegex(), """ + | "${setting.name}": { + | "version": "${setting.version}", | "reason": "", | "isUpdateAvailable": false, | "isFailure": false - | } - """.trimMargin(), """ - | "releaseCandidate": { - | "version": "6.7-rc-1", - | "reason": "", - | "isUpdateAvailable": true, - | "isFailure": false | } """.trimMargin() ) + } + return result +} - val rcReport = tempFolder.newFile("rcReport.json").apply { writeText(rcReportText) } +private fun asList(vararg settings: TestChannelSettings) = arrayOf(settings.toList()) - // given - val expectedVersions = - File("src/test/kotlin/flank/scripts/dependencies/update/testfiles/expected_gradle-wrapper.properties_RC.test") - val copyOfTestVersions = tempFolder.newFile("gradle-wrapper.properties").apply { - writeText(testGradleVersionFile.readText()) - } +private fun File.replaceGradleVersion(version: String) = readText() + .replace("\r\n", "\n") + .replace("6.5.1", version) - // when - rcReport.updateGradle(tempFolder.root.absolutePath) +sealed class TestChannelSettings( + val name: String, + val version: String, + val isExpected: Boolean +) + +private class ReleaseCandidate( + version: String, + isExpected: Boolean = false +) : TestChannelSettings("releaseCandidate", version, isExpected) + +private class Running( + version: String, + isExpected: Boolean = false +) : TestChannelSettings("running", version, isExpected) + +private class Current( + version: String, + isExpected: Boolean = false +) : TestChannelSettings("current", version, isExpected) - // then - assertEquals(copyOfTestVersions.readText(), expectedVersions.readText()) - } -} diff --git a/flank-scripts/src/test/kotlin/flank/scripts/dependencies/update/UpdateVersionsInFileTest.kt b/flank-scripts/src/test/kotlin/flank/scripts/dependencies/update/UpdateVersionsInFileTest.kt index c220ea6773..3b13c91293 100644 --- a/flank-scripts/src/test/kotlin/flank/scripts/dependencies/update/UpdateVersionsInFileTest.kt +++ b/flank-scripts/src/test/kotlin/flank/scripts/dependencies/update/UpdateVersionsInFileTest.kt @@ -1,5 +1,6 @@ package flank.scripts.dependencies.update +import flank.scripts.utils.toDependencyUpdate import org.junit.Assert.assertEquals import org.junit.Test import skipIfWindows @@ -19,11 +20,11 @@ class UpdateVersionsInFileTest { val expectedFileAfterChanged = File("src/test/kotlin/flank/scripts/dependencies/update/testfiles/ExpectedTestVersionsAfterUpdateVersion") val dependenciesToUpdate = listOf( - DependencyUpdate("DD_PLIST", "DD_PLIST", "1.23", "3.21"), - DependencyUpdate("DETEKT", "DETEKT", "1.11.0", "0.11.1"), - DependencyUpdate("PICOCLI", "PICOCLI", "4.4.0", "0.4.4"), - DependencyUpdate("JACKSON", "JACKSON", "2.11.0", "0.11.2"), - DependencyUpdate("LOGBACK", "LOGBACK", "1.2.3", "3.2.1") + toDependencyUpdate("DD_PLIST", "DD_PLIST", "1.23", "3.21"), + toDependencyUpdate("DETEKT", "DETEKT", "1.11.0", "0.11.1"), + toDependencyUpdate("PICOCLI", "PICOCLI", "4.4.0", "0.4.4"), + toDependencyUpdate("JACKSON", "JACKSON", "2.11.0", "0.11.2"), + toDependencyUpdate("LOGBACK", "LOGBACK", "1.2.3", "3.2.1") ) // when diff --git a/flank-scripts/src/test/kotlin/flank/scripts/dependencies/update/testfiles/expected_gradle-wrapper.properties_RC.test b/flank-scripts/src/test/kotlin/flank/scripts/dependencies/update/testfiles/expected_gradle-wrapper.properties_RC.test deleted file mode 100644 index 21f9f98d6c..0000000000 --- a/flank-scripts/src/test/kotlin/flank/scripts/dependencies/update/testfiles/expected_gradle-wrapper.properties_RC.test +++ /dev/null @@ -1,5 +0,0 @@ -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-rc-1-bin.zip -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists diff --git a/flank-scripts/src/test/kotlin/flank/scripts/utils/TestParsers.kt b/flank-scripts/src/test/kotlin/flank/scripts/utils/TestParsers.kt new file mode 100644 index 0000000000..87d54447a2 --- /dev/null +++ b/flank-scripts/src/test/kotlin/flank/scripts/utils/TestParsers.kt @@ -0,0 +1,33 @@ +package flank.scripts.utils + +import flank.scripts.dependencies.update.AvailableVersion +import flank.scripts.dependencies.update.Dependency +import flank.scripts.dependencies.update.DependencyUpdate +import flank.scripts.dependencies.update.GradleReleaseChannel + +fun toGradleReleaseChannel( + version: String, + reason: String, + isUpdateAvailable: Boolean, + isFailure: Boolean +) = GradleReleaseChannel(parseToVersion(version), reason, isUpdateAvailable, isFailure) + +fun toDependency( + group: String, + version: String, + name: String? = null, + availableVersion: AvailableVersion? = null +) = Dependency(group, parseToVersion(version), name, availableVersion) + +fun toDependencyUpdate( + name: String, + valName: String, + oldVersion: String, + newVersion: String +) = DependencyUpdate(name, valName, parseToVersion(oldVersion), parseToVersion(newVersion)) + +fun toAvailableVersion( + release: String?, + milestone: String?, + integration: String? +) = AvailableVersion(release?.let(::parseToVersion), milestone?.let(::parseToVersion), integration?.let(::parseToVersion)) diff --git a/flank-scripts/src/test/kotlin/flank/scripts/utils/VersionParseTest.kt b/flank-scripts/src/test/kotlin/flank/scripts/utils/VersionParseTest.kt new file mode 100644 index 0000000000..eedbf4f45c --- /dev/null +++ b/flank-scripts/src/test/kotlin/flank/scripts/utils/VersionParseTest.kt @@ -0,0 +1,53 @@ +package flank.scripts.utils + +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized + +@RunWith(Parameterized::class) +class VersionParseTest( + private val left: String, + private val right: String, + private val expectedResult: Compare +) { + companion object { + + @JvmStatic + @Parameterized.Parameters(name = "compare {0} and {1} with result {2}") + fun inputs() = listOf( + arrayOf("6.6.1", "6.7", Smaller), + arrayOf("1", "2", Smaller), + arrayOf("2.0.0", "1.9.9.9", Greater), + arrayOf("2.0.0", "2.0.0-rc-1", Greater), + arrayOf("2.0.0-rc-1", "2.0", Smaller), + arrayOf("6.7.1", "6.8-rc-1", Smaller), + arrayOf("v1beta3-rev20201029-1.30.10", "v1beta3-rev20201028-1.30.10", Greater), + arrayOf("v1beta3-rev20201029-1.30.10", "v1beta3-rev20201228-1.30.10", Smaller), + arrayOf("1.2.3", "1.2.3.0", Equal), + arrayOf("1.2.3.0", "1.2.3", Equal), + arrayOf("6.8.1-rc-1", "6.8.1-rc-2", Smaller), + ) + } + + @Test + fun `compare versions`() { + // We do not use > operator to test equality + val result = parseToVersion(left).compareTo(parseToVersion(right)) + assertTrue( + "$left should be evaluated as ${expectedResult.name()} $right: result = $result", + expectedResult(result) + ) + } +} + +sealed class Compare(private val lambda: (Int) -> Boolean) { + operator fun invoke(int: Int) = lambda(int) + fun name() = this::class.simpleName?.toUpperCase() ?: "" + override fun toString() = name() +} + +private object Greater : Compare({ it > 0 }) +private object Smaller : Compare({ it < 0 }) +private object Equal : Compare({ it == 0 }) + diff --git a/flank-scripts/src/test/kotlin/flank/scripts/utils/VersionToStringTest.kt b/flank-scripts/src/test/kotlin/flank/scripts/utils/VersionToStringTest.kt new file mode 100644 index 0000000000..02e4e2ff2c --- /dev/null +++ b/flank-scripts/src/test/kotlin/flank/scripts/utils/VersionToStringTest.kt @@ -0,0 +1,33 @@ +package flank.scripts.utils + +import org.junit.Assert +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized + +@RunWith(Parameterized::class) +class VersionToStringTest( + private val left: String, +) { + companion object { + + @JvmStatic + @Parameterized.Parameters + fun inputs() = listOf( + "6.6.1", + "1", + "2.0.0", + "2.0.0-rc-1", + "2.0", + "6.8-rc-1", + "v1beta3-rev20201029-1.30.10", + "1.2.3.0", + "1.10.3-jdk8" + ).map { arrayOf(it) } + } + + @Test + fun `compare strings`() { + Assert.assertEquals(left, parseToVersion(left).toString()) + } +}