From b45c31043b5b83d8b7154eec6bf20d9b9ab6f826 Mon Sep 17 00:00:00 2001 From: Pat Gavlin Date: Wed, 24 Nov 2021 11:15:38 -0800 Subject: [PATCH] Use `pulumictl cover merge` instead of `covmerge` Depends on https://github.com/pulumi/pulumictl/pull/42. --- .github/workflows/master.yml | 2 +- .../run-build-and-acceptance-tests.yml | 2 +- Makefile | 7 +- pkg/cmd/covmerge/main.go | 156 ------------------ 4 files changed, 4 insertions(+), 163 deletions(-) delete mode 100644 pkg/cmd/covmerge/main.go diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index 317842f84..139433e58 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -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 diff --git a/.github/workflows/run-build-and-acceptance-tests.yml b/.github/workflows/run-build-and-acceptance-tests.yml index 35fc6b1dd..834a45ecb 100644 --- a/.github/workflows/run-build-and-acceptance-tests.yml +++ b/.github/workflows/run-build-and-acceptance-tests.yml @@ -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 diff --git a/Makefile b/Makefile index c1cb94137..304821b9d 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/pkg/cmd/covmerge/main.go b/pkg/cmd/covmerge/main.go deleted file mode 100644 index a4bc2c7bc..000000000 --- a/pkg/cmd/covmerge/main.go +++ /dev/null @@ -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) - } -}