/
coverage.go
81 lines (64 loc) · 1.99 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
package coverage
import (
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/sirupsen/logrus"
"github.com/Liu-Chunhui/line-coverage/pkg/fileparser"
)
func Calculate(profileFilename string, module string, basePath string) ([]*Result, error) {
logrus.Info(fmt.Sprintf("Processing profile file: %s", profileFilename))
// scan coverage.out to build []*coverageProfile
profiles, err := loadProfiles(profileFilename, module, basePath)
if err != nil {
return nil, err
}
logrus.Info("Building profile targets map")
// map key: code filename
maps := make(map[string][]*coverageProfile)
// loop profiles to build map[string]*coverageProfile
for _, profile := range profiles {
if values, ok := maps[profile.TargetFilename]; ok {
maps[profile.TargetFilename] = append(values, profile)
} else {
maps[profile.TargetFilename] = []*coverageProfile{profile}
}
}
logrus.Info("Building target branches map")
// code in lines
targetBranches := make(map[string][]*branch)
// loop map
for filename, coverageProfiles := range maps {
codeInLines, err := fileparser.ReadLines(filename)
if err != nil {
return nil, err
}
for _, p := range coverageProfiles {
logrus.Debug(spew.Sprintln("coverageProfile: %+v", p))
target, branches := convertProfileToBranch(p, codeInLines)
if values, ok := targetBranches[target]; ok {
targetBranches[target] = append(values, branches...)
} else {
targetBranches[target] = branches
}
}
}
logrus.Info("Building results")
var results []*Result
for target, branches := range targetBranches {
r, err := calculateTargetResult(target, branches)
if err != nil {
return nil, err
}
results = append(results, r)
}
return results, nil
}
func CalculateOverall(results []*Result) float64 {
totalCoveredLines := 0
totalUncoveredLines := 0
for _, r := range results {
totalCoveredLines += r.CoveredLines
totalUncoveredLines += r.UncoveredLines
}
return float64(totalCoveredLines) / float64(totalCoveredLines+totalUncoveredLines)
}