Enable testifylint and fix up lint issues (#1276)

This commit is contained in:
Oleksandr Redko 2024-12-20 11:03:23 +02:00 committed by GitHub
parent 36c81ed69b
commit 54c2185ae6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 20 deletions

View file

@ -23,6 +23,7 @@ linters:
- nolintlint - nolintlint
- revive - revive
- staticcheck - staticcheck
- testifylint
- typecheck - typecheck
- unconvert - unconvert
- unparam - unparam
@ -40,6 +41,9 @@ linters-settings:
- all - all
- '-SA1019' - '-SA1019'
testifylint:
enable-all: true
revive: revive:
rules: rules:
- name: dot-imports - name: dot-imports

View file

@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/securego/gosec/v2/issue" "github.com/securego/gosec/v2/issue"
) )
@ -44,17 +45,16 @@ func TestGenerateSolutionByGemini_Success(t *testing.T) {
mockClient := new(MockGenAIClient) mockClient := new(MockGenAIClient)
mockModel := new(MockGenAIGenerativeModel) mockModel := new(MockGenAIGenerativeModel)
mockClient.On("GenerativeModel", GeminiModel).Return(mockModel) mockClient.On("GenerativeModel", GeminiModel).Return(mockModel).Once()
mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("Autofix for issue 1", nil) mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("Autofix for issue 1", nil).Once()
// Act // Act
err := generateSolutionByGemini(mockClient, issues) err := generateSolutionByGemini(mockClient, issues)
// Assert // Assert
assert.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "Autofix for issue 1", issues[0].Autofix) assert.Equal(t, []*issue.Issue{{What: "Example issue 1", Autofix: "Autofix for issue 1"}}, issues)
mockClient.AssertExpectations(t) mock.AssertExpectationsForObjects(t, mockClient, mockModel)
mockModel.AssertExpectations(t)
} }
func TestGenerateSolutionByGemini_NoCandidates(t *testing.T) { func TestGenerateSolutionByGemini_NoCandidates(t *testing.T) {
@ -65,17 +65,15 @@ func TestGenerateSolutionByGemini_NoCandidates(t *testing.T) {
mockClient := new(MockGenAIClient) mockClient := new(MockGenAIClient)
mockModel := new(MockGenAIGenerativeModel) mockModel := new(MockGenAIGenerativeModel)
mockClient.On("GenerativeModel", GeminiModel).Return(mockModel) mockClient.On("GenerativeModel", GeminiModel).Return(mockModel).Once()
mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("", nil) mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("", nil).Once()
// Act // Act
err := generateSolutionByGemini(mockClient, issues) err := generateSolutionByGemini(mockClient, issues)
// Assert // Assert
assert.Error(t, err) require.EqualError(t, err, "no autofix returned by gemini")
assert.Equal(t, "no autofix returned by gemini", err.Error()) mock.AssertExpectationsForObjects(t, mockClient, mockModel)
mockClient.AssertExpectations(t)
mockModel.AssertExpectations(t)
} }
func TestGenerateSolutionByGemini_APIError(t *testing.T) { func TestGenerateSolutionByGemini_APIError(t *testing.T) {
@ -86,17 +84,15 @@ func TestGenerateSolutionByGemini_APIError(t *testing.T) {
mockClient := new(MockGenAIClient) mockClient := new(MockGenAIClient)
mockModel := new(MockGenAIGenerativeModel) mockModel := new(MockGenAIGenerativeModel)
mockClient.On("GenerativeModel", GeminiModel).Return(mockModel) mockClient.On("GenerativeModel", GeminiModel).Return(mockModel).Once()
mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("", errors.New("API error")) mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("", errors.New("API error")).Once()
// Act // Act
err := generateSolutionByGemini(mockClient, issues) err := generateSolutionByGemini(mockClient, issues)
// Assert // Assert
assert.Error(t, err) require.EqualError(t, err, "generating autofix with gemini: API error")
assert.Equal(t, "generating autofix with gemini: API error", err.Error()) mock.AssertExpectationsForObjects(t, mockClient, mockModel)
mockClient.AssertExpectations(t)
mockModel.AssertExpectations(t)
} }
func TestGenerateSolution_UnsupportedProvider(t *testing.T) { func TestGenerateSolution_UnsupportedProvider(t *testing.T) {
@ -109,6 +105,5 @@ func TestGenerateSolution_UnsupportedProvider(t *testing.T) {
err := GenerateSolution("unsupported-provider", "test-api-key", "", issues) err := GenerateSolution("unsupported-provider", "test-api-key", "", issues)
// Assert // Assert
assert.Error(t, err) require.EqualError(t, err, "ai provider not supported")
assert.Equal(t, "ai provider not supported", err.Error())
} }