Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ linters:
- nolintlint
- revive
- staticcheck
- testifylint
- typecheck
- unconvert
- unparam
Expand All @@ -40,6 +41,9 @@ linters-settings:
- all
- '-SA1019'

testifylint:
enable-all: true

revive:
rules:
- name: dot-imports
Expand Down
35 changes: 15 additions & 20 deletions autofix/ai_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/securego/gosec/v2/issue"
)
Expand Down Expand Up @@ -44,17 +45,16 @@ func TestGenerateSolutionByGemini_Success(t *testing.T) {

mockClient := new(MockGenAIClient)
mockModel := new(MockGenAIGenerativeModel)
mockClient.On("GenerativeModel", GeminiModel).Return(mockModel)
mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("Autofix for issue 1", nil)
mockClient.On("GenerativeModel", GeminiModel).Return(mockModel).Once()
mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("Autofix for issue 1", nil).Once()

// Act
err := generateSolutionByGemini(mockClient, issues)

// Assert
assert.NoError(t, err)
assert.Equal(t, "Autofix for issue 1", issues[0].Autofix)
mockClient.AssertExpectations(t)
mockModel.AssertExpectations(t)
require.NoError(t, err)
assert.Equal(t, []*issue.Issue{{What: "Example issue 1", Autofix: "Autofix for issue 1"}}, issues)
mock.AssertExpectationsForObjects(t, mockClient, mockModel)
}

func TestGenerateSolutionByGemini_NoCandidates(t *testing.T) {
Expand All @@ -65,17 +65,15 @@ func TestGenerateSolutionByGemini_NoCandidates(t *testing.T) {

mockClient := new(MockGenAIClient)
mockModel := new(MockGenAIGenerativeModel)
mockClient.On("GenerativeModel", GeminiModel).Return(mockModel)
mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("", nil)
mockClient.On("GenerativeModel", GeminiModel).Return(mockModel).Once()
mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("", nil).Once()

// Act
err := generateSolutionByGemini(mockClient, issues)

// Assert
assert.Error(t, err)
assert.Equal(t, "no autofix returned by gemini", err.Error())
mockClient.AssertExpectations(t)
mockModel.AssertExpectations(t)
require.EqualError(t, err, "no autofix returned by gemini")
mock.AssertExpectationsForObjects(t, mockClient, mockModel)
}

func TestGenerateSolutionByGemini_APIError(t *testing.T) {
Expand All @@ -86,17 +84,15 @@ func TestGenerateSolutionByGemini_APIError(t *testing.T) {

mockClient := new(MockGenAIClient)
mockModel := new(MockGenAIGenerativeModel)
mockClient.On("GenerativeModel", GeminiModel).Return(mockModel)
mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("", errors.New("API error"))
mockClient.On("GenerativeModel", GeminiModel).Return(mockModel).Once()
mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("", errors.New("API error")).Once()

// Act
err := generateSolutionByGemini(mockClient, issues)

// Assert
assert.Error(t, err)
assert.Equal(t, "generating autofix with gemini: API error", err.Error())
mockClient.AssertExpectations(t)
mockModel.AssertExpectations(t)
require.EqualError(t, err, "generating autofix with gemini: API error")
mock.AssertExpectationsForObjects(t, mockClient, mockModel)
}

func TestGenerateSolution_UnsupportedProvider(t *testing.T) {
Expand All @@ -109,6 +105,5 @@ func TestGenerateSolution_UnsupportedProvider(t *testing.T) {
err := GenerateSolution("unsupported-provider", "test-api-key", "", issues)

// Assert
assert.Error(t, err)
assert.Equal(t, "ai provider not supported", err.Error())
require.EqualError(t, err, "ai provider not supported")
}
Loading