Skip to content

Commit

Permalink
chore(lint): relinted
Browse files Browse the repository at this point in the history
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
  • Loading branch information
fredbi committed Mar 3, 2024
1 parent 69f070e commit beacc0f
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 57 deletions.
6 changes: 3 additions & 3 deletions default_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (d *defaultValidator) validateDefaultValueValidAgainstSchema() *Result {
// reset explored schemas to get depth-first recursive-proof exploration
d.resetVisited()
for nm, sch := range s.spec.Spec().Definitions {
res.Merge(d.validateDefaultValueSchemaAgainstSchema(fmt.Sprintf("definitions.%s", nm), "body", &sch)) //#nosec
res.Merge(d.validateDefaultValueSchemaAgainstSchema("definitions."+nm, "body", &sch)) //#nosec
}
}
return res
Expand Down Expand Up @@ -263,7 +263,7 @@ func (d *defaultValidator) validateDefaultValueSchemaAgainstSchema(path, in stri
}
if schema.AdditionalItems != nil && schema.AdditionalItems.Schema != nil {
// NOTE: we keep validating values, even though additionalItems is not supported by Swagger 2.0 (and 3.0 as well)
res.Merge(d.validateDefaultValueSchemaAgainstSchema(fmt.Sprintf("%s.additionalItems", path), in, schema.AdditionalItems.Schema))
res.Merge(d.validateDefaultValueSchemaAgainstSchema(path+".additionalItems", in, schema.AdditionalItems.Schema))
}
for propName, prop := range schema.Properties {
res.Merge(d.validateDefaultValueSchemaAgainstSchema(path+"."+propName, in, &prop)) //#nosec
Expand All @@ -272,7 +272,7 @@ func (d *defaultValidator) validateDefaultValueSchemaAgainstSchema(path, in stri
res.Merge(d.validateDefaultValueSchemaAgainstSchema(path+"."+propName, in, &prop)) //#nosec
}
if schema.AdditionalProperties != nil && schema.AdditionalProperties.Schema != nil {
res.Merge(d.validateDefaultValueSchemaAgainstSchema(fmt.Sprintf("%s.additionalProperties", path), in, schema.AdditionalProperties.Schema))
res.Merge(d.validateDefaultValueSchemaAgainstSchema(path+".additionalProperties", in, schema.AdditionalProperties.Schema))
}
if schema.AllOf != nil {
for i, aoSch := range schema.AllOf {
Expand Down
6 changes: 3 additions & 3 deletions example_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (ex *exampleValidator) validateExampleValueValidAgainstSchema() *Result {
// reset explored schemas to get depth-first recursive-proof exploration
ex.resetVisited()
for nm, sch := range s.spec.Spec().Definitions {
res.Merge(ex.validateExampleValueSchemaAgainstSchema(fmt.Sprintf("definitions.%s", nm), "body", &sch)) //#nosec
res.Merge(ex.validateExampleValueSchemaAgainstSchema("definitions."+nm, "body", &sch)) //#nosec
}
}
return res
Expand Down Expand Up @@ -256,7 +256,7 @@ func (ex *exampleValidator) validateExampleValueSchemaAgainstSchema(path, in str
}
if schema.AdditionalItems != nil && schema.AdditionalItems.Schema != nil {
// NOTE: we keep validating values, even though additionalItems is unsupported in Swagger 2.0 (and 3.0 as well)
res.Merge(ex.validateExampleValueSchemaAgainstSchema(fmt.Sprintf("%s.additionalItems", path), in, schema.AdditionalItems.Schema))
res.Merge(ex.validateExampleValueSchemaAgainstSchema(path+".additionalItems", in, schema.AdditionalItems.Schema))
}
for propName, prop := range schema.Properties {
res.Merge(ex.validateExampleValueSchemaAgainstSchema(path+"."+propName, in, &prop)) //#nosec
Expand All @@ -265,7 +265,7 @@ func (ex *exampleValidator) validateExampleValueSchemaAgainstSchema(path, in str
res.Merge(ex.validateExampleValueSchemaAgainstSchema(path+"."+propName, in, &prop)) //#nosec
}
if schema.AdditionalProperties != nil && schema.AdditionalProperties.Schema != nil {
res.Merge(ex.validateExampleValueSchemaAgainstSchema(fmt.Sprintf("%s.additionalProperties", path), in, schema.AdditionalProperties.Schema))
res.Merge(ex.validateExampleValueSchemaAgainstSchema(path+".additionalProperties", in, schema.AdditionalProperties.Schema))
}
if schema.AllOf != nil {
for i, aoSch := range schema.AllOf {
Expand Down
2 changes: 1 addition & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (h *paramHelper) safeExpandedParamsFor(path, method, operationID string, re
operation.Parameters = resolvedParams

for _, ppr := range s.expandedAnalyzer().SafeParamsFor(method, path,
func(p spec.Parameter, err error) bool {
func(_ spec.Parameter, err error) bool {
// since params have already been expanded, there are few causes for error
res.AddErrors(someParametersBrokenMsg(path, method, operationID))
// original error from analyzer
Expand Down
4 changes: 2 additions & 2 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package validate

import (
"fmt"
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -24,7 +24,7 @@ import (

func TestHelpers_addPointerError(t *testing.T) {
res := new(Result)
r := errorHelp.addPointerError(res, fmt.Errorf("my error"), "my ref", "path")
r := errorHelp.addPointerError(res, errors.New("my error"), "my ref", "path")
require.NotEmpty(t, r.Errors)
msg := r.Errors[0].Error()
assert.Contains(t, msg, "could not resolve reference in path to $ref my ref: my error")
Expand Down
17 changes: 9 additions & 8 deletions messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func checkMustHalt(t *testing.T, haltOnErrors bool) {
}

func testWalkSpecs(t *testing.T, tested ExpectedMap, haltOnErrors, continueOnErrors bool) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
return func(path string, info os.FileInfo, _ error) error {
thisTest, found := tested.Get(info.Name())

if info.IsDir() || !found { // skip
Expand Down Expand Up @@ -255,6 +255,7 @@ func recapTest(t *testing.T, config ExpectedMap) {
}
}
func reportTest(t *testing.T, path string, res *Result, expectedMessages []ExpectedMessage, msgtype string, continueOnErrors bool) {
const expected = "Expected "
// Prints out a recap of error messages. To be enabled during development / test iterations
verifiedErrors := make([]string, 0, 50)
lines := make([]string, 0, 50)
Expand All @@ -263,17 +264,17 @@ func reportTest(t *testing.T, path string, res *Result, expectedMessages []Expec
}
t.Logf("DEVMODE:Recap of returned %s messages while validating %s ", msgtype, path)
for _, v := range verifiedErrors {
status := fmt.Sprintf("Unexpected %s", msgtype)
status := "Unexpected " + msgtype
for _, s := range expectedMessages {
if (s.WithContinueOnErrors && continueOnErrors) || !s.WithContinueOnErrors {
if s.IsRegexp {
if matched, _ := regexp.MatchString(s.Message, v); matched {
status = fmt.Sprintf("Expected %s", msgtype)
status = expected + msgtype
break
}
} else {
if strings.Contains(v, s.Message) {
status = fmt.Sprintf("Expected %s", msgtype)
status = expected + msgtype
break
}
}
Expand All @@ -284,21 +285,21 @@ func reportTest(t *testing.T, path string, res *Result, expectedMessages []Expec

for _, s := range expectedMessages {
if (s.WithContinueOnErrors && continueOnErrors) || !s.WithContinueOnErrors {
status := fmt.Sprintf("Missing %s", msgtype)
status := "Missing " + msgtype
for _, v := range verifiedErrors {
if s.IsRegexp {
if matched, _ := regexp.MatchString(s.Message, v); matched {
status = fmt.Sprintf("Expected %s", msgtype)
status = expected + msgtype
break
}
} else {
if strings.Contains(v, s.Message) {
status = fmt.Sprintf("Expected %s", msgtype)
status = expected + msgtype
break
}
}
}
if status != fmt.Sprintf("Expected %s", msgtype) {
if status != expected+msgtype {
lines = append(lines, fmt.Sprintf("[%s]%s", status, s.Message))
}
}
Expand Down
6 changes: 3 additions & 3 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package validate

import (
"fmt"
stderrors "errors"
"reflect"
"strings"

Expand Down Expand Up @@ -379,13 +379,13 @@ func (r *Result) keepRelevantErrors() *Result {
strippedErrors := []error{}
for _, e := range r.Errors {
if strings.HasPrefix(e.Error(), "IMPORTANT!") {
strippedErrors = append(strippedErrors, fmt.Errorf(strings.TrimPrefix(e.Error(), "IMPORTANT!")))
strippedErrors = append(strippedErrors, stderrors.New(strings.TrimPrefix(e.Error(), "IMPORTANT!")))
}
}
strippedWarnings := []error{}
for _, e := range r.Warnings {
if strings.HasPrefix(e.Error(), "IMPORTANT!") {
strippedWarnings = append(strippedWarnings, fmt.Errorf(strings.TrimPrefix(e.Error(), "IMPORTANT!")))
strippedWarnings = append(strippedWarnings, stderrors.New(strings.TrimPrefix(e.Error(), "IMPORTANT!")))
}
}
var strippedResult *Result
Expand Down
72 changes: 36 additions & 36 deletions result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package validate

import (
"fmt"
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -25,52 +25,52 @@ import (
// Test AddError() uniqueness
func TestResult_AddError(t *testing.T) {
r := Result{}
r.AddErrors(fmt.Errorf("one error"))
r.AddErrors(fmt.Errorf("another error"))
r.AddErrors(fmt.Errorf("one error"))
r.AddErrors(fmt.Errorf("one error"))
r.AddErrors(fmt.Errorf("one error"))
r.AddErrors(fmt.Errorf("one error"), fmt.Errorf("another error"))
r.AddErrors(errors.New("one error"))
r.AddErrors(errors.New("another error"))
r.AddErrors(errors.New("one error"))
r.AddErrors(errors.New("one error"))
r.AddErrors(errors.New("one error"))
r.AddErrors(errors.New("one error"), errors.New("another error"))

assert.Len(t, r.Errors, 2)
assert.Contains(t, r.Errors, fmt.Errorf("one error"))
assert.Contains(t, r.Errors, fmt.Errorf("another error"))
assert.Contains(t, r.Errors, errors.New("one error"))
assert.Contains(t, r.Errors, errors.New("another error"))
}

func TestResult_AddNilError(t *testing.T) {
r := Result{}
r.AddErrors(nil)
assert.Empty(t, r.Errors)

errArray := []error{fmt.Errorf("one Error"), nil, fmt.Errorf("another error")}
errArray := []error{errors.New("one Error"), nil, errors.New("another error")}
r.AddErrors(errArray...)
assert.Len(t, r.Errors, 2)
}

func TestResult_AddWarnings(t *testing.T) {
r := Result{}
r.AddErrors(fmt.Errorf("one Error"))
r.AddErrors(errors.New("one Error"))
assert.Len(t, r.Errors, 1)
assert.Empty(t, r.Warnings)

r.AddWarnings(fmt.Errorf("one Warning"))
r.AddWarnings(errors.New("one Warning"))
assert.Len(t, r.Errors, 1)
assert.Len(t, r.Warnings, 1)
}

func TestResult_Merge(t *testing.T) {
r := Result{}
r.AddErrors(fmt.Errorf("one Error"))
r.AddWarnings(fmt.Errorf("one Warning"))
r.AddErrors(errors.New("one Error"))
r.AddWarnings(errors.New("one Warning"))
r.Inc()
assert.Len(t, r.Errors, 1)
assert.Len(t, r.Warnings, 1)
assert.Equal(t, 1, r.MatchCount)

// Merge with same
r2 := Result{}
r2.AddErrors(fmt.Errorf("one Error"))
r2.AddWarnings(fmt.Errorf("one Warning"))
r2.AddErrors(errors.New("one Error"))
r2.AddWarnings(errors.New("one Warning"))
r2.Inc()

r.Merge(&r2)
Expand All @@ -81,8 +81,8 @@ func TestResult_Merge(t *testing.T) {

// Merge with new
r3 := Result{}
r3.AddErrors(fmt.Errorf("new Error"))
r3.AddWarnings(fmt.Errorf("new Warning"))
r3.AddErrors(errors.New("new Error"))
r3.AddWarnings(errors.New("new Warning"))
r3.Inc()

r.Merge(&r3)
Expand All @@ -94,20 +94,20 @@ func TestResult_Merge(t *testing.T) {

func errorFixture() (Result, Result, Result) {
r := Result{}
r.AddErrors(fmt.Errorf("one Error"))
r.AddWarnings(fmt.Errorf("one Warning"))
r.AddErrors(errors.New("one Error"))
r.AddWarnings(errors.New("one Warning"))
r.Inc()

// same
r2 := Result{}
r2.AddErrors(fmt.Errorf("one Error"))
r2.AddWarnings(fmt.Errorf("one Warning"))
r2.AddErrors(errors.New("one Error"))
r2.AddWarnings(errors.New("one Warning"))
r2.Inc()

// new
r3 := Result{}
r3.AddErrors(fmt.Errorf("new Error"))
r3.AddWarnings(fmt.Errorf("new Warning"))
r3.AddErrors(errors.New("new Error"))
r3.AddWarnings(errors.New("new Warning"))
r3.Inc()
return r, r2, r3
}
Expand Down Expand Up @@ -144,11 +144,11 @@ func TestResult_IsValid(t *testing.T) {
assert.True(t, r.IsValid())
assert.False(t, r.HasErrors())

r.AddWarnings(fmt.Errorf("one Warning"))
r.AddWarnings(errors.New("one Warning"))
assert.True(t, r.IsValid())
assert.False(t, r.HasErrors())

r.AddErrors(fmt.Errorf("one Error"))
r.AddErrors(errors.New("one Error"))
assert.False(t, r.IsValid())
assert.True(t, r.HasErrors())
}
Expand All @@ -158,10 +158,10 @@ func TestResult_HasWarnings(t *testing.T) {

assert.False(t, r.HasWarnings())

r.AddErrors(fmt.Errorf("one Error"))
r.AddErrors(errors.New("one Error"))
assert.False(t, r.HasWarnings())

r.AddWarnings(fmt.Errorf("one Warning"))
r.AddWarnings(errors.New("one Warning"))
assert.True(t, r.HasWarnings())
}

Expand All @@ -171,10 +171,10 @@ func TestResult_HasErrorsOrWarnings(t *testing.T) {

assert.False(t, r.HasErrorsOrWarnings())

r.AddErrors(fmt.Errorf("one Error"))
r.AddErrors(errors.New("one Error"))
assert.True(t, r.HasErrorsOrWarnings())

r2.AddWarnings(fmt.Errorf("one Warning"))
r2.AddWarnings(errors.New("one Warning"))
assert.True(t, r2.HasErrorsOrWarnings())

r.Merge(&r2)
Expand All @@ -183,19 +183,19 @@ func TestResult_HasErrorsOrWarnings(t *testing.T) {

func TestResult_keepRelevantErrors(t *testing.T) {
r := Result{}
r.AddErrors(fmt.Errorf("one Error"))
r.AddErrors(fmt.Errorf("IMPORTANT!Another Error"))
r.AddWarnings(fmt.Errorf("one warning"))
r.AddWarnings(fmt.Errorf("IMPORTANT!Another warning"))
r.AddErrors(errors.New("one Error"))
r.AddErrors(errors.New("IMPORTANT!Another Error"))
r.AddWarnings(errors.New("one warning"))
r.AddWarnings(errors.New("IMPORTANT!Another warning"))
assert.Len(t, r.keepRelevantErrors().Errors, 1)
assert.Len(t, r.keepRelevantErrors().Warnings, 1)
}

func TestResult_AsError(t *testing.T) {
r := Result{}
require.NoError(t, r.AsError())
r.AddErrors(fmt.Errorf("one Error"))
r.AddErrors(fmt.Errorf("additional Error"))
r.AddErrors(errors.New("one Error"))
r.AddErrors(errors.New("additional Error"))
res := r.AsError()
require.Error(t, res)

Expand Down
2 changes: 1 addition & 1 deletion swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func wantSwaggerTest(info os.FileInfo) bool {
// Just validates all fixtures in ./fixtures/go-swagger (excluded codegen cases)
func testGoSwaggerSpecs(t *testing.T, path string, expectToFail, expectToFailOnLoad map[string]bool, haltOnErrors bool) {
err := filepath.Walk(path,
func(path string, info os.FileInfo, err error) error {
func(path string, info os.FileInfo, _ error) error {
t.Run(path, func(t *testing.T) {
if !DebugTest { // when running in dev mode, run serially
t.Parallel()
Expand Down

0 comments on commit beacc0f

Please sign in to comment.