From 1e8d4dc09ae48b8e55d5544ca4af0961efb372a9 Mon Sep 17 00:00:00 2001 From: Sascha Grunert Date: Fri, 15 Mar 2024 09:20:55 +0100 Subject: [PATCH] Update golangci-lint and apply lint fixes Signed-off-by: Sascha Grunert --- .golangci.yml | 46 ++++++++++++++++++++++++++----------- Makefile | 2 +- cmd/main.go | 6 ++--- demo.go | 4 ++-- demo_test.go | 3 +-- run.go | 7 ++++-- run_test.go | 15 ++++++------ test/framework/framework.go | 3 ++- util.go | 3 ++- util_test.go | 3 +-- 10 files changed, 57 insertions(+), 35 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index d6eb6d8..eccc160 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -11,6 +11,7 @@ linters: - bodyclose - containedctx - contextcheck + - cyclop - decorder - dogsled - dupl @@ -25,8 +26,14 @@ linters: - exportloopref - forbidigo - forcetypeassert + - funlen + - gci + - ginkgolinter + - gocheckcompilerdirectives - gochecknoglobals - gochecknoinits + - gochecksumtype + - gocognit - goconst - gocritic - gocyclo @@ -37,14 +44,17 @@ linters: - gofumpt - goheader - goimports + - gomnd - gomoddirectives - gomodguard - goprintffuncname - gosec - gosimple + - gosmopolitan - govet - grouper - importas + - inamedparam - ineffassign - interfacebloat - ireturn @@ -52,7 +62,9 @@ linters: - loggercheck - maintidx - makezero + - mirror - misspell + - musttag - nakedret - nestif - nilerr @@ -63,18 +75,26 @@ linters: - nonamedreturns - nosprintfhostport - paralleltest + - perfsprint - prealloc - predeclared - promlinter + - protogetter - reassign + - revive - rowserrcheck + - sloglint + - spancheck - sqlclosecheck - staticcheck - stylecheck + - tagalign - tagliatelle - tenv - testableexamples + - testifylint - testpackage + - thelper - tparallel - typecheck - unconvert @@ -83,22 +103,23 @@ linters: - usestdlibvars - wastedassign - whitespace - # - cyclop + - wrapcheck + - zerologlint # - depguard # - exhaustruct - # - funlen - # - gci - # - gocognit - # - gomnd - # - revive - # - thelper # - varnamelen - # - wrapcheck # - wsl linters-settings: - errcheck: - check-type-assertions: true - check-blank: true + funlen: + lines: 150 + cyclop: + max-complexity: 15 + gocognit: + min-complexity: 40 + revive: + rules: + - name: dot-imports + disabled: true gocritic: enabled-checks: - appendAssign @@ -110,6 +131,7 @@ linters-settings: - badLock - badRegexp - badSorting + - badSyncOnceFunc - boolExprSimplify - builtinShadow - builtinShadowDecl @@ -175,7 +197,6 @@ linters-settings: - sliceClear - sloppyLen - sloppyReassign - - sloppyTestFuncName - sloppyTypeAssert - sortSlice - sprintfQuotedString @@ -185,7 +206,6 @@ linters-settings: - stringsCompare - switchTrue - syncMapLoadAndDelete - - timeCmpSimplify - timeExprSimplify - todoCommentWithoutDetail - tooManyResultsChecker diff --git a/Makefile b/Makefile index 6816636..2a2aec7 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ test: ${GOLANGCI_LINT}: export \ - VERSION=v1.55.2 \ + VERSION=v1.56.2 \ URL=https://raw.githubusercontent.com/golangci/golangci-lint \ BINDIR=${BUILD_PATH} && \ curl -sfL $$URL/$$VERSION/install.sh | sh -s $$VERSION diff --git a/cmd/main.go b/cmd/main.go index a4f42ed..eb1f156 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,7 +1,7 @@ package main import ( - . "github.com/saschagrunert/demo" //nolint:revive,stylecheck // dot imports are intended here + . "github.com/saschagrunert/demo" //nolint:stylecheck // dot imports are intended here "github.com/urfave/cli/v2" ) @@ -28,7 +28,7 @@ func main() { } // setup will run before every demo. -func setup(ctx *cli.Context) error { +func setup(*cli.Context) error { // Ensure can be used for easy sequential command execution return Ensure( "echo 'Doing first setup…'", @@ -38,7 +38,7 @@ func setup(ctx *cli.Context) error { } // setup will run after every demo. -func cleanup(ctx *cli.Context) error { +func cleanup(*cli.Context) error { return Ensure("echo 'Doing cleanup…'") } diff --git a/demo.go b/demo.go index f7f18b1..18de207 100644 --- a/demo.go +++ b/demo.go @@ -1,10 +1,10 @@ package demo import ( - "fmt" "log" "os" "os/signal" + "strconv" "time" "github.com/urfave/cli/v2" @@ -194,7 +194,7 @@ func (d *Demo) Cleanup(cleanupFn func(*cli.Context) error) { func (d *Demo) Add(run *Run, name, description string) { flag := &cli.BoolFlag{ - Name: fmt.Sprintf("%d", len(d.runs)), + Name: strconv.Itoa(len(d.runs)), Aliases: []string{name}, Usage: description, } diff --git a/demo_test.go b/demo_test.go index 4a210f8..6a5745a 100644 --- a/demo_test.go +++ b/demo_test.go @@ -5,9 +5,8 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/urfave/cli/v2" - "github.com/saschagrunert/demo" + "github.com/urfave/cli/v2" ) var _ = t.Describe("Demo", func() { diff --git a/run.go b/run.go index 1ef4be8..6012365 100644 --- a/run.go +++ b/run.go @@ -196,8 +196,11 @@ func (r *Run) printTitleAndDescription() error { func write(w io.Writer, str string) error { _, err := w.Write([]byte(str)) + if err != nil { + return fmt.Errorf("write: %w", err) + } - return err + return nil } func (s *step) run(current, max int) error { @@ -284,7 +287,7 @@ func (s *step) print(msg ...string) error { for _, m := range msg { for _, c := range m { if !s.r.options.Immediate { - //nolint:gosec // the sleep has no security implications + //nolint:gosec,gomnd // the sleep has no security implications and is randomly chosen time.Sleep(time.Duration(rand.Intn(40)) * time.Millisecond) } if err := write(s.r.out, fmt.Sprintf("%c", c)); err != nil { diff --git a/run_test.go b/run_test.go index 1b2a7ae..eb8c3eb 100644 --- a/run_test.go +++ b/run_test.go @@ -6,9 +6,8 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/urfave/cli/v2" - "github.com/saschagrunert/demo" + "github.com/urfave/cli/v2" ) var _ = t.Describe("Run", func() { @@ -26,7 +25,7 @@ var _ = t.Describe("Run", func() { out = &strings.Builder{} err := sut.SetOutput(out) - Expect(err).To(BeNil()) + Expect(err).ToNot(HaveOccurred()) opts = demo.Options{ AutoTimeout: 0, @@ -42,7 +41,7 @@ var _ = t.Describe("Run", func() { err := sut.RunWithOptions(opts) // Then - Expect(err).To(BeNil()) + Expect(err).ToNot(HaveOccurred()) Expect(out).To(ContainSubstring(title)) Expect(out).To(ContainSubstring(description[0])) Expect(out).To(ContainSubstring(description[1])) @@ -60,7 +59,7 @@ var _ = t.Describe("Run", func() { err := sut.RunWithOptions(opts) // Then - Expect(err).To(BeNil()) + Expect(err).ToNot(HaveOccurred()) Expect(out).To(ContainSubstring(title)) Expect(out).To(ContainSubstring(description[0])) Expect(out).To(ContainSubstring(description[1])) @@ -80,7 +79,7 @@ var _ = t.Describe("Run", func() { err := sut.RunWithOptions(opts) // Then - Expect(err).To(BeNil()) + Expect(err).ToNot(HaveOccurred()) }) It("should fail to set nil output", func() { @@ -89,7 +88,7 @@ var _ = t.Describe("Run", func() { err := sut.SetOutput(nil) // Then - Expect(err).NotTo(BeNil()) + Expect(err).To(HaveOccurred()) }) It("should succeed to run from a cli context", func() { @@ -105,6 +104,6 @@ var _ = t.Describe("Run", func() { err := sut.Run(ctx) // Then - Expect(err).To(BeNil()) + Expect(err).ToNot(HaveOccurred()) }) }) diff --git a/test/framework/framework.go b/test/framework/framework.go index 74f30af..d8bd260 100644 --- a/test/framework/framework.go +++ b/test/framework/framework.go @@ -23,7 +23,7 @@ func NewTestFramework(setup, teardown func(*TestFramework) error) *TestFramework } // NilFunc is a convenience function which simply does nothing. -func NilFunc(f *TestFramework) error { +func NilFunc(*TestFramework) error { return nil } @@ -52,5 +52,6 @@ func (t *TestFramework) Describe(text string, body func()) bool { // RunFrameworkSpecs is a convenience wrapper for running tests. func RunFrameworkSpecs(t *testing.T, suiteName string) { + t.Helper() ginkgo.RunSpecs(t, suiteName) } diff --git a/util.go b/util.go index 520efcd..f520756 100644 --- a/util.go +++ b/util.go @@ -1,6 +1,7 @@ package demo import ( + "fmt" "os/exec" ) @@ -12,7 +13,7 @@ func Ensure(commands ...string) error { cmd.Stderr = nil cmd.Stdout = nil if err := cmd.Run(); err != nil { - return err + return fmt.Errorf("run command: %w", err) } } diff --git a/util_test.go b/util_test.go index 67628d7..520e87d 100644 --- a/util_test.go +++ b/util_test.go @@ -3,7 +3,6 @@ package demo_test import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/saschagrunert/demo" ) @@ -14,6 +13,6 @@ var _ = t.Describe("Util", func() { err := demo.Ensure("echo hi") // When - Expect(err).To(BeNil()) + Expect(err).ToNot(HaveOccurred()) }) })