-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
221 lines (188 loc) · 4.89 KB
/
Copy pathmain.go
File metadata and controls
221 lines (188 loc) · 4.89 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
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
221
// Package main is the main entry point for the program.
package main
import (
"errors"
"fmt"
"io"
"os"
"sort"
"golang.org/x/tools/cover"
)
var errHelp = errors.New(`usage: gocovdedup [<file1> <file2> ... <fileN>|-]
files must be in go cover format or if '-' is supplied then read from stdin`)
func processArgs(args []string, stdIn io.Reader) ([]*cover.Profile, error) {
var profiles []*cover.Profile
switch len(args) {
case 1:
return nil, errHelp
default:
var files []string
readStdin := false
for _, arg := range args[1:] {
if arg == "-" {
readStdin = true
} else {
files = append(files, arg)
}
}
if readStdin {
stdInProfiles, err := cover.ParseProfilesFromReader(stdIn)
if err != nil {
return nil, err
}
profiles = append(profiles, stdInProfiles...)
}
fileProfiles, err := loadProfilesForFiles(files)
if err != nil {
return nil, err
}
profiles = append(profiles, fileProfiles...)
}
return profiles, nil
}
func loadProfilesForFiles(files []string) ([]*cover.Profile, error) {
profiles := []*cover.Profile{}
for _, file := range files {
profile, err := cover.ParseProfiles(file)
if err != nil {
return nil, err
}
profiles = append(profiles, profile...)
}
return profiles, nil
}
type orderedBlocks []cover.ProfileBlock
func (b orderedBlocks) Len() int { return len(b) }
func (b orderedBlocks) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b orderedBlocks) Less(i, j int) bool {
if b[i].StartLine < b[j].StartLine {
return true
}
if b[i].StartLine == b[j].StartLine {
if b[i].StartCol < b[j].StartCol {
return true
}
if b[i].StartCol == b[j].StartCol {
if b[i].EndLine < b[j].EndLine {
return true
}
if b[i].EndLine == b[j].EndLine {
if b[i].EndCol < b[j].EndCol {
return true
}
}
}
}
return false
}
func maxEndLine(b1, b2 *cover.ProfileBlock) (int, int) {
if b1.EndLine > b2.EndLine {
return b1.EndLine, b1.EndCol
}
if b2.EndLine > b1.EndLine {
return b2.EndLine, b2.EndCol
}
if b1.EndCol > b2.EndCol {
return b1.EndLine, b1.EndCol
}
return b2.EndLine, b2.EndCol
}
func combine(profiles []*cover.Profile) map[string]*cover.Profile {
fileMap := make(map[string]*cover.Profile)
// combine all blocks by file name
for _, profile := range profiles {
if p, found := fileMap[profile.FileName]; found {
p.Blocks = append(p.Blocks, profile.Blocks...)
} else {
fileMap[profile.FileName] = profile
}
}
return fileMap
}
type byFileName []*cover.Profile
func (p byFileName) Len() int { return len(p) }
func (p byFileName) Less(i, j int) bool { return p[i].FileName < p[j].FileName }
func (p byFileName) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func max(a, b int) int {
if a > b {
return a
}
return b
}
func overlaps(b1, b2 *cover.ProfileBlock) bool {
// return true if b2 overlaps b1
if b1.EndLine < b2.StartLine {
return false
}
if b1.EndLine > b2.StartLine {
return true
}
return b1.EndCol >= b2.StartCol
}
func deDuplicate(profiles []*cover.Profile) []*cover.Profile {
combined := combine(profiles)
result := make([]*cover.Profile, 0, len(combined))
// dedup blocks
for _, profile := range combined {
sort.Sort(orderedBlocks(profile.Blocks))
// dedup blocks
deduped := make([]cover.ProfileBlock, 0, len(profile.Blocks))
var current *cover.ProfileBlock
for _, blockIterator := range profile.Blocks {
block := blockIterator
if current == nil {
current = &block
continue
}
if overlaps(current, &block) {
// merge blocks as overlapping
current.EndLine, current.EndCol = maxEndLine(current, &block)
current.NumStmt = max(current.NumStmt, block.NumStmt)
current.Count = max(current.Count, block.Count)
} else {
deduped = append(deduped, *current)
current = &block
}
}
if current != nil {
deduped = append(deduped, *current)
}
profile.Blocks = deduped
result = append(result, profile)
}
sort.Sort(byFileName(result))
return result
}
func printProfile(profile *cover.Profile, w io.Writer) {
// name.go:line.column,line.column numberOfStatements count
name := profile.FileName
for _, block := range profile.Blocks {
fmt.Fprintf(w, "%s:%d.%d,%d.%d %d %d\n", name, block.StartLine, block.StartCol, block.EndLine, block.EndCol, block.NumStmt, block.Count)
}
}
func printProfiles(profiles []*cover.Profile, w io.Writer) {
if len(profiles) > 0 {
fmt.Fprintf(w, "mode: %s\n", profiles[0].Mode)
}
for _, profile := range profiles {
printProfile(profile, w)
}
}
func checkError(err error, w io.Writer, exit func(code int)) {
if err != nil {
if errors.Is(err, errHelp) {
fmt.Fprintln(w, err)
exit(99)
return
}
fmt.Fprintln(w, err)
exit(1)
}
}
func main() {
profiles, err := processArgs(os.Args, os.Stdin)
checkError(err, os.Stderr, os.Exit)
profiles, err = filterProfiles(profiles, ".coverignore")
checkError(err, os.Stderr, os.Exit)
printProfiles(deDuplicate(profiles), os.Stdout)
}