diff --git a/tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/CalculateVersion.kt b/tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/CalculateVersion.kt index 283f2ef..968c004 100644 --- a/tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/CalculateVersion.kt +++ b/tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/CalculateVersion.kt @@ -2,6 +2,7 @@ package com.zegreatrob.tools.tagger.cli import com.github.ajalt.clikt.core.CliktCommand import com.github.ajalt.clikt.parameters.arguments.argument +import com.github.ajalt.clikt.parameters.options.check import com.github.ajalt.clikt.parameters.options.default import com.github.ajalt.clikt.parameters.options.option import com.github.ajalt.clikt.parameters.types.boolean @@ -14,11 +15,15 @@ class CalculateVersion : CliktCommand() { private val dir by argument("git-repo") private val implicitPatch by option().boolean().default(true) private val releaseBranch by option() - private val versionRegex by option() private val majorRegex by option().default(VersionRegex.Defaults.major.pattern) private val minorRegex by option().default(VersionRegex.Defaults.minor.pattern) private val patchRegex by option().default(VersionRegex.Defaults.patch.pattern) private val noneRegex by option().default(VersionRegex.Defaults.none.pattern) + private val versionRegex by option().check( + message = VersionRegex.MISSING_GROUP_ERROR, + validator = VersionRegex.Companion::containsAllGroups, + ) + override fun run() { TaggerCore(GitAdapter(dir)) .calculateNextVersion( diff --git a/tools/tagger-cli/src/jvmTest/kotlin/com/zegreatrob/tools/tagger/cli/CalculateVersionCommandTest.kt b/tools/tagger-cli/src/jvmTest/kotlin/com/zegreatrob/tools/tagger/cli/CalculateVersionCommandTest.kt index 95336e6..84ced2f 100644 --- a/tools/tagger-cli/src/jvmTest/kotlin/com/zegreatrob/tools/tagger/cli/CalculateVersionCommandTest.kt +++ b/tools/tagger-cli/src/jvmTest/kotlin/com/zegreatrob/tools/tagger/cli/CalculateVersionCommandTest.kt @@ -2,6 +2,7 @@ package com.zegreatrob.tools.tagger.cli import com.github.ajalt.clikt.testing.test import com.zegreatrob.tools.tagger.CalculateVersionTestSpec +import com.zegreatrob.tools.tagger.TestResult import org.junit.jupiter.api.io.TempDir import java.io.File import kotlin.test.BeforeTest @@ -42,8 +43,16 @@ class CalculateVersionCommandTest : CalculateVersionTestSpec { arguments += projectDir.absolutePath } - override fun runCalculateVersion(): String = CalculateVersion() - .test(arguments) - .output - .trim() + override fun runCalculateVersion(): TestResult { + val test = CalculateVersion() + .test(arguments) + return if (test.statusCode == 0) { + test + .output + .trim() + .let { TestResult.Success(it) } + } else { + TestResult.Failure(test.output.trim()) + } + } } diff --git a/tools/tagger-core/src/commonMain/kotlin/com/zegreatrob/tools/tagger/core/VersionRegex.kt b/tools/tagger-core/src/commonMain/kotlin/com/zegreatrob/tools/tagger/core/VersionRegex.kt index e8e7b5d..6f05d81 100644 --- a/tools/tagger-core/src/commonMain/kotlin/com/zegreatrob/tools/tagger/core/VersionRegex.kt +++ b/tools/tagger-core/src/commonMain/kotlin/com/zegreatrob/tools/tagger/core/VersionRegex.kt @@ -15,4 +15,14 @@ data class VersionRegex( val minor = MessageDigger.Defaults.minorRegex val major = MessageDigger.Defaults.majorRegex } + + companion object { + + fun containsAllGroups(regexPattern: String) = regexPattern.contains("?") && + regexPattern.contains("?") && + regexPattern.contains("?") && + regexPattern.contains("?") + + const val MISSING_GROUP_ERROR = "version regex must include groups named 'major', 'minor', 'patch', and 'none'." + } } diff --git a/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TaggerPluginCalculateVersionFunctionalTest.kt b/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/CalculateVersionFunctionalTest.kt similarity index 82% rename from tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TaggerPluginCalculateVersionFunctionalTest.kt rename to tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/CalculateVersionFunctionalTest.kt index be79731..ce68a8d 100644 --- a/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TaggerPluginCalculateVersionFunctionalTest.kt +++ b/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/CalculateVersionFunctionalTest.kt @@ -13,10 +13,9 @@ import kotlin.io.path.absolutePathString import kotlin.io.path.createTempDirectory import kotlin.test.BeforeTest import kotlin.test.Test -import kotlin.test.assertContains import kotlin.test.assertEquals -class TaggerPluginCalculateVersionFunctionalTest : CalculateVersionTestSpec { +class CalculateVersionFunctionalTest : CalculateVersionTestSpec { @field:TempDir override lateinit var projectDir: File @@ -114,32 +113,18 @@ class TaggerPluginCalculateVersionFunctionalTest : CalculateVersionTestSpec { assertEquals("1.0.0", gitAdapter.showTag("HEAD")) } - @Test - fun unifiedGroupWillReportErrorsWhenMissingGroupsWithCorrectNames() { - setupWithOverrides(implicitPatch = true, versionRegex = ".*") - - initializeGitRepo(listOf("init", "commit (no) 1"), "1.2.3") - val runner = GradleRunner.create() - runner.forwardOutput() - runner.withPluginClasspath() - runner.withArguments("calculateVersion", "-q") - runner.withProjectDir(projectDir) - val result = runCatching { runner.build() }.exceptionOrNull() - - assertContains( - charSequence = result.toString(), - other = "version regex must include groups named 'major', 'minor', 'patch', and 'none'.", - ) - } - - override fun runCalculateVersion(): String { + override fun runCalculateVersion(): TestResult { val runner = GradleRunner.create() runner.forwardOutput() runner.withPluginClasspath() runner.withArguments("calculateVersion", "-q") runner.withProjectDir(projectDir) - val result = runner.build() - return result.output.trim() + return try { + val result = runner.build() + result.output.trim().let(TestResult::Success) + } catch (e: Exception) { + TestResult.Failure(e.message!!) + } } private fun disableGpgSign(directory: File) { diff --git a/tools/tagger-plugin/src/main/kotlin/com/zegreatrob/tools/tagger/TaggerExtension.kt b/tools/tagger-plugin/src/main/kotlin/com/zegreatrob/tools/tagger/TaggerExtension.kt index ccd7ba9..df2b1ea 100644 --- a/tools/tagger-plugin/src/main/kotlin/com/zegreatrob/tools/tagger/TaggerExtension.kt +++ b/tools/tagger-plugin/src/main/kotlin/com/zegreatrob/tools/tagger/TaggerExtension.kt @@ -67,13 +67,10 @@ open class TaggerExtension( private fun Regex.validateVersionRegex() { if ( - pattern.contains("?") && - pattern.contains("?") && - pattern.contains("?") && - pattern.contains("?") + VersionRegex.containsAllGroups(pattern) ) { return } else { - throw GradleException("version regex must include groups named 'major', 'minor', 'patch', and 'none'.") + throw GradleException(VersionRegex.MISSING_GROUP_ERROR) } } diff --git a/tools/tagger-test/src/jvmMain/kotlin/com/zegreatrob/tools/tagger/CalculateVersionTestSpec.kt b/tools/tagger-test/src/jvmMain/kotlin/com/zegreatrob/tools/tagger/CalculateVersionTestSpec.kt index 5189c1f..ac91bbc 100644 --- a/tools/tagger-test/src/jvmMain/kotlin/com/zegreatrob/tools/tagger/CalculateVersionTestSpec.kt +++ b/tools/tagger-test/src/jvmMain/kotlin/com/zegreatrob/tools/tagger/CalculateVersionTestSpec.kt @@ -9,7 +9,9 @@ import org.ajoberstar.grgit.operation.RemoteAddOp import org.ajoberstar.grgit.operation.TagAddOp import java.io.File import kotlin.test.Test +import kotlin.test.assertContains import kotlin.test.assertEquals +import kotlin.test.fail interface CalculateVersionTestSpec { var projectDir: File @@ -37,14 +39,19 @@ interface CalculateVersionTestSpec { commits = commits, ) - fun runCalculateVersion(): String + fun runCalculateVersion(): TestResult + fun runCalculateVersionSuccessfully(): String = + when (val result = runCalculateVersion()) { + is TestResult.Success -> result.message + is TestResult.Failure -> fail("Expected success but got ${result.reason}") + } @Test fun `calculating version with no tags produces zero version`() { setupWithDefaults() initializeGitRepo(listOf("init", "[patch] commit 1", "[patch] commit 2")) - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("0.0.0", version) } @@ -77,7 +84,7 @@ interface CalculateVersionTestSpec { it.url = projectDir.absolutePath }, ) - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("1.0.23-SNAPSHOT", version) } @@ -87,7 +94,7 @@ interface CalculateVersionTestSpec { setupWithDefaults() initializeGitRepo(listOf("init", "[patch] commit 1", "[patch] commit 2"), initialTag = "1.2.3") - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("1.2.4", version) } @@ -97,7 +104,7 @@ interface CalculateVersionTestSpec { setupWithOverrides(implicitPatch = false) initializeGitRepo(commits = listOf("init", "commit 1", "commit 2"), initialTag = "1.2.3") - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("1.2.3-SNAPSHOT", version) } @@ -107,7 +114,7 @@ interface CalculateVersionTestSpec { setupWithOverrides(implicitPatch = false) initializeGitRepo(commits = listOf("init", "commit 1", "commit 2"), initialTag = "1.2.3-SNAPSHOT") - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("1.2.3-SNAPSHOT", version) } @@ -117,7 +124,7 @@ interface CalculateVersionTestSpec { setupWithOverrides(implicitPatch = true) initializeGitRepo(commits = listOf("init", "commit 1", "commit 2"), initialTag = "1.2.3") - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("1.2.4", version) } @@ -127,7 +134,7 @@ interface CalculateVersionTestSpec { setupWithOverrides(implicitPatch = true) initializeGitRepo(commits = listOf("init", "[none] commit 1", "commit 2"), initialTag = "1.2.3") - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("1.2.4", version) } @@ -137,7 +144,7 @@ interface CalculateVersionTestSpec { setupWithOverrides(implicitPatch = true) initializeGitRepo(commits = listOf("init", "[None] commit 1", "[none] commit 2"), initialTag = "1.2.3") - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("1.2.3-SNAPSHOT", version) } @@ -150,7 +157,7 @@ interface CalculateVersionTestSpec { commits = listOf("init", "[patch] commit 1", "[minor] commit 2", "[patch] commit 3"), initialTag = "1.2.3", ) - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("1.3.0", version) } @@ -163,7 +170,7 @@ interface CalculateVersionTestSpec { commits = listOf("init", "[patch] commit 1", "commit (big) 2", "[patch] commit 3"), initialTag = "1.2.3", ) - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("2.0.0", version) } @@ -180,7 +187,7 @@ interface CalculateVersionTestSpec { initialTag = "1.2.3", ) - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("2.0.0", version) } @@ -196,7 +203,7 @@ interface CalculateVersionTestSpec { commits = listOf("init", "[patch] commit 1", "commit (middle) 2", "[patch] commit 3"), initialTag = "1.2.3", ) - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("1.3.0", version) } @@ -209,7 +216,7 @@ interface CalculateVersionTestSpec { commits = listOf("init", "[patch] commit 1", "commit (middle) 2", "[patch] commit 3"), initialTag = "1.2.3", ) - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("1.3.0", version) } @@ -219,7 +226,7 @@ interface CalculateVersionTestSpec { setupWithOverrides(implicitPatch = false, patchRegex = ".*(tiny).*") initializeGitRepo(commits = listOf("init", "commit 1", "commit (tiny) 2", "commit 3"), initialTag = "1.2.3") - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("1.2.4", version) } @@ -232,7 +239,7 @@ interface CalculateVersionTestSpec { ) initializeGitRepo(commits = listOf("init", "commit 1", "commit (widdle) 2", "commit 3"), initialTag = "1.2.3") - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("1.2.4", version) } @@ -245,7 +252,7 @@ interface CalculateVersionTestSpec { ) initializeGitRepo(commits = listOf("init", "commit (no) 1"), initialTag = "1.2.3") - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("1.2.3-SNAPSHOT", version) } @@ -258,7 +265,7 @@ interface CalculateVersionTestSpec { ) initializeGitRepo(commits = listOf("init", "commit (no) 1"), initialTag = "1.2.3") - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("1.2.3-SNAPSHOT", version) } @@ -271,7 +278,22 @@ interface CalculateVersionTestSpec { commits = listOf("init", "[major] commit 1", "[minor] commit 2", "[patch] commit 3"), initialTag = "1.2.3", ) - val version = runCalculateVersion() + val version = runCalculateVersionSuccessfully() assertEquals("2.0.0", version) } + + @Test + fun unifiedGroupWillReportErrorsWhenMissingGroupsWithCorrectNames() { + setupWithOverrides(implicitPatch = true, versionRegex = ".*") + + initializeGitRepo(listOf("init", "commit (no) 1"), "1.2.3") + when (val result = runCalculateVersion()) { + is TestResult.Failure -> assertContains( + result.reason, + "version regex must include groups named 'major', 'minor', 'patch', and 'none'.", + ) + + is TestResult.Success -> fail("Should not have succeeded.") + } + } } diff --git a/tools/tagger-test/src/jvmMain/kotlin/com/zegreatrob/tools/tagger/TestResult.kt b/tools/tagger-test/src/jvmMain/kotlin/com/zegreatrob/tools/tagger/TestResult.kt new file mode 100644 index 0000000..4265877 --- /dev/null +++ b/tools/tagger-test/src/jvmMain/kotlin/com/zegreatrob/tools/tagger/TestResult.kt @@ -0,0 +1,6 @@ +package com.zegreatrob.tools.tagger + +sealed class TestResult { + data class Success(val message: String) : TestResult() + data class Failure(val reason: String) : TestResult() +}