From b7794b0d79facc4b5e293c148dd85b6c3ce791b3 Mon Sep 17 00:00:00 2001 From: cornelk Date: Wed, 26 Jun 2024 09:09:12 -0600 Subject: [PATCH] update linter and fix linting issues --- .golangci.yml | 43 +++++++++++++++++++++++-------------- Makefile | 2 +- client.go | 1 + client_int_test.go | 2 +- consumer_multitopic_test.go | 18 ++++++++-------- consumer_test.go | 4 ++-- logger.go | 2 +- logger_test.go | 2 +- messaging_test.go | 22 +++++++++---------- producer_test.go | 12 +++++------ 10 files changed, 60 insertions(+), 48 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index b251d6c..240d39a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -9,10 +9,13 @@ 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 @@ -20,15 +23,17 @@ linters: - 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 @@ -36,6 +41,7 @@ linters: - 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 @@ -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 @@ -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 diff --git a/Makefile b/Makefile index c026041..a4e8ae7 100644 --- a/Makefile +++ b/Makefile @@ -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}' diff --git a/client.go b/client.go index 5749b65..5b41473 100644 --- a/client.go +++ b/client.go @@ -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) diff --git a/client_int_test.go b/client_int_test.go index 95945a1..58e6af9 100644 --- a/client_int_test.go +++ b/client_int_test.go @@ -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() diff --git a/consumer_multitopic_test.go b/consumer_multitopic_test.go index b0c2420..bdadde7 100644 --- a/consumer_multitopic_test.go +++ b/consumer_multitopic_test.go @@ -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() @@ -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{ @@ -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() @@ -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() @@ -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) diff --git a/consumer_test.go b/consumer_test.go index 88a4ae3..16c8221 100644 --- a/consumer_test.go +++ b/consumer_test.go @@ -3,7 +3,7 @@ package pulsar import ( "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestConsumerConfigValidate(t *testing.T) { @@ -12,5 +12,5 @@ func TestConsumerConfigValidate(t *testing.T) { } err := conf.Validate() - assert.Nil(t, err) + require.NoError(t, err) } diff --git a/logger.go b/logger.go index 799d2fe..05ce620 100644 --- a/logger.go +++ b/logger.go @@ -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), } diff --git a/logger_test.go b/logger_test.go index 44a488c..692d264 100644 --- a/logger_test.go +++ b/logger_test.go @@ -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), diff --git a/messaging_test.go b/messaging_test.go index 7086d66..718b40c 100644 --- a/messaging_test.go +++ b/messaging_test.go @@ -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) @@ -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, "") @@ -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, "") @@ -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, "") @@ -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{ @@ -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{ @@ -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() @@ -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()) @@ -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, "") @@ -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, "") @@ -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() diff --git a/producer_test.go b/producer_test.go index ea3e135..3821edf 100644 --- a/producer_test.go +++ b/producer_test.go @@ -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 } @@ -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, "") @@ -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{ @@ -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() @@ -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()