Skip to content

Commit

Permalink
Ignore reckon properties in composite builds
Browse files Browse the repository at this point in the history
Composite builds that include multiple builds that use reckon can run
into unexpected behavior, because any reckon.* props passed in on the
command line are used by all builds. This can cause it to try to
increment a version incorrectly.

The new behavior makes it so that only the main build within a composite
will actually respect the gradle properties.

This does imply that you can't really release a version of an included
build, but the expectation is that that's not what anyone would intend
to do. That could be proven wrong, but it would require something more
involved to support.

This fixes #74.
  • Loading branch information
ajoberstar committed Apr 15, 2018
1 parent c332caf commit d800e15
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,26 @@ public VcsInventorySupplier git(Grgit grgit) {
}

public NormalStrategy scopeFromProp() {
Function<VcsInventory, Optional<String>> supplier = ignored -> Optional.ofNullable(project.findProperty(SCOPE_PROP)).map(Object::toString);
Function<VcsInventory, Optional<String>> 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<String> stageSet = Arrays.stream(stages).collect(Collectors.toSet());
BiFunction<VcsInventory, Version, Optional<String>> supplier = (inventory, targetNormal) -> Optional.ofNullable(project.findProperty(STAGE_PROP)).map(Object::toString);
BiFunction<VcsInventory, Version, Optional<String>> 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<VcsInventory, Version, Optional<Boolean>> 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);
Expand Down

0 comments on commit d800e15

Please sign in to comment.