I have custom gradle plugin with such task:
@TaskAction
def buildSemanticVersion() {
int major = project.semanticVersion.major
int minor = project.semanticVersion.minor
int patch = "git rev-list HEAD --count".execute().text.toInteger()
project.setVersion("${major}.${minor}.${patch}")
}
I have integration test for it:
@Test
public void testBuildSemanticVersion() throws Exception {
// GIVEN
Project project = ProjectBuilder.builder().withProjectDir(new File("build/tmp/git-repository")).build()
project.apply plugin: 'com.github.moleksyuk.vcs-semantic-version'
project.semanticVersion.with { major = 1; minor = 2 }
// WHEN
project.tasks.buildSemanticVersion.execute()
// THEN
assertThat(project.version, Matchers.equalTo('1.2.3'))
}
But it fails because my command "git rev-list HEAD --count".execute().text.toInteger() in task is executed agains my project dir but not against my test dir "build/tmp/git-repository".
Is it possible to execute this task agains test project directory?
Update:
Thanks to @Mark Vieira and @Rene Groeschke. According to their suggestion I fixed it in such way:
@TaskAction
def buildSemanticVersion() {
int major = project.semanticVersion.major
int minor = project.semanticVersion.minor
def stdout = new ByteArrayOutputStream()
def execResult = project.exec({
commandLine 'git'
args 'rev-list', 'HEAD', '--count'
standardOutput = stdout;
})
int patch = stdout.toString().toInteger()
project.setVersion("${major}.${minor}.${patch}")
}
groovys "...".execute()
always uses the current working directory of the java process and as far as I know it is not configurable.
This failing test already shows a problem with your task implementation as the working directory is not necessarily the project directory. you could use an Exec
task or wrap the project.exec
method into your own task. both, project.exec and Exec allow you to configure the working directory.