Skip to content

Commit

Permalink
update linter and fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cornelk committed Jun 26, 2024
1 parent 579805b commit b7794b0
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 48 deletions.
43 changes: 27 additions & 16 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,39 @@ linters:
- asasalint # check for pass []any as any in variadic func(...any)
- asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers
- bidichk # Checks for dangerous unicode character sequences
- bodyclose # checks whether HTTP response body is closed successfully
- contextcheck # check the function whether use a non-inherited context
- cyclop # checks function and package cyclomatic complexity
- decorder # check declaration order and count of types, constants, variables and functions
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())
- durationcheck # check for two durations multiplied together
- err113 # Golang linter to check the errors handling expressions
- errcheck # checking for unchecked errors
- errname # Checks that errors are prefixed with the `Err` and error types are suffixed with the `Error`
- errorlint # finds code that will cause problems with the error wrapping scheme introduced in Go 1.13
- exportloopref # checks for pointers to enclosing loop variables
- gci # controls golang package import order and makes it always deterministic
- gocheckcompilerdirectives # Checks that go compiler directive comments (//go:) are valid
- gocognit # Computes and checks the cognitive complexity of functions
- goconst # Finds repeated strings that could be replaced by a constant
- gocritic # Provides diagnostics that check for bugs, performance and style issues
- gocyclo # Computes and checks the cyclomatic complexity of functions
- godot # Check if comments end in a period
- goerr113 # Golang linter to check the errors handling expressions
- gofmt # checks whether code was gofmt-ed
- goimports # Check import statements are formatted according to the 'goimport' command
- gosimple # Linter for Go source code that specializes in simplifying a code
- govet # reports suspicious constructs, such as Printf calls with wrong arguments
- grouper # An analyzer to analyze expression groups
- ineffassign # Detects when assignments to existing variables are not used
- ireturn # accept Interfaces, Return Concrete Types
- maintidx # measures the maintainability index of each function
- makezero # Finds slice declarations with non-zero initial length
- mirror # reports wrong mirror patterns of bytes/strings usage
- misspell # Finds commonly misspelled English words in comments
- nakedret # Finds naked returns in functions
- nilerr # Finds the code that returns nil even if it checks that the error is not nil
- nilnil # Checks that there is no simultaneous return of `nil` error and an invalid value
- noctx # Finds sending http request without context.Context
- perfsprint # Checks that fmt.Sprintf can be replaced with a faster alternative
- prealloc # Finds slice declarations that could potentially be preallocated
- predeclared # find code that shadows one of Go's predeclared identifiers
Expand All @@ -44,6 +50,7 @@ linters:
- staticcheck # drop-in replacement of go vet
- stylecheck # Stylecheck is a replacement for golint
- tenv # detects using os.Setenv instead of t.Setenv
- testifylint # Checks usage of github.com/stretchr/testify
- thelper # checks the consistency of test helpers
- tparallel # detects inappropriate usage of t.Parallel()
- typecheck # parses and type-checks Go code
Expand All @@ -54,23 +61,27 @@ linters:
- wastedassign # finds wasted assignment statements
- whitespace # detects leading and trailing whitespace

# TODO - containedctx # detects struct contained context.Context field
# TODO - cyclop # checks function and package cyclomatic complexity
# TODO - funlen # Tool for detection of long functions
# TODO - goconst # Finds repeated strings that could be replaced by a constant
# TODO - nestif # Reports deeply nested if statements
# TODO - testifylint # Checks usage of github.com/stretchr/testify
# TODO - wrapcheck # Checks that errors returned from external packages are wrapped
# TODO - containedctx # detects struct contained context.Context field
# TODO - funlen # Tool for detection of long functions
# TODO - nestif # Reports deeply nested if statements
# TODO - wrapcheck # Checks that errors returned from external packages are wrapped

issues:
exclude-use-default: false
exclude-rules:
- linters:
- goerr113
- err113
text: "do not define dynamic errors"
- linters:
- stylecheck
text: "ST1003: should not use underscores in package names"
- linters:
- revive
text: "var-naming: don't use an underscore in package name"

linters-settings:
cyclop:
max-complexity: 13
revive:
rules:
- name: var-naming
disabled: true
stylecheck:
# should not use underscores in package names
checks: [ "all", "-ST1003" ]
whitespace:
multi-if: true # Enforces newlines (or comments) after every multi-line if statement
multi-func: true # Enforces newlines (or comments) after every multi-line function signature
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
GOLANGCI_VERSION = v1.56.2
GOLANGCI_VERSION = v1.59.1

