Run go command as a gradle task

748 views Asked by At

I am using gradle as the build tool for a terraform project. I do have unit tests written in go for the project under the ..test/... folder . The way I am running test locally is just on the commandline go test ..test/..' which will run all tests under the test folder. I want to integrate this in the build , so that every build will run this command 'go test ..test/..', How do I achieve this in gradle. Can a custom task be utilized to run a go command?

I am trying to do something like the following

task testExec(type: Exec) {
    workingDir "${buildDir}/test"
    commandLine 'go','test'
} doLast {
    println "Test Executed!"
}

But I get the error

> A problem occurred starting process 'command 'go''

For what its worth , I tried other commands and get the same erorr for ex

task testExec(type: Exec) {
     workingDir "${buildDir}/test"
     commandLine 'echo','${}buildDir'
} doLast {
     println "Test Executed!"
}

gives similar error

> A problem occurred starting process 'command 'echo''
1

There are 1 answers

0
flaxel On

You can use the gradle plugin. First you can follow the starting guide and add the plugin:

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!
}

Then you can run the following command to execute all go files that follow the file name convention <name>_test.go:

gradlew goTest 

Otherwise you can also create a complete custom task or a custom task with the plugin.

EDIT:

I found the cause of your error. The variable buildDir refers to the build folder in your project: <project_folder>/build. The problem now is that the folder test does not exists and the exception is thrown. Instead, you can use the variable projectDir.