Skip to content

Commit

Permalink
different profiles for tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kartikay <[email protected]>
  • Loading branch information
kartikaysaxena committed Feb 5, 2025
1 parent 80503bb commit 64f1377
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
4 changes: 2 additions & 2 deletions magefiles/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (t Test) All() error {
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
Expand All @@ -31,7 +31,7 @@ func (t Test) UnitCover() error {
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
Expand Down
47 changes: 45 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 @@ -24,7 +25,7 @@ func goDirTest(dir string, path string, args ...string) error {
testArgs := append([]string{
"test",
"-covermode=atomic",
"-coverprofile=coverage.txt",
fmt.Sprintf("-coverprofile=coverage-%s.txt", sanitizePath(path)),
"-failfast",
"-count=1",
}, args...)
Expand All @@ -35,7 +36,7 @@ func goDirTestWithEnv(dir string, path string, env map[string]string, args ...st
testArgs := append([]string{
"test",
"-covermode=atomic",
"-coverprofile=coverage.txt",
fmt.Sprintf("-coverprofile=coverage-%s.txt", sanitizePath(path)),
"-failfast",
"-count=1",
}, args...)
Expand Down Expand Up @@ -222,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 {
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"}
args = append(args, files...)

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

for _, file := range files {
if err := os.Remove(file); err != nil {
return err
}
}

return nil
}

0 comments on commit 64f1377

Please sign in to comment.