From 8da26ff5106b32a30409b0a3457b4f309f038424 Mon Sep 17 00:00:00 2001 From: RoB Murdock Date: Mon, 26 Aug 2024 09:28:37 -0400 Subject: [PATCH] [patch] adding tag functionality to cli --- .../com/zegreatrob/tools/tagger/cli/Tag.kt | 19 +++++++++ .../tagger/cli/CalculateVersionCommandTest.kt | 2 +- .../tools/tagger/cli/TagCommandTest.kt | 41 +++++++++++++++++++ .../tagger/CalculateVersionFunctionalTest.kt | 2 +- ...FunctionalTest.kt => TagFunctionalTest.kt} | 6 +-- .../tools/tagger/CalculateVersionTestSpec.kt | 12 +++--- .../zegreatrob/tools/tagger/TagTestSpec.kt} | 11 +++-- 7 files changed, 78 insertions(+), 15 deletions(-) create mode 100644 tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/Tag.kt create mode 100644 tools/tagger-cli/src/jvmTest/kotlin/com/zegreatrob/tools/tagger/cli/TagCommandTest.kt rename tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/{TagAndPushFunctionalTest.kt => TagFunctionalTest.kt} (91%) rename tools/{tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TagAndPushTestSpec.kt => tagger-test/src/jvmMain/kotlin/com/zegreatrob/tools/tagger/TagTestSpec.kt} (83%) diff --git a/tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/Tag.kt b/tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/Tag.kt new file mode 100644 index 0000000..45d4ba0 --- /dev/null +++ b/tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/Tag.kt @@ -0,0 +1,19 @@ +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.option +import com.github.ajalt.clikt.parameters.options.required +import com.zegreatrob.tools.adapter.git.GitAdapter +import com.zegreatrob.tools.tagger.core.TaggerCore +import com.zegreatrob.tools.tagger.core.tag + +class Tag : CliktCommand() { + private val dir by argument("git-repo") + private val releaseBranch by option().required() + private val version: String by option().required() + override fun run() { + TaggerCore(GitAdapter(dir)) + .tag(version, releaseBranch) + } +} 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 2e5ae6f..06cac06 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 @@ -20,7 +20,7 @@ class CalculateVersionCommandTest : CalculateVersionTestSpec { arguments = emptyList() } - override fun setupWithDefaults() { + override fun configureWithDefaults() { arguments += "--release-branch=master" arguments += projectDir.absolutePath } diff --git a/tools/tagger-cli/src/jvmTest/kotlin/com/zegreatrob/tools/tagger/cli/TagCommandTest.kt b/tools/tagger-cli/src/jvmTest/kotlin/com/zegreatrob/tools/tagger/cli/TagCommandTest.kt new file mode 100644 index 0000000..f1256be --- /dev/null +++ b/tools/tagger-cli/src/jvmTest/kotlin/com/zegreatrob/tools/tagger/cli/TagCommandTest.kt @@ -0,0 +1,41 @@ +package com.zegreatrob.tools.tagger.cli + +import com.github.ajalt.clikt.testing.test +import com.zegreatrob.tools.tagger.TagTestSpec +import com.zegreatrob.tools.tagger.TestResult +import org.junit.jupiter.api.io.TempDir +import java.io.File +import kotlin.test.BeforeTest + +class TagCommandTest : TagTestSpec { + + @field:TempDir + override lateinit var projectDir: File + + override val addFileNames: Set = emptySet() + private lateinit var arguments: List + + @BeforeTest + fun setup() { + arguments = emptyList() + } + + override fun configureWithDefaults() { + arguments += "--release-branch=master" + arguments += projectDir.absolutePath + } + + override fun execute(version: String): TestResult { + arguments += "--version=$version" + val test = Tag() + .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-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/CalculateVersionFunctionalTest.kt b/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/CalculateVersionFunctionalTest.kt index bd0da35..ac447f8 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 @@ -21,7 +21,7 @@ class CalculateVersionFunctionalTest : CalculateVersionTestSpec { ignoreFile.writeText(".gradle") } - override fun setupWithDefaults() { + override fun configureWithDefaults() { buildFile.writeText( """ plugins { 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/TagFunctionalTest.kt similarity index 91% rename from tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TagAndPushFunctionalTest.kt rename to tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TagFunctionalTest.kt index 36343de..62897c5 100644 --- a/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TagAndPushFunctionalTest.kt +++ b/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TagFunctionalTest.kt @@ -6,7 +6,7 @@ import org.junit.jupiter.api.io.TempDir import java.io.File import kotlin.test.BeforeTest -class TagAndPushFunctionalTest : TagAndPushTestSpec { +class TagFunctionalTest : TagTestSpec { @field:TempDir override lateinit var projectDir: File @@ -36,14 +36,14 @@ class TagAndPushFunctionalTest : TagAndPushTestSpec { ) } - override fun execute(): TestResult { + override fun execute(version: String): 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.withArguments("tag", "-Pversion=$version") runner.withProjectDir(projectDir) return try { val result = runner.build() 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 ecbbb60..94d0c2a 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 @@ -17,7 +17,7 @@ interface CalculateVersionTestSpec { var projectDir: File val addFileNames: Set - fun setupWithDefaults() + fun configureWithDefaults() fun configureWithOverrides( implicitPatch: Boolean? = null, majorRegex: String? = null, @@ -48,7 +48,7 @@ interface CalculateVersionTestSpec { @Test fun `calculating version with no tags produces zero version`() { - setupWithDefaults() + configureWithDefaults() initializeGitRepo(listOf("init", "[patch] commit 1", "[patch] commit 2")) val version = runCalculateVersionSuccessfully() @@ -58,7 +58,7 @@ interface CalculateVersionTestSpec { @Test fun `calculating version when current commit already has tag will use tag`() { - setupWithDefaults() + configureWithDefaults() val grgit = Grgit.init(mapOf("dir" to projectDir.absolutePath)) disableGpgSign(projectDir.absolutePath) @@ -91,7 +91,7 @@ interface CalculateVersionTestSpec { @Test fun `calculating version with all patch commits only increments patch`() { - setupWithDefaults() + configureWithDefaults() initializeGitRepo(listOf("init", "[patch] commit 1", "[patch] commit 2"), initialTag = "1.2.3") val version = runCalculateVersionSuccessfully() @@ -151,7 +151,7 @@ interface CalculateVersionTestSpec { @Test fun `calculating version with one minor commits only increments minor`() { - setupWithDefaults() + configureWithDefaults() initializeGitRepo( commits = listOf("init", "[patch] commit 1", "[minor] commit 2", "[patch] commit 3"), @@ -272,7 +272,7 @@ interface CalculateVersionTestSpec { @Test fun `calculating version with one major commits only increments major`() { - setupWithDefaults() + configureWithDefaults() initializeGitRepo( commits = listOf("init", "[major] commit 1", "[minor] commit 2", "[patch] commit 3"), diff --git a/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TagAndPushTestSpec.kt b/tools/tagger-test/src/jvmMain/kotlin/com/zegreatrob/tools/tagger/TagTestSpec.kt similarity index 83% rename from tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TagAndPushTestSpec.kt rename to tools/tagger-test/src/jvmMain/kotlin/com/zegreatrob/tools/tagger/TagTestSpec.kt index 670c56c..209f4e0 100644 --- a/tools/tagger-plugin/src/functionalTest/kotlin/com/zegreatrob/tools/tagger/TagAndPushTestSpec.kt +++ b/tools/tagger-test/src/jvmMain/kotlin/com/zegreatrob/tools/tagger/TagTestSpec.kt @@ -10,8 +10,9 @@ import kotlin.io.path.absolutePathString import kotlin.io.path.createTempDirectory import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertIsNot -interface TagAndPushTestSpec { +interface TagTestSpec { var projectDir: File val addFileNames: Set @@ -28,7 +29,7 @@ interface TagAndPushTestSpec { ) fun configureWithDefaults() - fun execute(): TestResult + fun execute(version: String): TestResult @Test fun tagWillTagAndPushSuccessfully() { @@ -48,9 +49,11 @@ interface TagAndPushTestSpec { ) grgit.push() - execute() + val expectedVersion = "1.0.0" + val result = execute(expectedVersion) + assertIsNot(result, message = "$result") val gitAdapter = GitAdapter(this.projectDir.absolutePath) - assertEquals("1.0.0", gitAdapter.showTag("HEAD")) + assertEquals(expectedVersion, gitAdapter.showTag("HEAD")) } }