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 ( import (
"bytes" "bytes"
"fmt" "fmt"
log "github.com/sirupsen/logrus"
"io" "io"
"os" "os"
"github.com/qiniu/goc/pkg/cover" "github.com/qiniu/goc/pkg/cover"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -74,16 +74,24 @@ goc profile --force
}, },
} }
var output string var (
var force bool svrList []string // --service flag
var svrList []string addrList []string // --address flag
var addrList []string force bool // --force flag
output string // --output flag
coverPkg []string // --coverpkg flag
)
func init() { func init() {
profileCmd.Flags().StringVarP(&output, "output", "o", "", "download cover profile") 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(&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().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().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()) addBasicFlags(profileCmd.Flags())
rootCmd.AddCommand(profileCmd) rootCmd.AddCommand(profileCmd)
} }
func filterProfile() {
}

View File

@ -25,6 +25,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"regexp"
"strconv" "strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -83,11 +84,13 @@ type Service struct {
Address string `form:"address" json:"address" binding:"required"` Address string `form:"address" json:"address" binding:"required"`
} }
// ProfileParam is param of profile API (TODO) // ProfileParam is param of profile API
type ProfileParam struct { type ProfileParam struct {
Force bool `form:"force"` Force bool `form:"force"`
Service []string `form:"service" json:"service"` Service []string `form:"service" json:"service"`
Address []string `form:"address" json:"address"` Address []string `form:"address" json:"address"`
CoverPkg []string
} }
//listServices list all the registered services //listServices list all the registered services
@ -138,6 +141,7 @@ func profile(c *gin.Context) {
c.JSON(http.StatusExpectationFailed, gin.H{"error": "invalid param"}) c.JSON(http.StatusExpectationFailed, gin.H{"error": "invalid param"})
return return
} }
serviceList := removeDuplicateElement(c.QueryArray("service")) serviceList := removeDuplicateElement(c.QueryArray("service"))
addressList := removeDuplicateElement(c.QueryArray("address")) addressList := removeDuplicateElement(c.QueryArray("address"))
allInfos := DefaultStore.GetAll() 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) { func clear(c *gin.Context) {
svrsUnderTest := DefaultStore.GetAll() svrsUnderTest := DefaultStore.GetAll()
for svc, addrs := range svrsUnderTest { for svc, addrs := range svrsUnderTest {

View File

@ -214,3 +214,12 @@ func TestInitService(t *testing.T) {
assert.Equal(t, http.StatusInternalServerError, w.Code) assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Contains(t, w.Body.String(), "lala error") 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
// }{}
}