-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mockgen): record code coverage for test scripts
Prior to this change, the test scripts (under ./cmd/mockgen) would run the command using go-run, which builds the mockgen binary and then runs it. This meant that no code coverage was collected for these tests. This change introduces a custom TestMain function that builds the binary with the -cover option (new in go 1.20) which produces coverage data in a binary format. The go-test command can also produce data in the same format when given the -test.gocoverdir option. When the ./cmd/mockgen tests find this option, coverage for the script tests is enabled. This allows us to then combine the coverage from the tests across all packages. At this time, the go-test command doesn't appear to be able to do this automatically and the coverage reported is inaccurate. To generate the combined coverage use the covdata tool after running go-test: mkdir "$PWD/covdata" go test -cover ./... -test.gocoverdir="$PWD/covdata" go tool covdata percent -i="$PWD/covdata" The same tool can also convert to the old text format: go tool covdata textfmt -o cover.out -i="$PWD/covdata" See https://go.dev/doc/build-cover
- Loading branch information
1 parent
b75a1e6
commit da222c7
Showing
22 changed files
with
88 additions
and
38 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,78 @@ | ||
package main_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"rsc.io/script" | ||
"rsc.io/script/scripttest" | ||
) | ||
|
||
func TestMain(t *testing.T) { | ||
var env []string | ||
|
||
func TestMockgen(t *testing.T) { | ||
ctx, eng := context.Background(), script.NewEngine() | ||
scripttest.Test(t, ctx, eng, env, "testdata/*.txt") | ||
} | ||
|
||
func TestMain(m *testing.M) { | ||
// Temporary directory for mockgen binary. | ||
bindir, err := os.MkdirTemp("", "mockgen") | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
defer os.RemoveAll(bindir) | ||
|
||
// The Module Under Test (MUT) directory. | ||
mutdir, err := os.Getwd() | ||
if err != nil { | ||
t.Fatal(err) | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
mutdir = filepath.Join(mutdir, "..", "..") | ||
scripttest.Test( | ||
t, | ||
context.Background(), | ||
script.NewEngine(), | ||
[]string{ | ||
"PATH=" + os.Getenv("PATH"), | ||
"HOME=" + os.Getenv("HOME"), | ||
"MUT=" + mutdir, | ||
}, | ||
"testdata/*.txt", | ||
) | ||
|
||
// When running with -test.gocoverdir, forward this setting to each | ||
// test script. | ||
fset := flag.NewFlagSet("mockgen", flag.ContinueOnError) | ||
fset.SetOutput(&bytes.Buffer{}) // ignore errors | ||
covdir := fset.String("test.gocoverdir", "", "write coverage intermediate files to this directory") | ||
err = fset.Parse(os.Args[1:]) | ||
for err != nil { | ||
err = fset.Parse(fset.Args()) | ||
} | ||
|
||
env = []string{ | ||
"PATH=" + strings.Join([]string{bindir, os.Getenv("PATH")}, ":"), | ||
"HOME=" + os.Getenv("HOME"), | ||
"MUT=" + mutdir, | ||
"BINDIR=" + bindir, | ||
} | ||
if *covdir != "" { | ||
env = append(env, "GOCOVERDIR="+*covdir) | ||
} | ||
|
||
run(env, "testdata/setup.sh") | ||
m.Run() | ||
if *covdir != "" { | ||
run(env, "testdata/report.sh") | ||
} | ||
} | ||
|
||
func run(env []string, file string) { | ||
cmd := exec.Command("bash", file) | ||
cmd.Env = env | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
if err := cmd.Run(); err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
go tool covdata percent -i=$GOCOVERDIR |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exec go build ${GOCOVERDIR:+-cover} -o $BINDIR/mockgen . |
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