diff --git a/CHANGELOG.md b/CHANGELOG.md index 026b53a..d589860 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Branch specific snapshot suffixes. - Add task to create a properties file with all version information. +## [2.3.3] +### Fixed +- Fix dirty working tree in the conventional commits incrementer. + ## [2.3.2] ### Fixed - Fix conventional commits incrementer for BREAKING CHANGES marked by a ! after the scope. diff --git a/build.gradle.kts b/build.gradle.kts index bd38145..99c738a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,7 +2,7 @@ plugins { `maven-publish` `java-gradle-plugin` `jacoco` - kotlin("jvm") version "1.3.21" + kotlin("jvm") version "1.4.31" id("com.gradle.plugin-publish") version "0.11.0" } @@ -10,12 +10,12 @@ group = "io.wusa" version = "3.0.0" dependencies { - implementation(kotlin("stdlib-jdk8")) + implementation(kotlin("stdlib-jdk8:1.4.31")) implementation("org.koin:koin-gradle-plugin:2.1.6") - testImplementation("org.junit.jupiter:junit-jupiter:5.4.0") - testImplementation("org.junit.jupiter:junit-jupiter-api:5.4.0") - testImplementation("io.mockk:mockk:1.9") - testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.4.0") + testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.1") + testImplementation(kotlin("test-junit5")) + testImplementation("io.mockk:mockk:1.10.6") + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.1") } repositories { diff --git a/src/main/kotlin/io/wusa/GitService.kt b/src/main/kotlin/io/wusa/GitService.kt index d94e10c..d663d39 100644 --- a/src/main/kotlin/io/wusa/GitService.kt +++ b/src/main/kotlin/io/wusa/GitService.kt @@ -39,9 +39,9 @@ class GitService(private val gitCommandRunner: GitCommandRunner) { @Throws(NoLastTagFoundException::class) fun lastTag(tagPrefix: String = "", tagType: TagType = TagType.ANNOTATED): String { - var cmdArgs = arrayOf("describe", "--dirty", "--abbrev=7", "--match", "$tagPrefix*") + var cmdArgs = arrayOf("describe", "--abbrev=7", "--match", "$tagPrefix*") if (tagType == TagType.LIGHTWEIGHT) { - cmdArgs = arrayOf("describe", "--tags", "--dirty", "--abbrev=7", "--match", "$tagPrefix*") + cmdArgs = arrayOf("describe", "--tags", "--abbrev=7", "--match", "$tagPrefix*") } return try { gitCommandRunner.execute(cmdArgs) @@ -67,9 +67,9 @@ class GitService(private val gitCommandRunner: GitCommandRunner) { } fun getCommitsSinceLastTag(tagPrefix: String = "", tagType: TagType = TagType.ANNOTATED): List { - var cmdArgs = arrayOf("describe", "--dirty", "--abbrev=0", "--match", "$tagPrefix*") + var cmdArgs = arrayOf("describe", "--abbrev=0", "--match", "$tagPrefix*") if (tagType == TagType.LIGHTWEIGHT) { - cmdArgs = arrayOf("describe", "--tags", "--dirty", "--abbrev=0", "--match", "$tagPrefix*") + cmdArgs = arrayOf("describe", "--tags", "--abbrev=0", "--match", "$tagPrefix*") } return try { val lastTag = gitCommandRunner.execute(cmdArgs) @@ -79,8 +79,10 @@ class GitService(private val gitCommandRunner: GitCommandRunner) { } } - private fun isGitDifferent() = - gitCommandRunner.execute(arrayOf("diff", "--stat")) != "" + + private fun isGitDifferent(): Boolean { + return gitCommandRunner.execute(arrayOf("status", "-s")).isNotBlank() + } private fun getCurrentCommit(): String { return try { diff --git a/src/main/kotlin/io/wusa/Info.kt b/src/main/kotlin/io/wusa/Info.kt index 8a23d66..8e44cd8 100644 --- a/src/main/kotlin/io/wusa/Info.kt +++ b/src/main/kotlin/io/wusa/Info.kt @@ -60,6 +60,6 @@ data class Info(var semverGitPluginExtension: SemverGitPluginExtension) { get() = versionService.getVersion() override fun toString(): String { - return SemanticVersionFormatter.format(this, semverGitPluginExtension.branches, semverGitPluginExtension.snapshotSuffix) + return SemanticVersionFormatter.format(this, semverGitPluginExtension.branches, semverGitPluginExtension.dirtyMarker) } } diff --git a/src/main/kotlin/io/wusa/VersionService.kt b/src/main/kotlin/io/wusa/VersionService.kt index ef7cdfa..a433c83 100644 --- a/src/main/kotlin/io/wusa/VersionService.kt +++ b/src/main/kotlin/io/wusa/VersionService.kt @@ -3,8 +3,6 @@ package io.wusa import io.wusa.exception.* import io.wusa.extension.SemverGitPluginExtension import org.gradle.api.GradleException -import org.gradle.api.Project -import org.koin.java.KoinJavaComponent.inject class VersionService(private val semverGitPluginExtension: SemverGitPluginExtension, private val gitService: GitService) { @@ -18,6 +16,22 @@ class VersionService(private val semverGitPluginExtension: SemverGitPluginExtens throw GradleException("The current tag is not a semantic version.") } catch (ex: NoCurrentTagFoundException) { handleNoCurrentTagFound(versionFactory) + } catch (ex: DirtyWorkingTreeException) { + handleDirtyWorkingTree(versionFactory) + } + } + + @Throws(GradleException::class) + private fun handleDirtyWorkingTree(versionFactory: IVersionFactory): Version { + return try { + val lastVersion = getLastVersion(versionFactory) + incrementVersion(lastVersion) + } catch (ex: NoValidSemverTagFoundException) { + throw GradleException(ex.localizedMessage) + } catch (ex: NoIncrementerFoundException) { + throw GradleException(ex.localizedMessage) + } catch (ex: NoLastTagFoundException) { + buildInitialVersion(versionFactory) } } @@ -70,6 +84,10 @@ class VersionService(private val semverGitPluginExtension: SemverGitPluginExtens if (!curTag.startsWith(tagPrefix)) { throw NoCurrentTagFoundException("$curTag doesn't match $tagPrefix") } + val isDirty = gitService.isDirty() + if (isDirty) { + throw DirtyWorkingTreeException("The current working tree is dirty.") + } return versionFactory.createFromString(curTag.substring(tagPrefix.length)) } diff --git a/src/main/kotlin/io/wusa/exception/DirtyWorkingTreeException.kt b/src/main/kotlin/io/wusa/exception/DirtyWorkingTreeException.kt new file mode 100644 index 0000000..c798a2b --- /dev/null +++ b/src/main/kotlin/io/wusa/exception/DirtyWorkingTreeException.kt @@ -0,0 +1,7 @@ +package io.wusa.exception + +class DirtyWorkingTreeException: Exception { + constructor(message: String, ex: Throwable?): super(message, ex) {} + constructor(message: String): super(message) {} + constructor(ex: Throwable): super(ex) {} +} \ No newline at end of file diff --git a/src/main/kotlin/io/wusa/formatter/SemanticVersionFormatter.kt b/src/main/kotlin/io/wusa/formatter/SemanticVersionFormatter.kt index 153616c..517dfce 100644 --- a/src/main/kotlin/io/wusa/formatter/SemanticVersionFormatter.kt +++ b/src/main/kotlin/io/wusa/formatter/SemanticVersionFormatter.kt @@ -2,7 +2,6 @@ package io.wusa.formatter import io.wusa.Info import io.wusa.RegexResolver -import io.wusa.Suffix import io.wusa.extension.Branches import io.wusa.extension.SemverGitPluginExtension import org.gradle.api.Transformer @@ -12,12 +11,12 @@ class SemanticVersionFormatter { fun format(info: Info, branches: Branches, dirtyMarker: String): String { if (!hasFirstCommit(info)) return appendSuffix(buildVersionString(info), branches, info) - if (hasTag(info)) { + if (hasTag(info) && !isDirty(info)) { return formatVersionWithTag(info) } - val formattedVersion = formatVersionWithoutTag(branches, info, dirtyMarker) - if (!hasTag(info)) { + val formattedVersion = formatVersion(branches, info, dirtyMarker) + if (!hasTag(info) || hasTag(info) && isDirty(info)) { return appendSuffix(formattedVersion, branches, info) } return formattedVersion @@ -48,19 +47,25 @@ class SemanticVersionFormatter { private fun buildVersionString(info: Info) = "${info.version.major}.${info.version.minor}.${info.version.patch}" - private fun formatVersionWithoutTag(branches: Branches, info: Info, dirtyMarker: String): String { + private fun formatVersion(branches: Branches, info: Info, dirtyMarker: String): String { val regexFormatterPair = RegexResolver.findMatchingRegex(branches, info.branch.name) var formattedVersion = transform(SemverGitPluginExtension.DEFAULT_FORMATTER, info) - formattedVersion = appendDirtyMarker(formattedVersion, info.version.suffix, dirtyMarker) + if (isDirty(info)) { + formattedVersion = appendDirtyMarker(formattedVersion, dirtyMarker) + } regexFormatterPair?.let { formattedVersion = transform(regexFormatterPair.formatter, info) - formattedVersion = appendDirtyMarker(formattedVersion, info.version.suffix, dirtyMarker) + if (isDirty(info)) { + formattedVersion = appendDirtyMarker(formattedVersion, dirtyMarker) + } } return formattedVersion } private fun hasTag(info: Info) = info.version.suffix == null + private fun isDirty(info: Info) = info.dirty + private fun hasVersionBuildInformation(info: Info) = info.version.build != "" private fun hasVersionPrerelease(info: Info) = info.version.prerelease != "" @@ -83,12 +88,9 @@ class SemanticVersionFormatter { return true } - private fun appendDirtyMarker(version: String, suffix: Suffix?, dirtyMarker: String): String { - if (suffix != null && suffix.dirty) { - if (dirtyMarker != "") { - return "$version-$dirtyMarker" - } - return version + private fun appendDirtyMarker(version: String, dirtyMarker: String): String { + if (dirtyMarker != "") { + return "$version-$dirtyMarker" } return version } diff --git a/src/main/kotlin/io/wusa/incrementer/ConventionalCommitsVersionIncrementer.kt b/src/main/kotlin/io/wusa/incrementer/ConventionalCommitsVersionIncrementer.kt index 11090d8..0a965e3 100644 --- a/src/main/kotlin/io/wusa/incrementer/ConventionalCommitsVersionIncrementer.kt +++ b/src/main/kotlin/io/wusa/incrementer/ConventionalCommitsVersionIncrementer.kt @@ -21,6 +21,8 @@ object ConventionalCommitsVersionIncrementer : Transformer { val fix = "^fix$optionalScope" val breakingChange = "\\bBREAKING CHANGE\\b:" + if (semverGitPluginExtension.info.dirty) patch = 1 + listOfCommits.forEach { when { it.contains("$feat!:".toRegex()) -> major += 1 diff --git a/src/test/kotlin/io/wusa/ConventionalCommitsVersionIncrementerTest.kt b/src/test/kotlin/io/wusa/ConventionalCommitsVersionIncrementerTest.kt index 21434d9..35b1797 100644 --- a/src/test/kotlin/io/wusa/ConventionalCommitsVersionIncrementerTest.kt +++ b/src/test/kotlin/io/wusa/ConventionalCommitsVersionIncrementerTest.kt @@ -30,6 +30,9 @@ class ConventionalCommitsVersionIncrementerTest { semverGitPluginExtension = mockkClass(SemverGitPluginExtension::class) every { semverGitPluginExtension.tagPrefix } returns "" every { semverGitPluginExtension.tagType } returns TagType.ANNOTATED + val info = Info(semverGitPluginExtension) + every { semverGitPluginExtension.info } returns info + every { gitService.isDirty() } returns false startKoin { modules(modules) } diff --git a/src/test/kotlin/io/wusa/FunctionalBaseTest.kt b/src/test/kotlin/io/wusa/FunctionalBaseTest.kt index cf9ea57..f156ac5 100644 --- a/src/test/kotlin/io/wusa/FunctionalBaseTest.kt +++ b/src/test/kotlin/io/wusa/FunctionalBaseTest.kt @@ -1,35 +1,40 @@ package io.wusa import org.gradle.internal.impldep.org.eclipse.jgit.api.Git +import org.gradle.internal.impldep.org.eclipse.jgit.lib.Repository import java.io.File abstract class FunctionalBaseTest { - fun initializeGitWithBranch(directory: File, tag: String = "0.1.0", branch: String = "develop"): Git { - val git = Git.init().setDirectory(directory).call() - val commit = git.commit().setMessage("").call() - git.checkout().setCreateBranch(true).setName(branch).call() - git.tag().setName(tag).setObjectId(commit).call() - return git + fun initializeGitWithBranch(repository: Repository, directory: File, tag: String = "0.1.0", branch: String = "develop") { + val gitIgnore = File(directory, ".gitignore") + gitIgnore.writeText(".gradle") + Git(repository).add().addFilepattern(".").call() + val commit = Git(repository).commit().setMessage("").call() + Git(repository).checkout().setCreateBranch(true).setName(branch).call() + Git(repository).tag().setName(tag).setObjectId(commit).call() } - fun initializeGitWithoutBranchAnnotated(directory: File, tag: String = "0.1.0"): Git { - val git = Git.init().setDirectory(directory).call() - val commit = git.commit().setMessage("").call() - git.tag().setName(tag).setMessage(tag).setAnnotated(true).setObjectId(commit).call() - return git + fun initializeGitWithoutBranchAnnotated(repository: Repository, directory: File, tag: String = "0.1.0") { + val gitIgnore = File(directory, ".gitignore") + gitIgnore.writeText(".gradle") + Git(repository).add().addFilepattern(".").call() + val commit = Git(repository).commit().setMessage("").call() + Git(repository).tag().setName(tag).setMessage(tag).setAnnotated(true).setObjectId(commit).call() } - fun initializeGitWithoutBranchLightweight(directory: File, tag: String = "0.1.0"): Git { - val git = Git.init().setDirectory(directory).call() - val commit = git.commit().setMessage("").call() - git.tag().setName(tag).setObjectId(commit).setAnnotated(false).call() - return git + fun initializeGitWithoutBranchLightweight(repository: Repository, directory: File, tag: String = "0.1.0") { + val gitIgnore = File(directory, ".gitignore") + gitIgnore.writeText(".gradle") + Git(repository).add().addFilepattern(".").call() + val commit = Git(repository).commit().setMessage("").call() + Git(repository).tag().setName(tag).setObjectId(commit).setAnnotated(false).call() } - fun initializeGitWithoutBranchAndWithoutTag(directory: File): Git { - val git = Git.init().setDirectory(directory).call() - git.commit().setMessage("").call() - return git + fun initializeGitWithoutBranchAndWithoutTag(repository: Repository, directory: File) { + val gitIgnore = File(directory, ".gitignore") + gitIgnore.writeText(".gradle") + Git(repository).add().addFilepattern(".").call() + Git(repository).commit().setMessage("").call() } } diff --git a/src/test/kotlin/io/wusa/InfoTest.kt b/src/test/kotlin/io/wusa/InfoTest.kt index bb1b168..2253eb3 100644 --- a/src/test/kotlin/io/wusa/InfoTest.kt +++ b/src/test/kotlin/io/wusa/InfoTest.kt @@ -110,6 +110,7 @@ class InfoTest { fun `get version`() { val info = Info(semverGitPluginExtension) every { gitService.currentTag() } returns "0.1.0" + every { gitService.isDirty() } returns false Assertions.assertEquals("Version(major=0, minor=1, patch=0, prerelease=, build=, suffix=null)", info.version.toString()) } @@ -118,6 +119,7 @@ class InfoTest { semverGitPluginExtension.tagType = TagType.LIGHTWEIGHT val info = Info(semverGitPluginExtension) every { gitService.currentTag(any(), tagType = TagType.LIGHTWEIGHT) } returns "0.1.0" + every { gitService.isDirty() } returns false Assertions.assertEquals("Version(major=0, minor=1, patch=0, prerelease=, build=, suffix=null)", info.version.toString()) } @@ -126,6 +128,7 @@ class InfoTest { semverGitPluginExtension.tagPrefix = "prj_" val info = Info(semverGitPluginExtension) every { gitService.currentTag(any(), any()) } returns "prj_0.1.0" + every { gitService.isDirty() } returns false Assertions.assertEquals("Version(major=0, minor=1, patch=0, prerelease=, build=, suffix=null)", info.version.toString()) } @@ -143,6 +146,7 @@ class InfoTest { fun `current version has not tag with tag prefix`() { val info = Info(semverGitPluginExtension) every { gitService.currentTag() } returns "prj_0.1.0" + every { gitService.isDirty() } returns false Assertions.assertThrows(NoValidSemverTagFoundException::class.java ) { info.version } diff --git a/src/test/kotlin/io/wusa/SemverGitPluginGroovyFunctionalTest.kt b/src/test/kotlin/io/wusa/SemverGitPluginGroovyFunctionalTest.kt index 08be585..a91c3bc 100644 --- a/src/test/kotlin/io/wusa/SemverGitPluginGroovyFunctionalTest.kt +++ b/src/test/kotlin/io/wusa/SemverGitPluginGroovyFunctionalTest.kt @@ -1,6 +1,7 @@ package io.wusa import org.gradle.internal.impldep.org.eclipse.jgit.api.Git +import org.gradle.internal.impldep.org.eclipse.jgit.lib.Repository import org.gradle.testkit.runner.GradleRunner import org.gradle.testkit.runner.UnexpectedBuildFailure import org.junit.jupiter.api.* @@ -8,33 +9,40 @@ import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Assertions.assertFalse import java.io.File -@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@TestInstance(TestInstance.Lifecycle.PER_METHOD) class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { private lateinit var gradleRunner: GradleRunner + private lateinit var testProjectDirectory: File + private lateinit var git: Git + private lateinit var repository: Repository - @BeforeAll + @BeforeEach fun setUp() { + testProjectDirectory = createTempDir() gradleRunner = GradleRunner.create() + gradleRunner.withProjectDir(testProjectDirectory) + git = Git.init().setDirectory(testProjectDirectory).call() + repository = Git.open(testProjectDirectory).repository } - @AfterAll + @AfterEach fun tearDown() { - gradleRunner.projectDir.deleteRecursively() + testProjectDirectory.deleteOnExit() } @Test fun defaults() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" plugins { id 'io.wusa.semver-git-plugin' } """) - initializeGitWithoutBranchAnnotated(testProjectDirectory) + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory) val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -44,7 +52,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `custom version incrementer`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -74,10 +82,10 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/test") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/test") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -87,7 +95,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `version formatter for all branches`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -108,9 +116,9 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - initializeGitWithoutBranchAnnotated(testProjectDirectory) + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory) val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -120,7 +128,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `version formatter for feature branches use specific`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -147,10 +155,10 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/test") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/test") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -160,7 +168,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `version formatter for feature branches with camelCase`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -187,10 +195,10 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/testAbc10") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/testAbc10") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -200,7 +208,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `version formatter for feature branches with kebab-case`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -227,10 +235,10 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/test-abc-10") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/test-abc-10") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -240,7 +248,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `version formatter for feature branches with PascalCase`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -267,10 +275,10 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/TestAbc10") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/TestAbc10") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -280,7 +288,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `version formatter for feature branches with snake_case`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -307,10 +315,10 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/test_abc_10") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/test_abc_10") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -320,7 +328,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `version formatter for feature branches with UPPER_CASE`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -347,10 +355,10 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/TEST_ABC_10") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/TEST_ABC_10") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -360,17 +368,17 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `no existing tag`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" plugins { id 'io.wusa.semver-git-plugin' } """) - val git = Git.init().setDirectory(testProjectDirectory).call() - git.commit().setMessage("").call() + + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -381,7 +389,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `no existing tag with custom initial version`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" plugins { @@ -392,10 +400,10 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { initialVersion = '1.0.0' } """) - val git = Git.init().setDirectory(testProjectDirectory).call() - git.commit().setMessage("").call() + + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -406,7 +414,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `no existing tag with configuration without commits`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyNoVersionIncrementer @@ -427,9 +435,8 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - Git.init().setDirectory(testProjectDirectory).call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -439,7 +446,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `no existing tag with configuration`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyNoVersionIncrementer @@ -460,10 +467,10 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - val git = Git.init().setDirectory(testProjectDirectory).call() - git.commit().setMessage("").call() + + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -474,7 +481,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `patch release with custom configuration`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -495,9 +502,9 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - initializeGitWithoutBranchAnnotated(testProjectDirectory, "0.0.1") + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory, "0.0.1") val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -507,7 +514,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `minor release with custom configuration`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -528,9 +535,9 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - initializeGitWithoutBranchAnnotated(testProjectDirectory) + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory) val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -540,7 +547,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `major release with custom configuration`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -561,9 +568,9 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - initializeGitWithoutBranchAnnotated(testProjectDirectory, "1.0.0") + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory, "1.0.0") val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -573,7 +580,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `release alpha with custom configuration`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -594,9 +601,9 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - initializeGitWithoutBranchAnnotated(testProjectDirectory, "0.1.0-alpha") + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory, "0.1.0-alpha") val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -606,7 +613,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `release alpha beta with custom configuration`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -627,9 +634,9 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - initializeGitWithoutBranchAnnotated(testProjectDirectory, "0.1.0-alpha.beta") + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory, "0.1.0-alpha.beta") val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -639,7 +646,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `release alpha 1 with custom configuration`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -660,9 +667,9 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - initializeGitWithoutBranchAnnotated(testProjectDirectory, "0.1.0-alpha.1") + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory, "0.1.0-alpha.1") val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -672,7 +679,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `release beta with custom configuration`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -693,9 +700,9 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - initializeGitWithoutBranchAnnotated(testProjectDirectory, "0.1.0-beta") + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory, "0.1.0-beta") val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -705,7 +712,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `release rc with custom configuration`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -726,9 +733,9 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - initializeGitWithoutBranchAnnotated(testProjectDirectory, "0.1.0-rc") + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory, "0.1.0-rc") val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -738,7 +745,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `bump patch version`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyPatchVersionIncrementer @@ -759,10 +766,10 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithoutBranchAnnotated(testProjectDirectory) - git.commit().setMessage("").call() + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory) + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -772,7 +779,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `bump minor version`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -793,10 +800,10 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithoutBranchAnnotated(testProjectDirectory) - git.commit().setMessage("").call() + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory) + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -806,7 +813,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `bump major version`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMajorVersionIncrementer @@ -827,10 +834,10 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithoutBranchAnnotated(testProjectDirectory) - git.commit().setMessage("").call() + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory) + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -841,7 +848,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `don't bump version`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyNoVersionIncrementer @@ -862,10 +869,10 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithoutBranchAnnotated(testProjectDirectory) - git.commit().setMessage("").call() + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory) + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -875,19 +882,19 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `non-semver tag`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" plugins { id 'io.wusa.semver-git-plugin' } """) - val git = initializeGitWithoutBranchAnnotated(testProjectDirectory) - val commit = git.commit().setMessage("").call() - git.tag().setName("test-tag").setObjectId(commit).call() + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory) + val commit = Git(repository).commit().setMessage("").call() + Git(repository).tag().setName("test-tag").setObjectId(commit).call() Assertions.assertThrows(UnexpectedBuildFailure::class.java) { gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -896,17 +903,17 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `full info of master branch with one commit after the tag`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" plugins { id 'io.wusa.semver-git-plugin' } """) - val git = initializeGitWithoutBranchAnnotated(testProjectDirectory) - val commit = git.commit().setMessage("").call() + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory) + val commit = Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showInfo") .withPluginClasspath() .build() @@ -928,17 +935,17 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `full info of feature-test branch with one commit after the tag`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" plugins { id 'io.wusa.semver-git-plugin' } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/test") - val commit = git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/test") + val commit = Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showInfo") .withPluginClasspath() .build() @@ -960,17 +967,17 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `full info of feature-test branch with no commit after the tag`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" plugins { id 'io.wusa.semver-git-plugin' } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/test") - val head = git.repository.allRefs["HEAD"] + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/test") + val head = Git(repository).repository.allRefs["HEAD"] val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showInfo") .withPluginClasspath() .build() @@ -991,7 +998,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `issues-23 fix branch logic release branch`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -1032,10 +1039,10 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "5.2.1", "release/5.3.0") - val commit = git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "5.2.1", "release/5.3.0") + val commit = Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showInfo") .withPluginClasspath() .build() @@ -1058,7 +1065,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `issues-23 fix branch logic hotfix branch`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -1099,10 +1106,10 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "5.2.1", "hotfix/5.3.1") - val commit = git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "5.2.1", "hotfix/5.3.1") + val commit = Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showInfo") .withPluginClasspath() .build() @@ -1125,7 +1132,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `issues-35 fix branch regex`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyPatchVersionIncrementer @@ -1147,10 +1154,10 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "5.2.1", "feature/bellini/test-branch-version") - val commit = git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "5.2.1", "feature/bellini/test-branch-version") + val commit = Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showInfo") .withPluginClasspath() .build() @@ -1173,7 +1180,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `empty snapshotSuffix must not append a hyphen`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyMinorVersionIncrementer @@ -1194,10 +1201,10 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "4.2.0") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "4.2.0") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -1208,7 +1215,7 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { @Test fun `empty dirty marker must not append a hyphen`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle") buildFile.writeText(""" import io.wusa.incrementer.GroovyNoVersionIncrementer @@ -1230,15 +1237,15 @@ class SemverGitPluginGroovyFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithoutBranchAndWithoutTag(testProjectDirectory) - git.commit().setMessage("").call() + initializeGitWithoutBranchAndWithoutTag(repository, testProjectDirectory) + Git(repository).commit().setMessage("").call() val dirtyFile = File(testProjectDirectory, "test.dirty") dirtyFile.writeText("""not dirty!""") - git.add().addFilepattern(".").call() - git.commit().setMessage("").call() + Git(repository).add().addFilepattern(".").call() + Git(repository).commit().setMessage("").call() dirtyFile.writeText("""dirty!""") val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showInfo") .withPluginClasspath() .build() diff --git a/src/test/kotlin/io/wusa/SemverGitPluginKotlinFunctionalTest.kt b/src/test/kotlin/io/wusa/SemverGitPluginKotlinFunctionalTest.kt index 8458ad7..3c63da5 100644 --- a/src/test/kotlin/io/wusa/SemverGitPluginKotlinFunctionalTest.kt +++ b/src/test/kotlin/io/wusa/SemverGitPluginKotlinFunctionalTest.kt @@ -1,28 +1,37 @@ package io.wusa +import org.gradle.internal.impldep.org.eclipse.jgit.api.Git +import org.gradle.internal.impldep.org.eclipse.jgit.lib.Repository import org.gradle.testkit.runner.GradleRunner -import org.gradle.testkit.runner.UnexpectedBuildFailure import org.junit.jupiter.api.* +import org.junit.jupiter.api.io.TempDir import java.io.File -@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@TestInstance(TestInstance.Lifecycle.PER_METHOD) class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { + lateinit var testProjectDirectory: File private lateinit var gradleRunner: GradleRunner + private lateinit var git: Git + private lateinit var repository: Repository @BeforeEach fun setUp() { + testProjectDirectory = createTempDir() gradleRunner = GradleRunner.create() + gradleRunner.withProjectDir(testProjectDirectory) + git = Git.init().setDirectory(testProjectDirectory).call() + repository = Git.open(testProjectDirectory).repository } @AfterEach fun tearDown() { - //gradleRunner.projectDir.deleteRecursively() + testProjectDirectory.deleteOnExit() } @Test fun `version formatter`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -43,9 +52,9 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - initializeGitWithoutBranchAnnotated(testProjectDirectory) + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory) val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -55,7 +64,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `version properties`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -76,9 +85,9 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - initializeGitWithoutBranchAnnotated(testProjectDirectory) + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory) val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("createVersionProperties") .withPluginClasspath() .build() @@ -93,7 +102,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `custom version incrementer`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -121,10 +130,10 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/test") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/test") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -134,7 +143,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `version formatter for feature branches use specific`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -161,10 +170,10 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/test") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/test") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -174,7 +183,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `snapshot suffix for feature branches use specific`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -202,10 +211,10 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/test") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/test") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -215,7 +224,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `snapshot suffix for feature branches use specific which is empty`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -243,10 +252,10 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/test") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/test") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -256,7 +265,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `snapshot suffix are both empty`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -284,10 +293,10 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/test") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/test") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -297,7 +306,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `snapshot suffix for feature branches use specific which is not set`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -324,10 +333,10 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/test") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/test") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -337,7 +346,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `version formatter for feature branches with camelCase`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -364,10 +373,10 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/testAbc") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/testAbc") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -377,7 +386,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `version formatter for feature branches with kebab-case`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -404,10 +413,10 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/test-abc-10") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/test-abc-10") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -417,7 +426,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `version formatter for feature branches with snake_case`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -444,10 +453,10 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/test_abc_10") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/test_abc_10") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -457,7 +466,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `version formatter for feature branches with PascalCase`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -484,10 +493,10 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/TestAbc10") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/TestAbc10") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -497,7 +506,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `version formatter for feature branches with UPPERCASE`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -524,10 +533,10 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/TESTABC10") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/TESTABC10") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -537,7 +546,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `version formatter for feature branches use general`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -564,10 +573,10 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1", "feature/test") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1", "feature/test") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -577,7 +586,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `version no increment`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -598,10 +607,10 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -611,7 +620,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `version patch increment`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -632,10 +641,10 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -645,7 +654,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `version minor increment`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -666,10 +675,10 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -679,7 +688,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `version major increment`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -700,10 +709,10 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithBranch(testProjectDirectory, "0.0.1") - git.commit().setMessage("").call() + initializeGitWithBranch(repository, testProjectDirectory, "0.0.1") + Git(repository).commit().setMessage("").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -713,7 +722,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `version formatter with prefix`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -735,9 +744,9 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - initializeGitWithoutBranchAnnotated(testProjectDirectory, "prj_0.1.0") + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory, "prj_0.1.0") val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -747,7 +756,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `version formatter with prefix and multiple tags not head`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -769,12 +778,12 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithoutBranchAnnotated(testProjectDirectory, "prj_0.1.0") - val commit = git.commit().setMessage("another commit").call() - git.tag().setName("foo-1.0.0").setObjectId(commit).call() + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory, "prj_0.1.0") + val commit = Git(repository).commit().setMessage("another commit").call() + Git(repository).tag().setName("foo-1.0.0").setObjectId(commit).call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -784,7 +793,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `version formatter with prefix and multiple tags from head`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -806,12 +815,12 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithoutBranchAnnotated(testProjectDirectory, "prj_0.1.0") - val commit = git.commit().setMessage("another commit").call() - git.tag().setName("foo-1.0.0").setObjectId(commit).call() + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory, "prj_0.1.0") + val commit = Git(repository).commit().setMessage("another commit").call() + Git(repository).tag().setName("foo-1.0.0").setObjectId(commit).call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -821,7 +830,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `issue-47 increment minor by one with a lightweight tag`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -844,13 +853,13 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithoutBranchLightweight(testProjectDirectory, "2.0.42") - git.commit().setMessage("feat: added semver plugin incrementer parameter").call() - git.commit().setMessage("feat: added semver plugin incrementer parameter").call() - git.commit().setMessage("feat: added semver plugin incrementer parameter").call() + initializeGitWithoutBranchLightweight(repository, testProjectDirectory, "2.0.42") + Git(repository).commit().setMessage("feat: added semver plugin incrementer parameter").call() + Git(repository).commit().setMessage("feat: added semver plugin incrementer parameter").call() + Git(repository).commit().setMessage("feat: added semver plugin incrementer parameter").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() @@ -860,7 +869,7 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { @Test fun `issue-47 increment minor by one with a annotated tag`() { - val testProjectDirectory = createTempDir() + val buildFile = File(testProjectDirectory, "build.gradle.kts") buildFile.writeText(""" import io.wusa.Info @@ -883,19 +892,101 @@ class SemverGitPluginKotlinFunctionalTest : FunctionalBaseTest() { } } """) - val git = initializeGitWithoutBranchAnnotated(testProjectDirectory, "2.0.42") - val commit = git.commit().setMessage("feat: another commit").call() - git.tag().setName("2.2.0").setObjectId(commit).setAnnotated(false).call() - git.commit().setMessage("feat: added semver plugin incrementer parameter").call() - git.commit().setMessage("feat: added semver plugin incrementer parameter").call() - git.commit().setMessage("feat: added semver plugin incrementer parameter").call() + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory, "2.0.42") + val commit = Git(repository).commit().setMessage("feat: another commit").call() + Git(repository).tag().setName("2.2.0").setObjectId(commit).setAnnotated(false).call() + Git(repository).commit().setMessage("feat: added semver plugin incrementer parameter").call() + Git(repository).commit().setMessage("feat: added semver plugin incrementer parameter").call() + Git(repository).commit().setMessage("feat: added semver plugin incrementer parameter").call() val result = gradleRunner - .withProjectDir(testProjectDirectory) + .withArguments("showVersion") .withPluginClasspath() .build() println(result.output) Assertions.assertTrue(result.output.contains("Version: 2.1.0-SNAPSHOT")) } + + @Test + fun `issue-59 increment minor by one with a dirty working tree`() { + + val buildFile = File(testProjectDirectory, "build.gradle.kts") + buildFile.writeText(""" + import io.wusa.Info + import io.wusa.TagType + import io.wusa.incrementer.ConventionalCommitsVersionIncrementer + + plugins { + id("io.wusa.semver-git-plugin") + } + + semver { + tagPrefix = "" + tagType = TagType.ANNOTATED + branches { + branch { + regex = ".+" + incrementer = ConventionalCommitsVersionIncrementer + formatter = Transformer{ info:Info -> "${'$'}{info.version.major}.${'$'}{info.version.minor}.${'$'}{info.version.patch}" } + } + } + } + """) + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory, "2.0.42") + Git(repository).commit().setMessage("feat: another commit").call() + Git(repository).commit().setMessage("feat: added semver plugin incrementer parameter").call() + Git(repository).commit().setMessage("feat: added semver plugin incrementer parameter").call() + Git(repository).commit().setMessage("feat: added semver plugin incrementer parameter").call() + val dirty = File(testProjectDirectory, "dirty.file") + dirty.writeText("dirty") + Git(repository).add().addFilepattern(".").call() + + val result = gradleRunner + + .withArguments("showInfo") + .withPluginClasspath() + .build() + println(result.output) + Assertions.assertTrue(result.output.contains("Version: 2.1.0-dirty-SNAPSHOT")) + } + + /*@Test + fun `issue-59 dirty working tree with no commits`() { + + val buildFile = File(testProjectDirectory, "build.gradle.kts") + buildFile.writeText(""" + import io.wusa.Info + import io.wusa.TagType + import io.wusa.incrementer.ConventionalCommitsVersionIncrementer + + plugins { + id("io.wusa.semver-git-plugin") + } + + semver { + tagPrefix = "" + tagType = TagType.ANNOTATED + branches { + branch { + regex = ".+" + incrementer = ConventionalCommitsVersionIncrementer + formatter = Transformer{ info:Info -> "${'$'}{info.version.major}.${'$'}{info.version.minor}.${'$'}{info.version.patch}" } + } + } + } + """) + initializeGitWithoutBranchAnnotated(repository, testProjectDirectory, "2.0.42") + val dirty = File(testProjectDirectory, "dirty.file") + dirty.writeText("dirty") + Git(repository).add().addFilepattern(".").call() + + val result = gradleRunner + + .withArguments("showInfo") + .withPluginClasspath() + .build() + println(result.output) + Assertions.assertTrue(result.output.contains("Version: 2.0.43-dirty-SNAPSHOT")) + }*/ }