Skip to content

Commit

Permalink
Add more linters (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacome authored Jun 5, 2024
1 parent 62fb9b5 commit 79293e6
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 16 deletions.
34 changes: 33 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
linters-settings:
ginkgolinter:
forbid-focus-container: true
misspell:
locale: US
revive:
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/generator/code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

func TestGenerateCode(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)

cfg := parsingConfig{
Expand Down
7 changes: 4 additions & 3 deletions cmd/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"errors"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 8 additions & 7 deletions cmd/generator/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"errors"
"fmt"
"go/ast"
"go/token"
Expand Down Expand Up @@ -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:]
Expand All @@ -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
Expand Down Expand Up @@ -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(),
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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(),
}
}

Expand All @@ -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,
}
}

Expand Down
9 changes: 7 additions & 2 deletions cmd/generator/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type someStruct struct{}
type SomeStruct struct{}

type DataNotEmbeddedStructField struct {
SomeField SomeStruct //nolint:unused
SomeField SomeStruct
}

type SomeInterface interface{}
Expand Down Expand Up @@ -86,6 +86,7 @@ type EmbeddedDuplicateFields struct {
}

func TestParseErrors(t *testing.T) {
t.Parallel()
tests := []struct {
name string
expectedErrMsg string
Expand Down Expand Up @@ -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{
Expand All @@ -194,6 +197,7 @@ func TestParseErrors(t *testing.T) {
}

func TestParseLoadingFailures(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)

cfg := parsingConfig{
Expand All @@ -209,6 +213,7 @@ func TestParseLoadingFailures(t *testing.T) {
}

func TestParseSuccess(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)

cfg := parsingConfig{
Expand Down Expand Up @@ -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))
}
1 change: 1 addition & 0 deletions cmd/generator/scheme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

func TestGenerateScheme(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)

parseCfg := parsingConfig{
Expand Down
2 changes: 2 additions & 0 deletions cmd/generator/tests/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

func TestData_Attributes(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)

data := Data{
Expand Down Expand Up @@ -61,6 +62,7 @@ func TestData_Attributes(t *testing.T) {
}

func TestData_AttributesEmpty(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)

data := Data{}
Expand Down
1 change: 1 addition & 0 deletions pkg/telemetry/error_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

func TestErrorHandler(t *testing.T) {
t.Parallel()
g := NewWithT(t)

testErr1 := errors.New("test error 1")
Expand Down
1 change: 1 addition & 0 deletions pkg/telemetry/telemetry_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

func TestTelemetry(t *testing.T) {
t.Parallel()
RegisterFailHandler(Fail)
RunSpecs(t, "Telemetry Suite")
}
3 changes: 2 additions & 1 deletion tests/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"log/slog"
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tests/tests_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

func TestExporter(t *testing.T) {
t.Parallel()
RegisterFailHandler(Fail)
RunSpecs(t, "Tests Suite")
}

0 comments on commit 79293e6

Please sign in to comment.