-
Notifications
You must be signed in to change notification settings - Fork 291
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
80503bb
64f1377
3c14817
59fd80d
3f62b00
752a5ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"log" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/magefile/mage/mg" | ||
|
@@ -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) | ||
} | ||
|
||
|
@@ -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 { | ||
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"} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
args = append(args, files...) | ||
|
||
err = RunSh(goCmdForTests(), WithV(), WithStdout(f))(args...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, file := range files { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why deleting the tests? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done