Skip to content

Commit

Permalink
Merge 503ac9c into 993f686
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW authored Oct 8, 2023
2 parents 993f686 + 503ac9c commit f34d6c1
Show file tree
Hide file tree
Showing 9 changed files with 435 additions and 33 deletions.
8 changes: 6 additions & 2 deletions book.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,12 @@ func (bk *book) parseGRPCRunnerWithDetailed(name string, b []byte) (bool, error)
r.key = b
}
r.skipVerify = c.SkipVerify
r.importPaths = c.ImportPaths
r.protos = c.Protos
for _, p := range c.ImportPaths {
r.importPaths = append(r.importPaths, fp(p, root))
}
for _, p := range c.Protos {
r.protos = append(r.protos, fp(p, root))
}
bk.grpcRunners[name] = r
return true, nil
}
Expand Down
162 changes: 162 additions & 0 deletions cmd/coverage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
Copyright © 2023 Ken'ichiro Oyama <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

import (
"context"
"fmt"
"net/http"
"os"
"path/filepath"
"slices"
"sort"
"strings"

"github.com/k1LoW/runn"
"github.com/olekukonko/tablewriter"
"github.com/samber/lo"
"github.com/spf13/cobra"
)

var sortByMethod = []string{
http.MethodGet,
http.MethodHead,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
http.MethodConnect,
http.MethodOptions,
http.MethodTrace,
}

// coverageCmd represents the coverage command
var coverageCmd = &cobra.Command{
Use: "coverage [PATH_PATTERN ...]",
Short: "show coverage for paths/operations of OpenAPI spec and methods of protocol buffers",
Long: `show coverage for paths/operations of OpenAPI spec and methods of protocol buffers.`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
opts, err := flgs.ToOpts()
if err != nil {
return err
}
pathp := strings.Join(args, string(filepath.ListSeparator))
opts = append(opts, runn.LoadOnly())

// setup cache dir
if err := runn.SetCacheDir(flgs.CacheDir); err != nil {
return err
}
defer func() {
if !flgs.RetainCacheDir {
_ = runn.RemoveCacheDir()
}
}()

o, err := runn.Load(pathp, opts...)
if err != nil {
return err
}

cov, err := o.CollectCoverage(ctx)
if err != nil {
return err
}
table := tablewriter.NewWriter(os.Stdout)
ct := "Coverage"
if flgs.Long {
ct = "Coverage/Count"
}
table.SetHeader([]string{"Spec", ct})
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(false)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderColor(tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{tablewriter.Bold})
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_RIGHT})
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("-")
table.SetHeaderLine(true)
table.SetBorder(false)
for _, spec := range cov.Specs {
var total, covered int
for _, v := range spec.Coverages {
total++
if v > 0 {
covered++
}
}
persent := float64(covered) / float64(total) * 100
table.Append([]string{spec.Key, fmt.Sprintf("%.1f%%", persent)})
if flgs.Long {
keys := lo.Keys(spec.Coverages)
sort.SliceStable(keys, func(i, j int) bool {
if !strings.Contains(keys[i], " ") || !strings.Contains(keys[j], " ") {
// Sort by method ( protocol buffers )
return keys[i] < keys[j]
}
// Sort by path ( OpenAPI )
mpi := strings.SplitN(keys[i], " ", 2)
mpj := strings.SplitN(keys[j], " ", 2)
if mpi[1] == mpj[1] {
// Sort by method ( OpenAPI )
return slices.Index(sortByMethod, mpi[0]) < slices.Index(sortByMethod, mpj[0])
}
return mpi[1] < mpj[1]
})
for _, k := range keys {
v := spec.Coverages[k]
if v == 0 {
table.Rich([]string{fmt.Sprintf(" %s", k), ""}, []tablewriter.Colors{{tablewriter.FgRedColor}, {}})
continue
}
table.Rich([]string{fmt.Sprintf(" %s", k), fmt.Sprintf("%d", v)}, []tablewriter.Colors{{tablewriter.FgGreenColor}, {tablewriter.FgHiGreenColor}})
}
}
}
if flgs.Debug {
cmd.Println()
}
table.Render()
return nil
},
}

func init() {
rootCmd.AddCommand(coverageCmd)
coverageCmd.Flags().BoolVarP(&flgs.Long, "long", "l", false, flgs.Usage("Long"))
coverageCmd.Flags().BoolVarP(&flgs.Debug, "debug", "", false, flgs.Usage("Debug"))
coverageCmd.Flags().StringSliceVarP(&flgs.Vars, "var", "", []string{}, flgs.Usage("Vars"))
coverageCmd.Flags().StringSliceVarP(&flgs.Runners, "runner", "", []string{}, flgs.Usage("Runners"))
coverageCmd.Flags().StringSliceVarP(&flgs.Overlays, "overlay", "", []string{}, flgs.Usage("Overlays"))
coverageCmd.Flags().StringSliceVarP(&flgs.Underlays, "underlay", "", []string{}, flgs.Usage("Underlays"))
coverageCmd.Flags().StringVarP(&flgs.RunMatch, "run", "", "", flgs.Usage("RunMatch"))
coverageCmd.Flags().StringVarP(&flgs.RunID, "id", "", "", flgs.Usage("RunID"))
coverageCmd.Flags().BoolVarP(&flgs.SkipIncluded, "skip-included", "", false, flgs.Usage("SkipIncluded"))
coverageCmd.Flags().BoolVarP(&flgs.GRPCNoTLS, "grpc-no-tls", "", false, flgs.Usage("GRPCNoTLS"))
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().BoolVarP(&flgs.RetainCacheDir, "retain-cache-dir", "", false, flgs.Usage("RetainCacheDir"))
}
148 changes: 148 additions & 0 deletions coverage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package runn

