Skip to content

Commit

Permalink
Upgrade golangci-lint (#90)
Browse files Browse the repository at this point in the history
Let's bring the linters up to date so that we can run them with a
recent Go compiler. We also enable various new linters and remove
a couple of deprecated ones. Some of the new linters introduced
violations that we had to address (mainly with local suppression
comments).
  • Loading branch information
magnusbaeck authored Jun 18, 2024
1 parent fbe9eba commit 3900560
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 32 deletions.
30 changes: 28 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,50 @@
linters:
disable-all: true
enable:
- bidichk
- bodyclose
- deadcode
- canonicalheader
- contextcheck
- dupword
- durationcheck
- errcheck
- errchkjson
- exhaustive
- exportloopref
- fatcontext
- forcetypeassert
- gocyclo
- godot
- gofmt
- gosec
- gosimple
- govet
- inamedparam
- ineffassign
- makezero
- mirror
- misspell
- noctx
- nosprintfhostport
- predeclared
- reassign
- spancheck
- staticcheck
- tagalign
- tenv
- testableexamples
- testifylint
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
linters-settings:
misspell:
locale: US
usestdlibvars:
crypto-hash: true
http-method: false
sql-isolation-level: true
time-layout: true
tls-signature-scheme: true
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ GOMOD = $(GOCMD) mod
GOTEST = $(GOCMD) test -race -cover
GOLANGCI_LINT = $(GOBIN)/golangci-lint

GOLANGCI_LINT_VERSION := v1.42.1
GOLANGCI_LINT_INSTALLATION_SHA256 := 294771225087ee48c8e0a45a99ac82ed8f9c6e9d384e692ab201986479c8594f
GOLANGCI_LINT_BINARY_SHA256 := f4d62220f2484f1584f91791a040dd2ba8af7a49c5ef151fad63c75f18a8db44
GOLANGCI_LINT_VERSION := v1.59.1
GOLANGCI_LINT_INSTALLATION_SHA256 := 562223cd80a52743745ed3fc2d05927942f141b64ecc3423d0a17b312a036fe2
GOLANGCI_LINT_BINARY_SHA256 := 874eace08fa6422e3d2fdff2d12423d00654e0572f29d8f6fea772214eb8b12e

# Install tools locally instead of in $HOME/go/bin.
export GOBIN := $(CURDIR)/bin
Expand Down
10 changes: 5 additions & 5 deletions any.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,25 @@ func (a *Any) UnmarshalJSON(b []byte) error {

// ID returns the value of the meta.id field.
func (a Any) ID() string {
return a.event.(MetaTeller).ID()
return a.event.(MetaTeller).ID() // nolint:forcetypeassert
}

// Type returns the value of the meta.type field.
func (a Any) Type() string {
return a.event.(MetaTeller).Type()
return a.event.(MetaTeller).Type() // nolint:forcetypeassert
}

// Version returns the value of the meta.version field.
func (a Any) Version() string {
return a.event.(MetaTeller).Version()
return a.event.(MetaTeller).Version() // nolint:forcetypeassert
}

// Time returns the value of the meta.time field.
func (a Any) Time() int64 {
return a.event.(MetaTeller).Time()
return a.event.(MetaTeller).Time() // nolint:forcetypeassert
}

// DomainID returns the value of the meta.source.domainId field.
func (a Any) DomainID() string {
return a.event.(MetaTeller).DomainID()
return a.event.(MetaTeller).DomainID() // nolint:forcetypeassert
}
10 changes: 5 additions & 5 deletions events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ func TestMajorVersionFactory(t *testing.T) {
// We're deliberately avoiding the most recent major version to avoid having
// to update this test if there's another minor version of the event.
event, err := NewActivityTriggeredV3()
assert.NoError(t, err)
require.NoError(t, err)

assert.Equal(t, "EiffelActivityTriggeredEvent", event.Meta.Type)
_, err = uuid.Parse(event.Meta.ID)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "3.0.0", event.Meta.Version)

// Sanity check that meta.time is within two minutes of the current time.
Expand All @@ -77,17 +77,17 @@ func TestMajorVersionFactory(t *testing.T) {
func TestFactoriesWithModifiers(t *testing.T) {
// Single modifier
event, err := NewActivityTriggeredV3(WithSourceName("name"))
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "name", event.Meta.Source.Name)

// Modifiers are applied in order
event, err = NewActivityTriggeredV3(WithSourceName("name1"), WithSourceName("name2"))
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "name2", event.Meta.Source.Name)

// Different kinds of modifiers
event, err = NewActivityTriggeredV3(WithSourceHost("host"), WithSourceName("name"))
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "host", event.Meta.Source.Host)
assert.Equal(t, "name", event.Meta.Source.Name)

Expand Down
5 changes: 3 additions & 2 deletions fieldsetter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

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

type setFieldLevel1TestStruct struct {
Expand Down Expand Up @@ -95,11 +96,11 @@ func TestSetField(t *testing.T) {
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
err := setField(reflect.ValueOf(&tc.input), tc.field, tc.value)
err := setField(reflect.ValueOf(&tc.input), tc.field, tc.value) // nolint:gosec
if tc.expectedErr != "" {
assert.Contains(t, err.Error(), tc.expectedErr)
} else {
assert.NoError(t, err)
require.NoError(t, err)
}
assert.Equal(t, tc.expected, tc.input)
})
Expand Down
10 changes: 5 additions & 5 deletions internal/cmd/eventgen/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ import (

// goEnum represents a Go type that acts as an enumeration. This type has the following form:
//
// type EnumName UnderlyingType
// type EnumName UnderlyingType
//
// const (
// EnumName_Value1 EnumName = "VALUE1"
// EnumName_Value2 EnumName = "VALUE2"
// )
// const (
// EnumName_Value1 EnumName = "VALUE1"
// EnumName_Value2 EnumName = "VALUE2"
// )
//
// It currently assumes that the underlying type is a string and that the
// string (with limited mangling) can be appended to the name of the enum type
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/eventgen/eventtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func goTypeFromSchema(parent *goStruct, name string, schema *jsschema.Schema) (g

fullName := parent.qualifiedFieldName(name)

switch schema.Type[0] {
switch schema.Type[0] { // nolint:exhaustive
case jsschema.StringType:
typ = &goPrimitiveType{name: "string"}
case jsschema.IntegerType:
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/eventgen/schemadiscovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

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

func TestFindSchemas(t *testing.T) {
Expand Down Expand Up @@ -163,7 +164,7 @@ func TestFindSchemas(t *testing.T) {
if tc.expectedError != nil {
assert.ErrorIs(t, err, tc.expectedError)
} else {
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, tc.expected, schemas)
}
})
Expand Down
17 changes: 9 additions & 8 deletions modifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

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

func ExampleModifier() {
Expand Down Expand Up @@ -60,46 +61,46 @@ func TestPurlFromBuildInfo(t *testing.T) {
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expected, purlFromBuildInfo(&tc.input).String())
assert.Equal(t, tc.expected, purlFromBuildInfo(&tc.input).String()) // nolint:gosec
})
}
}

func TestWithSourceDomainID(t *testing.T) {
event, err := NewCompositionDefinedV3(WithSourceDomainID("example.com"))
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "example.com", event.Meta.Source.DomainID)
}

