From 79293e6984af98a7e306dc52fc66273b269e3465 Mon Sep 17 00:00:00 2001 From: Luca Comellini Date: Wed, 5 Jun 2024 14:05:48 -0700 Subject: [PATCH] Add more linters (#119) --- .golangci.yml | 34 ++++++++++++++++++++++++++- .pre-commit-config.yaml | 4 ++-- cmd/generator/code_test.go | 1 + cmd/generator/main.go | 7 +++--- cmd/generator/parser.go | 15 ++++++------ cmd/generator/parser_test.go | 9 +++++-- cmd/generator/scheme_test.go | 1 + cmd/generator/tests/data_test.go | 2 ++ pkg/telemetry/error_handler_test.go | 1 + pkg/telemetry/telemetry_suite_test.go | 1 + tests/exporter_test.go | 3 ++- tests/tests_suite_test.go | 1 + 12 files changed, 63 insertions(+), 16 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 0433da7..dd7ece9 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,4 +1,6 @@ linters-settings: + ginkgolinter: + forbid-focus-container: true misspell: locale: US revive: @@ -7,13 +9,17 @@ linters-settings: - name: blank-imports - name: context-as-argument - name: context-keys-type + - name: dot-imports + arguments: + - allowedPackages: + - github.com/onsi/gomega + - github.com/onsi/ginkgo/v2 - name: empty-block - name: error-naming - name: error-return - name: error-strings - name: errorf - name: exported - - name: if-return - name: increment-decrement - name: indent-error-flow - name: package-comments @@ -34,32 +40,58 @@ linters-settings: - fieldalignment lll: line-length: 120 + dupword: + ignore: + - "test" linters: enable: + - asasalint - asciicheck + - dupword - errcheck + - errname - errorlint + - exportloopref + - fatcontext + - forcetypeassert + - ginkgolinter + - gocheckcompilerdirectives - gocyclo + - godot - gofmt - gofumpt - goimports - gosec - gosimple + - gosmopolitan - govet - ineffassign + - intrange - lll + - loggercheck - makezero - misspell - nilerr - noctx + - nolintlint + - paralleltest + - prealloc - predeclared + - reassign - revive + - spancheck - staticcheck + - stylecheck + - tenv + - thelper + - tparallel - typecheck - unconvert - unparam - unused + - usestdlibvars - wastedassign + - perfsprint disable-all: true issues: max-issues-per-linter: 0 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1e51602..ea947a2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,7 +5,7 @@ repos: rev: v4.5.0 hooks: - id: trailing-whitespace - exclude: (^tests/results/) + exclude: (^tests/results/|\.avdl$|_generated.go$) - id: end-of-file-fixer - id: check-yaml args: [--allow-multiple-documents] @@ -37,7 +37,7 @@ repos: pass_filenames: false - repo: https://github.com/golangci/golangci-lint - rev: v1.55.2 + rev: v1.59.0 hooks: - id: golangci-lint name: golangci-lint-root diff --git a/cmd/generator/code_test.go b/cmd/generator/code_test.go index 244c179..58fa582 100644 --- a/cmd/generator/code_test.go +++ b/cmd/generator/code_test.go @@ -12,6 +12,7 @@ import ( ) func TestGenerateCode(t *testing.T) { + t.Parallel() g := NewGomegaWithT(t) cfg := parsingConfig{ diff --git a/cmd/generator/main.go b/cmd/generator/main.go index 44a03bc..21a40c9 100644 --- a/cmd/generator/main.go +++ b/cmd/generator/main.go @@ -3,6 +3,7 @@ package main import ( + "errors" "flag" "fmt" "os" @@ -54,7 +55,7 @@ func main() { pkgName := os.Getenv("GOPACKAGE") if pkgName == "" { - exitWithError(fmt.Errorf("GOPACKAGE is not set")) + exitWithError(errors.New("GOPACKAGE is not set")) } var buildFlags []string @@ -78,7 +79,7 @@ func main() { if *code { fmt.Println("Generating code") - fileName := fmt.Sprintf("%s_attributes_generated.go", strings.ToLower(*typeName)) + fileName := strings.ToLower(*typeName) + "_attributes_generated.go" file, err := os.Create(fileName) if err != nil { @@ -107,7 +108,7 @@ func main() { if *scheme { fmt.Println("Generating scheme") - fileName := fmt.Sprintf("%s.avdl", strings.ToLower(*typeName)) + fileName := strings.ToLower(*typeName) + ".avdl" file, err := os.Create(fileName) if err != nil { diff --git a/cmd/generator/parser.go b/cmd/generator/parser.go index 0f70e05..69be2ab 100644 --- a/cmd/generator/parser.go +++ b/cmd/generator/parser.go @@ -3,6 +3,7 @@ package main import ( + "errors" "fmt" "go/ast" "go/token" @@ -36,7 +37,7 @@ func newDocStringFieldsProvider(loadTests bool, buildFlags []string) *docStringF func parseFullTypeName(fullTypeName string) (pkgName, typeName string) { idx := strings.LastIndex(fullTypeName, ".") if idx == -1 { - panic(fmt.Sprintf("invalid full type name: %s", fullTypeName)) + panic("invalid full type name: " + fullTypeName) } return fullTypeName[:idx], fullTypeName[idx+1:] @@ -61,12 +62,12 @@ func (p *docStringFieldsProvider) getDocString(fullTypeName, fieldName string) ( doc, exists := p.docStrings[getDocStringKey(pkgName, typeName, fieldName)] if !exists { - return "", fmt.Errorf("doc string not found") + return "", errors.New("doc string not found") } trimmedComment := strings.TrimSpace(doc) if trimmedComment == "" { - return "", fmt.Errorf("trimmed doc string is empty") + return "", errors.New("trimmed doc string is empty") } return trimmedComment, nil @@ -269,7 +270,7 @@ func parseStruct(s *types.Struct, typeName string, docStringProvider *docStringF return field{}, parsingError{ typeName: typeName, fieldName: f.Name(), - msg: fmt.Sprintf("must be struct, got %s", t.Underlying().String()), + msg: "must be struct, got " + t.Underlying().String(), } } @@ -383,7 +384,7 @@ func parseStruct(s *types.Struct, typeName string, docStringProvider *docStringF parseRecursively = func(s *types.Struct, typeName string) ([]field, error) { var fields []field - for i := 0; i < s.NumFields(); i++ { + for i := range s.NumFields() { f := s.Field(i) var parsedField field @@ -400,7 +401,7 @@ func parseStruct(s *types.Struct, typeName string, docStringProvider *docStringF err = parsingError{ typeName: typeName, fieldName: f.Name(), - msg: fmt.Sprintf("must be of embedded struct, basic type or slice of basic type, got %s", f.Type().String()), + msg: "must be of embedded struct, basic type or slice of basic type, got " + f.Type().String(), } } @@ -414,7 +415,7 @@ func parseStruct(s *types.Struct, typeName string, docStringProvider *docStringF return nil, parsingError{ typeName: typeName, fieldName: f.Name(), - msg: fmt.Sprintf("already exists in %s", owner), + msg: "already exists in " + owner, } } diff --git a/cmd/generator/parser_test.go b/cmd/generator/parser_test.go index 01950b9..96ea2fa 100644 --- a/cmd/generator/parser_test.go +++ b/cmd/generator/parser_test.go @@ -29,7 +29,7 @@ type someStruct struct{} type SomeStruct struct{} type DataNotEmbeddedStructField struct { - SomeField SomeStruct //nolint:unused + SomeField SomeStruct } type SomeInterface interface{} @@ -86,6 +86,7 @@ type EmbeddedDuplicateFields struct { } func TestParseErrors(t *testing.T) { + t.Parallel() tests := []struct { name string expectedErrMsg string @@ -176,7 +177,9 @@ func TestParseErrors(t *testing.T) { } for _, test := range tests { + test := test t.Run(test.name, func(t *testing.T) { + t.Parallel() g := NewGomegaWithT(t) cfg := parsingConfig{ @@ -194,6 +197,7 @@ func TestParseErrors(t *testing.T) { } func TestParseLoadingFailures(t *testing.T) { + t.Parallel() g := NewGomegaWithT(t) cfg := parsingConfig{ @@ -209,6 +213,7 @@ func TestParseLoadingFailures(t *testing.T) { } func TestParseSuccess(t *testing.T) { + t.Parallel() g := NewGomegaWithT(t) cfg := parsingConfig{ @@ -369,6 +374,6 @@ func TestParseSuccess(t *testing.T) { result, err := parse(cfg) - g.Expect(err).To(BeNil()) + g.Expect(err).ToNot(HaveOccurred()) g.Expect(expectedResult).To(Equal(result)) } diff --git a/cmd/generator/scheme_test.go b/cmd/generator/scheme_test.go index 66c1a15..b1b1f70 100644 --- a/cmd/generator/scheme_test.go +++ b/cmd/generator/scheme_test.go @@ -12,6 +12,7 @@ import ( ) func TestGenerateScheme(t *testing.T) { + t.Parallel() g := NewGomegaWithT(t) parseCfg := parsingConfig{ diff --git a/cmd/generator/tests/data_test.go b/cmd/generator/tests/data_test.go index 860a4cf..87af25a 100644 --- a/cmd/generator/tests/data_test.go +++ b/cmd/generator/tests/data_test.go @@ -12,6 +12,7 @@ import ( ) func TestData_Attributes(t *testing.T) { + t.Parallel() g := NewGomegaWithT(t) data := Data{ @@ -61,6 +62,7 @@ func TestData_Attributes(t *testing.T) { } func TestData_AttributesEmpty(t *testing.T) { + t.Parallel() g := NewGomegaWithT(t) data := Data{} diff --git a/pkg/telemetry/error_handler_test.go b/pkg/telemetry/error_handler_test.go index 12cb637..e1fcba5 100644 --- a/pkg/telemetry/error_handler_test.go +++ b/pkg/telemetry/error_handler_test.go @@ -8,6 +8,7 @@ import ( ) func TestErrorHandler(t *testing.T) { + t.Parallel() g := NewWithT(t) testErr1 := errors.New("test error 1") diff --git a/pkg/telemetry/telemetry_suite_test.go b/pkg/telemetry/telemetry_suite_test.go index 04c897a..86d9ec8 100644 --- a/pkg/telemetry/telemetry_suite_test.go +++ b/pkg/telemetry/telemetry_suite_test.go @@ -8,6 +8,7 @@ import ( ) func TestTelemetry(t *testing.T) { + t.Parallel() RegisterFailHandler(Fail) RunSpecs(t, "Telemetry Suite") } diff --git a/tests/exporter_test.go b/tests/exporter_test.go index 5aa69ea..cd40c7d 100644 --- a/tests/exporter_test.go +++ b/tests/exporter_test.go @@ -3,6 +3,7 @@ package tests import ( "bufio" "context" + "errors" "fmt" "io" "log/slog" @@ -79,7 +80,7 @@ func getCollectorImageFromDockerfile() (string, error) { for { line, err := reader.ReadString('\n') if err == io.EOF { - return "", fmt.Errorf("FROM not found in Dockerfile") + return "", errors.New("FROM not found in Dockerfile") } if err != nil { return "", fmt.Errorf("failed to read Dockerfile: %w", err) diff --git a/tests/tests_suite_test.go b/tests/tests_suite_test.go index 968d531..6ef13cd 100644 --- a/tests/tests_suite_test.go +++ b/tests/tests_suite_test.go @@ -8,6 +8,7 @@ import ( ) func TestExporter(t *testing.T) { + t.Parallel() RegisterFailHandler(Fail) RunSpecs(t, "Tests Suite") }