Skip to content

Commit

Permalink
Merge pull request #629 from k1LoW/coverage-json
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW authored Oct 8, 2023
2 parents 1aa646b + c06da50 commit 35ec49a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
12 changes: 12 additions & 0 deletions cmd/coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -83,6 +84,16 @@ var coverageCmd = &cobra.Command{
if err != nil {
return err
}

if flgs.Format == "json" {
b, err := json.MarshalIndent(cov, "", " ")
if err != nil {
return err
}
_, _ = fmt.Println(string(b))
return nil
}

table := tablewriter.NewWriter(os.Stdout)
ct := "Coverage"
if flgs.Long {
Expand Down Expand Up @@ -159,5 +170,6 @@ func init() {
coverageCmd.Flags().StringSliceVarP(&flgs.GRPCProtos, "grpc-proto", "", []string{}, flgs.Usage("GRPCProtos"))
coverageCmd.Flags().StringSliceVarP(&flgs.GRPCImportPaths, "grpc-import-path", "", []string{}, flgs.Usage("GRPCImportPaths"))
coverageCmd.Flags().StringVarP(&flgs.CacheDir, "cache-dir", "", "", flgs.Usage("CacheDir"))
coverageCmd.Flags().StringVarP(&flgs.Format, "format", "", "", flgs.Usage("Format"))
coverageCmd.Flags().BoolVarP(&flgs.RetainCacheDir, "retain-cache-dir", "", false, flgs.Usage("RetainCacheDir"))
}
22 changes: 12 additions & 10 deletions coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ import (
var varRep = regexp.MustCompile(`\{\{([^}]+)\}\}`)
var qRep = regexp.MustCompile(`\?.+$`)

type coverage struct {
Specs []*specCoverage `json:"specs"`
// Coverage is a coverage of runbooks
type Coverage struct {
Specs []*SpecCoverage `json:"specs"`
}

type specCoverage struct {
// SpecCoverage is a coverage of spec (e.g. OpenAPI Document, servive of protocol buffers)
type SpecCoverage struct {
Key string `json:"key"`
Coverages map[string]int `json:"coverages"`
}

func (o *operator) collectCoverage(ctx context.Context) (*coverage, error) {
cov := &coverage{}
func (o *operator) collectCoverage(ctx context.Context) (*Coverage, error) {
cov := &Coverage{}
// Collect coverage for openapi3
for name, r := range o.httpRunners {
ov, ok := r.validator.(*openApi3Validator)
Expand All @@ -35,11 +37,11 @@ func (o *operator) collectCoverage(ctx context.Context) (*coverage, error) {
continue
}
key := fmt.Sprintf("%s:%s", ov.doc.Info.Title, ov.doc.Info.Version)
scov, ok := lo.Find(cov.Specs, func(scov *specCoverage) bool {
scov, ok := lo.Find(cov.Specs, func(scov *SpecCoverage) bool {
return scov.Key == key
})
if !ok {
scov = &specCoverage{
scov = &SpecCoverage{
Key: key,
Coverages: map[string]int{},
}
Expand Down Expand Up @@ -120,11 +122,11 @@ func (o *operator) collectCoverage(ctx context.Context) (*coverage, error) {
sm := strings.Split(k, "/")
service := sm[0]
method := sm[1]
scov, ok := lo.Find(cov.Specs, func(scov *specCoverage) bool {
scov, ok := lo.Find(cov.Specs, func(scov *SpecCoverage) bool {
return scov.Key == service
})
if !ok {
scov = &specCoverage{
scov = &SpecCoverage{
Key: service,
Coverages: map[string]int{},
}
Expand All @@ -140,7 +142,7 @@ func (o *operator) collectCoverage(ctx context.Context) (*coverage, error) {
sm := strings.Split(k, "/")
service := sm[0]
method := sm[1]
scov, ok := lo.Find(cov.Specs, func(scov *specCoverage) bool {
scov, ok := lo.Find(cov.Specs, func(scov *SpecCoverage) bool {
return scov.Key == service
})
if !ok {
Expand Down
6 changes: 3 additions & 3 deletions operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1341,16 +1341,16 @@ func (ops *operators) SelectedOperators() ([]*operator, error) {
return tops, nil
}

func (ops *operators) CollectCoverage(ctx context.Context) (*coverage, error) {
cov := &coverage{}
func (ops *operators) CollectCoverage(ctx context.Context) (*Coverage, error) {
cov := &Coverage{}
for _, o := range ops.ops {
c, err := o.collectCoverage(ctx)
if err != nil {
return nil, err
}
// Merge coverage
for _, sc := range c.Specs {
spec, ok := lo.Find(cov.Specs, func(i *specCoverage) bool {
spec, ok := lo.Find(cov.Specs, func(i *SpecCoverage) bool {
return sc.Key == i.Key
})
if !ok {
Expand Down

0 comments on commit 35ec49a

Please sign in to comment.