Skip to content

Commit

Permalink
[none] configuring tag+push feature of tagger into a test spec in ant…
Browse files Browse the repository at this point in the history
…icipation of adding it to cli
  • Loading branch information
robertfmurdock committed Aug 26, 2024
1 parent 7bb373c commit ad22409
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CalculateVersionCommandTest : CalculateVersionTestSpec {
arguments += projectDir.absolutePath
}

override fun setupWithOverrides(
override fun configureWithOverrides(
implicitPatch: Boolean?,
majorRegex: String?,
minorRegex: String?,
Expand All @@ -43,7 +43,7 @@ class CalculateVersionCommandTest : CalculateVersionTestSpec {
arguments += projectDir.absolutePath
}

override fun runCalculateVersion(): TestResult {
override fun execute(): TestResult {
val test = CalculateVersion()
.test(arguments)
return if (test.statusCode == 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
package com.zegreatrob.tools.tagger

import com.zegreatrob.tools.adapter.git.GitAdapter
import com.zegreatrob.tools.adapter.git.runProcess
import org.ajoberstar.grgit.Grgit
import org.ajoberstar.grgit.operation.CommitOp
import org.ajoberstar.grgit.operation.InitOp
import org.gradle.testkit.runner.GradleRunner
import org.junit.jupiter.api.io.TempDir
import java.io.File
import java.io.FileOutputStream
import kotlin.io.path.absolutePathString
import kotlin.io.path.createTempDirectory
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals

class CalculateVersionFunctionalTest : CalculateVersionTestSpec {
@field:TempDir
Expand Down Expand Up @@ -44,7 +34,7 @@ class CalculateVersionFunctionalTest : CalculateVersionTestSpec {
)
}

override fun setupWithOverrides(
override fun configureWithOverrides(
implicitPatch: Boolean?,
majorRegex: String?,
minorRegex: String?,
Expand Down Expand Up @@ -81,39 +71,7 @@ class CalculateVersionFunctionalTest : CalculateVersionTestSpec {
)
}

@Test
fun tagWillTagAndPushSuccessfully() {
setupWithDefaults()

val originDirectory = createTempDirectory()
val originGrgit = Grgit.init(fun InitOp.() {
this.dir = originDirectory.absolutePathString()
})
disableGpgSign(originDirectory.toFile())
originGrgit.commit(fun CommitOp.() {
this.message = "init"
})
val grgit = initializeGitRepo(
listOf("init", "[patch] commit 1", "[patch] commit 2"),
remoteUrl = originDirectory.absolutePathString(),
)
grgit.push()

runProcess(listOf("git", "config", "user.email", "[email protected]"), this.projectDir.absolutePath)
runProcess(listOf("git", "config", "user.name", "RoB as Test"), this.projectDir.absolutePath)

val runner = GradleRunner.create()
runner.forwardOutput()
runner.withPluginClasspath()
runner.withArguments("tag", "-Pversion=1.0.0")
runner.withProjectDir(projectDir)
runner.build()

val gitAdapter = GitAdapter(this.projectDir.absolutePath)
assertEquals("1.0.0", gitAdapter.showTag("HEAD"))
}

override fun runCalculateVersion(): TestResult {
override fun execute(): TestResult {
val runner = GradleRunner.create()
runner.forwardOutput()
runner.withPluginClasspath()
Expand All @@ -126,11 +84,4 @@ class CalculateVersionFunctionalTest : CalculateVersionTestSpec {
TestResult.Failure(e.message!!)
}
}

private fun disableGpgSign(directory: File) {
FileOutputStream(directory.resolve(".git/config"), true)
.writer().use {
it.write("[commit]\n gpgsign = false")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.zegreatrob.tools.tagger

import com.zegreatrob.tools.adapter.git.runProcess
import org.gradle.testkit.runner.GradleRunner
import org.junit.jupiter.api.io.TempDir
import java.io.File
import kotlin.test.BeforeTest

class TagAndPushFunctionalTest : TagAndPushTestSpec {

@field:TempDir
override lateinit var projectDir: File

private val buildFile by lazy { projectDir.resolve("build.gradle.kts") }
private val settingsFile by lazy { projectDir.resolve("settings.gradle") }
private val ignoreFile by lazy { projectDir.resolve(".gitignore") }
override val addFileNames: Set<String>
get() = setOf(buildFile, settingsFile, ignoreFile).map { it.name }.toSet()

@BeforeTest
fun setup() {
settingsFile.writeText("")
ignoreFile.writeText(".gradle")
}

override fun configureWithDefaults() {
buildFile.writeText(
"""
plugins {
id("com.zegreatrob.tools.tagger")
}
tagger {
releaseBranch = "master"
}
""".trimIndent(),
)
}

override fun execute(): TestResult {
runProcess(listOf("git", "config", "user.email", "[email protected]"), this.projectDir.absolutePath)
runProcess(listOf("git", "config", "user.name", "RoB as Test"), this.projectDir.absolutePath)

val runner = GradleRunner.create()
runner.forwardOutput()
runner.withPluginClasspath()
runner.withArguments("tag", "-Pversion=1.0.0")
runner.withProjectDir(projectDir)
return try {
val result = runner.build()
result.output.trim().let(TestResult::Success)
} catch (e: Exception) {
TestResult.Failure(e.message!!)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.zegreatrob.tools.tagger

import com.zegreatrob.tools.adapter.git.GitAdapter
import com.zegreatrob.tools.test.git.disableGpgSign
import org.ajoberstar.grgit.Grgit
import org.ajoberstar.grgit.operation.CommitOp
import org.ajoberstar.grgit.operation.InitOp
import java.io.File
import kotlin.io.path.absolutePathString
import kotlin.io.path.createTempDirectory
import kotlin.test.Test
import kotlin.test.assertEquals

interface TagAndPushTestSpec {
var projectDir: File
val addFileNames: Set<String>

fun initializeGitRepo(
commits: List<String>,
initialTag: String? = null,
remoteUrl: String = projectDir.absolutePath,
) = com.zegreatrob.tools.test.git.initializeGitRepo(
directory = projectDir.absolutePath,
remoteUrl = remoteUrl,
addFileNames = addFileNames,
initialTag = initialTag,
commits = commits,
)

fun configureWithDefaults()
fun execute(): TestResult

@Test
fun tagWillTagAndPushSuccessfully() {
configureWithDefaults()

val originDirectory = createTempDirectory()
val originGrgit = Grgit.init(fun InitOp.() {
this.dir = originDirectory.absolutePathString()
})
disableGpgSign(originDirectory.absolutePathString())
originGrgit.commit(fun CommitOp.() {
this.message = "init"
})
val grgit = initializeGitRepo(
listOf("init", "[patch] commit 1", "[patch] commit 2"),
remoteUrl = originDirectory.absolutePathString(),
)
grgit.push()

execute()

val gitAdapter = GitAdapter(this.projectDir.absolutePath)
assertEquals("1.0.0", gitAdapter.showTag("HEAD"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface CalculateVersionTestSpec {
val addFileNames: Set<String>

fun setupWithDefaults()
fun setupWithOverrides(
fun configureWithOverrides(
implicitPatch: Boolean? = null,
majorRegex: String? = null,
minorRegex: String? = null,
Expand All @@ -39,9 +39,9 @@ interface CalculateVersionTestSpec {
commits = commits,
)

fun runCalculateVersion(): TestResult
fun execute(): TestResult
fun runCalculateVersionSuccessfully(): String =
when (val result = runCalculateVersion()) {
when (val result = execute()) {
is TestResult.Success -> result.message
is TestResult.Failure -> fail("Expected success but got ${result.reason}")
}
Expand Down Expand Up @@ -101,7 +101,7 @@ interface CalculateVersionTestSpec {

@Test
fun `given no implicit patch, calculating version with unlabeled commits does not increment`() {
setupWithOverrides(implicitPatch = false)
configureWithOverrides(implicitPatch = false)

initializeGitRepo(commits = listOf("init", "commit 1", "commit 2"), initialTag = "1.2.3")
val version = runCalculateVersionSuccessfully()
Expand All @@ -111,7 +111,7 @@ interface CalculateVersionTestSpec {

@Test
fun `given initial tag with suffix, ignore suffix and follow normal rules`() {
setupWithOverrides(implicitPatch = false)
configureWithOverrides(implicitPatch = false)

initializeGitRepo(commits = listOf("init", "commit 1", "commit 2"), initialTag = "1.2.3-SNAPSHOT")
val version = runCalculateVersionSuccessfully()
Expand All @@ -121,7 +121,7 @@ interface CalculateVersionTestSpec {

@Test
fun `given implicit patch, calculating version with unlabeled commits increments patch`() {
setupWithOverrides(implicitPatch = true)
configureWithOverrides(implicitPatch = true)

initializeGitRepo(commits = listOf("init", "commit 1", "commit 2"), initialTag = "1.2.3")
val version = runCalculateVersionSuccessfully()
Expand All @@ -131,7 +131,7 @@ interface CalculateVersionTestSpec {

@Test
fun `given implicit patch, calculating version with none and then unlabeled commits increments patch`() {
setupWithOverrides(implicitPatch = true)
configureWithOverrides(implicitPatch = true)

initializeGitRepo(commits = listOf("init", "[none] commit 1", "commit 2"), initialTag = "1.2.3")
val version = runCalculateVersionSuccessfully()
Expand All @@ -141,7 +141,7 @@ interface CalculateVersionTestSpec {

@Test
fun `given implicit patch, calculating version with None commits does not increment and is always snapshot`() {
setupWithOverrides(implicitPatch = true)
configureWithOverrides(implicitPatch = true)

initializeGitRepo(commits = listOf("init", "[None] commit 1", "[none] commit 2"), initialTag = "1.2.3")
val version = runCalculateVersionSuccessfully()
Expand All @@ -164,7 +164,7 @@ interface CalculateVersionTestSpec {

@Test
fun canReplaceMajorRegex() {
setupWithOverrides(implicitPatch = false, majorRegex = ".*(big).*")
configureWithOverrides(implicitPatch = false, majorRegex = ".*(big).*")

initializeGitRepo(
commits = listOf("init", "[patch] commit 1", "commit (big) 2", "[patch] commit 3"),
Expand All @@ -177,7 +177,7 @@ interface CalculateVersionTestSpec {

@Test
fun unifiedGroupRegexCanReplaceMajorRegex() {
setupWithOverrides(
configureWithOverrides(
implicitPatch = false,
versionRegex = "(?<major>.*big.*)?(?<minor>.*mid.*)?(?<patch>.*widdle.*)?(?<none>.*no.*)?",
)
Expand All @@ -194,7 +194,7 @@ interface CalculateVersionTestSpec {

@Test
fun unifiedGroupCanReplaceMinorRegex() {
setupWithOverrides(
configureWithOverrides(
implicitPatch = false,
versionRegex = "(?<major>.*big.*)?(?<minor>.*mid.*)?(?<patch>.*widdle.*)?(?<none>.*no.*)?",
)
Expand All @@ -210,7 +210,7 @@ interface CalculateVersionTestSpec {

@Test
fun canReplaceMinorRegex() {
setupWithOverrides(implicitPatch = false, minorRegex = ".*(middle).*")
configureWithOverrides(implicitPatch = false, minorRegex = ".*(middle).*")

initializeGitRepo(
commits = listOf("init", "[patch] commit 1", "commit (middle) 2", "[patch] commit 3"),
Expand All @@ -223,7 +223,7 @@ interface CalculateVersionTestSpec {

@Test
fun canReplacePatchRegex() {
setupWithOverrides(implicitPatch = false, patchRegex = ".*(tiny).*")
configureWithOverrides(implicitPatch = false, patchRegex = ".*(tiny).*")

initializeGitRepo(commits = listOf("init", "commit 1", "commit (tiny) 2", "commit 3"), initialTag = "1.2.3")
val version = runCalculateVersionSuccessfully()
Expand All @@ -233,7 +233,7 @@ interface CalculateVersionTestSpec {

@Test
fun unifiedGroupCanReplacePatchRegex() {
setupWithOverrides(
configureWithOverrides(
implicitPatch = false,
versionRegex = "(?<major>.*big.*)?(?<minor>.*mid.*)?(?<patch>.*widdle.*)?(?<none>.*no.*)?",
)
Expand All @@ -246,7 +246,7 @@ interface CalculateVersionTestSpec {

@Test
fun canReplaceNoneRegex() {
setupWithOverrides(
configureWithOverrides(
implicitPatch = true,
noneRegex = ".*(no).*",
)
Expand All @@ -259,7 +259,7 @@ interface CalculateVersionTestSpec {

@Test
fun unifiedGroupCanReplaceNoneRegex() {
setupWithOverrides(
configureWithOverrides(
implicitPatch = true,
versionRegex = "(?<major>.*big.*)?(?<minor>.*mid.*)?(?<patch>.*widdle.*)?(?<none>.*no.*)?",
)
Expand All @@ -284,10 +284,10 @@ interface CalculateVersionTestSpec {

@Test
fun unifiedGroupWillReportErrorsWhenMissingGroupsWithCorrectNames() {
setupWithOverrides(implicitPatch = true, versionRegex = ".*")
configureWithOverrides(implicitPatch = true, versionRegex = ".*")

initializeGitRepo(listOf("init", "commit (no) 1"), "1.2.3")
when (val result = runCalculateVersion()) {
when (val result = execute()) {
is TestResult.Failure -> assertContains(
result.reason,
"version regex must include groups named 'major', 'minor', 'patch', and 'none'.",
Expand Down

0 comments on commit ad22409

Please sign in to comment.