func TestWithSourceHost(t *testing.T) {
event, err := NewCompositionDefinedV3(WithSourceHost("hostname.example.com"))
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "hostname.example.com", event.Meta.Source.Host)
}

func TestWithSourceName(t *testing.T) {
event, err := NewCompositionDefinedV3(WithSourceName("My Application"))
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "My Application", event.Meta.Source.Name)
}

func TestWithSourceSerializer(t *testing.T) {
event, err := NewCompositionDefinedV3(WithSourceSerializer("pkg:golang/github.com/foo/bar"))
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "pkg:golang/github.com/foo/bar", event.Meta.Source.Serializer)
}

func TestWithSourceURI(t *testing.T) {
event, err := NewCompositionDefinedV3(WithSourceURI("http://www.example.com"))
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "http://www.example.com", event.Meta.Source.URI)
}

func TestWithVersion(t *testing.T) {
newestEventVersion, err := NewCompositionDefinedV3()
assert.NoError(t, err)
require.NoError(t, err)
customEventVersion, err := NewCompositionDefinedV3(WithVersion("3.1.0"))
assert.NoError(t, err)
require.NoError(t, err)

// Make sure we're not accidentally getting a pass because the default
// version happens to coincide with the one we're request (which shouldn't
Expand Down

0 comments on commit 3900560

Please sign in to comment.