Skip to content

Instantly share code, notes, and snippets.

@dlespiau
Last active May 28, 2017 19:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dlespiau/5224ee64f1ea401c00ccc852fc13afe1 to your computer and use it in GitHub Desktop.
Building coverage-instrumented programs with Go take 1
func TestMain(m *testing.M) {
// Parse the command line using the stdlib flag package so the flags
// defined in the testing package are populated. This include the
// -coverprofile flag.
flag.Parse()
// Strip command line arguments that were for the testing package and
// that are now handled. This will remove arguments that wouldn't be
// recognised when using a 3rd party command line parser like
// github.com/urfave/cli.
var programArgs []string
for _, arg := range os.Args {
if strings.HasPrefix(arg, "-test.") ||
strings.HasPrefix(arg, "-httptest.") {
continue
}
programArgs = append(programArgs, arg)
}
os.Args = programArgs
// If the test binary name is $program_name or $program_name.coverage,
// we are being asked to run the coverage-instrumented program. So call
// main() directly.
if path.Base(os.Args[0]) == "cc-runtime" ||
path.Base(os.Args[0]) == "cc-runtime.coverage" {
main()
return
}
// Run unit-tests as usual.
os.Exit(m.Run())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment