Skip to content

Commit

Permalink
Merge pull request #264 from k1LoW/more-validate
Browse files Browse the repository at this point in the history
Enhance validation related to custom metrics (e.g., key uniqueness)
  • Loading branch information
k1LoW authored Sep 3, 2023
2 parents c1b14db + bc74fc8 commit 30a8e47
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,16 @@ var dumpCmd = &cobra.Command{
}
}

if err := r.CollectCustomMetrics(); err != nil {
cmd.PrintErrf("Skip collecting custom metrics: %v\n", err)
}

if r.CountMeasured() == 0 {
return errors.New("nothing could be measured")
}

if err := r.Validate(); err != nil {
return fmt.Errorf("validation error: %w\n", err)
return fmt.Errorf("validation error: %w", err)
}
cmd.Println(r.String())
return nil
Expand Down
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ var rootCmd = &cobra.Command{
return errors.New("nothing could be measured")
}

if err := r.Validate(); err != nil {
return fmt.Errorf("validation error: %w", err)
}

cmd.Println("")
if err := r.Out(os.Stdout); err != nil {
return err
Expand Down
9 changes: 9 additions & 0 deletions report/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/olekukonko/tablewriter"
"github.com/samber/lo"
"github.com/xeipuuv/gojsonschema"
)

Expand Down Expand Up @@ -172,6 +173,14 @@ func (s *CustomMetricSet) Validate() error {
}
return errs
}
if len(s.Metrics) != len(lo.UniqBy(s.Metrics, func(m *CustomMetric) string {
return m.Key
})) {
return fmt.Errorf("key of metrics must be unique: %s", lo.Map(s.Metrics, func(m *CustomMetric, _ int) string {
return m.Key
}))
}

return nil
}

Expand Down
8 changes: 8 additions & 0 deletions report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,14 @@ func (r *Report) Validate() error {
if r.Commit == "" {
return fmt.Errorf("coverage report '%s' (env %s) is not set", "commit", "GITHUB_SHA")
}

if len(r.CustomMetrics) != len(lo.UniqBy(r.CustomMetrics, func(s *CustomMetricSet) string {
return s.Key
})) {
return fmt.Errorf("key of custom metrics must be unique: %s", lo.Map(r.CustomMetrics, func(s *CustomMetricSet, _ int) string {
return s.Key
}))
}
return nil
}

Expand Down

0 comments on commit 30a8e47

Please sign in to comment.