11package testcoverage
22
33import (
4- "bufio"
5- "fmt"
6- "io"
7- "os"
84 "strings"
9- "text/tabwriter"
105)
116
12- //nolint:wsl // relax
13- func Analyze (cfg Config , coverageStats []CoverageStats ) bool {
14- thr := cfg .Threshold
7+ type AnalyzeResult struct {
8+ FilesBelowThreshold []CoverageStats
9+ PackagesBelowThreshold []CoverageStats
10+ MeetsTotalCoverage bool
11+ TotalCoverage int
12+ }
13+
14+ func (r * AnalyzeResult ) Pass () bool {
15+ return r .MeetsTotalCoverage &&
16+ len (r .FilesBelowThreshold ) == 0 &&
17+ len (r .PackagesBelowThreshold ) == 0
18+ }
1519
16- out := bufio . NewWriter ( os . Stdout )
17- defer out . Flush ()
20+ func Analyze ( cfg Config , coverageStats [] CoverageStats ) AnalyzeResult {
21+ thr := cfg . Threshold
1822
1923 filesBelowThreshold := checkCoverageStatsBelowThreshold (coverageStats , thr .File )
2024 packagesBelowThreshold := checkCoverageStatsBelowThreshold (
@@ -23,44 +27,23 @@ func Analyze(cfg Config, coverageStats []CoverageStats) bool {
2327 totalStats := calcTotalStats (coverageStats )
2428 meetsTotalCoverage := totalStats .CoveredPercentage () >= thr .Total
2529
26- fmt .Fprintf (out , "Files test coverage meeting the threshold\t (%d%%): " , thr .File )
27- if len (filesBelowThreshold ) > 0 {
28- fmt .Fprintf (out , "FAIL" )
29- report (out , filesBelowThreshold , cfg .LocalPrefix )
30- } else {
31- fmt .Fprintf (out , "PASS" )
30+ localPrefix := cfg .LocalPrefix
31+ if localPrefix != "" && (strings .LastIndex (localPrefix , "/" ) != len (localPrefix )- 1 ) {
32+ localPrefix += "/"
3233 }
3334
34- fmt .Fprintf (out , "\n Packages test coverage meeting the threshold\t (%d%%): " , thr .Package )
35- if len (packagesBelowThreshold ) > 0 {
36- fmt .Fprintf (out , "FAIL" )
37- report (out , packagesBelowThreshold , cfg .LocalPrefix )
38- } else {
39- fmt .Fprintf (out , "PASS" )
35+ return AnalyzeResult {
36+ FilesBelowThreshold : stripLocalPrefix (filesBelowThreshold , localPrefix ),
37+ PackagesBelowThreshold : stripLocalPrefix (packagesBelowThreshold , localPrefix ),
38+ MeetsTotalCoverage : meetsTotalCoverage ,
39+ TotalCoverage : totalStats .CoveredPercentage (),
4040 }
41-
42- fmt .Fprintf (out , "\n Total test coverage meeting the threshold\t (%d%%): " , thr .Total )
43- if ! meetsTotalCoverage {
44- fmt .Fprintf (out , "FAIL" )
45- } else {
46- fmt .Fprintf (out , "PASS" )
47- }
48-
49- fmt .Fprintf (out , "\n Total test coverage: %d%%\n " , totalStats .CoveredPercentage ())
50-
51- return len (filesBelowThreshold ) == 0 && len (packagesBelowThreshold ) == 0 && meetsTotalCoverage
5241}
5342
54- func report (w io.Writer , coverageStats []CoverageStats , localPrefix string ) {
55- localPrefix += "/"
56-
57- tabber := tabwriter .NewWriter (w , 1 , 8 , 1 , '\t' , 0 ) //nolint:gomnd // relax
58- defer tabber .Flush ()
59-
60- for _ , stats := range coverageStats {
61- name := strings .Replace (stats .name , localPrefix , "" , 1 )
62- fmt .Fprintf (tabber , "\n %s\t %d%%" , name , stats .CoveredPercentage ())
43+ func stripLocalPrefix (coverageStats []CoverageStats , localPrefix string ) []CoverageStats {
44+ for i , stats := range coverageStats {
45+ coverageStats [i ].name = strings .Replace (stats .name , localPrefix , "" , 1 )
6346 }
6447
65- fmt . Fprintf ( tabber , " \n " )
48+ return coverageStats
6649}
0 commit comments