Skip to content

Commit

Permalink
[patch] adding tag functionality to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
robertfmurdock committed Aug 26, 2024
1 parent ad22409 commit 8da26ff
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CalculateVersionCommandTest : CalculateVersionTestSpec {
arguments = emptyList()
}

override fun setupWithDefaults() {
override fun configureWithDefaults() {
arguments += "--release-branch=master"
arguments += projectDir.absolutePath
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> = emptySet()
private lateinit var arguments: List<String>

@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())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CalculateVersionFunctionalTest : CalculateVersionTestSpec {
ignoreFile.writeText(".gradle")
}

override fun setupWithDefaults() {
override fun configureWithDefaults() {
buildFile.writeText(
"""
plugins {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -36,14 +36,14 @@ class TagAndPushFunctionalTest : TagAndPushTestSpec {
)
}

override fun execute(): TestResult {
override fun execute(version: String): 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.withArguments("tag", "-Pversion=$version")
runner.withProjectDir(projectDir)
return try {
val result = runner.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface CalculateVersionTestSpec {
var projectDir: File
val addFileNames: Set<String>

fun setupWithDefaults()
fun configureWithDefaults()
fun configureWithOverrides(
implicitPatch: Boolean? = null,
majorRegex: String? = null,
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>

Expand All @@ -28,7 +29,7 @@ interface TagAndPushTestSpec {
)

fun configureWithDefaults()
fun execute(): TestResult
fun execute(version: String): TestResult

@Test
fun tagWillTagAndPushSuccessfully() {
Expand All @@ -48,9 +49,11 @@ interface TagAndPushTestSpec {
)
grgit.push()

execute()
val expectedVersion = "1.0.0"
val result = execute(expectedVersion)
assertIsNot<TestResult.Failure>(result, message = "$result")

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

0 comments on commit 8da26ff

Please sign in to comment.