Skip to content

Commit 0201d0c

Browse files
committed
Relint
* updated linter config * fixed linting errors Signed-off-by: Frederic BIDON <[email protected]>
1 parent de35272 commit 0201d0c

20 files changed

+167
-148
lines changed

.golangci.yml

+13-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
linters-settings:
2-
govet:
3-
check-shadowing: true
4-
golint:
5-
min-confidence: 0
62
gocyclo:
73
min-complexity: 45
8-
maligned:
9-
suggest-new: true
104
dupl:
115
threshold: 200
126
goconst:
@@ -16,7 +10,7 @@ linters-settings:
1610
linters:
1711
enable-all: true
1812
disable:
19-
- maligned
13+
- recvcheck
2014
- unparam
2115
- lll
2216
- gochecknoinits
@@ -29,17 +23,13 @@ linters:
2923
- wrapcheck
3024
- testpackage
3125
- nlreturn
32-
- gomnd
33-
- exhaustivestruct
34-
- goerr113
3526
- errorlint
3627
- nestif
3728
- godot
3829
- gofumpt
3930
- paralleltest
4031
- tparallel
4132
- thelper
42-
- ifshort
4333
- exhaustruct
4434
- varnamelen
4535
- gci
@@ -52,10 +42,15 @@ linters:
5242
- forcetypeassert
5343
- cyclop
5444
# deprecated linters
55-
- deadcode
56-
- interfacer
57-
- scopelint
58-
- varcheck
59-
- structcheck
60-
- golint
61-
- nosnakecase
45+
#- deadcode
46+
#- interfacer
47+
#- scopelint
48+
#- varcheck
49+
#- structcheck
50+
#- golint
51+
#- nosnakecase
52+
#- maligned
53+
#- goerr113
54+
#- ifshort
55+
#- gomnd
56+
#- exhaustivestruct

