Skip to content

Commit

Permalink
chore: updated linting config, relinted
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
fredbi committed Nov 30, 2023
1 parent 832384d commit 0da2f93
Show file tree
Hide file tree
Showing 29 changed files with 434 additions and 450 deletions.
21 changes: 20 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ linters-settings:
threshold: 200
goconst:
min-len: 2
min-occurrences: 2
min-occurrences: 3

linters:
enable-all: true
Expand Down Expand Up @@ -40,3 +40,22 @@ linters:
- tparallel
- thelper
- ifshort
- exhaustruct
- varnamelen
- gci
- depguard
- errchkjson
- inamedparam
- nonamedreturns
- musttag
- ireturn
- forcetypeassert
- cyclop
# deprecated linters
- deadcode
- interfacer
- scopelint
- varcheck
- structcheck
- golint
- nosnakecase
4 changes: 2 additions & 2 deletions circular_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func TestExpandCircular_SpecExpansion(t *testing.T) {

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

assert.NoError(t, ExpandSchemaWithBasePath(root, nil, &ExpandOptions{}))
require.NoError(t, ExpandSchemaWithBasePath(root, nil, &ExpandOptions{}))

jazon = asJSON(t, root)

Expand Down
11 changes: 5 additions & 6 deletions contact_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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

func TestIntegrationContactInfo(t *testing.T) {
b, err := json.MarshalIndent(contactInfo, "", "\t")
if assert.NoError(t, err) {
assert.Equal(t, contactInfoJSON, string(b))
}
require.NoError(t, err)
assert.Equal(t, contactInfoJSON, string(b))

actual := ContactInfo{}
err = json.Unmarshal([]byte(contactInfoJSON), &actual)
if assert.NoError(t, err) {
assert.EqualValues(t, contactInfo, actual)
}
require.NoError(t, err)
assert.EqualValues(t, contactInfo, actual)
}
1 change: 0 additions & 1 deletion expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
// all relative $ref's will be resolved from there.
//
// PathLoader injects a document loading method. By default, this resolves to the function provided by the SpecLoader package variable.
//
type ExpandOptions struct {
RelativeBase string // the path to the root document to expand. This is a file, not a directory
SkipSchemas bool // do not expand schemas, just paths, parameters and responses
Expand Down
10 changes: 3 additions & 7 deletions expander_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func TestExpand_ContinueOnError(t *testing.T) {

// missing $ref in spec
missingRefDoc, err := jsonDoc(specPath)
assert.NoError(t, err)
require.NoError(t, err)

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

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

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

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

rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(200)
bb, _ := json.Marshal(ctnt)
_, _ = rw.Write(bb)
return
}

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

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

if req.URL.Path == "/deeper/arrayProp.json" {
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(200)
b, _ := json.Marshal(map[string]interface{}{
"type": "array",
"items": map[string]interface{}{
Expand Down
49 changes: 26 additions & 23 deletions header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ import (

"github.com/go-openapi/swag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const epsilon = 1e-9

func float64Ptr(f float64) *float64 {
return &f
}
Expand Down Expand Up @@ -83,47 +86,47 @@ const headerJSON = `{

func TestIntegrationHeader(t *testing.T) {
var actual Header
if assert.NoError(t, json.Unmarshal([]byte(headerJSON), &actual)) {
assert.EqualValues(t, actual, header)
}
require.NoError(t, json.Unmarshal([]byte(headerJSON), &actual))
assert.EqualValues(t, actual, header)

assertParsesJSON(t, headerJSON, header)
}

func TestJSONLookupHeader(t *testing.T) {
var def string
res, err := header.JSONLookup("default")
if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, def, res) {
t.FailNow()
return
}
def = res.(string)
require.NoError(t, err)
require.NotNil(t, res)
require.IsType(t, def, res)

var ok bool
def, ok = res.(string)
require.True(t, ok)
assert.Equal(t, "8", def)

var x *interface{}
res, err = header.JSONLookup("x-framework")
if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, x, res) {
t.FailNow()
return
}
require.NoError(t, err)
require.NotNil(t, res)
require.IsType(t, x, res)

x = res.(*interface{})
x, ok = res.(*interface{})
require.True(t, ok)
assert.EqualValues(t, "swagger-go", *x)

res, err = header.JSONLookup("unknown")
if !assert.Error(t, err) || !assert.Nil(t, res) {
t.FailNow()
return
}
require.Error(t, err)
require.Nil(t, res)

var max *float64
res, err = header.JSONLookup("maximum")
if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, max, res) {
t.FailNow()
return
}
max = res.(*float64)
assert.Equal(t, float64(100), *max)
require.NoError(t, err)
require.NotNil(t, res)
require.IsType(t, max, res)

max, ok = res.(*float64)
require.True(t, ok)
assert.InDelta(t, float64(100), *max, epsilon)
}

func TestResponseHeaueder(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion helpers_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func assertRefInJSONRegexp(t testing.TB, jazon, match string) {
// assertRefExpand ensures that all $ref in some json doc expand properly against a root document.
//
// "exclude" is a regexp pattern to ignore certain $ref (e.g. some specs may embed $ref that are not processed, such as extensions).
func assertRefExpand(t *testing.T, jazon, exclude string, root interface{}, opts ...*spec.ExpandOptions) {
func assertRefExpand(t *testing.T, jazon, _ string, root interface{}, opts ...*spec.ExpandOptions) {
if len(opts) > 0 {
assertRefWithFunc(t, "expand-with-base", jazon, "", func(t *testing.T, match string) {
ref := spec.RefSchema(match)
Expand Down
2 changes: 1 addition & 1 deletion helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func assertNoRef(t testing.TB, jazon string) {
// assertRefExpand ensures that all $ref in some json doc expand properly against a root document.
//
// "exclude" is a regexp pattern to ignore certain $ref (e.g. some specs may embed $ref that are not processed, such as extensions).
func assertRefExpand(t *testing.T, jazon, exclude string, root interface{}, opts ...*ExpandOptions) {
func assertRefExpand(t *testing.T, jazon, _ string, root interface{}, opts ...*ExpandOptions) {
assertRefWithFunc(t, jazon, "", func(t *testing.T, match string) {
ref := RefSchema(match)
if len(opts) > 0 {
Expand Down
19 changes: 7 additions & 12 deletions info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const infoJSON = `{
Expand Down Expand Up @@ -57,25 +58,19 @@ var info = Info{

func TestIntegrationInfo_Serialize(t *testing.T) {
b, err := json.MarshalIndent(info, "", "\t")
if assert.NoError(t, err) {
assert.Equal(t, infoJSON, string(b))
}
require.NoError(t, err)
assert.Equal(t, infoJSON, string(b))
}

func TestIntegrationInfo_Deserialize(t *testing.T) {
actual := Info{}
err := json.Unmarshal([]byte(infoJSON), &actual)
if assert.NoError(t, err) {
assert.EqualValues(t, info, actual)
}
require.NoError(t, json.Unmarshal([]byte(infoJSON), &actual))
assert.EqualValues(t, info, actual)
}

func TestInfoGobEncoding(t *testing.T) {
var src, dst Info
if assert.NoError(t, json.Unmarshal([]byte(infoJSON), &src)) {
assert.EqualValues(t, src, info)
} else {
t.FailNow()
}
require.NoError(t, json.Unmarshal([]byte(infoJSON), &src))
assert.EqualValues(t, src, info)
doTestAnyGobEncoding(t, &src, &dst)
}
52 changes: 26 additions & 26 deletions items_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/go-openapi/swag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var items = Items{
Expand Down Expand Up @@ -74,9 +75,8 @@ const itemsJSON = `{

func TestIntegrationItems(t *testing.T) {
var actual Items
if assert.NoError(t, json.Unmarshal([]byte(itemsJSON), &actual)) {
assert.EqualValues(t, actual, items)
}
require.NoError(t, json.Unmarshal([]byte(itemsJSON), &actual))
assert.EqualValues(t, actual, items)

assertParsesJSON(t, itemsJSON, items)
}
Expand Down Expand Up @@ -152,38 +152,38 @@ func TestItemsBuilder(t *testing.T) {

func TestJSONLookupItems(t *testing.T) {
res, err := items.JSONLookup("$ref")
if !assert.NoError(t, err) {
t.FailNow()
return
}
if assert.IsType(t, &Ref{}, res) {
ref := res.(*Ref)
assert.EqualValues(t, MustCreateRef("Dog"), *ref)
}
require.NoError(t, err)
require.NotNil(t, res)
require.IsType(t, &Ref{}, res)

var ok bool
ref, ok := res.(*Ref)
require.True(t, ok)
assert.EqualValues(t, MustCreateRef("Dog"), *ref)

var max *float64
res, err = items.JSONLookup("maximum")
if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, max, res) {
t.FailNow()
return
}
max = res.(*float64)
assert.Equal(t, float64(100), *max)
require.NoError(t, err)
require.NotNil(t, res)
require.IsType(t, max, res)

max, ok = res.(*float64)
require.True(t, ok)
assert.InDelta(t, float64(100), *max, epsilon)

var f string
res, err = items.JSONLookup("collectionFormat")
if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, f, res) {
t.FailNow()
return
}
f = res.(string)
require.NoError(t, err)
require.NotNil(t, res)
require.IsType(t, f, res)

f, ok = res.(string)
require.True(t, ok)
assert.Equal(t, "csv", f)

res, err = items.JSONLookup("unknown")
if !assert.Error(t, err) || !assert.Nil(t, res) {
t.FailNow()
return
}
require.Error(t, err)
require.Nil(t, res)
}

func TestItemsWithValidation(t *testing.T) {
Expand Down
11 changes: 5 additions & 6 deletions license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var license = License{
Expand All @@ -36,13 +37,11 @@ func TestIntegrationLicense(t *testing.T) {
// const licenseYAML = "name: the name\nurl: the url\n"

b, err := json.MarshalIndent(license, "", "\t")
if assert.NoError(t, err) {
assert.Equal(t, licenseJSON, string(b))
}
require.NoError(t, err)
assert.Equal(t, licenseJSON, string(b))

actual := License{}
err = json.Unmarshal([]byte(licenseJSON), &actual)
if assert.NoError(t, err) {
assert.EqualValues(t, license, actual)
}
require.NoError(t, err)
assert.EqualValues(t, license, actual)
}
2 changes: 1 addition & 1 deletion normalizer_nonwindows.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ func repairURI(in string) (*url.URL, string) {
return u, ""
}

func fixWindowsURI(u *url.URL, in string) {
func fixWindowsURI(_ *url.URL, _ string) {
}
Loading

0 comments on commit 0da2f93

Please sign in to comment.