forked from sguiheux/go-coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coverage.go
170 lines (153 loc) · 4.65 KB
/
coverage.go
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package coverage
import (
"bufio"
"encoding/xml"
"fmt"
"io"
"os"
"strconv"
"strings"
)
// New creates a new lcov parser
func New(mode CoverageMode) Parser {
return Parser{
mode: mode,
}
}
// Parse parses the lcov file
func (l Parser) Parse(path string) (Report, error) {
reader, err := os.Open(path)
if err != nil {
return Report{}, fmt.Errorf("coverage.Parse> Unable to open file: %v", err)
}
defer reader.Close()
return l.ParseReader(reader)
}
func (l Parser) ParseReader(reader io.Reader) (Report, error) {
switch l.mode {
case LCOV:
return l.processLcov(reader)
case COBERTURA:
return l.processCobertura(reader)
case CLOVER:
return l.processClover(reader)
}
return Report{}, fmt.Errorf("coverage.Parse> Unknown mode %s", l.mode)
}
func (l Parser) processClover(reader io.Reader) (Report, error) {
b, errR := io.ReadAll(reader)
if errR != nil {
return Report{}, fmt.Errorf("coverage.processClover> Unable to read file: %v", errR)
}
var cloReport CloverCoverage
if err := xml.Unmarshal(b, &cloReport); err != nil {
return Report{}, fmt.Errorf("coverage.processClover> Unable to unmarshal content: %v", err)
}
report := Report{
CoveredBranches: int(cloReport.Project.Metrics.CoveredConditionals),
CoveredFunctions: int(cloReport.Project.Metrics.CoveredMethods),
CoveredLines: int(cloReport.Project.Metrics.CoveredStatements),
TotalBranches: int(cloReport.Project.Metrics.Conditionnals),
TotalFunctions: int(cloReport.Project.Metrics.Methods),
TotalLines: int(cloReport.Project.Metrics.Statements),
Files: make([]FileReport, 0, cloReport.Project.Metrics.Files),
}
for _, p := range cloReport.Project.Package {
for _, f := range p.File {
fp := FileReport{
TotalLines: int(f.Metrics.Statements),
TotalFunctions: int(f.Metrics.Methods),
TotalBranches: int(f.Metrics.Conditionnals),
CoveredLines: int(f.Metrics.CoveredStatements),
CoveredFunctions: int(f.Metrics.CoveredMethods),
CoveredBranches: int(f.Metrics.CoveredConditionals),
Path: f.Path,
}
report.Files = append(report.Files, fp)
}
}
return report, nil
}
func (l Parser) processCobertura(reader io.Reader) (Report, error) {
b, errR := io.ReadAll(reader)
if errR != nil {
return Report{}, fmt.Errorf("coverage.processCobertura> Unable to read file: %v", errR)
}
var cobReport CoberturaCoverage
if err := xml.Unmarshal(b, &cobReport); err != nil {
return Report{}, fmt.Errorf("coverage.processCobertura> Unable to unmarshal content: %v", err)
}
report := Report{
TotalLines: getInt(cobReport.LinesValid),
TotalBranches: getInt(cobReport.BranchesValid),
CoveredLines: getInt(cobReport.LinesCovered),
CoveredBranches: getInt(cobReport.BranchesCovered),
}
return report, nil
}
func (l Parser) processLcov(reader io.Reader) (Report, error) {
r := bufio.NewReader(reader)
report := Report{
Files: make([]FileReport, 0),
}
fileReport := FileReport{}
for {
line, errR := r.ReadString('\n')
if errR != nil && errR != io.EOF {
return report, fmt.Errorf("coverage.processLcov> Unable to read line: %v", errR)
}
if errR == io.EOF {
break
}
line = strings.Replace(line, "\n", "", -1)
// Test new file
if strings.HasPrefix(line, "SF:") {
if fileReport.Path != "" {
report.Files = append(report.Files, fileReport)
}
fileReport = FileReport{
Path: strings.Replace(line, "SF:", "", 1),
}
} else {
l.processLcovLine(line, &report, &fileReport)
}
}
if fileReport.Path != "" {
// Add last file
report.Files = append(report.Files, fileReport)
}
return report, nil
}
func (l Parser) processLcovLine(line string, report *Report, fileReport *FileReport) {
switch {
case strings.HasPrefix(line, "FNF:"):
nb := getInt(strings.Replace(line, "FNF:", "", -1))
fileReport.TotalFunctions = nb
report.TotalFunctions += nb
case strings.HasPrefix(line, "FNH:"):
nb := getInt(strings.Replace(line, "FNH:", "", -1))
fileReport.CoveredFunctions = nb
report.CoveredFunctions += nb
case strings.HasPrefix(line, "BRF:"):
nb := getInt(strings.Replace(line, "BRF:", "", -1))
fileReport.TotalBranches = nb
report.TotalBranches += nb
case strings.HasPrefix(line, "BRH:"):
nb := getInt(strings.Replace(line, "BRH:", "", -1))
fileReport.CoveredBranches = nb
report.CoveredBranches += nb
case strings.HasPrefix(line, "LF:"):
nb := getInt(strings.Replace(line, "LF:", "", -1))
fileReport.TotalLines = nb
report.TotalLines += nb
case strings.HasPrefix(line, "LH:"):
nb := getInt(strings.Replace(line, "LH:", "", -1))
fileReport.CoveredLines = nb
report.CoveredLines += nb
}
}
func getInt(s string) int {
i := 0
i, _ = strconv.Atoi(s)
return i
}