Skip to content

Commit

Permalink
feat: deprecate usage of linter alternative names (#4562)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Mar 28, 2024
1 parent ed20557 commit 5a9f5c1
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pkg/golinters/internal/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ index 0000000..6399915
+// line
`

log := logutils.NewMockLog()
log.On("Infof", "The diff contains only additions: no original or deleted lines: %#v", mock.Anything)
log := logutils.NewMockLog().
OnInfof("The diff contains only additions: no original or deleted lines: %#v", mock.Anything)

var noChanges []Change
testDiffProducesChanges(t, log, diff, noChanges...)
Expand Down
36 changes: 35 additions & 1 deletion pkg/lint/lintersdb/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package lintersdb
import (
"errors"
"fmt"
"os"
"slices"
"strings"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/logutils"
)

type Validator struct {
Expand All @@ -28,6 +30,7 @@ func (v Validator) Validate(cfg *config.Config) error {
validators := []func(cfg *config.Linters) error{
v.validateLintersNames,
v.validatePresets,
v.alternativeNamesDeprecation,
}

for _, v := range validators {
Expand All @@ -40,7 +43,7 @@ func (v Validator) Validate(cfg *config.Config) error {
}

func (v Validator) validateLintersNames(cfg *config.Linters) error {
allNames := append([]string{}, cfg.Enable...)
allNames := cfg.Enable
allNames = append(allNames, cfg.Disable...)

var unknownNames []string
Expand Down Expand Up @@ -75,3 +78,34 @@ func (v Validator) validatePresets(cfg *config.Linters) error {

return nil
}

func (v Validator) alternativeNamesDeprecation(cfg *config.Linters) error {
if v.m.cfg.InternalTest || v.m.cfg.InternalCmdTest || os.Getenv(logutils.EnvTestRun) == "1" {
return nil
}

altNames := map[string][]string{}
for _, lc := range v.m.GetAllSupportedLinterConfigs() {
for _, alt := range lc.AlternativeNames {
altNames[alt] = append(altNames[alt], lc.Name())
}
}

names := cfg.Enable
names = append(names, cfg.Disable...)

for _, name := range names {
lc, ok := altNames[name]
if !ok {
continue
}

if len(lc) > 1 {
v.m.log.Warnf("The linter named %q is deprecated. It has been split into: %s.", name, strings.Join(lc, ", "))
} else {
v.m.log.Warnf("The name %q is deprecated. The linter has been renamed to: %s.", name, lc[0])
}
}

return nil
}
25 changes: 25 additions & 0 deletions pkg/lint/lintersdb/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/logutils"
)

type validateErrorTestCase struct {
Expand Down Expand Up @@ -215,3 +216,27 @@ func TestValidator_validatePresets_error(t *testing.T) {
})
}
}

func TestValidator_alternativeNamesDeprecation(t *testing.T) {
t.Setenv(logutils.EnvTestRun, "0")

log := logutils.NewMockLog().
OnWarnf("The name %q is deprecated. The linter has been renamed to: %s.", "vet", "govet").
OnWarnf("The name %q is deprecated. The linter has been renamed to: %s.", "vetshadow", "govet").
OnWarnf("The name %q is deprecated. The linter has been renamed to: %s.", "logrlint", "loggercheck").
OnWarnf("The linter named %q is deprecated. It has been split into: %s.", "megacheck", "gosimple, staticcheck, unused").
OnWarnf("The name %q is deprecated. The linter has been renamed to: %s.", "gas", "gosec")

m, err := NewManager(log, nil, NewLinterBuilder())
require.NoError(t, err)

v := NewValidator(m)

cfg := &config.Linters{
Enable: []string{"vet", "vetshadow", "logrlint"},
Disable: []string{"megacheck", "gas"},
}

err = v.alternativeNamesDeprecation(cfg)
require.NoError(t, err)
}
55 changes: 45 additions & 10 deletions pkg/logutils/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,23 @@ func NewMockLog() *MockLog {
}

func (m *MockLog) Fatalf(format string, args ...any) {
mArgs := []any{format}
m.Called(append(mArgs, args...)...)
m.Called(append([]any{format}, args...)...)
}

func (m *MockLog) Panicf(format string, args ...any) {
mArgs := []any{format}
m.Called(append(mArgs, args...)...)
m.Called(append([]any{format}, args...)...)
}

func (m *MockLog) Errorf(format string, args ...any) {
mArgs := []any{format}
m.Called(append(mArgs, args...)...)
m.Called(append([]any{format}, args...)...)
}

func (m *MockLog) Warnf(format string, args ...any) {
mArgs := []any{format}
m.Called(append(mArgs, args...)...)
m.Called(append([]any{format}, args...)...)
}

func (m *MockLog) Infof(format string, args ...any) {
mArgs := []any{format}
m.Called(append(mArgs, args...)...)
m.Called(append([]any{format}, args...)...)
}

func (m *MockLog) Child(name string) Log {
Expand All @@ -45,3 +40,43 @@ func (m *MockLog) Child(name string) Log {
func (m *MockLog) SetLevel(level LogLevel) {
m.Called(level)
}

func (m *MockLog) OnFatalf(format string, args ...any) *MockLog {
arguments := append([]any{format}, args...)

m.On("Fatalf", arguments...)

return m
}

func (m *MockLog) OnPanicf(format string, args ...any) *MockLog {
arguments := append([]any{format}, args...)

m.On("Panicf", arguments...)

return m
}

func (m *MockLog) OnErrorf(format string, args ...any) *MockLog {
arguments := append([]any{format}, args...)

m.On("Errorf", arguments...)

return m
}

func (m *MockLog) OnWarnf(format string, args ...any) *MockLog {
arguments := append([]any{format}, args...)

m.On("Warnf", arguments...)

return m
}

func (m *MockLog) OnInfof(format string, args ...any) *MockLog {
arguments := append([]any{format}, args...)

m.On("Infof", arguments...)

return m
}

0 comments on commit 5a9f5c1

Please sign in to comment.