From ad2240996d057782ab052ba67187cffea745e7dc Mon Sep 17 00:00:00 2001 From: RoB Murdock Date: Sun, 25 Aug 2024 21:31:00 -0400 Subject: [PATCH] [none] configuring tag+push feature of tagger into a test spec in anticipation of adding it to cli --- .../tagger/cli/CalculateVersionCommandTest.kt | 4 +- .../tagger/CalculateVersionFunctionalTest.kt | 53 +----------------- .../tools/tagger/TagAndPushFunctionalTest.kt | 55 ++++++++++++++++++ .../tools/tagger/TagAndPushTestSpec.kt | 56 +++++++++++++++++++ .../tools/tagger/CalculateVersionTestSpec.kt | 36 ++++++------ 5 files changed, 133 insertions(+), 71 deletions(-) create mode 100644 tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TagAndPushFunctionalTest.kt create mode 100644 tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TagAndPushTestSpec.kt 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 84ced2f..2e5ae6f 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 @@ -25,7 +25,7 @@ class CalculateVersionCommandTest : CalculateVersionTestSpec { arguments += projectDir.absolutePath } - override fun setupWithOverrides( + override fun configureWithOverrides( implicitPatch: Boolean?, majorRegex: String?, minorRegex: String?, @@ -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) { diff --git a/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/CalculateVersionFunctionalTest.kt b/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/CalculateVersionFunctionalTest.kt index ce68a8d..bd0da35 100644 --- a/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/CalculateVersionFunctionalTest.kt +++ b/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/CalculateVersionFunctionalTest.kt @@ -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 @@ -44,7 +34,7 @@ class CalculateVersionFunctionalTest : CalculateVersionTestSpec { ) } - override fun setupWithOverrides( + override fun configureWithOverrides( implicitPatch: Boolean?, majorRegex: String?, minorRegex: String?, @@ -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", "test@zegreatrob.com"), 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() @@ -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") - } - } } diff --git a/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TagAndPushFunctionalTest.kt b/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TagAndPushFunctionalTest.kt new file mode 100644 index 0000000..36343de --- /dev/null +++ b/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TagAndPushFunctionalTest.kt @@ -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 + 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", "test@zegreatrob.com"), 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!!) + } + } +} diff --git a/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TagAndPushTestSpec.kt b/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TagAndPushTestSpec.kt new file mode 100644 index 0000000..670c56c --- /dev/null +++ b/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TagAndPushTestSpec.kt @@ -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 + + fun initializeGitRepo( + commits: List, + 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")) + } +} 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 ac91bbc..ecbbb60 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 @@ -18,7 +18,7 @@ interface CalculateVersionTestSpec { val addFileNames: Set fun setupWithDefaults() - fun setupWithOverrides( + fun configureWithOverrides( implicitPatch: Boolean? = null, majorRegex: String? = null, minorRegex: String? = null, @@ -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}") } @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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"), @@ -177,7 +177,7 @@ interface CalculateVersionTestSpec { @Test fun unifiedGroupRegexCanReplaceMajorRegex() { - setupWithOverrides( + configureWithOverrides( implicitPatch = false, versionRegex = "(?.*big.*)?(?.*mid.*)?(?.*widdle.*)?(?.*no.*)?", ) @@ -194,7 +194,7 @@ interface CalculateVersionTestSpec { @Test fun unifiedGroupCanReplaceMinorRegex() { - setupWithOverrides( + configureWithOverrides( implicitPatch = false, versionRegex = "(?.*big.*)?(?.*mid.*)?(?.*widdle.*)?(?.*no.*)?", ) @@ -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"), @@ -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() @@ -233,7 +233,7 @@ interface CalculateVersionTestSpec { @Test fun unifiedGroupCanReplacePatchRegex() { - setupWithOverrides( + configureWithOverrides( implicitPatch = false, versionRegex = "(?.*big.*)?(?.*mid.*)?(?.*widdle.*)?(?.*no.*)?", ) @@ -246,7 +246,7 @@ interface CalculateVersionTestSpec { @Test fun canReplaceNoneRegex() { - setupWithOverrides( + configureWithOverrides( implicitPatch = true, noneRegex = ".*(no).*", ) @@ -259,7 +259,7 @@ interface CalculateVersionTestSpec { @Test fun unifiedGroupCanReplaceNoneRegex() { - setupWithOverrides( + configureWithOverrides( implicitPatch = true, versionRegex = "(?.*big.*)?(?.*mid.*)?(?.*widdle.*)?(?.*no.*)?", ) @@ -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'.",