Skip to content

Commit b264c30

Browse files
lunnyguillep2k
andauthored
Move gocovmerge as vendor (#10947)
* Move gocovmerge as vendor * Update Makefile Co-Authored-By: guillep2k <[email protected]> Co-authored-by: guillep2k <[email protected]>
1 parent 14c97c0 commit b264c30

File tree

6 files changed

+382
-4
lines changed

6 files changed

+382
-4
lines changed

Makefile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,7 @@ test\#%:
293293

294294
.PHONY: coverage
295295
coverage:
296-
@hash gocovmerge > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
297-
$(GO) get -u github.com/wadey/gocovmerge; \
298-
fi
299-
gocovmerge integration.coverage.out $(shell find . -type f -name "coverage.out") > coverage.all;\
296+
GO111MODULE=on $(GO) run -mod=vendor build/gocovmerge.go integration.coverage.out $(shell find . -type f -name "coverage.out") > coverage.all
300297

301298
.PHONY: unit-test-coverage
302299
unit-test-coverage:

build/gocovmerge.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Copyright (c) 2015, Wade Simmons
3+
// Use of this source code is governed by a MIT-style
4+
// license that can be found in the LICENSE file.
5+
6+
// gocovmerge takes the results from multiple `go test -coverprofile` runs and
7+
// merges them into one profile
8+
9+
// +build ignore
10+
11+
package main
12+
13+
import (
14+
"flag"
15+
"fmt"
16+
"io"
17+
"log"
18+
"os"
19+
"sort"
20+
21+
"golang.org/x/tools/cover"
22+
)
23+
24+
func mergeProfiles(p *cover.Profile, merge *cover.Profile) {
25+
if p.Mode != merge.Mode {
26+
log.Fatalf("cannot merge profiles with different modes")
27+
}
28+
// Since the blocks are sorted, we can keep track of where the last block
29+
// was inserted and only look at the blocks after that as targets for merge
30+
startIndex := 0
31+
for _, b := range merge.Blocks {
32+
startIndex = mergeProfileBlock(p, b, startIndex)
33+
}
34+
}
35+
36+
func mergeProfileBlock(p *cover.Profile, pb cover.ProfileBlock, startIndex int) int {
37+
sortFunc := func(i int) bool {
38+
pi := p.Blocks[i+startIndex]
39+
return pi.StartLine >= pb.StartLine && (pi.StartLine != pb.StartLine || pi.StartCol >= pb.StartCol)
40+
}
41+
42+
i := 0
43+
if sortFunc(i) != true {
44+
i = sort.Search(len(p.Blocks)-startIndex, sortFunc)
45+
}
46+
i += startIndex
47+
if i < len(p.Blocks) && p.Blocks[i].StartLine == pb.StartLine && p.Blocks[i].StartCol == pb.StartCol {
48+
if p.Blocks[i].EndLine != pb.EndLine || p.Blocks[i].EndCol != pb.EndCol {
49+
log.Fatalf("OVERLAP MERGE: %v %v %v", p.FileName, p.Blocks[i], pb)
50+
}
51+
switch p.Mode {
52+
case "set":
53+
p.Blocks[i].Count |= pb.Count
54+
case "count", "atomic":
55+
p.Blocks[i].Count += pb.Count
56+
default:
57+
log.Fatalf("unsupported covermode: '%s'", p.Mode)
58+
}
59+
} else {
60+
if i > 0 {
61+
pa := p.Blocks[i-1]
62+
if pa.EndLine >= pb.EndLine && (pa.EndLine != pb.EndLine || pa.EndCol > pb.EndCol) {
63+
log.Fatalf("OVERLAP BEFORE: %v %v %v", p.FileName, pa, pb)
64+
}
65+
}
66+
if i < len(p.Blocks)-1 {
67+
pa := p.Blocks[i+1]
68+
if pa.StartLine <= pb.StartLine && (pa.StartLine != pb.StartLine || pa.StartCol < pb.StartCol) {
69+
log.Fatalf("OVERLAP AFTER: %v %v %v", p.FileName, pa, pb)
70+
}
71+
}
72+
p.Blocks = append(p.Blocks, cover.ProfileBlock{})
73+
copy(p.Blocks[i+1:], p.Blocks[i:])
74+
p.Blocks[i] = pb
75+
}
76+
return i + 1
77+
}
78+
79+
func addProfile(profiles []*cover.Profile, p *cover.Profile) []*cover.Profile {
80+
i := sort.Search(len(profiles), func(i int) bool { return profiles[i].FileName >= p.FileName })
81+
if i < len(profiles) && profiles[i].FileName == p.FileName {
82+
mergeProfiles(profiles[i], p)
83+
} else {
84+
profiles = append(profiles, nil)
85+
copy(profiles[i+1:], profiles[i:])
86+
profiles[i] = p
87+
}
88+
return profiles
89+
}
90+
91+
func dumpProfiles(profiles []*cover.Profile, out io.Writer) {
92+
if len(profiles) == 0 {
93+
return
94+
}
95+
fmt.Fprintf(out, "mode: %s\n", profiles[0].Mode)
96+
for _, p := range profiles {
97+
for _, b := range p.Blocks {
98+
fmt.Fprintf(out, "%s:%d.%d,%d.%d %d %d\n", p.FileName, b.StartLine, b.StartCol, b.EndLine, b.EndCol, b.NumStmt, b.Count)
99+
}
100+
}
101+
}
102+
103+
func main() {
104+
flag.Parse()
105+
106+
var merged []*cover.Profile
107+
108+
for _, file := range flag.Args() {
109+
profiles, err := cover.ParseProfiles(file)
110+
if err != nil {
111+
log.Fatalf("failed to parse profiles: %v", err)
112+
}
113+
for _, p := range profiles {
114+
merged = addProfile(merged, p)
115+
}
116+
}
117+
118+
dumpProfiles(merged, os.Stdout)
119+
}

build/vendor.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ import (
1515

1616
// for embed
1717
_ "github.com/shurcooL/vfsgen"
18+
19+
// for cover merge
20+
_ "golang.org/x/tools/cover"
1821
)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ require (
111111
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
112112
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527
113113
golang.org/x/text v0.3.2
114+
golang.org/x/tools v0.0.0-20200225230052-807dcd883420
114115
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
115116
gopkg.in/asn1-ber.v1 v1.0.0-20150924051756-4e86f4367175 // indirect
116117
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df

0 commit comments

Comments
 (0)