Skip to content

Commit

Permalink
[patch] adding a little more validation to cli calculate version
Browse files Browse the repository at this point in the history
  • Loading branch information
robertfmurdock committed Aug 24, 2024
1 parent c2f642b commit 7bb373c
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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("?<major>") &&
regexPattern.contains("?<minor>") &&
regexPattern.contains("?<patch>") &&
regexPattern.contains("?<none>")

const val MISSING_GROUP_ERROR = "version regex must include groups named 'major', 'minor', 'patch', and 'none'."
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,10 @@ open class TaggerExtension(

private fun Regex.validateVersionRegex() {
if (
pattern.contains("?<major>") &&
pattern.contains("?<minor>") &&
pattern.contains("?<patch>") &&
pattern.contains("?<none>")
VersionRegex.containsAllGroups(pattern)
) {
return
} else {
throw GradleException("version regex must include groups named 'major', 'minor', 'patch', and 'none'.")
throw GradleException(VersionRegex.MISSING_GROUP_ERROR)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -77,7 +84,7 @@ interface CalculateVersionTestSpec {
it.url = projectDir.absolutePath
},
)
val version = runCalculateVersion()
val version = runCalculateVersionSuccessfully()

assertEquals("1.0.23-SNAPSHOT", version)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -180,7 +187,7 @@ interface CalculateVersionTestSpec {
initialTag = "1.2.3",
)

val version = runCalculateVersion()
val version = runCalculateVersionSuccessfully()

assertEquals("2.0.0", version)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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.")
}
}
}
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 7bb373c

Please sign in to comment.