contact_info_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var contactInfo = ContactInfo{ContactInfoProps: ContactInfoProps{
3838
func TestIntegrationContactInfo(t *testing.T) {
3939
b, err := json.MarshalIndent(contactInfo, "", "\t")
4040
require.NoError(t, err)
41-
assert.Equal(t, contactInfoJSON, string(b))
41+
assert.JSONEq(t, contactInfoJSON, string(b))
4242

4343
actual := ContactInfo{}
4444
err = json.Unmarshal([]byte(contactInfoJSON), &actual)

debug_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ var (
2727
)
2828

2929
func TestDebug(t *testing.T) {
30-
tmpFile, _ := os.CreateTemp("", "debug-test")
30+
// usetesting linter disabled until https://github.com/golang/go/issues/71544 is fixed for windows
31+
tmpFile, _ := os.CreateTemp("", "debug-test") //nolint:usetesting
3132
tmpName := tmpFile.Name()
3233
defer func() {
3334
Debug = false

errors.go

+3
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ var (
1616

1717
// ErrExpandUnsupportedType indicates that $ref expansion is attempted on some invalid type
1818
ErrExpandUnsupportedType = errors.New("expand: unsupported type. Input should be of type *Parameter or *Response")
19+
20+
// ErrSpec is an error raised by the spec package
21+
ErrSpec = errors.New("spec error")
1922
)

expander.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"fmt"
2020
)
2121

22+
const smallPrealloc = 10
23+
2224
// ExpandOptions provides options for the spec expander.
2325
//
2426
// RelativeBase is the path to the root document. This can be a remote URL or a path to a local file.
@@ -56,7 +58,7 @@ func ExpandSpec(spec *Swagger, options *ExpandOptions) error {
5658

5759
if !options.SkipSchemas {
5860
for key, definition := range spec.Definitions {
59-
parentRefs := make([]string, 0, 10)
61+
parentRefs := make([]string, 0, smallPrealloc)
6062
parentRefs = append(parentRefs, "#/definitions/"+key)
6163

6264
def, err := expandSchema(definition, parentRefs, resolver, specBasePath)
@@ -160,7 +162,7 @@ func ExpandSchemaWithBasePath(schema *Schema, cache ResolutionCache, opts *Expan
160162

161163
resolver := defaultSchemaLoader(nil, opts, cache, nil)
162164

163-
parentRefs := make([]string, 0, 10)
165+
parentRefs := make([]string, 0, smallPrealloc)
164166
s, err := expandSchema(*schema, parentRefs, resolver, opts.RelativeBase)
165167
if err != nil {
166168
return err
@@ -386,7 +388,7 @@ func expandPathItem(pathItem *PathItem, resolver *schemaLoader, basePath string)
386388
return nil
387389
}
388390

389-
parentRefs := make([]string, 0, 10)
391+
parentRefs := make([]string, 0, smallPrealloc)
390392
if err := resolver.deref(pathItem, parentRefs, basePath); resolver.shouldStopOnError(err) {
391393
return err
392394
}
@@ -546,7 +548,7 @@ func expandParameterOrResponse(input interface{}, resolver *schemaLoader, basePa
546548
return nil
547549
}
548550

549-
parentRefs := make([]string, 0, 10)
551+
parentRefs := make([]string, 0, smallPrealloc)
550552
if ref != nil {
551553
// dereference this $ref
552554
if err = resolver.deref(input, parentRefs, basePath); resolver.shouldStopOnError(err) {

header.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ func (h *Header) WithDefault(defaultValue interface{}) *Header {
7474
}
7575

7676
// WithMaxLength sets a max length value
77-
func (h *Header) WithMaxLength(max int64) *Header {
78-
h.MaxLength = &max
77+
func (h *Header) WithMaxLength(maximum int64) *Header {
78+
h.MaxLength = &maximum
7979
return h
8080
}
8181

8282
// WithMinLength sets a min length value
83-
func (h *Header) WithMinLength(min int64) *Header {
84-
h.MinLength = &min
83+
func (h *Header) WithMinLength(minimum int64) *Header {
84+
h.MinLength = &minimum
8585
return h
8686
}
8787

@@ -98,15 +98,15 @@ func (h *Header) WithMultipleOf(number float64) *Header {
9898
}
9999

100100
// WithMaximum sets a maximum number value
101-
func (h *Header) WithMaximum(max float64, exclusive bool) *Header {
102-
h.Maximum = &max
101+
func (h *Header) WithMaximum(maximum float64, exclusive bool) *Header {
102+
h.Maximum = &maximum
103103
h.ExclusiveMaximum = exclusive
104104
return h
105105
}
106106

107107
// WithMinimum sets a minimum number value
108-
func (h *Header) WithMinimum(min float64, exclusive bool) *Header {
109-
h.Minimum = &min
108+
func (h *Header) WithMinimum(minimum float64, exclusive bool) *Header {
109+
h.Minimum = &minimum
110110
h.ExclusiveMinimum = exclusive
111111
return h
112112
}

header_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,15 @@ func TestJSONLookupHeader(t *testing.T) {
118118
require.Error(t, err)
119119
require.Nil(t, res)
120120

121-
var max *float64
121+
var maximum *float64
122122
res, err = header.JSONLookup("maximum")
123123
require.NoError(t, err)
124124
require.NotNil(t, res)
125-
require.IsType(t, max, res)
125+
require.IsType(t, maximum, res)
126126

127-
max, ok = res.(*float64)
127+
maximum, ok = res.(*float64)
128128
require.True(t, ok)
129-
assert.InDelta(t, float64(100), *max, epsilon)
129+
assert.InDelta(t, float64(100), *maximum, epsilon)
130130
}
131131

132132
func TestResponseHeaueder(t *testing.T) {

info_test.go

+18-16
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const infoJSON = `{
3939
"x-framework": "go-swagger"
4040
}`
4141

42-
var info = Info{
42+
var testInfo = Info{
4343
InfoProps: InfoProps{
4444
Version: "1.0.9-abcd",
4545
Title: "Swagger Sample API",
@@ -56,21 +56,23 @@ var info = Info{
5656
VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{"x-framework": "go-swagger"}},
5757
}
5858

59-
func TestIntegrationInfo_Serialize(t *testing.T) {
60-
b, err := json.MarshalIndent(info, "", "\t")
61-
require.NoError(t, err)
62-
assert.Equal(t, infoJSON, string(b))
63-
}
59+
func TestInfo(t *testing.T) {
60+
t.Run("should marshal Info", func(t *testing.T) {
61+
b, err := json.MarshalIndent(testInfo, "", "\t")
62+
require.NoError(t, err)
63+
assert.JSONEq(t, infoJSON, string(b))
64+
})
6465

65-
func TestIntegrationInfo_Deserialize(t *testing.T) {
66-
actual := Info{}
67-
require.NoError(t, json.Unmarshal([]byte(infoJSON), &actual))
68-
assert.EqualValues(t, info, actual)
69-
}
66+
t.Run("should unmarshal Info", func(t *testing.T) {
67+
actual := Info{}
68+
require.NoError(t, json.Unmarshal([]byte(infoJSON), &actual))
69+
assert.EqualValues(t, testInfo, actual)
70+
})
7071

71-
func TestInfoGobEncoding(t *testing.T) {
72-
var src, dst Info
73-
require.NoError(t, json.Unmarshal([]byte(infoJSON), &src))
74-
assert.EqualValues(t, src, info)
75-
doTestAnyGobEncoding(t, &src, &dst)
72+
t.Run("should GobEncode Info", func(t *testing.T) {
73+
var src, dst Info
74+
require.NoError(t, json.Unmarshal([]byte(infoJSON), &src))
75+
assert.EqualValues(t, src, testInfo)
76+
doTestAnyGobEncoding(t, &src, &dst)
77+
})
7678
}

items.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ func (i *Items) WithDefault(defaultValue interface{}) *Items {
9797
}
9898

9999
// WithMaxLength sets a max length value
100-
func (i *Items) WithMaxLength(max int64) *Items {
101-
i.MaxLength = &max
100+
func (i *Items) WithMaxLength(maximum int64) *Items {
101+
i.MaxLength = &maximum
102102
return i
103103
}
104104

105105
// WithMinLength sets a min length value
106-
func (i *Items) WithMinLength(min int64) *Items {
107-
i.MinLength = &min
106+
func (i *Items) WithMinLength(minimum int64) *Items {
107+
i.MinLength = &minimum
108108
return i
109109
}
110110

@@ -121,15 +121,15 @@ func (i *Items) WithMultipleOf(number float64) *Items {
121121
}
122122

123123
// WithMaximum sets a maximum number value
124-
func (i *Items) WithMaximum(max float64, exclusive bool) *Items {
125-
i.Maximum = &max
124+
func (i *Items) WithMaximum(maximum float64, exclusive bool) *Items {
125+
i.Maximum = &maximum
126126
i.ExclusiveMaximum = exclusive
127127
return i
128128
}
129129

130130
// WithMinimum sets a minimum number value
131-
func (i *Items) WithMinimum(min float64, exclusive bool) *Items {
132-
i.Minimum = &min
131+
func (i *Items) WithMinimum(minimum float64, exclusive bool) *Items {
132+
i.Minimum = &minimum
133133
i.ExclusiveMinimum = exclusive
134134
return i
135135
}

items_test.go

+46-38
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/stretchr/testify/require"
2424
)
2525

26-
var items = Items{
26+
var testItems = Items{
2727
Refable: Refable{Ref: MustCreateRef("Dog")},
2828
CommonValidations: CommonValidations{
2929
Maximum: float64Ptr(100),
@@ -76,17 +76,17 @@ const itemsJSON = `{
7676
func TestIntegrationItems(t *testing.T) {
7777
var actual Items
7878
require.NoError(t, json.Unmarshal([]byte(itemsJSON), &actual))
79-
assert.EqualValues(t, actual, items)
79+
assert.EqualValues(t, actual, testItems)
8080

81-
assertParsesJSON(t, itemsJSON, items)
81+
assertParsesJSON(t, itemsJSON, testItems)
8282
}
8383

8484
func TestTypeNameItems(t *testing.T) {
8585
var nilItems Items
8686
assert.Equal(t, "", nilItems.TypeName())
8787

88-
assert.Equal(t, "date", items.TypeName())
89-
assert.Equal(t, "", items.ItemsTypeName())
88+
assert.Equal(t, "date", testItems.TypeName())
89+
assert.Equal(t, "", testItems.ItemsTypeName())
9090

9191
nested := Items{
9292
SimpleSchema: SimpleSchema{
@@ -151,39 +151,47 @@ func TestItemsBuilder(t *testing.T) {
151151
}
152152

153153
func TestJSONLookupItems(t *testing.T) {
154-
res, err := items.JSONLookup("$ref")
155-
require.NoError(t, err)
156-
require.NotNil(t, res)
157-
require.IsType(t, &Ref{}, res)
158-
159-
var ok bool
160-
ref, ok := res.(*Ref)
161-
require.True(t, ok)
162-
assert.EqualValues(t, MustCreateRef("Dog"), *ref)
163-
164-
var max *float64
165-
res, err = items.JSONLookup("maximum")
166-
require.NoError(t, err)
167-
require.NotNil(t, res)
168-
require.IsType(t, max, res)
169-
170-
max, ok = res.(*float64)
171-
require.True(t, ok)
172-
assert.InDelta(t, float64(100), *max, epsilon)
173-
174-
var f string
175-
res, err = items.JSONLookup("collectionFormat")
176-
require.NoError(t, err)
177-
require.NotNil(t, res)
178-
require.IsType(t, f, res)
179-
180-
f, ok = res.(string)
181-
require.True(t, ok)
182-
assert.Equal(t, "csv", f)
183-
184-
res, err = items.JSONLookup("unknown")
185-
require.Error(t, err)
186-
require.Nil(t, res)
154+
t.Run(`lookup should find "$ref"`, func(t *testing.T) {
155+
res, err := testItems.JSONLookup("$ref")
156+
require.NoError(t, err)
157+
require.NotNil(t, res)
158+
require.IsType(t, &Ref{}, res)
159+
160+
ref, ok := res.(*Ref)
161+
require.True(t, ok)
162+
assert.EqualValues(t, MustCreateRef("Dog"), *ref)
163+
})
164+
165+
t.Run(`lookup should find "maximum"`, func(t *testing.T) {
166+
var maximum *float64
167+
res, err := testItems.JSONLookup("maximum")
168+
require.NoError(t, err)
169+
require.NotNil(t, res)
170+
require.IsType(t, maximum, res)
171+
172+
var ok bool
173+
maximum, ok = res.(*float64)
174+
require.True(t, ok)
175+
assert.InDelta(t, float64(100), *maximum, epsilon)
176+
})
177+
178+
t.Run(`lookup should find "collectionFormat"`, func(t *testing.T) {
179+
var f string
180+
res, err := testItems.JSONLookup("collectionFormat")
181+
require.NoError(t, err)
182+
require.NotNil(t, res)
183+
require.IsType(t, f, res)
184+
185+
f, ok := res.(string)
186+
require.True(t, ok)
187+
assert.Equal(t, "csv", f)
188+
})
189+
190+
t.Run(`lookup should fail on "unknown"`, func(t *testing.T) {
191+
res, err := testItems.JSONLookup("unknown")
192+
require.Error(t, err)
193+
require.Nil(t, res)
194+
})
187195
}
188196

189197
func TestItemsWithValidation(t *testing.T) {

0 commit comments

Comments
 (0)