From b6cec6762bb0e6752deddf64e589ba79a9d2f473 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Thu, 21 Mar 2024 22:48:42 +0100 Subject: [PATCH] chore: improve MockLog --- pkg/golinters/gofmt_test.go | 4 +-- pkg/lint/lintersdb/validator_test.go | 12 ++++---- pkg/logutils/mock.go | 45 ++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/pkg/golinters/gofmt_test.go b/pkg/golinters/gofmt_test.go index e017c7978ed0..2c8432eabbb5 100644 --- a/pkg/golinters/gofmt_test.go +++ b/pkg/golinters/gofmt_test.go @@ -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...) diff --git a/pkg/lint/lintersdb/validator_test.go b/pkg/lint/lintersdb/validator_test.go index 08eeff152d3f..7ede69be083e 100644 --- a/pkg/lint/lintersdb/validator_test.go +++ b/pkg/lint/lintersdb/validator_test.go @@ -218,12 +218,12 @@ func TestValidator_validatePresets_error(t *testing.T) { } func TestValidator_alternativeNamesDeprecation(t *testing.T) { - log := logutils.NewMockLog() - log.On("Warnf", "The linter name %q is deprecated. It has been renamed to: %s.", "vet", "govet") - log.On("Warnf", "The linter name %q is deprecated. It has been renamed to: %s.", "vetshadow", "govet") - log.On("Warnf", "The linter name %q is deprecated. It has been renamed to: %s.", "logrlint", "loggercheck") - log.On("Warnf", "The linter name %q is deprecated. It has been splited into: %s.", "megacheck", "gosimple, staticcheck, unused") - log.On("Warnf", "The linter name %q is deprecated. It has been renamed to: %s.", "gas", "gosec") + log := logutils.NewMockLog(). + OnWarnf("The linter name %q is deprecated. It has been renamed to: %s.", "vet", "govet"). + OnWarnf("The linter name %q is deprecated. It has been renamed to: %s.", "vetshadow", "govet"). + OnWarnf("The linter name %q is deprecated. It has been renamed to: %s.", "logrlint", "loggercheck"). + OnWarnf("The linter name %q is deprecated. It has been splited into: %s.", "megacheck", "gosimple, staticcheck, unused"). + OnWarnf("The linter name %q is deprecated. It has been renamed to: %s.", "gas", "gosec") m, err := NewManager(log, nil, NewLinterBuilder()) require.NoError(t, err) diff --git a/pkg/logutils/mock.go b/pkg/logutils/mock.go index efda8cc20f6e..2e72e0ffeae9 100644 --- a/pkg/logutils/mock.go +++ b/pkg/logutils/mock.go @@ -45,3 +45,48 @@ 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 := []any{format} + arguments = append(arguments, args...) + + m.On("Fatalf", arguments...) + + return m +} + +func (m *MockLog) OnPanicf(format string, args ...any) *MockLog { + arguments := []any{format} + arguments = append(arguments, args...) + + m.On("Panicf", arguments...) + + return m +} + +func (m *MockLog) OnErrorf(format string, args ...any) *MockLog { + arguments := []any{format} + arguments = append(arguments, args...) + + m.On("Errorf", arguments...) + + return m +} + +func (m *MockLog) OnWarnf(format string, args ...any) *MockLog { + arguments := []any{format} + arguments = append(arguments, args...) + + m.On("Warnf", arguments...) + + return m +} + +func (m *MockLog) OnInfof(format string, args ...any) *MockLog { + arguments := []any{format} + arguments = append(arguments, args...) + + m.On("Infof", arguments...) + + return m +}