goc profile: add coverpkg flag

This commit is contained in:
jichangjun 2020-09-01 21:56:56 +08:00
parent 4b184c9fb5
commit ca15b18860
4 changed files with 46 additions and 10 deletions

View File

@ -104,7 +104,3 @@ func TestDoDiffForLocalProfiles(t *testing.T) {
}
}
func TestDoDiffUnderProw(t *testing.T) {
}

View File

@ -19,11 +19,11 @@ package cmd
import (
"bytes"
"fmt"
log "github.com/sirupsen/logrus"
"io"
"os"
"github.com/qiniu/goc/pkg/cover"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
@ -74,16 +74,24 @@ goc profile --force
},
}
var output string
var force bool
var svrList []string
var addrList []string
var (
svrList []string // --service flag
addrList []string // --address flag
force bool // --force flag
output string // --output flag
coverPkg []string // --coverpkg flag
)
func init() {
profileCmd.Flags().StringVarP(&output, "output", "o", "", "download cover profile")
profileCmd.Flags().StringSliceVarP(&svrList, "service", "", nil, "service name to fetch profile, see 'goc list' for all services.")
profileCmd.Flags().StringSliceVarP(&addrList, "address", "", nil, "address to fetch profile, see 'goc list' for all addresses.")
profileCmd.Flags().BoolVarP(&force, "force", "f", false, "force fetching all available profiles")
profileCmd.Flags().StringSliceVarP(&coverPkg, "coverpkg", "", nil, "only output coverage data of the packages matching the patterns")
addBasicFlags(profileCmd.Flags())
rootCmd.AddCommand(profileCmd)
}
func filterProfile() {
}

View File

@ -25,6 +25,7 @@ import (
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"github.com/gin-gonic/gin"
@ -83,11 +84,13 @@ type Service struct {
Address string `form:"address" json:"address" binding:"required"`
}
// ProfileParam is param of profile API (TODO)
// ProfileParam is param of profile API
type ProfileParam struct {
Force bool `form:"force"`
Service []string `form:"service" json:"service"`
Address []string `form:"address" json:"address"`
CoverPkg []string
}
//listServices list all the registered services
@ -138,6 +141,7 @@ func profile(c *gin.Context) {
c.JSON(http.StatusExpectationFailed, gin.H{"error": "invalid param"})
return
}
serviceList := removeDuplicateElement(c.QueryArray("service"))
addressList := removeDuplicateElement(c.QueryArray("address"))
allInfos := DefaultStore.GetAll()
@ -183,6 +187,25 @@ func profile(c *gin.Context) {
}
}
// filterProfile output profiles of the packages matching the coverPkg
func filterProfile(coverPkg []string, profiles []*cover.Profile) ([]*cover.Profile, error) {
var out = make([]*cover.Profile, 0)
for _, profile := range profiles {
for _, pattern := range coverPkg {
matched, err := regexp.MatchString(pattern, profile.FileName)
if err != nil {
return nil, fmt.Errorf("filterProfile failed with pattern %s for profile %s", pattern, profile.FileName)
}
if matched {
out = append(out, profile)
}
}
}
return out, nil
}
func clear(c *gin.Context) {
svrsUnderTest := DefaultStore.GetAll()
for svc, addrs := range svrsUnderTest {

View File

@ -214,3 +214,12 @@ func TestInitService(t *testing.T) {
assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Contains(t, w.Body.String(), "lala error")
}
func TestFilterProfile(t *testing.T) {
// var tcs = []struct {
// pattern []string
// profile []*cover.Profile
// expected []*cover.Profile
// err error
// }{}
}