Rely on projectDir instead of whatever "working dir" we get from the shell for finding Git

This commit is contained in:
R. Tyler Croy 2015-07-02 13:42:28 -07:00
parent 97651843ca
commit 79f678987a
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
8 changed files with 45 additions and 24 deletions

View File

@ -0,0 +1,8 @@
package com.github.lookout.serviceartifact
import spock.lang.*
class VersionIntegrationSpec extends Specification {
}

View File

@ -0,0 +1,7 @@
package com.github.lookout.serviceartifact.metadata
/**
* Created by tyler on 7/2/15.
*/
class Data {
}

View File

@ -1,7 +1,7 @@
package com.github.lookout.serviceartifact.scm
import groovy.transform.TypeChecked
import org.gradle.api.Project
import org.slf4j.Logger
import org.slf4j.LoggerFactory
@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory
abstract class AbstractScmHandler {
/** Supplied environment variables */
protected Map<String, String> env
protected Project project
protected Logger logger = LoggerFactory.getLogger(this.class)
@ -24,7 +25,7 @@ abstract class AbstractScmHandler {
abstract String annotatedVersion(String baseVersion)
/** Build an instance of this handler */
static AbstractScmHandler build(Map<String, String> env) {
static AbstractScmHandler build(Project project, Map<String, String> env) {
throw new NoSuchMethodException("A subclass of AbstractScmHandler has not implemented build()!")
}

View File

@ -1,6 +1,7 @@
package com.github.lookout.serviceartifact.scm
import groovy.transform.TypeChecked
import org.gradle.api.Project
@TypeChecked
class GerritHandler extends AbstractScmHandler {
@ -8,7 +9,8 @@ class GerritHandler extends AbstractScmHandler {
private final String GERRIT_PATCH = 'GERRIT_PATCHSET_NUMBER'
private final String GERRIT_REVISION = 'GERRIT_PATCHSET_REVISION'
GerritHandler(Map<String, String> environment) {
GerritHandler(Project project, Map<String, String> environment) {
this.project = project
this.env = environment
}
@ -48,7 +50,7 @@ class GerritHandler extends AbstractScmHandler {
@Override
static AbstractScmHandler build(Map<String, String> env) {
return new GerritHandler(env)
static AbstractScmHandler build(Project project, Map<String, String> env) {
return new GerritHandler(project, env)
}
}

View File

@ -3,6 +3,7 @@ package com.github.lookout.serviceartifact.scm
import groovy.transform.TypeChecked
import org.ajoberstar.grgit.Grgit
import org.eclipse.jgit.errors.RepositoryNotFoundException
import org.gradle.api.Project
/**
* Git handler for a project in a traditional Git repository
@ -12,8 +13,9 @@ class GitHandler extends AbstractScmHandler {
private final String gitDir = '.git'
private Grgit _git = null
GitHandler(Map<String, String> environment) {
GitHandler(Project project, Map<String, String> environment) {
this.env = environment
this.project = project
}
boolean isAvailable() {
@ -40,8 +42,8 @@ class GitHandler extends AbstractScmHandler {
}
@Override
static AbstractScmHandler build(Map<String, String> env) {
return new GitHandler(env)
static AbstractScmHandler build(Project project, Map<String, String> env) {
return new GitHandler(project, env)
}
@ -49,7 +51,7 @@ class GitHandler extends AbstractScmHandler {
protected Grgit getGit() {
if (this._git == null) {
try {
File repoDir = findGitRoot('.')
File repoDir = findGitRoot(project?.projectDir)
if (!repoDir) {
return null
@ -72,19 +74,18 @@ class GitHandler extends AbstractScmHandler {
* @param currentDirectory A string representing a relative or absolute path
* @return File instance if we've found a git root, or null
*/
protected File findGitRoot(String currentDirectory) {
protected File findGitRoot(File current) {
/* this means we've been invoked recursively with a null parent */
if (currentDirectory == null) {
if (current == null) {
return null
}
File current = new File(currentDirectory)
File gitDir = getGitDirFor(current.absolutePath)
if (gitDir.isDirectory()) {
return current
}
return findGitRoot((new File(current.absolutePath)).parent)
return findGitRoot(current.parentFile)
}
protected File getGitDirFor(String absolutePath) {

View File

@ -132,7 +132,8 @@ class ServiceArtifactExtensionSpec extends Specification {
versionMap['version']
versionMap['name']
versionMap['buildDate']
versionMap['revision']
/* since we're in a temp dir, our revision should be empty */
versionMap['revision'] == null
versionMap['builtOn']
}

View File

@ -14,7 +14,7 @@ class GerritHandlerSpec extends Specification {
def "isAvailable() should be false by default"() {
given:
def handler = new GerritHandler([:])
def handler = new GerritHandler(null, [:])
expect:
!handler.isAvailable()
@ -22,7 +22,7 @@ class GerritHandlerSpec extends Specification {
def "isAvailable() should be true if the env has Gerrit env vars"() {
given:
def handler = new GerritHandler(gerritEnv())
def handler = new GerritHandler(null, gerritEnv())
expect:
handler.isAvailable()
@ -30,7 +30,7 @@ class GerritHandlerSpec extends Specification {
def "getRevision() should return an empty string by default"() {
given:
def handler = new GerritHandler([:])
def handler = new GerritHandler(null, [:])
expect:
handler.revision == ''
@ -38,7 +38,7 @@ class GerritHandlerSpec extends Specification {
def "getRevision() should return the GERRIT_PATCHSET_REVISION when present"() {
given:
def handler = new GerritHandler(gerritEnv())
def handler = new GerritHandler(null, gerritEnv())
expect:
handler.revision == gerritEnv()['GERRIT_PATCHSET_REVISION']
@ -46,7 +46,7 @@ class GerritHandlerSpec extends Specification {
def "annotatedVersion() should include change and patchset numbers, and SHA1"() {
given:
def handler = new GerritHandler(gerritEnv())
def handler = new GerritHandler(null, gerritEnv())
when:
String version = handler.annotatedVersion('1.0')

View File

@ -7,9 +7,14 @@ import org.ajoberstar.grgit.Grgit
import org.ajoberstar.grgit.Repository
class GitHandlerSpec extends Specification {
def handler
def setup() {
this.handler = Spy(GitHandler, constructorArgs: [null, [:]])
}
def "isAvailable() should be false by default"() {
given:
def handler = Spy(GitHandler, constructorArgs: [[:]])
1 * handler.getProperty('git') >> null
expect:
@ -19,7 +24,6 @@ class GitHandlerSpec extends Specification {
def "isAvailable() should be true if .git is present"() {
given:
def handler = Spy(GitHandler, constructorArgs: [[:]])
def gitMock = Mock(Grgit)
1 * handler.getProperty('git') >> gitMock
@ -30,7 +34,6 @@ class GitHandlerSpec extends Specification {
def "annotatedVersion() when .git is NOT present should no-op"() {
given:
def handler = Spy(GitHandler, constructorArgs: [[:]])
1 * handler.findGitRoot(_) >> null
when:
@ -42,7 +45,6 @@ class GitHandlerSpec extends Specification {
def "annotatedVersion() when .git is present should include SHA+1"() {
given:
def handler = Spy(GitHandler, constructorArgs: [[:]])
Repository repoMock = GroovyMock()
1 * repoMock.head() >> repoMock
1 * repoMock.getAbbreviatedId() >> '0xdeadbeef'
@ -57,7 +59,6 @@ class GitHandlerSpec extends Specification {
def "annotateVersion() when a Jenkins BUILD_NUMBER is available should include it"() {
given:
def handler = Spy(GitHandler, constructorArgs: [[:]])
_ * handler.getProperty('environment') >> ['BUILD_NUMBER' : '1']
1 * handler.findGitRoot(_) >> null