I am analyzing code coverage using go tooling. First I create test binary
go test -coverpkg="github.com/ypapax/flags" -c
Then I run it
./flags.test -test.coverprofile=/tmp/out.out
cat /tmp/out.out
And see profile content
PASS
coverage: 100.0% of statements in github.com/ypapax/flags
mode: set
github.com/ypapax/flags/main.go:5.12,7.2 1 1
The question is:
Is it possible to predefine flag -test.coverprofile=/tmp/out.out
so I could run without test.coverprofile
flag
./flags.test
cat /tmp/out.out
and still see /tmp/out.out
content.
My test code:
package main
import "testing"
func TestRunMain(t *testing.T) {
main()
}
My main code:
package main
import "time"
func main(){
time.Sleep(time.Second)
}
The full code is here:
go get github.com/ypapax/flags && cd $GOPATH/src/github.com/ypapax/flags
My question may seem weird but it may be helpful if a lot of functional tests already written which call flags.test
binary and it would be nice to avoid adding -test.coverprofile
to each flags.test
binary call.
I see 2 possible solutions:
- Set
-test.coverprofile
inside go code. How to do it? - Use bash to wrap up binary with a predefined flag. But in this case, there should not be extra files. It must be the only one
flags.test
with predefined-test.coverprofile
flag. How to do it?
I appreciate any advice. Thanks
Update
I am trying to run @dmitris answer
package main
import (
"io/ioutil"
"os/exec"
"log"
"fmt"
. "github.com/jteeuwen/go-bindata"
)
func main() {
prog, err := Asset("flags.test")
if err != nil {
panic(err)
}
dest := "/tmp/flags.test" // could also use https://golang.org/pkg/io/ioutil/#TempFile
coverProfile := "/tmp/flagstest.out"
err = ioutil.WriteFile(dest, prog, 0755)
if err != nil {
panic(err)
}
cmd := exec.Command(dest, "-test.coverprofile", coverProfile)
output, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("Output: %s, Error: %s", string(output), err)
}
fmt.Println(string(output))
fmt.Println("Coverprofile saved to ", coverProfile)
}
Not clear from where to import Asset
func on line prog, err := Asset("flags.test")
.
Running go build -o wrapper wrapper.go
gives me
./wrapper.go:13: cannot convert "flags.test" (type string) to type bindata.Asset
./wrapper.go:13: assignment count mismatch: 2 = 1