import (
"context"
"fmt"
"net/http"
"regexp"
"strings"

"github.com/getkin/kin-openapi/openapi3"
legacyrouter "github.com/getkin/kin-openapi/routers/legacy"
"github.com/samber/lo"
)

var varRep = regexp.MustCompile(`\{\{([^}]+)\}\}`)
var qRep = regexp.MustCompile(`\?.+$`)

type coverage struct {
Specs []*specCoverage `json:"specs"`
}

type specCoverage struct {
Key string `json:"key"`
Coverages map[string]int `json:"coverages"`
}

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)
if !ok {
o.Debugf("%s does not have openapi3 spec document (%s)\n", name, o.bookPath)
continue
}
key := fmt.Sprintf("%s:%s", ov.doc.Info.Title, ov.doc.Info.Version)
scov, ok := lo.Find(cov.Specs, func(scov *specCoverage) bool {
return scov.Key == key
})
if !ok {
scov = &specCoverage{
Key: key,
Coverages: map[string]int{},
}
cov.Specs = append(cov.Specs, scov)
}
paths := map[*openapi3.PathItem]string{}
for p, item := range ov.doc.Paths {
paths[item] = p
for m := range item.Operations() {
mkey := fmt.Sprintf("%s %s", m, p)
scov.Coverages[mkey] += 0
}
}
for _, s := range o.steps {
if s.httpRunner != r {
continue
}
L:
for p, m := range s.httpRequest {
mm := m.(map[string]any)
for mmm := range mm {
method := strings.ToUpper(mmm)
// Find path using openapi3 spec document (e.g. /v1/users/{id})
i := ov.doc.Paths.Find(varRep.ReplaceAllString(qRep.ReplaceAllString(p, ""), "{x}"))
if i == nil {
// Find path using router (e.g. /v1/users/1)
const host = "https://runn.test"
for _, s := range ov.doc.Servers {
s.URL = host
}
router, err := legacyrouter.NewRouter(ov.doc)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, host+p, nil)
if err != nil {
return nil, err
}
route, _, err := router.FindRoute(req)
if err != nil {
o.Debugf("%s %s was not matched in %s (%s)\n", method, p, key, o.bookPath)
continue
}
mkey := fmt.Sprintf("%s %s", method, route.Path)
scov.Coverages[mkey]++
continue
}
for m := range i.Operations() {
if method == m {
path, ok := paths[i]
if !ok {
return nil, fmt.Errorf("path not found in %s", p)
}
mkey := fmt.Sprintf("%s %s", method, path)
scov.Coverages[mkey]++
break L
}
}
o.Debugf("%s %s was not matched in %s (%s)\n", method, p, key, o.bookPath)
}
}
}
}

// Collect coverage for protocol buffers
for name, r := range o.grpcRunners {
if err := r.resolveAllMethodsUsingProtos(ctx); err != nil {
o.Debugf("%s was not resolved: %s (%s)\n", name, err, o.bookPath)
continue
}
for k := range r.mds {
sm := strings.Split(k, "/")
service := sm[0]
method := sm[1]
scov, ok := lo.Find(cov.Specs, func(scov *specCoverage) bool {
return scov.Key == service
})
if !ok {
scov = &specCoverage{
Key: service,
Coverages: map[string]int{},
}
cov.Specs = append(cov.Specs, scov)
}
scov.Coverages[method] += 0
}
for _, s := range o.steps {
if s.grpcRunner != r {
continue
}
for k := range s.grpcRequest {
sm := strings.Split(k, "/")
service := sm[0]
method := sm[1]
scov, ok := lo.Find(cov.Specs, func(scov *specCoverage) bool {
return scov.Key == service
})
if !ok {
o.Debugf("%s/%s was not matched (%s)\n", service, method, o.bookPath)
continue
}
scov.Coverages[method]++
}
}
}
return cov, nil
}
50 changes: 50 additions & 0 deletions coverage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package runn

import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/k1LoW/runn/testutil"
"github.com/tenntenn/golden"
)

func TestCoverage(t *testing.T) {
tests := []struct {
book string
}{
{"testdata/book/httpbin.yml"},
{"testdata/book/grpc.yml"},
}
t.Setenv("DEBUG", "false")
ctx := context.Background()
for _, tt := range tests {
tt := tt
t.Run(tt.book, func(t *testing.T) {
t.Parallel()
o, err := New(Book(tt.book))
if err != nil {
t.Fatal(err)
}
cov, err := o.collectCoverage(ctx)
if err != nil {
t.Fatal(err)
}
got, err := json.Marshal(cov)
if err != nil {
t.Fatal(err)
}
f := fmt.Sprintf("%s.coverage.json", filepath.Base(tt.book))
if os.Getenv("UPDATE_GOLDEN") != "" {
golden.Update(t, testutil.Testdata(), f, got)
return
}
if diff := golden.Diff(t, testutil.Testdata(), f, got); diff != "" {
t.Error(diff)
}
})
}
}
Loading

0 comments on commit f34d6c1

Please sign in to comment.