Skip to content

Commit

Permalink
Merge pull request #185 from nebula-plugins/fail-with-unsupported-ver…
Browse files Browse the repository at this point in the history
…sioning-scheme

Using a non-semver version for release.version should fail
  • Loading branch information
rpalcolea authored Apr 20, 2020
2 parents ee2d5f3 + 594eae0 commit 9530825
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,63 @@ class ReleasePluginIntegrationSpec extends GitVersioningIntegrationSpec {
result.standardError.contains('Nebula Release plugin does not support branches that end with dash (-)')
}

@Unroll
def 'useLastTag errors out if version has invalid number of digits - #version'() {
git.tag.add(name: "v$version")

when:
def result = runTasksWithFailure('final', '-Prelease.useLastTag=true')

then:
result.standardError.contains "Current commit has following tags: [v${version}] but they were not recognized as valid versions"
!new File(projectDir, "build/libs/${moduleName}-${version}.jar").exists()

where:
version << [
'42.5.3.5',
'42.5.3.5.3',
'42.5.3.1-rc.1',
'42.5.3.1.4-rc.1',
]
}

@Unroll
def 'release with the override of version calculation errors out if version has invalid number of digits - #version'() {
when:
def result = runTasksWithFailure('final', "-Prelease.version=${version}")

then:
result.standardError.contains "Supplied release.version ($version) is not valid per semver spec. For more information, please refer to https://semver.org/"
!new File(projectDir, "build/libs/${moduleName}-${version}.jar").exists()
!originGit.tag.list()*.name.contains("v${version}")

where:
version << [
'42.5.3.5',
'42.5.3.5.3',
'42.5.3.1-rc.1',
'42.5.3.1.4-rc.1',
]
}

@Unroll
def 'release with the override of version calculation does not errors out if version has invalid number of digits but verification is off - #version'() {
when:
def result = runTasksSuccessfully('final', "-Prelease.version=${version}", "-Prelease.ignoreSuppliedVersionVerification=true")

then:
!result.standardError.contains("Supplied release.version ($version) is not valid per semver spec. For more information, please refer to https://semver.org/")
new File(projectDir, "build/libs/${moduleName}-${version}.jar").exists()

where:
version << [
'42.5.3.5',
'42.5.3.5.3',
'42.5.3.1-rc.1',
'42.5.3.1.4-rc.1',
]
}

private void replaceDevWithImmutableSnapshot() {
new File(buildFile.parentFile, "gradle.properties").text = """
nebula.release.features.replaceDevWithImmutableSnapshot=true
Expand Down
20 changes: 20 additions & 0 deletions src/main/groovy/nebula/plugin/release/OverrideStrategies.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package nebula.plugin.release

import com.github.zafarkhaja.semver.UnexpectedCharacterException
import com.github.zafarkhaja.semver.Version
import nebula.plugin.release.git.base.ReleasePluginExtension
import nebula.plugin.release.git.base.ReleaseVersion
Expand Down Expand Up @@ -119,6 +120,8 @@ class OverrideStrategies {

static class GradlePropertyStrategy implements VersionStrategy {
static final String PROPERTY_NAME = 'release.version'
static final String VERSION_VERIFICATION_PROPERTY_NAME = 'release.ignoreSuppliedVersionVerification'

Project project
String propertyName

Expand Down Expand Up @@ -148,9 +151,26 @@ class OverrideStrategies {
requestedVersion = VersionSanitizerUtil.sanitize(requestedVersion.toString())
}

boolean isValidVersion = validateRequestedVersion(requestedVersion)
if(!isValidVersion) {
throw new GradleException("Supplied release.version ($requestedVersion) is not valid per semver spec. For more information, please refer to https://semver.org/")
}

new ReleaseVersion(requestedVersion, null, true)
}

private boolean validateRequestedVersion(String version) {
if(project.hasProperty(VERSION_VERIFICATION_PROPERTY_NAME)) {
return true
}

try {
Version.valueOf(version[0] == 'v' ? version[1..-1] : version)
return true
} catch(UnexpectedCharacterException e) {
return false
}
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class OverrideStrategiesSpec extends ProjectSpec {

def 'able to set via gradle property and sanitize'() {
setup:
project.ext.set('release.version', '42.5.0+feature')
project.ext.set('release.version', '42.5.0-rc.1+feature')
project.ext.set('release.sanitizeVersion', true)

when:
Expand All @@ -48,6 +48,6 @@ class OverrideStrategiesSpec extends ProjectSpec {
}

then:
project.version.toString() == '42.5.0.feature'
project.version.toString() == '42.5.0-rc.1.feature'
}
}
Empty file added stale_outputs_checked
Empty file.

0 comments on commit 9530825

Please sign in to comment.