From 18a5e8bdc8901b430181f4e07047a4967c8c2387 Mon Sep 17 00:00:00 2001 From: lyyyuna Date: Sun, 8 Aug 2021 15:44:55 +0800 Subject: [PATCH 1/5] fix: wrong project root --- pkg/build/goenv.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/build/goenv.go b/pkg/build/goenv.go index d6861b8..7dc3b87 100644 --- a/pkg/build/goenv.go +++ b/pkg/build/goenv.go @@ -29,7 +29,7 @@ func (b *Build) readProjectMetaInfo() { log.Fatalf("Go module is not enabled, please set GO111MODULE=auto or on") } // 工程根目录 - config.GocConfig.CurModProjectDir = pkg.Root + config.GocConfig.CurModProjectDir = pkg.Module.Dir config.GocConfig.ImportPath = pkg.Module.Path break From 52fd76365590c357aa256792bf22070d9a3576b0 Mon Sep 17 00:00:00 2001 From: lyyyuna Date: Sun, 8 Aug 2021 15:45:19 +0800 Subject: [PATCH 2/5] skip & continue unknown flags --- pkg/flag/build_flags.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/flag/build_flags.go b/pkg/flag/build_flags.go index 5f5ea0f..75857bd 100644 --- a/pkg/flag/build_flags.go +++ b/pkg/flag/build_flags.go @@ -43,6 +43,10 @@ func BuildCmdArgsParse(cmd *cobra.Command, args []string, cmdType int) []string allFlagSets := cmd.Flags() // 因为 args 里面含有 go 的 flag,所以需要忽略解析 go flag 的错误 allFlagSets.Init("GOC", pflag.ContinueOnError) + // 忽略 go flag 在 goc 中的解析错误 + allFlagSets.ParseErrorsWhitelist = pflag.ParseErrorsWhitelist{ + UnknownFlags: true, + } allFlagSets.Parse(args) // 重写 help @@ -109,7 +113,7 @@ func findAndDelGocFlag(a []string, x string, v string) []string { x = "--" + x x_v := x + "=" + v for i := 0; i < len(a); i++ { - if a[i] == "--debug" { + if a[i] == "--gocdebug" { // debug 是 bool,就一个元素 continue } else if a[i] == x { From 0704a4ce6729595dd8278a19277fab1cfb0dab2a Mon Sep 17 00:00:00 2001 From: lyyyuna Date: Sun, 8 Aug 2021 15:45:35 +0800 Subject: [PATCH 3/5] fix: use 1.16 to release --- .github/workflows/release.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5890ac0..e904b0a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -11,7 +11,7 @@ jobs: - name: Install Go uses: actions/setup-go@v2 with: - go-version: 1.14.x + go-version: 1.16.x - name: compile and release run: | ./hack/release.sh @@ -28,7 +28,7 @@ jobs: - name: Install Go uses: actions/setup-go@v2 with: - go-version: 1.14.x + go-version: 1.16.x - name: compile and release run: | ./hack/release.sh @@ -45,7 +45,7 @@ jobs: - name: Install Go uses: actions/setup-go@v2 with: - go-version: 1.14.x + go-version: 1.16.x - name: compile and release run: | ./hack/release.sh From 8b1f28225e0d080f243a4643690579d140f71e93 Mon Sep 17 00:00:00 2001 From: lyyyuna Date: Sun, 8 Aug 2021 16:16:10 +0800 Subject: [PATCH 4/5] watch: add numstmts --- pkg/cover/agentwatch.tpl | 20 +++++++++++--------- pkg/cover/internal/tool/cover.go | 3 ++- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/pkg/cover/agentwatch.tpl b/pkg/cover/agentwatch.tpl index 69191c8..485e27d 100644 --- a/pkg/cover/agentwatch.tpl +++ b/pkg/cover/agentwatch.tpl @@ -80,8 +80,8 @@ func init() { cov := fmt.Sprintf("%s:%d.%d,%d.%d %d %d", block.name, block.pos[3*i+0], uint16(block.pos[3*i+2]), block.pos[3*i+1], uint16(block.pos[3*i+2] >> 16), - 1, - 0) + block.stmts, + 1) err = ws.WriteMessage(websocket.TextMessage, []byte(cov)) if err != nil { @@ -128,13 +128,14 @@ func getRegisterInfo() (*processInfo, error) { // type blockInfo struct { - name string - pos []uint32 - i int + name string + pos []uint32 + i int + stmts int } // UploadCoverChangeEvent_{{.Random}} is non-blocking -func UploadCoverChangeEvent_{{.Random}}(name string, pos []uint32, i int) { +func UploadCoverChangeEvent_{{.Random}}(name string, pos []uint32, i int, stmts uint16) { if watchEnabled == false { return @@ -143,9 +144,10 @@ func UploadCoverChangeEvent_{{.Random}}(name string, pos []uint32, i int) { // make sure send is non-blocking select { case watchChannel <- &blockInfo{ - name: name, - pos: pos, - i: i, + name: name, + pos: pos, + i: i, + stmts: int(stmts), }: default: } diff --git a/pkg/cover/internal/tool/cover.go b/pkg/cover/internal/tool/cover.go index fb2a93a..53a186a 100644 --- a/pkg/cover/internal/tool/cover.go +++ b/pkg/cover/internal/tool/cover.go @@ -419,7 +419,8 @@ func atomicCounterStmt(f *File, counter string) string { // watchCounterStmt returns the expression: __count[23]++;UploadCoverChangeEvent(blockname, pos[:], index) func watchCounterStmt(f *File, counter string) string { - return fmt.Sprintf("%s++; UploadCoverChangeEvent_%v(%s.BlockName, %s.Pos[:], %v)", counter, f.random, f.varVar, f.varVar, len(f.blocks)) + index := len(f.blocks) + return fmt.Sprintf("%s++; UploadCoverChangeEvent_%v(%s.BlockName, %s.Pos[:], %v, %s.NumStmt[%v])", counter, f.random, f.varVar, f.varVar, index, f.varVar, index) } // QINIU From 0c6ce25f37cd0c44b0615b870ffbe1cbb3ebe147 Mon Sep 17 00:00:00 2001 From: lyyyuna Date: Sun, 8 Aug 2021 16:20:10 +0800 Subject: [PATCH 5/5] add merge command --- cmd/merge.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 cmd/merge.go diff --git a/cmd/merge.go b/cmd/merge.go new file mode 100644 index 0000000..b3c18ab --- /dev/null +++ b/cmd/merge.go @@ -0,0 +1,55 @@ +package cmd + +import ( + "github.com/qiniu/goc/v2/pkg/log" + "github.com/spf13/cobra" + "golang.org/x/tools/cover" + "k8s.io/test-infra/gopherage/pkg/cov" + "k8s.io/test-infra/gopherage/pkg/util" +) + +var mergeCmd = &cobra.Command{ + Use: "merge [files...]", + Short: "Merge multiple coherent Go coverage files into a single file.", + Long: `merge will merge multiple Go coverage files into a single coverage file. +merge requires that the files are 'coherent', meaning that if they both contain references to the +same paths, then the contents of those source files were identical for the binary that generated +each file. +`, + Run: func(cmd *cobra.Command, args []string) { + runMerge(args, outputMergeProfile) + }, +} + +var outputMergeProfile string + +func init() { + mergeCmd.Flags().StringVarP(&outputMergeProfile, "output", "o", "mergeprofile.cov", "output file") + + rootCmd.AddCommand(mergeCmd) +} + +func runMerge(args []string, output string) { + if len(args) == 0 { + log.Fatalf("Expected at least one coverage file.") + } + + profiles := make([][]*cover.Profile, len(args)) + for _, path := range args { + profile, err := util.LoadProfile(path) + if err != nil { + log.Fatalf("failed to open %s: %v", path, err) + } + profiles = append(profiles, profile) + } + + merged, err := cov.MergeMultipleProfiles(profiles) + if err != nil { + log.Fatalf("failed to merge files: %v", err) + } + + err = util.DumpProfile(output, merged) + if err != nil { + log.Fatalf("fail to dump the merged file: %v", err) + } +}