-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.go
More file actions
94 lines (78 loc) · 2.34 KB
/
report.go
File metadata and controls
94 lines (78 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package cli
import (
"context"
"fmt"
"os"
"github.com/cybertec-postgresql/pgcov/internal/coverage"
"github.com/cybertec-postgresql/pgcov/internal/report"
)
// Report generates a coverage report from saved coverage data
func Report(_ context.Context, coverageFile string, format string, outputPath string) error {
// Step 1: Load coverage data
store := coverage.NewStore(coverageFile)
if !store.Exists() {
return fmt.Errorf("coverage file not found: %s (run 'pgcov run' first)", coverageFile)
}
cov, err := store.Load()
if err != nil {
return fmt.Errorf("failed to load coverage data: %w", err)
}
// Step 2: Validate format
if !report.ValidFormat(format) {
return fmt.Errorf("unsupported format: %s (supported: %v)", format, report.SupportedFormats())
}
// Step 3: Get formatter
formatter, err := report.GetFormatter(report.FormatType(format))
if err != nil {
return err
}
// Step 4: Format and output
var writer *os.File
if outputPath == "-" || outputPath == "" {
// Write to stdout
writer = os.Stdout
} else {
// Write to file
writer, err = os.Create(outputPath)
if err != nil {
return fmt.Errorf("failed to create output file: %w", err)
}
defer writer.Close()
}
// Format coverage data
if err := formatter.Format(cov, writer); err != nil {
return fmt.Errorf("failed to format coverage data: %w", err)
}
// Print success message to stderr (so it doesn't interfere with stdout output)
if outputPath != "-" && outputPath != "" {
fmt.Fprintf(os.Stderr, "Report written to %s\n", outputPath)
}
return nil
}
// ReportSummary prints a human-readable summary of coverage
func ReportSummary(coverageFile string) error {
store := coverage.NewStore(coverageFile)
if !store.Exists() {
return fmt.Errorf("coverage file not found: %s", coverageFile)
}
cov, err := store.Load()
if err != nil {
return fmt.Errorf("failed to load coverage data: %w", err)
}
// Print overall coverage
fmt.Printf("Overall Coverage: %.2f%%\n\n", cov.TotalPositionCoveragePercent())
// Print per-file coverage
fmt.Println("File Coverage:")
for file, posHits := range cov.Positions {
covered := 0
for _, count := range posHits {
if count > 0 {
covered++
}
}
total := len(posHits)
percent := cov.PositionCoveragePercent(file)
fmt.Printf(" %s: %.2f%% (%d/%d positions)\n", file, percent, covered, total)
}
return nil
}