/
fullcover.go
220 lines (180 loc) · 5.76 KB
/
fullcover.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package gocover
import (
"context"
"fmt"
"go/build"
"path/filepath"
"strings"
"github.com/Azure/gocover/pkg/annotation"
"github.com/Azure/gocover/pkg/dbclient"
"github.com/Azure/gocover/pkg/parser"
"github.com/Azure/gocover/pkg/report"
"github.com/sirupsen/logrus"
)
func NewFullCover(o *FullOption) (GoCover, error) {
var (
dbClient dbclient.DbClient
err error
)
logger := o.Logger
if logger == nil {
logger = logrus.New()
}
logger = logger.WithField("source", "fullcover")
if o.DbOption.DataCollectionEnabled {
dbClient, err = o.DbOption.GetDbClient(o.Logger)
if err != nil {
return nil, fmt.Errorf("get db client: %w", err)
}
}
repositoryAbsPath, err := filepath.Abs(o.RepositoryPath)
if err != nil {
return nil, fmt.Errorf("get absolute path of repo: %w", err)
}
modulePath, err := parseGoModulePath(filepath.Join(repositoryAbsPath, o.ModuleDir))
if err != nil {
return nil, fmt.Errorf("parse go module path: %w", err)
}
logger.Debugf("repository path: %s, module path: %s, output dir: %s, exclude patterns: %s",
repositoryAbsPath, modulePath, o.OutputDir, o.Excludes)
return &fullCover{
coverFilenames: o.CoverProfiles,
modulePath: modulePath,
repositoryPath: repositoryAbsPath,
excludeFiles: make(excludeFileCache),
excludePatterns: o.Excludes,
moduleDir: o.ModuleDir,
coverageTree: report.NewCoverageTree(modulePath),
logger: logger,
dbClient: dbClient,
reportGenerator: report.NewReportGenerator(o.Style, o.OutputDir, o.ReportName, o.Logger),
}, nil
}
var _ GoCover = (*fullCover)(nil)
// diffCoverage implements the GoCover interface and generate the full coverage statistics.
type fullCover struct {
coverFilenames []string
moduleDir string
modulePath string
repositoryPath string
excludePatterns []string
ignoreProfiles []*annotation.IgnoreProfile
excludeFiles excludeFileCache
coverageTree report.CoverageTree
reportGenerator report.ReportGenerator
dbClient dbclient.DbClient
logger logrus.FieldLogger
}
func (full *fullCover) Run(ctx context.Context) error {
statistics, err := full.generateStatistics()
if err != nil {
return fmt.Errorf("full: %w", err)
}
if err := full.reportGenerator.GenerateReport(statistics); err != nil {
return fmt.Errorf("generate report: %w", err)
}
if err := full.dump(ctx); err != nil {
return fmt.Errorf("%w", err)
}
return nil
}
func (full *fullCover) dump(ctx context.Context) error {
all := full.coverageTree.All()
if full.dbClient != nil {
err := storeCoverageData(ctx, full.dbClient, all, FullCoverage, full.modulePath)
if err != nil {
return fmt.Errorf("store coverage data: %w", err)
}
err = storeIgnoreProfileData(ctx, full.dbClient, full.ignoreProfiles, FullCoverage, full.modulePath, full.repositoryPath, full.moduleDir)
if err != nil {
return fmt.Errorf("store ignore profile data: %w", err)
}
}
dump(all, full.logger)
return nil
}
func (full *fullCover) generateStatistics() (*report.Statistics, error) {
packages, err := parser.NewParser(full.coverFilenames, full.logger).Parse(nil)
if err != nil {
return nil, err
}
statistics := &report.Statistics{
StatisticsType: report.FullStatisticsType,
}
m := make(map[string]*report.CoverageProfile)
fileCache := make(fileContentsCache)
for _, pkg := range packages {
full.logger.Debugf("package: %s", pkg.Name)
full.ignoreProfiles = append(full.ignoreProfiles, pkg.IgnoreProfiles...)
p, err := build.Import(pkg.Name, ".", build.FindOnly)
if err != nil {
return nil, fmt.Errorf("build import %w", err)
}
for _, fun := range pkg.Functions {
if ok := inExclueds(
full.excludeFiles,
full.excludePatterns,
formatFilePath(p.Root, fun.File, full.modulePath),
full.logger,
); ok {
continue
}
// extract into single function
coverProfile, ok := m[fun.File]
if !ok {
coverProfile = &report.CoverageProfile{
FileName: formatFilePath(p.Root, fun.File, full.modulePath),
}
m[fun.File] = coverProfile
statistics.CoverageProfile = append(statistics.CoverageProfile, coverProfile)
}
fileContents, err := findFileContents(fileCache, fun.File)
if err != nil {
return nil, fmt.Errorf("find file contents: %w", err)
}
section := &report.ViolationSection{
StartLine: fun.StartLine,
EndLine: fun.EndLine,
}
for i := fun.StartLine; i <= fun.EndLine; i++ {
section.Contents = append(section.Contents, fileContents[i-1])
}
node := full.coverageTree.FindOrCreate(strings.TrimPrefix(fun.File, p.Root))
var total, ignored, covered, coveredButIgnored int
violated := false
for _, st := range fun.Statements {
total += 1
node.TotalLines += 1
if st.Mode == parser.Ignore && st.Reached > 0 {
coveredButIgnored++
node.TotalCoveredButIgnoreLines += 1
}
if st.Mode == parser.Ignore {
full.logger.Debugf("%s ignore line %d", fun.File, st.StartLine)
ignored++
node.TotalIgnoredLines += 1
}
if st.Reached > 0 {
node.TotalCoveredLines += 1
covered++
} else {
section.ViolationLines = append(section.ViolationLines, st.StartLine)
violated = true
}
}
node.TotalEffectiveLines = node.TotalLines - node.TotalIgnoredLines
coverProfile.TotalLines += total
coverProfile.CoveredLines += covered
coverProfile.CoveredButIgnoredLines += coveredButIgnored
coverProfile.TotalEffectiveLines += (total - ignored)
coverProfile.TotalIgnoredLines += ignored
coverProfile.TotalViolationLines = append(coverProfile.TotalViolationLines, section.ViolationLines...)
if violated {
coverProfile.ViolationSections = append(coverProfile.ViolationSections, section)
}
}
}
full.coverageTree.CollectCoverageData()
reBuildStatistics(statistics, full.excludeFiles)
return statistics, nil
}