Skip to content

Conversation

@mwbrooks
Copy link
Member

@mwbrooks mwbrooks commented Apr 9, 2025

Summary

This pull request updates test/testutil/commandtests.go to pass the mocked context into ExpectedAsserts.

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 use context.Background(). This works today, but it would fail a function required a value from ctx.

Requirements

@mwbrooks mwbrooks added code health M-T: Test improvements and anything that improves code health semver:patch Use on pull requests to describe the release version increment labels Apr 9, 2025
@mwbrooks mwbrooks added this to the Next Release milestone Apr 9, 2025
@mwbrooks mwbrooks self-assigned this Apr 9, 2025
@codecov
Copy link

codecov bot commented Apr 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 62.91%. Comparing base (bd884b6) to head (d0aa8ef).
Report is 1 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member Author

@mwbrooks mwbrooks left a 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) {
Copy link
Member Author

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. 🔁

Copy link
Member

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,
Copy link
Member Author

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.

Copy link
Member

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)
Copy link
Member Author

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)
Copy link
Member Author

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

Comment on lines -157 to +160
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")
Copy link
Member Author

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)
Copy link
Member Author

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.

Copy link
Member

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)
Copy link
Member Author

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. 🌟

@mwbrooks mwbrooks marked this pull request as ready for review April 9, 2025 22:53
@mwbrooks mwbrooks requested a review from a team April 9, 2025 22:56
Copy link
Member

@zimeg zimeg left a 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) {
Copy link
Member

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,
Copy link
Member

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)
Copy link
Member

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!

@mwbrooks
Copy link
Member Author

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.

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. 🫠

@mwbrooks mwbrooks merged commit f2a26f0 into main Apr 10, 2025
6 checks passed
@mwbrooks mwbrooks deleted the mbrooks-slackcontext-tabletestcommand-expectedasserts branch April 10, 2025 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code health M-T: Test improvements and anything that improves code health semver:patch Use on pull requests to describe the release version increment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants