Skip to content

Commit 85749e2

Browse files
authored
refactor: update .Context() references for consistency (#33)
* refactor: Add slackcontext package to DRY context management * refactor: Revert changes to unrelated error codes * test: mock context in TableTestCommand * test: mock context in TableTestCommand ExpectedAsserts * test: use ctx in mocked ExpectedAsserts * test: use ctx in mocked ExpectedAsserts to link_test.go * test: use ctx in mocked ExpectedAsserts to update_test.go * test: use .MockContext(...) instead of context.Background() * test: update context references for .Setup * refactor: update .Context() references for consistency * refactor: update span to not return a ctx since it's locally scoped * refactor: update root.go use to ctx for OnInitialize and OnFinalize * refactor: pass ctx into Init
1 parent c084fde commit 85749e2

File tree

30 files changed

+104
-73
lines changed

30 files changed

+104
-73
lines changed

cmd/app/app.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ func NewCommand(clients *shared.ClientFactory) *cobra.Command {
4646
return runListCommand(cmd, clients)
4747
},
4848
PostRunE: func(cmd *cobra.Command, args []string) error {
49+
ctx := cmd.Context()
4950
if cmd.CalledAs() == "workspace" {
50-
clients.IO.PrintInfo(cmd.Context(), false, fmt.Sprintf(
51+
clients.IO.PrintInfo(ctx, false, fmt.Sprintf(
5152
"\n%s It looks like you used %s. This command will be deprecated in an upcoming release.\n You can now use %s instead of %s.\n ",
5253
style.Emoji("bulb"),
5354
style.Commandf("workspace", true),

cmd/app/link_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func Test_Apps_Link(t *testing.T) {
6161
mockLinkSlackAuth1,
6262
}, nil)
6363
cm.AddDefaultMocks()
64-
setupAppLinkCommandMocks(t, cm, cf)
64+
setupAppLinkCommandMocks(t, ctx, cm, cf)
6565
cm.IO.On("SelectPrompt",
6666
mock.Anything,
6767
"Select the existing app team",
@@ -118,7 +118,7 @@ func Test_Apps_Link(t *testing.T) {
118118
mockLinkSlackAuth1,
119119
}, nil)
120120
cm.AddDefaultMocks()
121-
setupAppLinkCommandMocks(t, cm, cf)
121+
setupAppLinkCommandMocks(t, ctx, cm, cf)
122122
cm.IO.On("SelectPrompt",
123123
mock.Anything,
124124
"Select the existing app team",
@@ -177,7 +177,7 @@ func Test_Apps_Link(t *testing.T) {
177177
mockLinkSlackAuth2,
178178
}, nil)
179179
cm.AddDefaultMocks()
180-
setupAppLinkCommandMocks(t, cm, cf)
180+
setupAppLinkCommandMocks(t, ctx, cm, cf)
181181
existingApp := types.App{
182182
AppID: mockLinkAppID1,
183183
TeamDomain: mockLinkSlackAuth1.TeamDomain,
@@ -246,7 +246,7 @@ func Test_Apps_Link(t *testing.T) {
246246
mockLinkSlackAuth2,
247247
}, nil)
248248
cm.AddDefaultMocks()
249-
setupAppLinkCommandMocks(t, cm, cf)
249+
setupAppLinkCommandMocks(t, ctx, cm, cf)
250250
existingApp := types.App{
251251
AppID: mockLinkAppID1,
252252
TeamDomain: mockLinkSlackAuth1.TeamDomain,
@@ -321,7 +321,7 @@ func Test_Apps_Link(t *testing.T) {
321321
mockLinkSlackAuth2,
322322
}, nil)
323323
cm.AddDefaultMocks()
324-
setupAppLinkCommandMocks(t, cm, cf)
324+
setupAppLinkCommandMocks(t, ctx, cm, cf)
325325
existingApp := types.App{
326326
AppID: mockLinkAppID2,
327327
TeamDomain: mockLinkSlackAuth2.TeamDomain,
@@ -389,7 +389,7 @@ func Test_Apps_Link(t *testing.T) {
389389
mockLinkSlackAuth2,
390390
}, nil)
391391
cm.AddDefaultMocks()
392-
setupAppLinkCommandMocks(t, cm, cf)
392+
setupAppLinkCommandMocks(t, ctx, cm, cf)
393393
cm.IO.On("SelectPrompt",
394394
mock.Anything,
395395
"Select the existing app team",
@@ -437,9 +437,9 @@ func Test_Apps_Link(t *testing.T) {
437437
mockLinkSlackAuth1,
438438
}, nil)
439439
cm.AddDefaultMocks()
440-
setupAppLinkCommandMocks(t, cm, cf)
440+
setupAppLinkCommandMocks(t, ctx, cm, cf)
441441
// Set manifest source to project to trigger confirmation prompt
442-
if err := cm.Config.ProjectConfig.SetManifestSource(t.Context(), config.MANIFEST_SOURCE_LOCAL); err != nil {
442+
if err := cm.Config.ProjectConfig.SetManifestSource(ctx, config.MANIFEST_SOURCE_LOCAL); err != nil {
443443
require.FailNow(t, fmt.Sprintf("Failed to set the manifest source in the memory-based file system: %s", err))
444444
}
445445
// Accept manifest source confirmation prompt
@@ -506,9 +506,9 @@ func Test_Apps_Link(t *testing.T) {
506506
"decline manifest source prompt should not link app": {
507507
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
508508
cm.AddDefaultMocks()
509-
setupAppLinkCommandMocks(t, cm, cf)
509+
setupAppLinkCommandMocks(t, ctx, cm, cf)
510510
// Set manifest source to project to trigger confirmation prompt
511-
if err := cm.Config.ProjectConfig.SetManifestSource(t.Context(), config.MANIFEST_SOURCE_LOCAL); err != nil {
511+
if err := cm.Config.ProjectConfig.SetManifestSource(ctx, config.MANIFEST_SOURCE_LOCAL); err != nil {
512512
require.FailNow(t, fmt.Sprintf("Failed to set the manifest source in the memory-based file system: %s", err))
513513
}
514514
// Decline manifest source confirmation prompt
@@ -593,8 +593,7 @@ func Test_Apps_LinkAppHeaderSection(t *testing.T) {
593593
}
594594
}
595595

596-
func setupAppLinkCommandMocks(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
597-
ctx := t.Context()
596+
func setupAppLinkCommandMocks(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
598597
projectDirPath := slackdeps.MockWorkingDirectory
599598
cm.Os.On("Getwd").Return(projectDirPath, nil)
600599

cmd/auth/list.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ func NewListCommand(clients *shared.ClientFactory) *cobra.Command {
5151

5252
// runListCommand will execute the list command
5353
func runListCommand(cmd *cobra.Command, clients *shared.ClientFactory) error {
54+
ctx := cmd.Context()
5455
log := newListLogger(cmd, clients.IO)
55-
userAuthList, err := listFunc(cmd.Context(), clients, log)
56+
userAuthList, err := listFunc(ctx, clients, log)
5657
if err != nil {
5758
return err
5859
}
@@ -87,6 +88,8 @@ func newListLogger(cmd *cobra.Command, IO iostreams.IOStreamer) *logger.Logger {
8788
// API Host: https://dev.slack.com (optional, only shown for custom API Hosts)
8889
// Last Updated: 2021-03-12 11:18:00 -0700
8990
func printAuthList(cmd *cobra.Command, IO iostreams.IOStreamer, userAuthList []types.SlackAuth) {
91+
ctx := cmd.Context()
92+
9093
// Based on loosely on time.RFC3339
9194
timeFormat := "2006-01-02 15:04:05 Z07:00"
9295

@@ -121,7 +124,7 @@ func printAuthList(cmd *cobra.Command, IO iostreams.IOStreamer, userAuthList []t
121124
cmd.Println()
122125

123126
// Print a trace with info about the authorization
124-
IO.PrintTrace(cmd.Context(), slacktrace.AuthListInfo, authInfo.UserID, authInfo.TeamID)
127+
IO.PrintTrace(ctx, slacktrace.AuthListInfo, authInfo.UserID, authInfo.TeamID)
125128
}
126129

127130
// When there are no authorizations
@@ -130,11 +133,12 @@ func printAuthList(cmd *cobra.Command, IO iostreams.IOStreamer, userAuthList []t
130133
}
131134

132135
// Print a trace with the total number of authorized workspaces
133-
IO.PrintTrace(cmd.Context(), slacktrace.AuthListCount, fmt.Sprint(len(userAuthList)))
136+
IO.PrintTrace(ctx, slacktrace.AuthListCount, fmt.Sprint(len(userAuthList)))
134137
}
135138

136139
// printAuthListSuccess is displayed at the very end and helps guide the developer toward next steps.
137140
func printAuthListSuccess(cmd *cobra.Command, IO iostreams.IOStreamer, userAuthList []types.SlackAuth) {
141+
ctx := cmd.Context()
138142
commandText := style.Commandf("login", true)
139143

140144
// When there are no authorizations, guide the user to creating an authorization.
@@ -145,5 +149,5 @@ func printAuthListSuccess(cmd *cobra.Command, IO iostreams.IOStreamer, userAuthL
145149
)
146150
}
147151

148-
IO.PrintTrace(cmd.Context(), slacktrace.AuthListSuccess)
152+
IO.PrintTrace(ctx, slacktrace.AuthListSuccess)
149153
}

cmd/auth/login.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,15 @@ func RunLoginCommand(clients *shared.ClientFactory, cmd *cobra.Command) (types.S
127127
}
128128

129129
func printAuthSuccess(cmd *cobra.Command, IO iostreams.IOStreamer, credentialsPath string, token string) {
130+
ctx := cmd.Context()
131+
130132
var secondaryLog string
131133
if credentialsPath != "" {
132134
secondaryLog = fmt.Sprintf("Authorization data was saved to %s", style.HomePath(credentialsPath))
133135
} else if serviceTokenFlag && tokenFlag == "" {
134136
secondaryLog = fmt.Sprintf("Service token:\n\n %s\n\nMake sure to copy the token now and save it safely.", token)
135137
}
136138

137-
ctx := cmd.Context()
138139
IO.PrintInfo(ctx, false, "\n%s", style.Sectionf(style.TextSection{
139140
Emoji: "key",
140141
Text: "You've successfully authenticated!",

cmd/collaborators/add.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ func NewAddCommand(clients *shared.ClientFactory) *cobra.Command {
5858
return cmdutil.IsValidProjectDirectory(clients)
5959
},
6060
RunE: func(cmd *cobra.Command, args []string) error {
61-
return runAddCommandFunc(cmd.Context(), clients, cmd, args)
61+
ctx := cmd.Context()
62+
return runAddCommandFunc(ctx, clients, cmd, args)
6263
},
6364
}
6465
cmd.Flags().StringVarP(&addFlags.permissionType, "permission-type", "P", "", "collaborator permission type: reader, owner")

cmd/collaborators/list.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ func NewListCommand(clients *shared.ClientFactory) *cobra.Command {
5353

5454
// runListCommand will execute the list command
5555
func runListCommand(cmd *cobra.Command, clients *shared.ClientFactory) error {
56-
var span opentracing.Span
5756
ctx := cmd.Context()
58-
span, ctx = opentracing.StartSpanFromContext(ctx, "cmd.Collaborators.List")
57+
span, _ := opentracing.StartSpanFromContext(ctx, "cmd.Collaborators.List")
5958
defer span.Finish()
6059

6160
// Get the app auth selection from the flag or prompt

cmd/collaborators/remove.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ func NewRemoveCommand(clients *shared.ClientFactory) *cobra.Command {
4848
return cmdutil.IsValidProjectDirectory(clients)
4949
},
5050
RunE: func(cmd *cobra.Command, args []string) error {
51-
return runRemoveCommandFunc(cmd.Context(), clients, cmd, args)
51+
ctx := cmd.Context()
52+
return runRemoveCommandFunc(ctx, clients, cmd, args)
5253
},
5354
}
5455
}

cmd/collaborators/update.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,8 @@ func NewUpdateCommand(clients *shared.ClientFactory) *cobra.Command {
7474

7575
// runUpdateCommand will execute the update command
7676
func runUpdateCommand(cmd *cobra.Command, clients *shared.ClientFactory, args []string) error {
77-
var span opentracing.Span
7877
ctx := cmd.Context()
79-
span, ctx = opentracing.StartSpanFromContext(ctx, "cmd.Collaborators.Update")
78+
span, _ := opentracing.StartSpanFromContext(ctx, "cmd.Collaborators.Update")
8079
defer span.Finish()
8180

8281
var slackUser types.SlackUser

cmd/doctor/doctor_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func TestDoctorCommand(t *testing.T) {
5151
expectedUpdateTime := "0001-01-01 00:00:00 Z"
5252

5353
t.Run("creates a complete report", func(t *testing.T) {
54+
ctx := slackcontext.MockContext(t.Context())
5455
clientsMock := shared.NewClientsMock()
5556
clientsMock.AuthInterface.On("Auths", mock.Anything).Return([]types.SlackAuth{expectedCredentials}, nil)
5657
clientsMock.AuthInterface.On("ResolveApiHost", mock.Anything, mock.Anything, mock.Anything).Return("api.slack.com")
@@ -90,7 +91,7 @@ func TestDoctorCommand(t *testing.T) {
9091
err := cmd.Execute()
9192
require.NoError(t, err)
9293

93-
report, err := performChecks(cmd.Context(), clients)
94+
report, err := performChecks(ctx, clients)
9495
require.NoError(t, err)
9596

9697
expectedValues := DoctorReport{

cmd/fingerprint/fingerprint.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ func NewCommand(clients *shared.ClientFactory) *cobra.Command {
4343
{Command: "_fingerprint", Meaning: "Print the unique value that identifies the Slack CLI binary"},
4444
}),
4545
RunE: func(cmd *cobra.Command, args []string) error {
46-
var span, _ = opentracing.StartSpanFromContext(cmd.Context(), "cmd._fingerprint")
46+
ctx := cmd.Context()
47+
var span, _ = opentracing.StartSpanFromContext(ctx, "cmd._fingerprint")
4748
defer span.Finish()
4849

49-
clients.IO.PrintInfo(cmd.Context(), false, fingerprintHash)
50+
clients.IO.PrintInfo(ctx, false, fingerprintHash)
5051
return nil
5152
},
5253
}

0 commit comments

Comments
 (0)