diff --git a/reckon-gradle/src/compatTest/groovy/org/ajoberstar/reckon/gradle/BaseCompatTest.groovy b/reckon-gradle/src/compatTest/groovy/org/ajoberstar/reckon/gradle/BaseCompatTest.groovy index 99d23ace..3eb5c099 100644 --- a/reckon-gradle/src/compatTest/groovy/org/ajoberstar/reckon/gradle/BaseCompatTest.groovy +++ b/reckon-gradle/src/compatTest/groovy/org/ajoberstar/reckon/gradle/BaseCompatTest.groovy @@ -1,19 +1,4 @@ -/* - * Copyright 2015-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.ajoberstar.gradle.reckon.gradle +package org.ajoberstar.reckon.gradle import spock.lang.Specification import spock.lang.Unroll diff --git a/reckon-gradle/src/compatTest/groovy/org/ajoberstar/reckon/gradle/CompositeBuildCompatTest.groovy b/reckon-gradle/src/compatTest/groovy/org/ajoberstar/reckon/gradle/CompositeBuildCompatTest.groovy new file mode 100644 index 00000000..e0adebef --- /dev/null +++ b/reckon-gradle/src/compatTest/groovy/org/ajoberstar/reckon/gradle/CompositeBuildCompatTest.groovy @@ -0,0 +1,112 @@ +package org.ajoberstar.reckon.gradle + +import spock.lang.Specification +import spock.lang.Unroll + +import org.ajoberstar.grgit.Grgit +import org.gradle.testkit.runner.GradleRunner +import org.gradle.testkit.runner.BuildResult +import org.gradle.testkit.runner.TaskOutcome +import org.junit.Rule +import org.junit.rules.TemporaryFolder + +// Composite builds were added in 3.1 +@IgnoreIf({ System.properties['compat.gradle.version'] == '3.0' }) +class CompositeBuildCompatTest extends Specification { + @Rule TemporaryFolder tempDir = new TemporaryFolder() + File project1Dir + File project2Dir + File build1File + File build2File + + def setup() { + project1Dir = tempDir.newFolder('project1') + project2Dir = tempDir.newFolder('project2') + build1File = projectFile(project1Dir, 'build.gradle') + build2File = projectFile(project2Dir, 'build.gradle') + + def grgit1 = Grgit.init(dir: project1Dir) + projectFile(project1Dir, 'file.txt') << 'stuff' + projectFile(project1Dir, '.gitignore') << '.gradle\nbuild\n' + build1File << '''\ +plugins { + id 'org.ajoberstar.grgit' + id 'org.ajoberstar.reckon' +} + +reckon { + normal = scopeFromProp() + preRelease = stageFromProp('beta', 'final') +} + +task printVersion { + doLast { + println "${project.name} version is ${project.version}" + } +} +''' + grgit1.add(patterns: ['.']) + grgit1.commit(message: 'first commit') + grgit1.tag.add(name: '1.3.0', message: 'stuff') + grgit1.close() + + def grgit2 = Grgit.init(dir: project2Dir) + projectFile(project2Dir, 'file.txt') << 'stuff' + projectFile(project2Dir, '.gitignore') << '.gradle\nbuild\n' + build2File << '''\ +plugins { + id 'org.ajoberstar.grgit' + id 'org.ajoberstar.reckon' +} + +reckon { + normal = scopeFromProp() + preRelease = stageFromProp('beta', 'final') +} + +task printVersion { + doLast { + println "${project.name} version is ${project.version}" + } +} +''' + grgit2.add(patterns: ['.']) + grgit2.commit(message: 'first commit') + grgit2.tag.add(name: '1.0.0-beta.1', message: 'stuff') + grgit2.close() + } + + def 'if build included in composite build, reckon properties are ignored'() { + when: + def result = build(project2Dir, 'printVersion', '--include-build', project1Dir.absolutePath, '-Preckon.scope=major', '-Preckon.stage=beta') + then: + result.output.contains('Reckoned version: 1.3.0') + result.output.contains('Reckoned version: 1.0.0-beta.2') + } + + private BuildResult build(File projectDir, String... args = []) { + return GradleRunner.create() + .withGradleVersion(System.properties['compat.gradle.version']) + .withPluginClasspath() + .withProjectDir(projectDir) + .forwardOutput() + .withArguments((args + '--stacktrace') as String[]) + .build() + } + + private BuildResult buildAndFail(File projectDir, String... args = []) { + return GradleRunner.create() + .withGradleVersion(System.properties['compat.gradle.version']) + .withPluginClasspath() + .withProjectDir(projectDir) + .forwardOutput() + .withArguments((args + '--stacktrace') as String[]) + .buildAndFail() + } + + private File projectFile(File projectDir, String path) { + File file = new File(projectDir, path) + file.parentFile.mkdirs() + return file + } +} diff --git a/reckon-gradle/src/main/java/org/ajoberstar/reckon/gradle/ReckonExtension.java b/reckon-gradle/src/main/java/org/ajoberstar/reckon/gradle/ReckonExtension.java index 2de5cbb7..5c3526c9 100644 --- a/reckon-gradle/src/main/java/org/ajoberstar/reckon/gradle/ReckonExtension.java +++ b/reckon-gradle/src/main/java/org/ajoberstar/reckon/gradle/ReckonExtension.java @@ -52,18 +52,26 @@ public VcsInventorySupplier git(Grgit grgit) { } public NormalStrategy scopeFromProp() { - Function> supplier = ignored -> Optional.ofNullable(project.findProperty(SCOPE_PROP)).map(Object::toString); + Function> supplier = ignored -> Optional.ofNullable(project.findProperty(SCOPE_PROP)) + // composite builds have a parent Gradle build and can't trust the values of these properties + .filter(value -> project.getGradle().getParent() == null) + .map(Object::toString); return new ScopeNormalStrategy(supplier); } public PreReleaseStrategy stageFromProp(String... stages) { Set stageSet = Arrays.stream(stages).collect(Collectors.toSet()); - BiFunction> supplier = (inventory, targetNormal) -> Optional.ofNullable(project.findProperty(STAGE_PROP)).map(Object::toString); + BiFunction> supplier = (inventory, targetNormal) -> Optional.ofNullable(project.findProperty(STAGE_PROP)) + // composite builds have a parent Gradle build and can't trust the values of these properties + .filter(value -> project.getGradle().getParent() == null) + .map(Object::toString); return new StagePreReleaseStrategy(stageSet, supplier); } public PreReleaseStrategy snapshotFromProp() { BiFunction> supplier = (inventory, targetNormal) -> Optional.ofNullable(project.findProperty(SNAPSHOT_PROP)) + // composite builds have a parent Gradle build and can't trust the values of these properties + .filter(value -> project.getGradle().getParent() == null) .map(Object::toString) .map(Boolean::parseBoolean); return new SnapshotPreReleaseStrategy(supplier);