Use pulumictl cover merge instead of covmerge

Depends on https://github.com/pulumi/pulumictl/pull/42.
This commit is contained in:
Pat Gavlin 2021-11-24 11:15:38 -08:00
parent d2825522f2
commit b45c31043b
4 changed files with 4 additions and 163 deletions

View file

@ -290,7 +290,7 @@ jobs:
PULUMI_ROOT: ${{ runner.temp }}/opt/pulumi
- name: Merge Go coverage data
run: |
covmerge -in ./coverage -out ./coverage/go-all.txt
pulumictl cover merge -in ./coverage -out ./coverage/go-all.txt
rm ./coverage/*.cov
- name: Upload code coverage
uses: codecov/codecov-action@v2

View file

@ -241,7 +241,7 @@ jobs:
PULUMI_ROOT: ${{ runner.temp }}/opt/pulumi
- name: Merge Go coverage data
run: |
covmerge -in ./coverage -out ./coverage/go-all.txt
pulumictl cover merge -in ./coverage -out ./coverage/go-all.txt
rm ./coverage/*.cov
- name: Upload code coverage
uses: codecov/codecov-action@v2

View file

@ -38,10 +38,10 @@ build::
install::
cd pkg && GOBIN=$(PULUMI_BIN) go install -ldflags "-X github.com/pulumi/pulumi/pkg/v3/version.Version=${VERSION}" ${PROJECT}
else
build:: build_cover install_covmerge
build:: build_cover
mkdir -p $(PULUMI_TEST_COVERAGE_PATH)
install:: install_cover install_covmerge
install:: install_cover
endif
build_debug::
@ -53,9 +53,6 @@ build_cover::
install_cover:: build_cover
cp $(shell go env GOPATH)/bin/pulumi $(PULUMI_BIN)
install_covmerge::
cd pkg && go install ./cmd/covmerge
developer_docs::
cd developer-docs && make html

View file

@ -1,156 +0,0 @@
package main
import (
"flag"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"golang.org/x/tools/cover"
)
type mode string
func (m mode) merge(dest, src cover.ProfileBlock) cover.ProfileBlock {
if m == "set" {
if src.Count != 0 {
dest.Count = src.Count
}
} else {
dest.Count += src.Count
}
return dest
}
type blockLocation struct {
startLine, startCol, endLine, endCol int
}
func (l blockLocation) less(m blockLocation) bool {
return l.startLine < m.startLine || l.startCol < m.startCol || l.endLine < m.endLine || l.endCol < m.endCol
}
type profile struct {
fileName string
blocks map[blockLocation]cover.ProfileBlock
}
func (p *profile) merge(other *cover.Profile, mode mode) {
for _, block := range other.Blocks {
loc := blockLocation{block.StartLine, block.StartCol, block.EndLine, block.EndCol}
if b, ok := p.blocks[loc]; ok {
p.blocks[loc] = mode.merge(b, block)
} else {
p.blocks[loc] = block
}
}
}
func (p *profile) write(w io.Writer) error {
locs := make([]blockLocation, 0, len(p.blocks))
for loc := range p.blocks {
locs = append(locs, loc)
}
sort.Slice(locs, func(i, j int) bool { return locs[i].less(locs[j]) })
for _, l := range locs {
b := p.blocks[l]
if _, err := fmt.Fprintf(w, "%v:%v.%v,%v.%v %v %v\n",
p.fileName, b.StartLine, b.StartCol, b.EndLine, b.EndCol, b.NumStmt, b.Count); err != nil {
return err
}
}
return nil
}
type profiles struct {
mode mode
files map[string]*profile
}
func (ps *profiles) merge(other []*cover.Profile) error {
for _, o := range other {
switch {
case ps.mode == "":
ps.mode = mode(o.Mode)
case o.Mode != string(ps.mode):
return fmt.Errorf("%v's coverage mode '%v' does not match the merged mode '%v'", o.FileName, o.Mode, ps.mode)
}
p, ok := ps.files[o.FileName]
if !ok {
p = &profile{fileName: o.FileName, blocks: map[blockLocation]cover.ProfileBlock{}}
ps.files[o.FileName] = p
}
p.merge(o, ps.mode)
}
return nil
}
func (ps *profiles) write(w io.Writer) error {
if _, err := fmt.Fprintf(w, "mode: %v\n", ps.mode); err != nil {
return err
}
files := make([]string, 0, len(ps.files))
for fileName := range ps.files {
files = append(files, fileName)
}
sort.Strings(files)
for _, fileName := range files {
if err := ps.files[fileName].write(w); err != nil {
return err
}
}
return nil
}
func fatalf(m string, args ...interface{}) {
fmt.Fprintf(os.Stderr, m+"\n", args...)
os.Exit(1)
}
func main() {
inPath := flag.String("in", "", "the path to the directory containing coverage data to merge")
outPath := flag.String("out", "", "the path to the output file")
flag.Parse()
if *inPath == "" {
fatalf("the -in flag is required")
}
profiles := profiles{files: map[string]*profile{}}
entries, err := os.ReadDir(*inPath)
if err != nil {
fatalf("reading input: %v", err)
}
for _, e := range entries {
if !e.IsDir() && filepath.Ext(e.Name()) == ".cov" {
path := filepath.Join(*inPath, e.Name())
rawProfiles, err := cover.ParseProfiles(path)
if err != nil {
fatalf("parsing profiles from '%v': %v", path, err)
}
if err = profiles.merge(rawProfiles); err != nil {
fatalf("merging coverage from '%v': %v", path, err)
}
}
}
outFile := os.Stdout
if *outPath != "" {
outFile, err = os.Create(*outPath)
if err != nil {
fatalf("creating output file '%v': %v", *outPath)
}
defer outFile.Close()
}
if err = profiles.write(outFile); err != nil {
fatalf("writing merged profile: %v", err)
}
}