make early errors count towards code coverage

I recently added TODOs for bits of code we should cover in the tests.
I was looking at that again just now, and was puzzled;
we do indeed have test cases for many of them already.

We just weren't counting them towards code coverage due to a bug.
errJustExit works as expected, except that it calls os.Exit directly,
whereas testscript wants a non-zero return to run its "after" code.
Part of that code is what handles joining code coverage files.

The total code coverage jumps from 86.2% to 87.6%.
pull/506/head
Daniel Martí 3 years ago committed by lu4p
parent 91ea246349
commit 434de2e472

@ -237,7 +237,7 @@ func main1() int {
}
if err := mainErr(args); err != nil {
if code, ok := err.(errJustExit); ok {
os.Exit(int(code))
return int(code)
}
fmt.Fprintln(os.Stderr, err)
@ -270,14 +270,12 @@ func goVersionOK() bool {
// Go 1.15.x and older do not have GOVERSION yet.
// We could go the extra mile and fetch it via 'go version',
// but we'd have to error anyway.
// TODO: cover this in the tests.
fmt.Fprintf(os.Stderr, "Go version is too old; please upgrade to Go %s or a newer devel version\n", suggestedGoVersion)
return false
}
goVersionSemver = "v" + strings.TrimPrefix(version, "go")
if semver.Compare(goVersionSemver, minGoVersionSemver) < 0 {
// TODO: cover this in the tests.
fmt.Fprintf(os.Stderr, "Go version %q is too old; please upgrade to Go %s\n", version, suggestedGoVersion)
return false
}
@ -289,7 +287,6 @@ func mainErr(args []string) error {
// If we recognize an argument, we're not running within -toolexec.
switch command, args := args[0], args[1:]; command {
case "help":
// TODO: cover this in the tests.
if hasHelpFlag(args) || len(args) > 1 {
fmt.Fprintf(os.Stderr, "usage: garble help [command]\n")
return errJustExit(2)
@ -301,7 +298,6 @@ func mainErr(args []string) error {
return errJustExit(2)
case "version":
if hasHelpFlag(args) || len(args) > 0 {
// TODO: cover this in the tests.
fmt.Fprintf(os.Stderr, "usage: garble version\n")
return errJustExit(2)
}
@ -398,7 +394,6 @@ func toolexecCmd(command string, args []string) (*exec.Cmd, error) {
// to run 'go list' on the same set of packages.
flags, args := splitFlagsFromArgs(args)
if hasHelpFlag(flags) {
// TODO: cover this in the tests.
out, _ := exec.Command("go", command, "-h").CombinedOutput()
fmt.Fprintf(os.Stderr, `
usage: garble [garble flags] %s [arguments]

@ -83,6 +83,8 @@ func TestScripts(t *testing.T) {
// Don't reuse the build cache if we want to collect
// code coverage. Otherwise, many toolexec calls would
// be avoided and the coverage would be incomplete.
// TODO: to not make "go test" insanely slow, we could still use
// an empty GOCACHE, but share it between all the test scripts.
env.Vars = append(env.Vars, "GOCACHE="+filepath.Join(env.WorkDir, "go-cache-tmp"))
}
return nil

@ -20,7 +20,6 @@ import (
func commandReverse(args []string) error {
flags, args := splitFlagsFromArgs(args)
if hasHelpFlag(flags) || len(args) == 0 {
// TODO: cover this in the tests.
fmt.Fprintf(os.Stderr, `
usage: garble [garble flags] reverse [build flags] package [files]

Loading…
Cancel
Save