-
Notifications
You must be signed in to change notification settings - Fork 24
test: mock context in TableTestCommand.ExpectedAsserts #24
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
test: mock context in TableTestCommand.ExpectedAsserts #24
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #24 +/- ##
==========================================
- Coverage 62.92% 62.91% -0.01%
==========================================
Files 210 210
Lines 22127 22127
==========================================
- Hits 13924 13922 -2
- Misses 7115 7117 +2
Partials 1088 1088 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
mwbrooks
left a comment
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.
Notes for the patient reviewers!
| cm.Config.ProjectConfig = mockProjectConfig | ||
| }, | ||
| ExpectedAsserts: func(t *testing.T, cm *shared.ClientsMock) { | ||
| ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
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: Every reference to ExpectedAsserts: func(...) had to be updated to include ctx. 🔁
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.
Another tedious change - thanks for rolling up the sleeves - but this matches actual command execution so much better 🙏 ✨
| } | ||
| actualApp, err := cm.AppClient.GetDeployed( | ||
| context.Background(), | ||
| ctx, |
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: The main motivation of this PR is to replace context.Background() references with the mocked context ctx. Here's one example.
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.
Replacing these instances with the shared mock ctx IMO is a huge win for all of our tests 😭
| // Set experiment flag | ||
| clientsMock.Config.ExperimentsFlag = append(clientsMock.Config.ExperimentsFlag, "read-only-collaborators") | ||
| clientsMock.Config.LoadExperiments(context.Background(), clientsMock.IO.PrintDebug) | ||
| clientsMock.Config.LoadExperiments(ctx, clientsMock.IO.PrintDebug) |
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: Here's another example where we're replacing a context.Background() with the new, mocked context ctx
| // TODO this can probably be replaced by a helper that sets up an apps.json file in | ||
| // the right place on the afero memfs instance | ||
| err := clients.AppClient().SaveDeployed(context.Background(), fakeApp) | ||
| err := clients.AppClient().SaveDeployed(ctx, fakeApp) |
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: Moar switch-a-roos
| ctx := context.Background() | ||
| ctx = context.WithValue(ctx, config.CONTEXT_TOKEN, "sometoken") | ||
| // Create mocks | ||
| ctxMock := slackcontext.MockContext(context.Background()) | ||
| ctxMock = context.WithValue(ctxMock, config.CONTEXT_TOKEN, "sometoken") |
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: One of the few unique changes, where the activity test modifies a value in the context.
| // Setup custom per-test mocks (higher priority than default mocks) | ||
| if tt.Setup != nil { | ||
| ctx = tt.Setup(t, ctx, clientsMock) | ||
| ctxMock = tt.Setup(t, ctxMock, clientsMock) |
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.
thought: I noticed that this activity test appears to have copy & pasted the TableTestCommand implementation, then modified it to work for an internal/pkg/ test. Probably something to revisit in the future because it could be simplified to match our other non-command table tests.
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.
🎁 Super intersting find @mwbrooks! I agree we should revisit these tests at some point since it's also surprising to find context changes here.
Untangling that might mean leaning more into passing app and auth as arguments but I don't recall the underhappenings of this package right now! It's no blocker to the changes otherwise either!
| // Assert mocks or other custom assertions | ||
| if tt.ExpectedAsserts != nil { | ||
| tt.ExpectedAsserts(t, clientsMock) | ||
| tt.ExpectedAsserts(t, ctxMock, clientsMock) |
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: Here's the nuts-and-bolts of this PR. We just pass the mock context into ExpectedAsserts so that the same context can be used everywhere. 🌟
zimeg
left a comment
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.
@mwbrooks Thank you so much for calling out the gems of this PR! It's all great but this made reviewing much easier for me 📝 ✨
I'm somewhat upset to find a +0.03% improvement to test coverage but that makes sense. I was hoping for more because these changes are so meaningful to better matching commands at runtime.
These are exciting improvements to the health of the code that I'm excited to merge and include in future tests - thanks again for making these changes 🤖
| cm.Config.ProjectConfig = mockProjectConfig | ||
| }, | ||
| ExpectedAsserts: func(t *testing.T, cm *shared.ClientsMock) { | ||
| ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
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.
Another tedious change - thanks for rolling up the sleeves - but this matches actual command execution so much better 🙏 ✨
| } | ||
| actualApp, err := cm.AppClient.GetDeployed( | ||
| context.Background(), | ||
| ctx, |
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.
Replacing these instances with the shared mock ctx IMO is a huge win for all of our tests 😭
| // Setup custom per-test mocks (higher priority than default mocks) | ||
| if tt.Setup != nil { | ||
| ctx = tt.Setup(t, ctx, clientsMock) | ||
| ctxMock = tt.Setup(t, ctxMock, clientsMock) |
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.
🎁 Super intersting find @mwbrooks! I agree we should revisit these tests at some point since it's also surprising to find context changes here.
Untangling that might mean leaning more into passing app and auth as arguments but I don't recall the underhappenings of this package right now! It's no blocker to the changes otherwise either!
Thanks for the review @zimeg and I'm glad to hear that the notes made reviewing smoother! I agree, it's tedious work with little upside. I think the main benefits are prevent errors down the road, when the values in context matter. Also, doing a full sweep now helps set better copy & paste standards going forward. 🫠 |
Summary
This pull request updates
test/testutil/commandtests.goto pass the mocked context intoExpectedAsserts.It's a lot of repetitive lines changes with the occasional 💎 gem hidden. Don't worry though, I'll leave GitHub comments to mark the non-repetitive areas! 🚩
The motivation is to use the mocked context throughout the table test's lifecycle. Right now, some
ExpectedAsserts: func(...)implementations require a context and usecontext.Background(). This works today, but it would fail a function required a value fromctx.Requirements