Skip to content

Commit 5cef3cb

Browse files
committed
fix: remove cyclic dependency on learn mock
1 parent 83a3cfb commit 5cef3cb

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

infrastructure/oss/unified_converter_test.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/stretchr/testify/assert"
2828
"github.com/stretchr/testify/require"
2929

30+
"github.com/snyk/snyk-ls/infrastructure/learn/mock_learn"
3031
ctx2 "github.com/snyk/snyk-ls/internal/context"
3132
"github.com/snyk/snyk-ls/internal/testutil"
3233
"github.com/snyk/snyk-ls/internal/types"
@@ -131,9 +132,18 @@ func Test_UnifiedIssue_HasUpgradeQuickFixAction(t *testing.T) {
131132
require.NoError(t, err)
132133
path := filepath.Join(workDir, "package.json")
133134
ctx = ctx2.NewContextWithWorkDirAndFilePath(ctx, types.FilePath(workDir), types.FilePath(path))
135+
136+
// Create learn service mock and inject into context (merge with existing dependencies)
137+
ctrl := gomock.NewController(t)
138+
learnMock := mock_learn.NewMockService(ctrl)
139+
learnMock.EXPECT().GetLesson(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
140+
deps, _ := ctx2.DependenciesFromContext(ctx)
141+
deps[ctx2.DepLearnService] = learnMock
142+
ctx = ctx2.NewContextWithDependencies(ctx, deps)
143+
134144
problems := asProblemsMap(ctx, []testapi.FindingData{finding})
135145
require.NotEmpty(t, problems)
136-
// Enable OSS quick-fix actions (deps already injected by UnitTestWithCtx)
146+
// Enable OSS quick-fix actions
137147
c.SetSnykOSSQuickFixCodeActionsEnabled(true)
138148

139149
// Pick first problem group
@@ -182,6 +192,15 @@ func Test_UnifiedIssue_ProducesUpgradeCodeLens(t *testing.T) {
182192
require.NoError(t, err)
183193
path := filepath.Join(workDir, "package.json")
184194
ctx = ctx2.NewContextWithWorkDirAndFilePath(ctx, types.FilePath(workDir), types.FilePath(path))
195+
196+
// Create learn service mock and inject into context (merge with existing dependencies)
197+
ctrl := gomock.NewController(t)
198+
learnMock := mock_learn.NewMockService(ctrl)
199+
learnMock.EXPECT().GetLesson(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
200+
deps, _ := ctx2.DependenciesFromContext(ctx)
201+
deps[ctx2.DepLearnService] = learnMock
202+
ctx = ctx2.NewContextWithDependencies(ctx, deps)
203+
185204
problems := asProblemsMap(ctx, []testapi.FindingData{finding})
186205
require.NotEmpty(t, problems)
187206
// Enable OSS quick-fix actions
@@ -192,7 +211,7 @@ func Test_UnifiedIssue_ProducesUpgradeCodeLens(t *testing.T) {
192211
require.NoError(t, err)
193212
require.NotNil(t, issue)
194213

195-
// deps already injected by UnitTestWithCtx
214+
// deps already injected above
196215
enriched := addUnifiedOssQuickFixesAndLenses(ctx, []types.Issue{issue})
197216
require.Len(t, enriched, 1)
198217
enrichedIssue := enriched[0]

internal/testutil/test_setup.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
ctx2 "github.com/snyk/snyk-ls/internal/context"
2727
"github.com/snyk/snyk-ls/internal/util"
2828

29-
"github.com/golang/mock/gomock"
3029
"github.com/snyk/go-application-framework/pkg/configuration"
3130
"github.com/snyk/go-application-framework/pkg/mocks"
3231
"github.com/stretchr/testify/require"
@@ -35,7 +34,6 @@ import (
3534
"github.com/snyk/go-application-framework/pkg/local_workflows/code_workflow/sast_contract"
3635

3736
"github.com/snyk/snyk-ls/application/config"
38-
"github.com/snyk/snyk-ls/infrastructure/learn/mock_learn"
3937
"github.com/snyk/snyk-ls/internal/constants"
4038
internal_er "github.com/snyk/snyk-ls/internal/observability/error_reporting"
4139
"github.com/snyk/snyk-ls/internal/progress"
@@ -87,14 +85,11 @@ func UnitTestWithCtx(t *testing.T) (*config.Config, context.Context) {
8785
progress.CleanupChannels()
8886
})
8987

90-
// Create mock learn service and error reporter for tests
91-
ctrl := gomock.NewController(t)
92-
ls := mock_learn.NewMockService(ctrl)
93-
ls.EXPECT().GetLesson(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
94-
88+
// Create error reporter for tests
89+
// Note: Learn service is not provided here to avoid import cycle with infrastructure/learn tests.
90+
// Tests that need a learn service should create their own mock (see oss_test.go getLearnMock for example).
9591
ctx := ctx2.NewContextWithDependencies(t.Context(), map[string]any{
9692
ctx2.DepConfig: c,
97-
ctx2.DepLearnService: ls,
9893
ctx2.DepErrorReporter: internal_er.NewTestErrorReporter(),
9994
})
10095
ctx = ctx2.NewContextWithLogger(ctx, c.Logger())

0 commit comments

Comments
 (0)