From c1654daab310b38c960b42cf52cbd4765ab79e16 Mon Sep 17 00:00:00 2001 From: Thomas Grabietz Date: Fri, 26 May 2023 09:49:18 +0200 Subject: [PATCH] #622 fix VersionVersionProperties.Creator (#627) * #622 fix: evaluate also instance VersionVersionProperties.Incrementer to run in right condition if dsl is kotlin Test & Documentation * #622 fix: evaluate also instance VersionVersionProperties.Creator to run in right condition if dsl is kotlin Test & Documentation --- docs/examples/examples.md | 6 ++ .../BranchVersionCreatorKotlinDslTest.groovy | 96 +++++++++++++++++++ .../KotlinDslCompatibilityTests.groovy | 7 ++ .../config/VersionPropertiesFactory.groovy | 6 +- 4 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 src/integration/groovy/pl/allegro/tech/build/axion/release/BranchVersionCreatorKotlinDslTest.groovy diff --git a/docs/examples/examples.md b/docs/examples/examples.md index 339a7fc2..b91ca336 100644 --- a/docs/examples/examples.md +++ b/docs/examples/examples.md @@ -112,6 +112,12 @@ scmVersion { ) ) + branchVersionCreator.putAll( + mapOf( + "master" to VersionProperties.Creator { s: String, scmPosition: ScmPosition -> "${s}-${scmPosition.branch}"} + ) + ) + versionCreator({versionFromTag,scmPosition -> "version"}) snapshotCreator({versionFromTag,scmPosition -> "version"}) versionIncrementer({versionIncrementerContext -> Version}) diff --git a/src/integration/groovy/pl/allegro/tech/build/axion/release/BranchVersionCreatorKotlinDslTest.groovy b/src/integration/groovy/pl/allegro/tech/build/axion/release/BranchVersionCreatorKotlinDslTest.groovy new file mode 100644 index 00000000..90707f88 --- /dev/null +++ b/src/integration/groovy/pl/allegro/tech/build/axion/release/BranchVersionCreatorKotlinDslTest.groovy @@ -0,0 +1,96 @@ +package pl.allegro.tech.build.axion.release + +import org.ajoberstar.grgit.Grgit +import org.gradle.testkit.runner.GradleRunner +import org.gradle.testkit.runner.TaskOutcome +import pl.allegro.tech.build.axion.release.domain.LocalOnlyResolver +import pl.allegro.tech.build.axion.release.domain.properties.PropertiesBuilder +import pl.allegro.tech.build.axion.release.domain.scm.ScmProperties +import pl.allegro.tech.build.axion.release.domain.scm.ScmRepository +import pl.allegro.tech.build.axion.release.infrastructure.di.ScmRepositoryFactory +import pl.allegro.tech.build.axion.release.infrastructure.di.VersionResolutionContext +import spock.lang.Specification +import spock.lang.TempDir + +import static pl.allegro.tech.build.axion.release.domain.scm.ScmPropertiesBuilder.scmProperties + +class BranchVersionCreatorKotlinDslTest extends Specification { + + @TempDir + File temporaryFolder + + ScmRepository repository + + VersionResolutionContext context + + void setup() { + def rawRepository = Grgit.init(dir: temporaryFolder) + + // let's make sure, not to use system wide user settings in tests + rawRepository.repository.jgit.repository.config.baseConfig.clear() + + ScmProperties scmProperties = scmProperties(temporaryFolder).build() + ScmRepository scmRepository = ScmRepositoryFactory.create(scmProperties) + + context = new VersionResolutionContext( + PropertiesBuilder.properties().build(), + scmRepository, + scmProperties, + temporaryFolder, + new LocalOnlyResolver(true) + ) + + repository = context.repository() + repository.commit(['*'], 'initial commit') + repository.tag("V1.0.0") + + } + + + def "should not fail using closure as argument for branchVersionIncrementer"() { + given: + + new FileTreeBuilder(temporaryFolder).file("build.gradle.kts", + """ + + import com.github.zafarkhaja.semver.Version + import pl.allegro.tech.build.axion.release.domain.VersionIncrementerContext + import pl.allegro.tech.build.axion.release.domain.properties.VersionProperties + import pl.allegro.tech.build.axion.release.domain.scm.ScmPosition + + plugins { + id ("pl.allegro.tech.build.axion-release") + } + + scmVersion { + tag { + prefix.set("V") + } + ignoreUncommittedChanges.set(false) + + branchVersionCreator.putAll( + mapOf( + "master" to VersionProperties.Creator { s: String, scmPosition: ScmPosition -> "\${s}-\${scmPosition.branch}"} + ) + ) + } + + + project.version = scmVersion.version + + """) + + when: + def result = GradleRunner.create() + .withProjectDir(temporaryFolder) + .withPluginClasspath() + .withArguments('currentVersion', '-s') + .build() + + then: + result.output.contains(" 1.0.1-master-SNAPSHOT") + result.task(":currentVersion").outcome == TaskOutcome.SUCCESS + + + } +} diff --git a/src/integration/groovy/pl/allegro/tech/build/axion/release/KotlinDslCompatibilityTests.groovy b/src/integration/groovy/pl/allegro/tech/build/axion/release/KotlinDslCompatibilityTests.groovy index 9b9c2304..c0062929 100644 --- a/src/integration/groovy/pl/allegro/tech/build/axion/release/KotlinDslCompatibilityTests.groovy +++ b/src/integration/groovy/pl/allegro/tech/build/axion/release/KotlinDslCompatibilityTests.groovy @@ -15,6 +15,7 @@ class KotlinDslCompatibilityTests extends Specification { """ import pl.allegro.tech.build.axion.release.domain.* import pl.allegro.tech.build.axion.release.domain.properties.* + import pl.allegro.tech.build.axion.release.domain.scm.ScmPosition plugins { id("pl.allegro.tech.build.axion-release") @@ -90,6 +91,12 @@ class KotlinDslCompatibilityTests extends Specification { ) ) + branchVersionCreator.putAll( + mapOf( + "custom" to VersionProperties.Creator { s: String, scmPosition: ScmPosition -> "\${s}-\${scmPosition.branch}"} + ) + ) + versionCreator(VersionProperties.Creator({a,b -> "c"})) versionCreator({a,b -> "c"}) diff --git a/src/main/groovy/pl/allegro/tech/build/axion/release/infrastructure/config/VersionPropertiesFactory.groovy b/src/main/groovy/pl/allegro/tech/build/axion/release/infrastructure/config/VersionPropertiesFactory.groovy index e5cf7495..c0d6fc17 100644 --- a/src/main/groovy/pl/allegro/tech/build/axion/release/infrastructure/config/VersionPropertiesFactory.groovy +++ b/src/main/groovy/pl/allegro/tech/build/axion/release/infrastructure/config/VersionPropertiesFactory.groovy @@ -82,10 +82,10 @@ class VersionPropertiesFactory { if (value == null) { return defaultValue - } else if (!(value instanceof Closure)) { - return converter.call(value) - } else { + } else if ((value instanceof Closure) || (value instanceof VersionProperties.Creator)) { return value + } else { + return converter.call(value) } } }