我正在使用gradle作为terraform项目的构建工具。我确实在..test / ...文件夹下为该项目编写了单元测试。我在本地运行测试的方式只是在命令行go test ..test/..
'上运行,它将在测试文件夹下运行所有tests
。我想将此集成到构建中,以便每个构建都将运行此命令'go test ..test/..'
,如何在gradle中实现这一点。可以利用自定义任务来运行go
命令吗?
我正在尝试执行以下操作
task testExec(type: Exec) {
workingDir "${buildDir}/test"
commandLine 'go','test'
} doLast {
println "Test Executed!"
}
但是我得到了错误
> A problem occurred starting process 'command 'go''
出于其价值,我尝试了其他命令并获得了相同的erorr
task testExec(type: Exec) {
workingDir "${buildDir}/test"
commandLine 'echo','${}buildDir'
} doLast {
println "Test Executed!"
}
给出类似的错误
> A problem occurred starting process 'command 'echo''
答案 0 :(得分:1)
您可以使用gradle plugin。首先,您可以按照starting guide并添加插件:
plugins {
id 'com.github.blindpirate.gogradle' version '0.11.4'
}
golang {
packagePath = 'github.com/your/package' // go import path of project to be built, NOT local file system path!
}
然后,您可以运行following command来执行所有遵循文件名约定<name>_test.go
的go文件:
gradlew goTest
否则,您也可以创建complete custom task或custom task with the plugin。
编辑:
我找到了导致您出错的原因。变量buildDir
引用项目中的build
文件夹:<project_folder>/build
。现在的问题是文件夹test
不存在,并且引发了异常。相反,您可以使用变量projectDir
。