我有这样的任务自定义 gradle 插件:
@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}")
}
我对其进行了集成测试:
@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'))
}
但它失败了,因为我在任务中的命令"git rev-list HEAD --count".execute().text.toInteger()是针对我的项目目录而不是针对我的测试目录"build/tmp/git-repository"执行的。
是否可以在测试项目目录中执行此任务?
更新:
感谢@Mark Vieira 和@Rene Groeschke。根据他们的建议,我以这种方式修复了它:
@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}")
}