diff options
| author | Paul Buetow <paul@buetow.org> | 2025-09-03 17:04:47 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-09-03 17:04:47 +0300 |
| commit | 6f28807c15daff08da6222687bcd846c052f07c2 (patch) | |
| tree | c0823d1449b2bcf951003d51eb4fb0cd94605396 /Magefile.go | |
| parent | 4a2d1c60e6ac518d69bbb490db7f28ca791e49b1 (diff) | |
mage: write separate coverage artifacts: docs/cover(.out|.html) and docs/coverall(.out|.html)
Diffstat (limited to 'Magefile.go')
| -rw-r--r-- | Magefile.go | 104 |
1 files changed, 54 insertions, 50 deletions
diff --git a/Magefile.go b/Magefile.go index 2b9cb2b..d33eba4 100644 --- a/Magefile.go +++ b/Magefile.go @@ -88,65 +88,69 @@ func Test() error { // - Prints function coverage summary to stdout // - Writes HTML report to coverage.html func Cover() error { - // Ensure a clean slate - _ = os.Remove("docs/coverage.out") - _ = os.Remove("docs/coverage.html") + // Ensure a clean slate + const prof = "docs/cover.out" + const html = "docs/cover.html" + _ = os.Remove(prof) + _ = os.Remove(html) if err := sh.RunV("go", "clean", "-testcache"); err != nil { return err } - if err := sh.RunV("go", "test", "-covermode=atomic", "-coverprofile=docs/coverage.out", "./..."); err != nil { - return err - } - // Print function-by-function coverage summary - if out, err := sh.Output("go", "tool", "cover", "-func=docs/coverage.out"); err == nil { - fmt.Print(out) - lines := strings.Split(strings.TrimSpace(out), "\n") - for i := len(lines) - 1; i >= 0; i-- { - if strings.HasPrefix(strings.TrimSpace(lines[i]), "total:") { - fmt.Println("\nTotal coverage:", strings.TrimSpace(lines[i])) - break - } - } - } else { - return err - } - // Generate an HTML report for browsers/editors - if err := sh.RunV("go", "tool", "cover", "-html=docs/coverage.out", "-o", "docs/coverage.html"); err != nil { - return err - } - fmt.Println("HTML coverage report written to docs/coverage.html") - return nil + if err := sh.RunV("go", "test", "-covermode=atomic", "-coverprofile="+prof, "./..."); err != nil { + return err + } + // Print function-by-function coverage summary + if out, err := sh.Output("go", "tool", "cover", "-func="+prof); err == nil { + fmt.Print(out) + lines := strings.Split(strings.TrimSpace(out), "\n") + for i := len(lines) - 1; i >= 0; i-- { + if strings.HasPrefix(strings.TrimSpace(lines[i]), "total:") { + fmt.Println("\nTotal coverage:", strings.TrimSpace(lines[i])) + break + } + } + } else { + return err + } + // Generate an HTML report for browsers/editors + if err := sh.RunV("go", "tool", "cover", "-html="+prof, "-o", html); err != nil { + return err + } + fmt.Println("HTML coverage report written to "+html) + return nil } // CoverAll generates a combined coverage profile across all packages (cross-package coverage). // Instruments all packages during each test run using -coverpkg=./... so that // coverage collected from one package's tests include code executed in others. func CoverAll() error { - _ = os.Remove("docs/coverage.out") - _ = os.Remove("docs/coverage.html") - if err := sh.RunV("go", "clean", "-testcache"); err != nil { - return err - } - if err := sh.RunV("go", "test", "-covermode=atomic", "-coverpkg=./...", "-coverprofile=docs/coverage.out", "./..."); err != nil { - return err - } - if out, err := sh.Output("go", "tool", "cover", "-func=docs/coverage.out"); err == nil { - fmt.Print(out) - lines := strings.Split(strings.TrimSpace(out), "\n") - for i := len(lines) - 1; i >= 0; i-- { - if strings.HasPrefix(strings.TrimSpace(lines[i]), "total:") { - fmt.Println("\nTotal coverage (cross-package):", strings.TrimSpace(lines[i])) - break - } - } - } else { - return err - } - if err := sh.RunV("go", "tool", "cover", "-html=docs/coverage.out", "-o", "docs/coverage.html"); err != nil { - return err - } - fmt.Println("HTML coverage report written to docs/coverage.html (cross-package)") - return nil + const prof = "docs/coverall.out" + const html = "docs/coverall.html" + _ = os.Remove(prof) + _ = os.Remove(html) + if err := sh.RunV("go", "clean", "-testcache"); err != nil { + return err + } + if err := sh.RunV("go", "test", "-covermode=atomic", "-coverpkg=./...", "-coverprofile="+prof, "./..."); err != nil { + return err + } + if out, err := sh.Output("go", "tool", "cover", "-func="+prof); err == nil { + fmt.Print(out) + lines := strings.Split(strings.TrimSpace(out), "\n") + for i := len(lines) - 1; i >= 0; i-- { + if strings.HasPrefix(strings.TrimSpace(lines[i]), "total:") { + fmt.Println("\nTotal coverage (cross-package):", strings.TrimSpace(lines[i])) + break + } + } + } else { + return err + } + if err := sh.RunV("go", "tool", "cover", "-html="+prof, "-o", html); err != nil { + return err + } + fmt.Println("HTML coverage report written to "+html+" (cross-package)") + return nil } // Vet runs go vet. |
