Skip to content

Commit

Permalink
improve variable naming & add coments
Browse files Browse the repository at this point in the history
  • Loading branch information
CubicrootXYZ committed Sep 15, 2024
1 parent 9f8c833 commit b197cbf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions gotesplit.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Options:
total := fs.Uint("total", 1, "total number of test splits (CIRCLE_NODE_TOTAL is used if set)")
index := fs.Uint("index", 0, "zero-based index number of test splits (CIRCLE_NODE_INDEX is used if set)")
junitDir := fs.String("junit-dir", "", "directory to store test result in JUnit format")
coverageDir := fs.String("coverprofile-dir", ".cover", "directory to store granular coverprofiles")
coverprofileDir := fs.String("coverprofile-dir", ".cover", "temporary directory for collecting coverprofile")
fs.VisitAll(func(f *flag.Flag) {
if f.Name == "index" || f.Name == "total" {
if s := os.Getenv("CIRCLE_NODE_" + strings.ToUpper(f.Name)); s != "" {
Expand All @@ -58,7 +58,7 @@ Options:
return rnr.run(ctx, argv[1:], outStream, errStream)
}
}
return run(ctx, *total, *index, *junitDir, *coverageDir, argv, outStream, errStream)
return run(ctx, *total, *index, *junitDir, *coverprofileDir, argv, outStream, errStream)
}

func getTestListsFromPkgs(pkgs []string, tags string, withRace bool) ([]testList, error) {
Expand Down
28 changes: 18 additions & 10 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"golang.org/x/sync/errgroup"
)

func run(_ context.Context, total, idx uint, junitDir string, coverageDir string, argv []string, outStream io.Writer, errStream io.Writer) error {
func run(_ context.Context, total, idx uint, junitDir string, coverprofilesDir string, argv []string, outStream io.Writer, errStream io.Writer) error {
if idx >= total {
return fmt.Errorf("`index` should be the range from 0 to `total`-1, but: %d (total:%d)", idx, total)
}
Expand Down Expand Up @@ -112,10 +112,12 @@ func run(_ context.Context, total, idx uint, junitDir string, coverageDir string
}
}

coverprofileOut := ""
// Check if coverprofile flag is set. If so remove it from args and replace it
// later with separate coverprofile files for each `go test` run.
coverprofileFile := ""
for i := range testOpts {
if strings.HasPrefix(testOpts[i], "-coverprofile=") {
coverprofileOut = strings.TrimPrefix(testOpts[i], "-coverprofile=")
coverprofileFile = strings.TrimPrefix(testOpts[i], "-coverprofile=")

if i == len(testOpts)-1 {
testOpts = testOpts[:i]
Expand All @@ -125,8 +127,9 @@ func run(_ context.Context, total, idx uint, junitDir string, coverageDir string
break
}
}
if coverprofileOut != "" {
if err := os.MkdirAll(coverageDir, 0755); err != nil {
if coverprofileFile != "" {
// Make temporary directory to store single coverprofile files in.
if err := os.MkdirAll(coverprofilesDir, 0755); err != nil {
return err
}
}
Expand All @@ -146,8 +149,9 @@ func run(_ context.Context, total, idx uint, junitDir string, coverageDir string
}

for i, args := range testArgsList {
if coverprofileOut != "" {
args = append(args, fmt.Sprintf("-coverprofile=%s/coverprofile_%d", coverageDir, i))
if coverprofileFile != "" {
// Write coverprofiles to temp folder.
args = append(args, fmt.Sprintf("-coverprofile=%s/coverprofile_%d", coverprofilesDir, i))
}

report := goTest(args, outStream, errStream, junitDir)
Expand Down Expand Up @@ -175,13 +179,15 @@ func run(_ context.Context, total, idx uint, junitDir string, coverageDir string
return err
}

if coverprofileOut != "" {
err = mergeCoverprofiles(coverageDir, coverprofileOut)
if coverprofileFile != "" {
// Merge single coverprofiles to one file.
err = mergeCoverprofiles(coverprofilesDir, coverprofileFile)
if err != nil {
return err
}

err = os.RemoveAll(coverageDir)
// Remove temp directory.
err = os.RemoveAll(coverprofilesDir)
if err != nil {
return err
}
Expand All @@ -205,6 +211,8 @@ func mergeCoverprofiles(dir string, coverprofileOut string) error {
}

if i != 0 {
// Cover mode is set in first line, remove it from all the following
// files.
content = modeRegex.ReplaceAll(content, []byte{})
}
mergedContent = append(mergedContent, content...)
Expand Down

0 comments on commit b197cbf

Please sign in to comment.