-
Notifications
You must be signed in to change notification settings - Fork 24
test: use slackcontext.MockContext(...) instead of context.Background() #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
51b3d28
f5ae262
9987754
f1d5fa8
82f4cd6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,7 +48,7 @@ func TestAppsUninstall(t *testing.T) { | |
| testutil.TableTestCommand(t, testutil.CommandTests{ | ||
| "Successfully uninstall": { | ||
| Setup: func(t *testing.T, ctx context.Context, clientsMock *shared.ClientsMock, clients *shared.ClientFactory) { | ||
| prepareCommonUninstallMocks(clients, clientsMock) | ||
| prepareCommonUninstallMocks(ctx, clients, clientsMock) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: This was missed in the PR that updated |
||
| clientsMock.ApiInterface.On("UninstallApp", mock.Anything, mock.Anything, fakeAppID, fakeAppTeamID). | ||
| Return(nil).Once() | ||
| }, | ||
|
|
@@ -58,7 +58,7 @@ func TestAppsUninstall(t *testing.T) { | |
| }, | ||
| "Successfully uninstall with a get-manifest hook error": { | ||
| Setup: func(t *testing.T, ctx context.Context, clientsMock *shared.ClientsMock, clients *shared.ClientFactory) { | ||
| prepareCommonUninstallMocks(clients, clientsMock) | ||
| prepareCommonUninstallMocks(ctx, clients, clientsMock) | ||
| clientsMock.ApiInterface.On("UninstallApp", mock.Anything, mock.Anything, fakeAppID, fakeAppTeamID). | ||
| Return(nil).Once() | ||
| manifestMock := &app.ManifestMockObject{} | ||
|
|
@@ -73,7 +73,7 @@ func TestAppsUninstall(t *testing.T) { | |
| "Fail to uninstall due to API error": { | ||
| ExpectedError: slackerror.New("something went wrong"), | ||
| Setup: func(t *testing.T, ctx context.Context, clientsMock *shared.ClientsMock, clients *shared.ClientFactory) { | ||
| prepareCommonUninstallMocks(clients, clientsMock) | ||
| prepareCommonUninstallMocks(ctx, clients, clientsMock) | ||
| clientsMock.ApiInterface.On("UninstallApp", mock.Anything, mock.Anything, fakeAppID, fakeAppTeamID). | ||
| Return(slackerror.New("something went wrong")).Once() | ||
| }, | ||
|
|
@@ -82,7 +82,7 @@ func TestAppsUninstall(t *testing.T) { | |
| CmdArgs: []string{}, | ||
| ExpectedError: slackerror.New(slackerror.ErrCredentialsNotFound), | ||
| Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { | ||
| prepareCommonUninstallMocks(cf, cm) | ||
| prepareCommonUninstallMocks(ctx, cf, cm) | ||
| appSelectMock := prompts.NewAppSelectMock() | ||
| uninstallAppSelectPromptFunc = appSelectMock.AppSelectPrompt | ||
| appSelectMock.On("AppSelectPrompt").Return(prompts.SelectedApp{App: fakeApp}, nil) | ||
|
|
@@ -95,7 +95,7 @@ func TestAppsUninstall(t *testing.T) { | |
| }) | ||
| } | ||
|
|
||
| func prepareCommonUninstallMocks(clients *shared.ClientFactory, clientsMock *shared.ClientsMock) *shared.ClientFactory { | ||
| func prepareCommonUninstallMocks(ctx context.Context, clients *shared.ClientFactory, clientsMock *shared.ClientsMock) *shared.ClientFactory { | ||
|
|
||
| // Mock App Selection | ||
| appSelectMock := prompts.NewAppSelectMock() | ||
|
|
@@ -124,7 +124,7 @@ func prepareCommonUninstallMocks(clients *shared.ClientFactory, clientsMock *sha | |
|
|
||
| clients.AppClient().AppClientInterface = appClientMock | ||
|
|
||
| err := clients.AppClient().SaveDeployed(context.Background(), fakeApp) | ||
| err := clients.AppClient().SaveDeployed(ctx, fakeApp) | ||
| if err != nil { | ||
| panic("error setting up test; cant write apps.json") | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,6 @@ | |
| package doctor | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "runtime" | ||
| "testing" | ||
|
|
@@ -26,6 +25,7 @@ import ( | |
| "github.com/slackapi/slack-cli/internal/pkg/version" | ||
| "github.com/slackapi/slack-cli/internal/shared" | ||
| "github.com/slackapi/slack-cli/internal/shared/types" | ||
| "github.com/slackapi/slack-cli/internal/slackcontext" | ||
| "github.com/slackapi/slack-cli/internal/slackerror" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/mock" | ||
|
|
@@ -34,10 +34,10 @@ import ( | |
|
|
||
| func TestDoctorCheckOS(t *testing.T) { | ||
| t.Run("returns the operating system version", func(t *testing.T) { | ||
| ctx := slackcontext.MockContext(t.Context()) | ||
| clientsMock := shared.NewClientsMock() | ||
| clientsMock.AddDefaultMocks() | ||
| clients := shared.NewClientFactory(clientsMock.MockClientFactory()) | ||
| ctx := context.Background() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👏 Creating the |
||
| expected := Section{ | ||
| Label: "Operating System", | ||
| Value: "the computer conductor", | ||
|
|
@@ -56,10 +56,10 @@ func TestDoctorCheckOS(t *testing.T) { | |
|
|
||
| func TestDoctorCheckCLIVersion(t *testing.T) { | ||
| t.Run("returns the current version of this tool", func(t *testing.T) { | ||
| ctx := slackcontext.MockContext(t.Context()) | ||
| clientsMock := shared.NewClientsMock() | ||
| clientsMock.AddDefaultMocks() | ||
| clients := shared.NewClientFactory(clientsMock.MockClientFactory()) | ||
| ctx := context.Background() | ||
| expected := Section{ | ||
| Label: "CLI", | ||
| Value: "this tool for building Slack apps", | ||
|
|
@@ -141,13 +141,13 @@ func TestDoctorCheckProjectConfig(t *testing.T) { | |
|
|
||
| for name, tt := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| ctx := slackcontext.MockContext(t.Context()) | ||
| clientsMock := shared.NewClientsMock() | ||
| clientsMock.AddDefaultMocks() | ||
| pcm := &config.ProjectConfigMock{} | ||
| pcm.On("ReadProjectConfigFile", mock.Anything).Return(tt.projectConfig, nil) | ||
| clientsMock.Config.ProjectConfig = pcm | ||
| clients := shared.NewClientFactory(clientsMock.MockClientFactory()) | ||
| ctx := context.Background() | ||
| expected := Section{ | ||
| Label: "Configurations", | ||
| Value: "your project's CLI settings", | ||
|
|
@@ -218,10 +218,10 @@ func TestDoctorCheckProjectDeps(t *testing.T) { | |
|
|
||
| for name, tt := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| ctx := slackcontext.MockContext(t.Context()) | ||
| clientsMock := shared.NewClientsMock() | ||
| clientsMock.AddDefaultMocks() | ||
| clients := tt.mockHookSetup(clientsMock) | ||
| ctx := context.Background() | ||
| expected := Section{ | ||
| Label: "Dependencies", | ||
| Value: "requisites for development", | ||
|
|
@@ -246,6 +246,7 @@ func TestDoctorCheckCLIConfig(t *testing.T) { | |
|
|
||
| for name, tt := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| ctx := slackcontext.MockContext(t.Context()) | ||
| clientsMock := shared.NewClientsMock() | ||
| clientsMock.AddDefaultMocks() | ||
| scm := &config.SystemConfigMock{} | ||
|
|
@@ -254,7 +255,6 @@ func TestDoctorCheckCLIConfig(t *testing.T) { | |
| }, nil) | ||
| clientsMock.Config.SystemConfig = scm | ||
| clients := shared.NewClientFactory(clientsMock.MockClientFactory()) | ||
| ctx := context.Background() | ||
| expected := Section{ | ||
| Label: "Configurations", | ||
| Value: "any adjustments to settings", | ||
|
|
@@ -297,10 +297,10 @@ func TestDoctorCheckCLICreds(t *testing.T) { | |
|
|
||
| for name := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| ctx := slackcontext.MockContext(t.Context()) | ||
| clientsMock := shared.NewClientsMock() | ||
| clientsMock.AddDefaultMocks() | ||
| clients := shared.NewClientFactory(clientsMock.MockClientFactory()) | ||
| ctx := context.Background() | ||
| expected := Section{ | ||
| Label: "Credentials", | ||
| Value: "your Slack authentication", | ||
|
|
@@ -391,10 +391,10 @@ func TestDoctorCheckProjectTooling(t *testing.T) { | |
|
|
||
| for name, tt := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| ctx := slackcontext.MockContext(t.Context()) | ||
| clientsMock := shared.NewClientsMock() | ||
| clientsMock.AddDefaultMocks() | ||
| clients := tt.mockHookSetup(clientsMock) | ||
| ctx := context.Background() | ||
| expected := Section{ | ||
| Label: "Runtime", | ||
| Value: "foundations for the application", | ||
|
|
@@ -410,7 +410,7 @@ func TestDoctorCheckProjectTooling(t *testing.T) { | |
|
|
||
| func TestDoctorCheckGit(t *testing.T) { | ||
| t.Run("returns the version of git", func(t *testing.T) { | ||
| ctx := context.Background() | ||
| ctx := slackcontext.MockContext(t.Context()) | ||
| gitVersion, err := deputil.GetGitVersion() | ||
| require.NoError(t, err) | ||
| expected := Section{ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ import ( | |
|
|
||
| "github.com/slackapi/slack-cli/internal/shared" | ||
| "github.com/slackapi/slack-cli/internal/shared/types" | ||
| "github.com/slackapi/slack-cli/internal/slackcontext" | ||
| "github.com/slackapi/slack-cli/internal/slackerror" | ||
| "github.com/slackapi/slack-cli/test/testutil" | ||
| "github.com/spf13/afero" | ||
|
|
@@ -326,6 +327,7 @@ func TestFunctionDistributionCommand_PermissionsFile(t *testing.T) { | |
|
|
||
| for name, tt := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| ctx := slackcontext.MockContext(t.Context()) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thought: It's generally best to declare the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Edit: I should've read on! I agree so much that this is a good change. |
||
| clientsMock := shared.NewClientsMock() | ||
| clientsMock.IO.AddDefaultMocks() | ||
| err := afero.WriteFile(clientsMock.Fs, tt.filename, []byte(tt.data), 0644) | ||
|
|
@@ -339,7 +341,6 @@ func TestFunctionDistributionCommand_PermissionsFile(t *testing.T) { | |
| clientsMock.ApiInterface.On("FunctionDistributionSet", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). | ||
| Return([]types.FunctionDistributionUser{}, nil) | ||
|
|
||
| ctx := context.Background() | ||
| clients := shared.NewClientFactory(clientsMock.MockClientFactory()) | ||
| app := types.App{AppID: "A123"} | ||
|
|
||
|
|
@@ -400,6 +401,7 @@ func TestFunctionDistributeCommand_UpdateNamedEntitiesDistribution(t *testing.T) | |
|
|
||
| for name, tt := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| ctx := slackcontext.MockContext(t.Context()) | ||
| clientsMock := shared.NewClientsMock() | ||
| clientsMock.ApiInterface.On("FunctionDistributionSet", mock.Anything, mock.Anything, mock.Anything, types.NAMED_ENTITIES, mock.Anything). | ||
| Return([]types.FunctionDistributionUser{}, nil). | ||
|
|
@@ -414,7 +416,6 @@ func TestFunctionDistributeCommand_UpdateNamedEntitiesDistribution(t *testing.T) | |
|
|
||
| app := types.App{AppID: "A123"} | ||
| function := "Ft123" | ||
| ctx := context.Background() | ||
| clients := shared.NewClientFactory(clientsMock.MockClientFactory()) | ||
|
|
||
| err := updateNamedEntitiesDistribution(ctx, clients, app, function, tt.updatedEntities) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: This is the standard, repetitive change that you'll see. We are swapping
context.Background()forslackcontext.MockContext(t.Context). 👯There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting up
ctxwith other mocks is an awesome change!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the
testingcontext is a neat update also! I haven't explore enough of that package, but I imagine more useful methods might be used elsewhere too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like using the
t.Context()as well. A few benefits are:context.Background()andcontext.TODO()to prevent those from slipping into production code.t.Context()is a cancel context and it's cancelled just before the test cleanup. So, any goroutines running and listening for<- ctx.Done()will have a chance to shutdown.