help: ## show help, shown by default if no target is specified
@grep -E '^[0-9a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
Expand Down
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func (c *Client) createNewConsumer(config ConsumerConfig) (*consumer, error) {

// NewConsumer creates a new Consumer, returning after the connection
// has been made.
// nolint: ireturn
func (c *Client) NewConsumer(ctx context.Context, config ConsumerConfig) (Consumer, error) {
if err := config.Validate(); err != nil {
return nil, fmt.Errorf("validating config: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion client_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestNewClient(t *testing.T) {
func TestClientTopics(t *testing.T) {
client := setup(t)
defer func() {
assert.Nil(t, client.Close())
assert.NoError(t, client.Close())
}()

topic := randomTopicName()
Expand Down
18 changes: 9 additions & 9 deletions consumer_multitopic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func TestConsumerTopicPattern(t *testing.T) {
client := setup(t)
defer func() {
assert.Nil(t, client.Close())
assert.NoError(t, client.Close())
}()

topic := randomTopicName()
Expand All @@ -43,8 +43,8 @@ func TestConsumerTopicPattern(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, m2)

assert.Nil(t, consumer.AckMessage(m1))
assert.Nil(t, consumer.AckMessage(m2))
assert.NoError(t, consumer.AckMessage(m1))
assert.NoError(t, consumer.AckMessage(m2))

messages := []string{string(m1.Body), string(m2.Body)}
m := map[string]*Message{
Expand All @@ -67,7 +67,7 @@ func TestConsumerTopicPattern(t *testing.T) {
func TestConsumerTopicPatternDiscovery(t *testing.T) {
client := setup(t)
defer func() {
assert.Nil(t, client.Close())
assert.NoError(t, client.Close())
}()

topic := randomTopicName()
Expand All @@ -91,14 +91,14 @@ func TestConsumerTopicPatternDiscovery(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, m)

assert.Nil(t, consumer.AckMessage(m))
require.NoError(t, consumer.AckMessage(m))
assert.Equal(t, msg.Body, m.Body)
}

func TestConsumerTopicPatternInitialPosition(t *testing.T) {
client := setup(t)
defer func() {
assert.Nil(t, client.Close())
assert.NoError(t, client.Close())
}()

topic := randomTopicName()
Expand Down Expand Up @@ -133,17 +133,17 @@ func TestConsumerTopicPatternInitialPosition(t *testing.T) {
m1, err := consumer.ReadMessage(ctx)
require.NoError(t, err)
require.NotNil(t, m1)
assert.Nil(t, consumer.AckMessage(m1))
require.NoError(t, consumer.AckMessage(m1))

m2, err := consumer.ReadMessage(ctx)
require.NoError(t, err)
require.NotNil(t, m2)
assert.Nil(t, consumer.AckMessage(m2))
require.NoError(t, consumer.AckMessage(m2))

m3, err := consumer.ReadMessage(ctx)
require.NoError(t, err)
require.NotNil(t, m3)
assert.Nil(t, consumer.AckMessage(m3))
require.NoError(t, consumer.AckMessage(m3))

messages := []string{string(m1.Body), string(m2.Body), string(m3.Body)}
sort.Strings(messages)
Expand Down
4 changes: 2 additions & 2 deletions consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package pulsar
import (
"testing"

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

func TestConsumerConfigValidate(t *testing.T) {
Expand All @@ -12,5 +12,5 @@ func TestConsumerConfigValidate(t *testing.T) {
}

err := conf.Validate()
assert.Nil(t, err)
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type logger struct {
logger *log.Logger
}

func newLogger() Logger {
func newLogger() logger {
return logger{
logger: log.New(io.Discard, "[Pulsar] ", log.LstdFlags),
}
Expand Down
2 changes: 1 addition & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type testLogger struct {
}

// newTestLogger returns a new logger that logs to the provided testing.TB.
func newTestLogger(tb testing.TB) Logger {
func newTestLogger(tb testing.TB) testLogger {
tb.Helper()
return testLogger{
logger: log.New(io.Discard, "[Pulsar] ", log.LstdFlags),
Expand Down
22 changes: 11 additions & 11 deletions messaging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func setup(t *testing.T) *Client {
resp, err := h.Do(req)
require.NoError(t, err)
err = resp.Body.Close()
assert.NoError(t, err)
require.NoError(t, err)

err = client.Dial(ctx)
require.NoError(t, err)
Expand All @@ -56,7 +56,7 @@ func readMessageAndCompare(t *testing.T, consumer Consumer, expected *Message) *
func TestSendReceiveEarliestPosition(t *testing.T) {
client := setup(t)
defer func() {
assert.Nil(t, client.Close())
assert.NoError(t, client.Close())
}()

producer, topic := newTestProducer(t, client, "")
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestSendReceiveEarliestPosition(t *testing.T) {
func TestSendReceiveLatestPositionExclusive(t *testing.T) {
client := setup(t)
defer func() {
assert.Nil(t, client.Close())
assert.NoError(t, client.Close())
}()

producer, topic := newTestProducer(t, client, "")
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestSendReceiveLatestPositionExclusive(t *testing.T) {
func TestSendReceiveLatestPositionInclusive(t *testing.T) {
client := setup(t)
defer func() {
assert.Nil(t, client.Close())
assert.NoError(t, client.Close())
}()

producer, topic := newTestProducer(t, client, "")
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestSendReceiveLatestPositionInclusive(t *testing.T) {
func TestConsumerEmptyTopicLatestPositionInclusive(t *testing.T) {
client := setup(t)
defer func() {
assert.Nil(t, client.Close())
assert.NoError(t, client.Close())
}()

consConf := ConsumerConfig{
Expand All @@ -180,7 +180,7 @@ func TestConsumerNonExistingTopic(t *testing.T) {

client := setup(t)
defer func() {
assert.Nil(t, client.Close())
assert.NoError(t, client.Close())
}()

consConf := ConsumerConfig{
Expand All @@ -195,7 +195,7 @@ func TestConsumerNonExistingTopic(t *testing.T) {
func TestConsumerNothingToReceive(t *testing.T) {
client := setup(t)
defer func() {
assert.Nil(t, client.Close())
assert.NoError(t, client.Close())
}()

topic := randomTopicName()
Expand All @@ -221,7 +221,7 @@ func TestConsumerNothingToReceive(t *testing.T) {
select {
case <-timeout:
case err := <-done:
assert.Error(t, err)
require.Error(t, err)
t.Fail()
}
assert.False(t, consumer.HasNext())
Expand All @@ -230,7 +230,7 @@ func TestConsumerNothingToReceive(t *testing.T) {
func TestConsumerSeek(t *testing.T) {
client := setup(t)
defer func() {
assert.Nil(t, client.Close())
assert.NoError(t, client.Close())
}()

producer, topic := newTestProducer(t, client, "")
Expand Down Expand Up @@ -264,7 +264,7 @@ func TestConsumerSeek(t *testing.T) {
func TestGetLastMessageID(t *testing.T) {
client := setup(t)
defer func() {
assert.Nil(t, client.Close())
assert.NoError(t, client.Close())
}()

producer, topic := newTestProducer(t, client, "")
Expand All @@ -284,7 +284,7 @@ func TestGetLastMessageID(t *testing.T) {
messageID, err := consumer.LastMessageID()
require.NoError(t, err)
require.NotNil(t, messageID)
assert.True(t, *messageID.EntryId == math.MaxUint64)
assert.Equal(t, *messageID.EntryId, uint64(math.MaxUint64))

sendMessage(t, producer, "hello world")
messageID, err = consumer.LastMessageID()
Expand Down
12 changes: 6 additions & 6 deletions producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func sendMessageAsync(t *testing.T, producer *Producer, s string) *Message {
Body: []byte(s),
}
ctx := context.Background()
require.Nil(t, producer.WriteMessageAsync(ctx, m.Body))
require.NoError(t, producer.WriteMessageAsync(ctx, m.Body))
return m
}

Expand All @@ -63,13 +63,13 @@ func TestProducerConfigValidate(t *testing.T) {
}

err := conf.Validate()
assert.Nil(t, err)
assert.NoError(t, err)
}

func TestProducerRestartSequence(t *testing.T) {
client := setup(t)
defer func() {
assert.Nil(t, client.Close())
assert.NoError(t, client.Close())
}()

prod, topic := newTestProducer(t, client, "")
Expand All @@ -94,7 +94,7 @@ func TestProducerRestartSequence(t *testing.T) {
func TestProducerBrokerGeneratedName(t *testing.T) {
client := setup(t)
defer func() {
assert.Nil(t, client.Close())
assert.NoError(t, client.Close())
}()

prodConf := ProducerConfig{
Expand All @@ -110,7 +110,7 @@ func TestProducerBrokerGeneratedName(t *testing.T) {
func TestProducerBatchSize(t *testing.T) {
client := setup(t)
defer func() {
assert.Nil(t, client.Close())
assert.NoError(t, client.Close())
}()

topic := randomTopicName()
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestProducerBatchSize(t *testing.T) {
func TestProducerBatchTimeout(t *testing.T) {
client := setup(t)
defer func() {
assert.Nil(t, client.Close())
assert.NoError(t, client.Close())
}()

topic := randomTopicName()
Expand Down

0 comments on commit b7794b0

Please sign in to comment.