add merge command

This commit is contained in:
lyyyuna 2020-08-05 17:28:11 +08:00
parent 9f42e1c2f5
commit 288f5404b2
6 changed files with 144 additions and 1 deletions

72
cmd/merge.go Normal file
View File

@ -0,0 +1,72 @@
/*
Copyright 2020 Qiniu Cloud (qiniu.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"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) {
run(cmd, args)
},
}
var outputMergeProfile string
func init() {
mergeCmd.Flags().StringVarP(&outputMergeProfile, "output", "o", "mergeprofile.cov", "output file")
rootCmd.AddCommand(mergeCmd)
}
func run(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Fatalln("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(outputMergeProfile, merged)
if err != nil {
log.Fatalln(err)
}
}

View File

@ -20,12 +20,13 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
)
// Action provides methods to contact with the covered service under test

60
tests/merge.bats Executable file
View File

@ -0,0 +1,60 @@
#!/usr/bin/env bats
# Copyright 2020 Qiniu Cloud (七牛云)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
load util.sh
setup_file() {
# run centered server
goc server 3>&- &
GOC_PID=$!
sleep 2
goc init
info "goc server started"
}
teardown_file() {
kill -9 $GOC_PID
}
setup() {
goc init
}
@test "test goc merge command" {
cd samples/merge_profile_samples
wait_profile_backend "merge1" &
profile_pid=$!
# merge two profiles with same binary
run gocc merge a.voc b.voc --debug --debugcisyncfile ci-sync.bak;
info merge1 output: $output
[ "$status" -eq 0 ]
run cat mergeprofile.cov
[[ "$output" == *"qiniu.com/kodo/apiserver/server/main.go:32.49,33.13 1 60"* ]]
[[ "$output" == *"qiniu.com/kodo/apiserver/server/main.go:42.49,43.13 1 2"* ]]
# merge two profiles from two binaries, but has some source code in common
run gocc merge a.voc c.voc --debug --debugcisyncfile ci-sync.bak;
info merge1 output: $output
[ "$status" -eq 0 ]
run cat mergeprofile.cov
[[ "$output" == *"qiniu.com/kodo/apiserver/server/main.go:32.49,33.13 1 60"* ]]
[[ "$output" == *"qiniu.com/kodo/apiserver/server/main.go:42.49,43.13 1 0"* ]]
[[ "$output" == *"qiniu.com/kodo/apiserver/server/wala.go:42.49,43.13 1 0"* ]]
wait $profile_pid
}

View File

@ -0,0 +1,3 @@
mode: atomic
qiniu.com/kodo/apiserver/server/main.go:32.49,33.13 1 30
qiniu.com/kodo/apiserver/server/main.go:42.49,43.13 1 0

View File

@ -0,0 +1,3 @@
mode: atomic
qiniu.com/kodo/apiserver/server/main.go:32.49,33.13 1 30
qiniu.com/kodo/apiserver/server/main.go:42.49,43.13 1 2

View File

@ -0,0 +1,4 @@
mode: atomic
qiniu.com/kodo/apiserver/server/main.go:32.49,33.13 1 30
qiniu.com/kodo/apiserver/server/main.go:42.49,43.13 1 0
qiniu.com/kodo/apiserver/server/wala.go:42.49,43.13 1 0