Skip to content

Commit

Permalink
Fixes error when working on project without Git repository
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdubiel authored Jul 5, 2017
1 parent d41f06c commit 59b1017
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package pl.allegro.tech.build.axion.release

import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification

class RepositorylessIntegrationTest extends Specification {

@Rule
TemporaryFolder temporaryFolder = new TemporaryFolder()

File directory

void setup() {
directory = temporaryFolder.root
}

def "should not fail build when calling currentVersion task on project without repo"() {
given:
File build = temporaryFolder.newFile('build.gradle')
build << """
plugins {
id 'pl.allegro.tech.build.axion-release'
}
project.version = scmVersion.version
"""

when:
def result = GradleRunner.create()
.withProjectDir(directory)
.withPluginClasspath()
.withArguments('currentVersion', '-s')
.build()

then:
result.task(":currentVersion").outcome == TaskOutcome.SUCCESS
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ class VersionResolver {
private Map readVersions(VersionFactory versionFactory,
TagProperties tagProperties,
NextVersionProperties nextVersionProperties) {
GitRepository repository = (GitRepository) this.repository
Pattern releaseTagPattern = ~/^${tagProperties.prefix}.*/
Pattern nextVersionTagPattern = ~/.*${nextVersionProperties.suffix}$/

Map currentVersionInfo = null
Map previousVersionInfo = null
List<TagsOnCommit> allTaggedCommits = repository.firstTaggedCommitAsList(releaseTagPattern)
Map currentVersionInfo, previousVersionInfo
List<TagsOnCommit> allTaggedCommits = [repository.latestTags(releaseTagPattern)]
currentVersionInfo = versionFromTaggedCommits(allTaggedCommits, false, nextVersionTagPattern, versionFactory)

TagsOnCommit previousTags = repository.latestTags(releaseTagPattern)
Expand All @@ -77,13 +75,10 @@ class VersionResolver {
private Map readVersionsByHighestVersion(VersionFactory versionFactory,
TagProperties tagProperties,
NextVersionProperties nextVersionProperties) {
GitRepository repository = (GitRepository) this.repository
Pattern releaseTagPattern = ~/^${tagProperties.prefix}.*/
Pattern nextVersionTagPattern = ~/.*${nextVersionProperties.suffix}$/

Map currentVersionInfo = null
Map previousVersionInfo = null

Map currentVersionInfo, previousVersionInfo
List<TagsOnCommit> allTaggedCommits = repository.taggedCommits(releaseTagPattern)

currentVersionInfo = versionFromTaggedCommits(allTaggedCommits, false, nextVersionTagPattern, versionFactory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ interface ScmRepository {

TagsOnCommit latestTags(Pattern pattern, String sinceCommit)

List<TagsOnCommit> taggedCommits(Pattern pattern)

boolean remoteAttached(String remoteName);

boolean checkUncommittedChanges()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class DryRepository implements ScmRepository {
return tags
}

@Override
List<TagsOnCommit> taggedCommits(Pattern pattern) {
return delegateRepository.taggedCommits(pattern)
}

@Override
boolean remoteAttached(String remoteName) {
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ class DummyRepository implements ScmRepository {
return new TagsOnCommit(null, [], false)
}

@Override
List<TagsOnCommit> taggedCommits(Pattern pattern) {
logger.quiet("Could not resolve current position on uninitialized repository, returning default")
return [new TagsOnCommit(null, [], false)]
}


@Override
boolean remoteAttached(String remoteName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,12 @@ class GitRepository implements ScmRepository {
)
}

@Override
TagsOnCommit latestTags(Pattern pattern) {
return latestTagsInternal(pattern, null, true)
}

@Override
TagsOnCommit latestTags(Pattern pattern, String sinceCommit) {
return latestTagsInternal(pattern, sinceCommit, false)
}
Expand All @@ -182,10 +184,7 @@ class GitRepository implements ScmRepository {
return taggedCommits.isEmpty() ? TagsOnCommit.empty() : taggedCommits[0]
}

List<TagsOnCommit> firstTaggedCommitAsList(Pattern pattern) {
return taggedCommitsInternal(pattern, null, true, true)
}

@Override
List<TagsOnCommit> taggedCommits(Pattern pattern) {
return taggedCommitsInternal(pattern, null, true, false)
}
Expand Down

0 comments on commit 59b1017

Please sign in to comment.