Skip to content

Commit 4e527aa

Browse files
authored
test: use slackcontext.MockContext(...) instead of context.Background() (#28)
* refactor: Add slackcontext package to DRY context management * refactor: Revert changes to unrelated error codes * test: use .MockContext(...) instead of context.Background() * test: remove duplicate test for collaborator printSuccess
1 parent 034ae57 commit 4e527aa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+325
-300
lines changed

cmd/app/link_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/slackapi/slack-cli/internal/iostreams"
2525
"github.com/slackapi/slack-cli/internal/shared"
2626
"github.com/slackapi/slack-cli/internal/shared/types"
27+
"github.com/slackapi/slack-cli/internal/slackcontext"
2728
"github.com/slackapi/slack-cli/internal/slackdeps"
2829
"github.com/slackapi/slack-cli/internal/slackerror"
2930
"github.com/slackapi/slack-cli/test/testutil"
@@ -569,10 +570,8 @@ func Test_Apps_LinkAppHeaderSection(t *testing.T) {
569570
}
570571
for name, tt := range tests {
571572
t.Run(name, func(t *testing.T) {
572-
// Setup parameters for test
573-
ctx := context.Background()
574-
575573
// Create mocks
574+
ctx := slackcontext.MockContext(t.Context())
576575
clientsMock := shared.NewClientsMock()
577576
clientsMock.AddDefaultMocks()
578577

cmd/app/uninstall_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestAppsUninstall(t *testing.T) {
4848
testutil.TableTestCommand(t, testutil.CommandTests{
4949
"Successfully uninstall": {
5050
Setup: func(t *testing.T, ctx context.Context, clientsMock *shared.ClientsMock, clients *shared.ClientFactory) {
51-
prepareCommonUninstallMocks(clients, clientsMock)
51+
prepareCommonUninstallMocks(ctx, clients, clientsMock)
5252
clientsMock.ApiInterface.On("UninstallApp", mock.Anything, mock.Anything, fakeAppID, fakeAppTeamID).
5353
Return(nil).Once()
5454
},
@@ -58,7 +58,7 @@ func TestAppsUninstall(t *testing.T) {
5858
},
5959
"Successfully uninstall with a get-manifest hook error": {
6060
Setup: func(t *testing.T, ctx context.Context, clientsMock *shared.ClientsMock, clients *shared.ClientFactory) {
61-
prepareCommonUninstallMocks(clients, clientsMock)
61+
prepareCommonUninstallMocks(ctx, clients, clientsMock)
6262
clientsMock.ApiInterface.On("UninstallApp", mock.Anything, mock.Anything, fakeAppID, fakeAppTeamID).
6363
Return(nil).Once()
6464
manifestMock := &app.ManifestMockObject{}
@@ -73,7 +73,7 @@ func TestAppsUninstall(t *testing.T) {
7373
"Fail to uninstall due to API error": {
7474
ExpectedError: slackerror.New("something went wrong"),
7575
Setup: func(t *testing.T, ctx context.Context, clientsMock *shared.ClientsMock, clients *shared.ClientFactory) {
76-
prepareCommonUninstallMocks(clients, clientsMock)
76+
prepareCommonUninstallMocks(ctx, clients, clientsMock)
7777
clientsMock.ApiInterface.On("UninstallApp", mock.Anything, mock.Anything, fakeAppID, fakeAppTeamID).
7878
Return(slackerror.New("something went wrong")).Once()
7979
},
@@ -82,7 +82,7 @@ func TestAppsUninstall(t *testing.T) {
8282
CmdArgs: []string{},
8383
ExpectedError: slackerror.New(slackerror.ErrCredentialsNotFound),
8484
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
85-
prepareCommonUninstallMocks(cf, cm)
85+
prepareCommonUninstallMocks(ctx, cf, cm)
8686
appSelectMock := prompts.NewAppSelectMock()
8787
uninstallAppSelectPromptFunc = appSelectMock.AppSelectPrompt
8888
appSelectMock.On("AppSelectPrompt").Return(prompts.SelectedApp{App: fakeApp}, nil)
@@ -95,7 +95,7 @@ func TestAppsUninstall(t *testing.T) {
9595
})
9696
}
9797

98-
func prepareCommonUninstallMocks(clients *shared.ClientFactory, clientsMock *shared.ClientsMock) *shared.ClientFactory {
98+
func prepareCommonUninstallMocks(ctx context.Context, clients *shared.ClientFactory, clientsMock *shared.ClientsMock) *shared.ClientFactory {
9999

100100
// Mock App Selection
101101
appSelectMock := prompts.NewAppSelectMock()
@@ -124,7 +124,7 @@ func prepareCommonUninstallMocks(clients *shared.ClientFactory, clientsMock *sha
124124

125125
clients.AppClient().AppClientInterface = appClientMock
126126

127-
err := clients.AppClient().SaveDeployed(context.Background(), fakeApp)
127+
err := clients.AppClient().SaveDeployed(ctx, fakeApp)
128128
if err != nil {
129129
panic("error setting up test; cant write apps.json")
130130
}

cmd/auth/token_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@
1515
package auth
1616

1717
import (
18-
"context"
1918
"testing"
2019

2120
"github.com/slackapi/slack-cli/internal/api"
2221
"github.com/slackapi/slack-cli/internal/shared"
2322
"github.com/slackapi/slack-cli/internal/shared/types"
23+
"github.com/slackapi/slack-cli/internal/slackcontext"
2424
"github.com/slackapi/slack-cli/test/testutil"
2525
"github.com/stretchr/testify/mock"
2626
"github.com/stretchr/testify/require"
2727
)
2828

2929
func TestTokenCommand(t *testing.T) {
30+
ctx := slackcontext.MockContext(t.Context())
31+
3032
clientsMock := shared.NewClientsMock()
3133
clientsMock.ApiInterface.On("ValidateSession", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(api.AuthSession{UserID: &mockOrgAuth.UserID,
3234
TeamID: &mockOrgAuth.TeamID,
@@ -42,7 +44,7 @@ func TestTokenCommand(t *testing.T) {
4244
tokenFlag = "xoxp-example-1234"
4345

4446
cmd := NewTokenCommand(clients)
45-
cmd.SetContext(context.Background())
47+
cmd.SetContext(ctx)
4648
testutil.MockCmdIO(clients.IO, cmd)
4749

4850
serviceTokenFlag = true

cmd/doctor/check_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package doctor
1616

1717
import (
18-
"context"
1918
"fmt"
2019
"runtime"
2120
"testing"
@@ -26,6 +25,7 @@ import (
2625
"github.com/slackapi/slack-cli/internal/pkg/version"
2726
"github.com/slackapi/slack-cli/internal/shared"
2827
"github.com/slackapi/slack-cli/internal/shared/types"
28+
"github.com/slackapi/slack-cli/internal/slackcontext"
2929
"github.com/slackapi/slack-cli/internal/slackerror"
3030
"github.com/stretchr/testify/assert"
3131
"github.com/stretchr/testify/mock"
@@ -34,10 +34,10 @@ import (
3434

3535
func TestDoctorCheckOS(t *testing.T) {
3636
t.Run("returns the operating system version", func(t *testing.T) {
37+
ctx := slackcontext.MockContext(t.Context())
3738
clientsMock := shared.NewClientsMock()
3839
clientsMock.AddDefaultMocks()
3940
clients := shared.NewClientFactory(clientsMock.MockClientFactory())
40-
ctx := context.Background()
4141
expected := Section{
4242
Label: "Operating System",
4343
Value: "the computer conductor",
@@ -56,10 +56,10 @@ func TestDoctorCheckOS(t *testing.T) {
5656

5757
func TestDoctorCheckCLIVersion(t *testing.T) {
5858
t.Run("returns the current version of this tool", func(t *testing.T) {
59+
ctx := slackcontext.MockContext(t.Context())
5960
clientsMock := shared.NewClientsMock()
6061
clientsMock.AddDefaultMocks()
6162
clients := shared.NewClientFactory(clientsMock.MockClientFactory())
62-
ctx := context.Background()
6363
expected := Section{
6464
Label: "CLI",
6565
Value: "this tool for building Slack apps",
@@ -141,13 +141,13 @@ func TestDoctorCheckProjectConfig(t *testing.T) {
141141

142142
for name, tt := range tests {
143143
t.Run(name, func(t *testing.T) {
144+
ctx := slackcontext.MockContext(t.Context())
144145
clientsMock := shared.NewClientsMock()
145146
clientsMock.AddDefaultMocks()
146147
pcm := &config.ProjectConfigMock{}
147148
pcm.On("ReadProjectConfigFile", mock.Anything).Return(tt.projectConfig, nil)
148149
clientsMock.Config.ProjectConfig = pcm
149150
clients := shared.NewClientFactory(clientsMock.MockClientFactory())
150-
ctx := context.Background()
151151
expected := Section{
152152
Label: "Configurations",
153153
Value: "your project's CLI settings",
@@ -218,10 +218,10 @@ func TestDoctorCheckProjectDeps(t *testing.T) {
218218

219219
for name, tt := range tests {
220220
t.Run(name, func(t *testing.T) {
221+
ctx := slackcontext.MockContext(t.Context())
221222
clientsMock := shared.NewClientsMock()
222223
clientsMock.AddDefaultMocks()
223224
clients := tt.mockHookSetup(clientsMock)
224-
ctx := context.Background()
225225
expected := Section{
226226
Label: "Dependencies",
227227
Value: "requisites for development",
@@ -246,6 +246,7 @@ func TestDoctorCheckCLIConfig(t *testing.T) {
246246

247247
for name, tt := range tests {
248248
t.Run(name, func(t *testing.T) {
249+
ctx := slackcontext.MockContext(t.Context())
249250
clientsMock := shared.NewClientsMock()
250251
clientsMock.AddDefaultMocks()
251252
scm := &config.SystemConfigMock{}
@@ -254,7 +255,6 @@ func TestDoctorCheckCLIConfig(t *testing.T) {
254255
}, nil)
255256
clientsMock.Config.SystemConfig = scm
256257
clients := shared.NewClientFactory(clientsMock.MockClientFactory())
257-
ctx := context.Background()
258258
expected := Section{
259259
Label: "Configurations",
260260
Value: "any adjustments to settings",
@@ -297,10 +297,10 @@ func TestDoctorCheckCLICreds(t *testing.T) {
297297

298298
for name := range tests {
299299
t.Run(name, func(t *testing.T) {
300+
ctx := slackcontext.MockContext(t.Context())
300301
clientsMock := shared.NewClientsMock()
301302
clientsMock.AddDefaultMocks()
302303
clients := shared.NewClientFactory(clientsMock.MockClientFactory())
303-
ctx := context.Background()
304304
expected := Section{
305305
Label: "Credentials",
306306
Value: "your Slack authentication",
@@ -391,10 +391,10 @@ func TestDoctorCheckProjectTooling(t *testing.T) {
391391

392392
for name, tt := range tests {
393393
t.Run(name, func(t *testing.T) {
394+
ctx := slackcontext.MockContext(t.Context())
394395
clientsMock := shared.NewClientsMock()
395396
clientsMock.AddDefaultMocks()
396397
clients := tt.mockHookSetup(clientsMock)
397-
ctx := context.Background()
398398
expected := Section{
399399
Label: "Runtime",
400400
Value: "foundations for the application",
@@ -410,7 +410,7 @@ func TestDoctorCheckProjectTooling(t *testing.T) {
410410

411411
func TestDoctorCheckGit(t *testing.T) {
412412
t.Run("returns the version of git", func(t *testing.T) {
413-
ctx := context.Background()
413+
ctx := slackcontext.MockContext(t.Context())
414414
gitVersion, err := deputil.GetGitVersion()
415415
require.NoError(t, err)
416416
expected := Section{

cmd/doctor/doctor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package doctor
1616

1717
import (
1818
"bytes"
19-
"context"
2019
"fmt"
2120
"runtime"
2221
"testing"
@@ -28,6 +27,7 @@ import (
2827
"github.com/slackapi/slack-cli/internal/pkg/version"
2928
"github.com/slackapi/slack-cli/internal/shared"
3029
"github.com/slackapi/slack-cli/internal/shared/types"
30+
"github.com/slackapi/slack-cli/internal/slackcontext"
3131
"github.com/slackapi/slack-cli/internal/slackerror"
3232
"github.com/slackapi/slack-cli/test/testutil"
3333
"github.com/stretchr/testify/assert"
@@ -401,7 +401,7 @@ func TestDoctorHook(t *testing.T) {
401401

402402
for name, tt := range tests {
403403
t.Run(name, func(t *testing.T) {
404-
ctx := context.Background()
404+
ctx := slackcontext.MockContext(t.Context())
405405
clientsMock := shared.NewClientsMock()
406406
clientsMock.AddDefaultMocks()
407407
clients := tt.mockHookSetup(clientsMock)

cmd/env/add_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func Test_Env_AddCommand(t *testing.T) {
145145
"add a variable using arguments": {
146146
CmdArgs: []string{"ENV_NAME", "ENV_VALUE"},
147147
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
148-
setupEnvAddCommandMocks(cm, cf)
148+
setupEnvAddCommandMocks(ctx, cm, cf)
149149
},
150150
ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) {
151151
cm.ApiInterface.AssertCalled(
@@ -169,7 +169,7 @@ func Test_Env_AddCommand(t *testing.T) {
169169
"provide a variable name by argument and value by prompt": {
170170
CmdArgs: []string{"ENV_NAME"},
171171
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
172-
setupEnvAddCommandMocks(cm, cf)
172+
setupEnvAddCommandMocks(ctx, cm, cf)
173173
cm.IO.On(
174174
"PasswordPrompt",
175175
mock.Anything,
@@ -200,7 +200,7 @@ func Test_Env_AddCommand(t *testing.T) {
200200
"provide a variable name by argument and value by flag": {
201201
CmdArgs: []string{"ENV_NAME", "--value", "example_value"},
202202
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
203-
setupEnvAddCommandMocks(cm, cf)
203+
setupEnvAddCommandMocks(ctx, cm, cf)
204204
cm.IO.On(
205205
"PasswordPrompt",
206206
mock.Anything,
@@ -231,7 +231,7 @@ func Test_Env_AddCommand(t *testing.T) {
231231
"provide both variable name and value by prompt": {
232232
CmdArgs: []string{},
233233
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
234-
setupEnvAddCommandMocks(cm, cf)
234+
setupEnvAddCommandMocks(ctx, cm, cf)
235235
cm.IO.On(
236236
"InputPrompt",
237237
mock.Anything,
@@ -276,10 +276,10 @@ func Test_Env_AddCommand(t *testing.T) {
276276
}
277277

278278
// setupEnvAddCommandMocks prepares common mocks for these tests
279-
func setupEnvAddCommandMocks(cm *shared.ClientsMock, cf *shared.ClientFactory) {
279+
func setupEnvAddCommandMocks(ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
280280
cf.SDKConfig = hooks.NewSDKConfigMock()
281281
cm.AddDefaultMocks()
282-
_ = cf.AppClient().SaveDeployed(context.Background(), mockApp)
282+
_ = cf.AppClient().SaveDeployed(ctx, mockApp)
283283

284284
appSelectMock := prompts.NewAppSelectMock()
285285
teamAppSelectPromptFunc = appSelectMock.TeamAppSelectPrompt

cmd/feedback/feedback_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/slackapi/slack-cli/internal/config"
2525
"github.com/slackapi/slack-cli/internal/iostreams"
2626
"github.com/slackapi/slack-cli/internal/shared"
27+
"github.com/slackapi/slack-cli/internal/slackcontext"
2728
"github.com/slackapi/slack-cli/internal/slackerror"
2829
"github.com/slackapi/slack-cli/internal/style"
2930
"github.com/stretchr/testify/assert"
@@ -46,6 +47,7 @@ func TestFeedbackCommand(t *testing.T) {
4647
}
4748

4849
// Prepare mocks
50+
ctx := slackcontext.MockContext(t.Context())
4951
clientsMock := shared.NewClientsMock()
5052
clientsMock.AddDefaultMocks()
5153

@@ -69,7 +71,7 @@ func TestFeedbackCommand(t *testing.T) {
6971

7072
// Execute test
7173
cmd := NewFeedbackCommand(clients)
72-
err := runFeedbackCommand(context.Background(), clients, cmd)
74+
err := runFeedbackCommand(ctx, clients, cmd)
7375
assert.NoError(t, err)
7476
clientsMock.Browser.AssertCalled(t, "OpenURL", "https://survey.com?project_id=projectID&system_id=systemID&utm_medium=cli&utm_source=cli")
7577
})
@@ -104,6 +106,7 @@ func TestFeedbackCommand(t *testing.T) {
104106
}
105107

106108
// Prepare mocks
109+
ctx := slackcontext.MockContext(t.Context())
107110
clientsMock := shared.NewClientsMock()
108111
clientsMock.AddDefaultMocks()
109112

@@ -134,7 +137,7 @@ func TestFeedbackCommand(t *testing.T) {
134137

135138
// Execute test
136139
cmd := NewFeedbackCommand(clients)
137-
err := runFeedbackCommand(context.Background(), clients, cmd)
140+
err := runFeedbackCommand(ctx, clients, cmd)
138141
assert.NoError(t, err)
139142
clientsMock.Browser.AssertCalled(t, "OpenURL", "https://survey.com?project_id=projectID&system_id=systemID&utm_medium=cli&utm_source=cli")
140143
})
@@ -196,6 +199,7 @@ func TestShowSurveyMessages(t *testing.T) {
196199
}
197200

198201
// Prepare mocks
202+
ctx := slackcontext.MockContext(t.Context())
199203
clientsMock := shared.NewClientsMock()
200204
clientsMock.AddDefaultMocks()
201205

@@ -239,7 +243,7 @@ func TestShowSurveyMessages(t *testing.T) {
239243
SurveyStore = surveys
240244

241245
// Execute test
242-
err := ShowSurveyMessages(context.Background(), clients)
246+
err := ShowSurveyMessages(ctx, clients)
243247
assert.NoError(t, err)
244248
clientsMock.Browser.AssertCalled(t, "OpenURL", "https://B.com?project_id=projectID&system_id=systemID&utm_medium=cli&utm_source=cli")
245249
clientsMock.Browser.AssertCalled(t, "OpenURL", "https://C.com?project_id=projectID&system_id=systemID&utm_medium=cli&utm_source=cli")

cmd/function/access_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/slackapi/slack-cli/internal/shared"
2323
"github.com/slackapi/slack-cli/internal/shared/types"
24+
"github.com/slackapi/slack-cli/internal/slackcontext"
2425
"github.com/slackapi/slack-cli/internal/slackerror"
2526
"github.com/slackapi/slack-cli/test/testutil"
2627
"github.com/spf13/afero"
@@ -326,6 +327,7 @@ func TestFunctionDistributionCommand_PermissionsFile(t *testing.T) {
326327

327328
for name, tt := range tests {
328329
t.Run(name, func(t *testing.T) {
330+
ctx := slackcontext.MockContext(t.Context())
329331
clientsMock := shared.NewClientsMock()
330332
clientsMock.IO.AddDefaultMocks()
331333
err := afero.WriteFile(clientsMock.Fs, tt.filename, []byte(tt.data), 0644)
@@ -339,7 +341,6 @@ func TestFunctionDistributionCommand_PermissionsFile(t *testing.T) {
339341
clientsMock.ApiInterface.On("FunctionDistributionSet", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
340342
Return([]types.FunctionDistributionUser{}, nil)
341343

342-
ctx := context.Background()
343344
clients := shared.NewClientFactory(clientsMock.MockClientFactory())
344345
app := types.App{AppID: "A123"}
345346

@@ -400,6 +401,7 @@ func TestFunctionDistributeCommand_UpdateNamedEntitiesDistribution(t *testing.T)
400401

401402
for name, tt := range tests {
402403
t.Run(name, func(t *testing.T) {
404+
ctx := slackcontext.MockContext(t.Context())
403405
clientsMock := shared.NewClientsMock()
404406
clientsMock.ApiInterface.On("FunctionDistributionSet", mock.Anything, mock.Anything, mock.Anything, types.NAMED_ENTITIES, mock.Anything).
405407
Return([]types.FunctionDistributionUser{}, nil).
@@ -414,7 +416,6 @@ func TestFunctionDistributeCommand_UpdateNamedEntitiesDistribution(t *testing.T)
414416

415417
app := types.App{AppID: "A123"}
416418
function := "Ft123"
417-
ctx := context.Background()
418419
clients := shared.NewClientFactory(clientsMock.MockClientFactory())
419420

420421
err := updateNamedEntitiesDistribution(ctx, clients, app, function, tt.updatedEntities)

0 commit comments

Comments
 (0)