Last active
May 28, 2017 19:04
Star
You must be signed in to star a gist
Building coverage-instrumented programs with Go take 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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