Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

magefile: added combined test coverage support for mage test:all #2235

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions magefiles/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,29 @@ type Test mg.Namespace

var emptyEnv map[string]string

// All Runs all test suites
// All Runs all test suites and generates a combined coverage report
func (t Test) All() error {
ds := Testds{}
c := Testcons{}
mg.Deps(t.Unit, t.Integration, t.Steelthread, t.Image, t.Analyzers,
ds.Crdb, ds.Postgres, ds.Spanner, ds.Mysql,
c.Crdb, c.Spanner, c.Postgres, c.Mysql)
return nil
return combineCoverage()
}

// UnitCover Runs the unit tests and generates a coverage report
func (t Test) UnitCover() error {
if err := t.unit(true); err != nil {
if err := t.Unit(); err != nil {
return err
}
fmt.Println("Running coverage...")
return sh.RunV("go", "tool", "cover", "-html=coverage.txt")
return sh.RunV("go", "tool", "cover", "-html=coverage-unit.txt")
}

// Unit Runs the unit tests
func (t Test) Unit() error {
return t.unit(false)
}

func (Test) unit(coverage bool) error {
fmt.Println("running unit tests")
args := []string{"-tags", "ci,skipintegrationtests", "-race", "-timeout", "10m", "-count=1"}
if coverage {
args = append(args, "-covermode=atomic", "-coverprofile=coverage.txt")
}
return goTest("./...", args...)
}

Expand Down
59 changes: 57 additions & 2 deletions magefiles/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/magefile/mage/mg"
Expand All @@ -21,12 +22,24 @@ func goTest(path string, args ...string) error {

// run go test in a directory
func goDirTest(dir string, path string, args ...string) error {
testArgs := append([]string{"test", "-failfast", "-count=1"}, args...)
testArgs := append([]string{
"test",
"-covermode=atomic",
fmt.Sprintf("-coverprofile=coverage-%s.txt", sanitizePath(path)),
"-failfast",
"-count=1",
}, args...)
return RunSh(goCmdForTests(), WithV(), WithDir(dir), WithArgs(testArgs...))(path)
}

func goDirTestWithEnv(dir string, path string, env map[string]string, args ...string) error {
testArgs := append([]string{"test", "-failfast", "-count=1"}, args...)
testArgs := append([]string{
"test",
"-covermode=atomic",
fmt.Sprintf("-coverprofile=coverage-%s.txt", sanitizePath(path)),
"-failfast",
"-count=1",
}, args...)
return RunSh(goCmdForTests(), WithV(), WithDir(dir), WithEnv(env), WithArgs(testArgs...))(path)
}

Expand Down Expand Up @@ -210,3 +223,45 @@ func run(dir string, env map[string]string, stdout, stderr io.Writer, cmd string
err = c.Run()
return sh.CmdRan(err), sh.ExitStatus(err), err
}

func sanitizePath(path string) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend hashing the path here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

parts := strings.Split(strings.TrimPrefix(path, "./"), "/")
for i := len(parts) - 1; i >= 0; i-- {
if parts[i] != "..." && parts[i] != "" {
return strings.ReplaceAll(parts[i], "/", "-")
}
}
return "all"
}

func combineCoverage() error {
files, err := filepath.Glob("coverage-*.txt")
if err != nil {
return err
}
if len(files) == 0 {
return fmt.Errorf("no coverage files found")
}

f, err := os.Create("coverage.txt")
if err != nil {
return err
}
defer f.Close()

args := []string{"run", "github.com/wadey/gocovmerge@latest"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use go tool covdata merge, see https://go.dev/doc/build-cover

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems we need to build a binary for this for specifying the -cover flag before the tests which would generate the files(tests should be carried on the same binary?)

args = append(args, files...)

err = RunSh(goCmdForTests(), WithV(), WithStdout(f))(args...)
if err != nil {
return err
}

for _, file := range files {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why deleting the tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was for deleting individual coverage files after merging them. reverting

if err := os.Remove(file); err != nil {
return err
}
}

return nil
}