Repository navigation
test: add unit tests for WatchManifest in internal/pkg/platform #831
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bc8ec89
test: add unit tests for WatchManifest in internal/pkg/platform
mwbrooks c7a650c
test: check WatchManifest blocks until the context ends
mwbrooks 9dd1b5d
Merge branch 'main' into mwbrooks-test-watch-manifest
mwbrooks 644185f
Merge branch 'main' into mwbrooks-test-watch-manifest
mwbrooks 51c0b4d
Merge remote-tracking branch 'origin/main' into mwbrooks-test-watch-m…
mwbrooks 731c506
test: set watch options per case now that a missing watch config show…
mwbrooks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,10 +24,12 @@ import ( | |
| "strings" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/gorilla/websocket" | ||
| "github.com/radovskyb/watcher" | ||
| "github.com/slackapi/slack-cli/internal/api" | ||
| "github.com/slackapi/slack-cli/internal/config" | ||
| "github.com/slackapi/slack-cli/internal/hooks" | ||
| "github.com/slackapi/slack-cli/internal/shared" | ||
| "github.com/slackapi/slack-cli/internal/shared/types" | ||
|
|
@@ -508,6 +510,79 @@ func Test_LocalServer_watchAppEvents(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func Test_LocalServer_WatchManifest(t *testing.T) { | ||
| for name, tc := range map[string]struct { | ||
| manifestSource config.ManifestSource | ||
| manifestErr error | ||
| watch *hooks.WatchOpts | ||
| expectedErr error | ||
| expectedOutput []string | ||
| }{ | ||
| "asks for watch configuration when the hooks omit it": { | ||
| manifestSource: config.ManifestSourceLocal, | ||
| expectedOutput: []string{"provide watch configuration"}, | ||
| }, | ||
| "returns the error when the manifest source cannot be read": { | ||
| manifestSource: config.ManifestSourceLocal, | ||
| watch: &hooks.WatchOpts{}, | ||
| manifestErr: slackerror.New(slackerror.ErrInvalidManifestSource), | ||
| expectedErr: slackerror.New(slackerror.ErrInvalidManifestSource), | ||
| }, | ||
| "waits for the context to end when manifest watching is not enabled": { | ||
| manifestSource: config.ManifestSourceLocal, | ||
| watch: &hooks.WatchOpts{}, | ||
| expectedOutput: []string{"Manifest watching is not enabled"}, | ||
| }, | ||
| "notes a remote manifest source before checking the watch config": { | ||
| manifestSource: config.ManifestSourceRemote, | ||
| watch: &hooks.WatchOpts{}, | ||
| expectedOutput: []string{"Manifest source is remote", "Manifest watching is not enabled"}, | ||
| }, | ||
| } { | ||
| t.Run(name, func(t *testing.T) { | ||
| ctx, cancel := context.WithCancel(slackcontext.MockContext(t.Context())) | ||
| defer cancel() | ||
| clientsMock := shared.NewClientsMock() | ||
| clientsMock.Config.DebugEnabled = true | ||
| projectConfigMock := config.NewProjectConfigMock() | ||
| projectConfigMock.On("GetManifestSource", mock.Anything).Return(tc.manifestSource, tc.manifestErr) | ||
| clientsMock.Config.ProjectConfig = projectConfigMock | ||
| clientsMock.AddDefaultMocks() | ||
| clients := shared.NewClientFactory(clientsMock.MockClientFactory()) | ||
| server := LocalServer{clients: clients} | ||
| server.cliConfig.Config.Watch = tc.watch | ||
| errChan := make(chan error, 1) | ||
| go func() { | ||
| errChan <- server.WatchManifest(ctx, types.SlackAuth{}, types.App{}) | ||
| }() | ||
| if tc.expectedErr != nil { | ||
| select { | ||
| case err := <-errChan: | ||
| assert.Equal(t, tc.expectedErr, err) | ||
| case <-time.After(time.Second): | ||
| require.FailNow(t, "WatchManifest did not return the error") | ||
| } | ||
| return | ||
| } | ||
| select { | ||
| case err := <-errChan: | ||
| require.FailNow(t, "WatchManifest returned before the context ended", "err: %v", err) | ||
|
Contributor
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. 💯 praise: Asserting no return before |
||
| case <-time.After(50 * time.Millisecond): | ||
| } | ||
| cancel() | ||
| select { | ||
| case err := <-errChan: | ||
| require.NoError(t, err) | ||
| case <-time.After(time.Second): | ||
| require.FailNow(t, "WatchManifest did not return after the context ended") | ||
| } | ||
| for _, expected := range tc.expectedOutput { | ||
| assert.Contains(t, clientsMock.GetStdoutOutput(), expected) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_LocalServer_watchManifestEvents(t *testing.T) { | ||
| for name, tc := range map[string]struct { | ||
| cancel bool | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🧪 suggestion (non-blocking): The enabled path (paths, filter regex,
w.Start) is still untested, leavingWatchManifestat 46% coverage. A test there would hang today since nothing closes the watcher when the context ends, so closingwonctx.Done()could be a nice follow-up that makes it testable.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.
Great idea, thanks! I'll close the watcher on
ctx.Done()in a follow-up PR, with a test for the enabled path, so this one stays test-only.