Skip to content

Commit 0da2f93

Browse files
committed
chore: updated linting config, relinted
* updated linter config * refactored tests: if !assert => require * fixed a few other linting issues other than test code: x := append(y, z...), typos Signed-off-by: Frederic BIDON <[email protected]>
1 parent 832384d commit 0da2f93

29 files changed

+434
-450
lines changed

.golangci.yml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ linters-settings:
1111
threshold: 200
1212
goconst:
1313
min-len: 2
14-
min-occurrences: 2
14+
min-occurrences: 3
1515

1616
linters:
1717
enable-all: true
@@ -40,3 +40,22 @@ linters:
4040
- tparallel
4141
- thelper
4242
- ifshort
43+
- exhaustruct
44+
- varnamelen
45+
- gci
46+
- depguard
47+
- errchkjson
48+
- inamedparam
49+
- nonamedreturns
50+
- musttag
51+
- ireturn
52+
- forcetypeassert
53+
- cyclop
54+
# deprecated linters
55+
- deadcode
56+
- interfacer
57+
- scopelint
58+
- varcheck
59+
- structcheck
60+
- golint
61+
- nosnakecase

circular_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func TestExpandCircular_SpecExpansion(t *testing.T) {
219219

220220
func TestExpandCircular_RemoteCircularID(t *testing.T) {
221221
go func() {
222-
err := http.ListenAndServe("localhost:1234", http.FileServer(http.Dir("fixtures/more_circulars/remote")))
222+
err := http.ListenAndServe("localhost:1234", http.FileServer(http.Dir("fixtures/more_circulars/remote"))) //#nosec
223223
if err != nil {
224224
panic(err.Error())
225225
}
@@ -232,7 +232,7 @@ func TestExpandCircular_RemoteCircularID(t *testing.T) {
232232
assertRefResolve(t, jazon, "", root, &ExpandOptions{RelativeBase: fixturePath})
233233
assertRefExpand(t, jazon, "", root, &ExpandOptions{RelativeBase: fixturePath})
234234

235-
assert.NoError(t, ExpandSchemaWithBasePath(root, nil, &ExpandOptions{}))
235+
require.NoError(t, ExpandSchemaWithBasePath(root, nil, &ExpandOptions{}))
236236

237237
jazon = asJSON(t, root)
238238

contact_info_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"testing"
2020

2121
"github.com/stretchr/testify/assert"
22+
"github.com/stretchr/testify/require"
2223
)
2324

2425
const contactInfoJSON = `{
@@ -36,13 +37,11 @@ var contactInfo = ContactInfo{ContactInfoProps: ContactInfoProps{
3637

3738
func TestIntegrationContactInfo(t *testing.T) {
3839
b, err := json.MarshalIndent(contactInfo, "", "\t")
39-
if assert.NoError(t, err) {
40-
assert.Equal(t, contactInfoJSON, string(b))
41-
}
40+
require.NoError(t, err)
41+
assert.Equal(t, contactInfoJSON, string(b))
4242

4343
actual := ContactInfo{}
4444
err = json.Unmarshal([]byte(contactInfoJSON), &actual)
45-
if assert.NoError(t, err) {
46-
assert.EqualValues(t, contactInfo, actual)
47-
}
45+
require.NoError(t, err)
46+
assert.EqualValues(t, contactInfo, actual)
4847
}

expander.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
// all relative $ref's will be resolved from there.
2828
//
2929
// PathLoader injects a document loading method. By default, this resolves to the function provided by the SpecLoader package variable.
30-
//
3130
type ExpandOptions struct {
3231
RelativeBase string // the path to the root document to expand. This is a file, not a directory
3332
SkipSchemas bool // do not expand schemas, just paths, parameters and responses

expander_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func TestExpand_ContinueOnError(t *testing.T) {
353353

354354
// missing $ref in spec
355355
missingRefDoc, err := jsonDoc(specPath)
356-
assert.NoError(t, err)
356+
require.NoError(t, err)
357357

358358
testCase := struct {
359359
Input *Swagger `json:"input"`
@@ -367,7 +367,7 @@ func TestExpand_ContinueOnError(t *testing.T) {
367367
}
368368
require.NoError(t, ExpandSpec(testCase.Input, opts))
369369

370-
assert.Equal(t, testCase.Input, testCase.Expected, "Should continue expanding spec when a definition can't be found.")
370+
assert.Equal(t, testCase.Expected, testCase.Input, "Should continue expanding spec when a definition can't be found.")
371371

372372
// missing $ref in items
373373
doc, err := jsonDoc("fixtures/expansion/missingItemRef.json")
@@ -791,7 +791,7 @@ func resolutionContextServer() *httptest.Server {
791791
ctnt["id"] = servedAt
792792

793793
rw.Header().Set("Content-Type", "application/json")
794-
rw.WriteHeader(200)
794+
rw.WriteHeader(http.StatusOK)
795795
bb, _ := json.Marshal(ctnt)
796796
_, _ = rw.Write(bb)
797797
return
@@ -803,15 +803,13 @@ func resolutionContextServer() *httptest.Server {
803803
ctnt["id"] = servedAt
804804

805805
rw.Header().Set("Content-Type", "application/json")
806-
rw.WriteHeader(200)
807806
bb, _ := json.Marshal(ctnt)
808807
_, _ = rw.Write(bb)
809808
return
810809
}
811810

812811
if req.URL.Path == "/boolProp.json" {
813812
rw.Header().Set("Content-Type", "application/json")
814-
rw.WriteHeader(200)
815813
b, _ := json.Marshal(map[string]interface{}{
816814
"type": "boolean",
817815
})
@@ -821,7 +819,6 @@ func resolutionContextServer() *httptest.Server {
821819

822820
if req.URL.Path == "/deeper/stringProp.json" {
823821
rw.Header().Set("Content-Type", "application/json")
824-
rw.WriteHeader(200)
825822
b, _ := json.Marshal(map[string]interface{}{
826823
"type": "string",
827824
})
@@ -831,7 +828,6 @@ func resolutionContextServer() *httptest.Server {
831828

832829
if req.URL.Path == "/deeper/arrayProp.json" {
833830
rw.Header().Set("Content-Type", "application/json")
834-
rw.WriteHeader(200)
835831
b, _ := json.Marshal(map[string]interface{}{
836832
"type": "array",
837833
"items": map[string]interface{}{

header_test.go

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ import (
2020

2121
"github.com/go-openapi/swag"
2222
"github.com/stretchr/testify/assert"
23+
"github.com/stretchr/testify/require"
2324
)
2425

26+
const epsilon = 1e-9
27+
2528
func float64Ptr(f float64) *float64 {
2629
return &f
2730
}
@@ -83,47 +86,47 @@ const headerJSON = `{
8386

8487
func TestIntegrationHeader(t *testing.T) {
8588
var actual Header
86-
if assert.NoError(t, json.Unmarshal([]byte(headerJSON), &actual)) {
87-
assert.EqualValues(t, actual, header)
88-
}
89+
require.NoError(t, json.Unmarshal([]byte(headerJSON), &actual))
90+
assert.EqualValues(t, actual, header)
8991

9092
assertParsesJSON(t, headerJSON, header)
9193
}
9294

9395
func TestJSONLookupHeader(t *testing.T) {
9496
var def string
9597
res, err := header.JSONLookup("default")
96-
if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, def, res) {
97-
t.FailNow()
98-
return
99-
}
100-
def = res.(string)
98+
require.NoError(t, err)
99+
require.NotNil(t, res)
100+
require.IsType(t, def, res)
101+
102+
var ok bool
103+
def, ok = res.(string)
104+
require.True(t, ok)
101105
assert.Equal(t, "8", def)
102106

103107
var x *interface{}
104108
res, err = header.JSONLookup("x-framework")
105-
if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, x, res) {
106-
t.FailNow()
107-
return
108-
}
109+
require.NoError(t, err)
110+
require.NotNil(t, res)
111+
require.IsType(t, x, res)
109112

110-
x = res.(*interface{})
113+
x, ok = res.(*interface{})
114+
require.True(t, ok)
111115
assert.EqualValues(t, "swagger-go", *x)
112116

113117
res, err = header.JSONLookup("unknown")
114-
if !assert.Error(t, err) || !assert.Nil(t, res) {
115-
t.FailNow()
116-
return
117-
}
118+
require.Error(t, err)
119+
require.Nil(t, res)
118120

119121
var max *float64
120122
res, err = header.JSONLookup("maximum")
121-
if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, max, res) {
122-
t.FailNow()
123-
return
124-
}
125-
max = res.(*float64)
126-
assert.Equal(t, float64(100), *max)
123+
require.NoError(t, err)
124+
require.NotNil(t, res)
125+
require.IsType(t, max, res)
126+
127+
max, ok = res.(*float64)
128+
require.True(t, ok)
129+
assert.InDelta(t, float64(100), *max, epsilon)
127130
}
128131

129132
func TestResponseHeaueder(t *testing.T) {

helpers_spec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func assertRefInJSONRegexp(t testing.TB, jazon, match string) {
7171
// assertRefExpand ensures that all $ref in some json doc expand properly against a root document.
7272
//
7373
// "exclude" is a regexp pattern to ignore certain $ref (e.g. some specs may embed $ref that are not processed, such as extensions).
74-
func assertRefExpand(t *testing.T, jazon, exclude string, root interface{}, opts ...*spec.ExpandOptions) {
74+
func assertRefExpand(t *testing.T, jazon, _ string, root interface{}, opts ...*spec.ExpandOptions) {
7575
if len(opts) > 0 {
7676
assertRefWithFunc(t, "expand-with-base", jazon, "", func(t *testing.T, match string) {
7777
ref := spec.RefSchema(match)

helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func assertNoRef(t testing.TB, jazon string) {
105105
// assertRefExpand ensures that all $ref in some json doc expand properly against a root document.
106106
//
107107
// "exclude" is a regexp pattern to ignore certain $ref (e.g. some specs may embed $ref that are not processed, such as extensions).
108-
func assertRefExpand(t *testing.T, jazon, exclude string, root interface{}, opts ...*ExpandOptions) {
108+
func assertRefExpand(t *testing.T, jazon, _ string, root interface{}, opts ...*ExpandOptions) {
109109
assertRefWithFunc(t, jazon, "", func(t *testing.T, match string) {
110110
ref := RefSchema(match)
111111
if len(opts) > 0 {

info_test.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"testing"
2020

2121
"github.com/stretchr/testify/assert"
22+
"github.com/stretchr/testify/require"
2223
)
2324

2425
const infoJSON = `{
@@ -57,25 +58,19 @@ var info = Info{
5758

5859
func TestIntegrationInfo_Serialize(t *testing.T) {
5960
b, err := json.MarshalIndent(info, "", "\t")
60-
if assert.NoError(t, err) {
61-
assert.Equal(t, infoJSON, string(b))
62-
}
61+
require.NoError(t, err)
62+
assert.Equal(t, infoJSON, string(b))
6363
}
6464

6565
func TestIntegrationInfo_Deserialize(t *testing.T) {
6666
actual := Info{}
67-
err := json.Unmarshal([]byte(infoJSON), &actual)
68-
if assert.NoError(t, err) {
69-
assert.EqualValues(t, info, actual)
70-
}
67+
require.NoError(t, json.Unmarshal([]byte(infoJSON), &actual))
68+
assert.EqualValues(t, info, actual)
7169
}
7270

7371
func TestInfoGobEncoding(t *testing.T) {
7472
var src, dst Info
75-
if assert.NoError(t, json.Unmarshal([]byte(infoJSON), &src)) {
76-
assert.EqualValues(t, src, info)
77-
} else {
78-
t.FailNow()
79-
}
73+
require.NoError(t, json.Unmarshal([]byte(infoJSON), &src))
74+
assert.EqualValues(t, src, info)
8075
doTestAnyGobEncoding(t, &src, &dst)
8176
}

items_test.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/go-openapi/swag"
2222
"github.com/stretchr/testify/assert"
23+
"github.com/stretchr/testify/require"
2324
)
2425

2526
var items = Items{
@@ -74,9 +75,8 @@ const itemsJSON = `{
7475

7576
func TestIntegrationItems(t *testing.T) {
7677
var actual Items
77-
if assert.NoError(t, json.Unmarshal([]byte(itemsJSON), &actual)) {
78-
assert.EqualValues(t, actual, items)
79-
}
78+
require.NoError(t, json.Unmarshal([]byte(itemsJSON), &actual))
79+
assert.EqualValues(t, actual, items)
8080

8181
assertParsesJSON(t, itemsJSON, items)
8282
}
@@ -152,38 +152,38 @@ func TestItemsBuilder(t *testing.T) {
152152

153153
func TestJSONLookupItems(t *testing.T) {
154154
res, err := items.JSONLookup("$ref")
155-
if !assert.NoError(t, err) {
156-
t.FailNow()
157-
return
158-
}
159-
if assert.IsType(t, &Ref{}, res) {
160-
ref := res.(*Ref)
161-
assert.EqualValues(t, MustCreateRef("Dog"), *ref)
162-
}
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)
163163

164164
var max *float64
165165
res, err = items.JSONLookup("maximum")
166-
if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, max, res) {
167-
t.FailNow()
168-
return
169-
}
170-
max = res.(*float64)
171-
assert.Equal(t, float64(100), *max)
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)
172173

173174
var f string
174175
res, err = items.JSONLookup("collectionFormat")
175-
if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, f, res) {
176-
t.FailNow()
177-
return
178-
}
179-
f = res.(string)
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)
180182
assert.Equal(t, "csv", f)
181183

182184
res, err = items.JSONLookup("unknown")
183-
if !assert.Error(t, err) || !assert.Nil(t, res) {
184-
t.FailNow()
185-
return
186-
}
185+
require.Error(t, err)
186+
require.Nil(t, res)
187187
}
188188

189189
func TestItemsWithValidation(t *testing.T) {

0 commit comments

Comments
 (0)