chore(wait): replace manual StrategyTarget mocks with generated mockery mocks#3603
chore(wait): replace manual StrategyTarget mocks with generated mockery mocks#3603mateenali66 wants to merge 4 commits intotestcontainers:mainfrom
Conversation
…y mocks Signed-off-by: Mateen Anjum <mateenali66@gmail.com>
✅ Deploy Preview for testcontainers-go ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
WalkthroughTests in the wait package were refactored to remove local hand-rolled stubs and use a centralized Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
wait/exec_test.go (1)
69-76: Consider usingRunAndReturn()for clearer intent with dynamic return values.The current
Return()pattern with a function argument works correctly because mockery-generated mocks have special handling that checks if the return value is a function matching the method signature and executes it. However,RunAndReturn()is more explicit and idiomatic for this use case:target.On("Exec", mock.Anything, mock.Anything, mock.Anything).RunAndReturn( func(ctx context.Context, cmd []string, opts ...tcexec.ProcessOption) (int, io.Reader, error) { if time.Now().After(successAfter) { return 0, nil, nil } return 10, nil, nil }, )🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@wait/exec_test.go` around lines 69 - 76, Replace the current mock setup that uses Return with a clearer RunAndReturn call: on the mock `target` where you call On("Exec", mock.Anything, mock.Anything, mock.Anything) switch from .Return(func(ctx context.Context, cmd []string, opts ...tcexec.ProcessOption) (int, io.Reader, error) { ... }) to .RunAndReturn(func(ctx context.Context, cmd []string, opts ...tcexec.ProcessOption) (int, io.Reader, error) { ... }); keep the same function signature and the dynamic successAfter logic so the mock executes that function at call time instead of relying on Return's special-case behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@wait/exec_test.go`:
- Around line 88-96: The mock setup for target.On("Exec", ...) incorrectly uses
Return() with a function literal so the function isn't executed for each call;
change this to use RunAndReturn() on the mock so the provided func(ctx
context.Context, cmd []string, opts ...tcexec.ProcessOption) (int, io.Reader,
error) is invoked at call time, preserve the logic that sleeps, checks ctx.Err()
and returns accordingly, and keep the same argument matchers (mock.Anything,
mock.Anything, mock.Anything) and return-value types to match the Exec method
signature.
In `@wait/health_test.go`:
- Around line 52-60: The mock currently uses Return(...) with a function literal
which will be returned as the value instead of being invoked; replace the
Return(...) call on the target mock for the "State" method with
RunAndReturn(...) so the provided function is executed each call. Specifically,
change target.On("State", mock.Anything).Return(func(_ context.Context)
(*container.State, error) { ... }) to target.On("State",
mock.Anything).RunAndReturn(func(ctx context.Context) (*container.State, error)
{ mtx.Lock(); defer mtx.Unlock(); s := *state; return &s, nil }) (i.e., provide
a function matching the State signature so the mock invokes it dynamically and
still returns a copy to avoid races).
---
Nitpick comments:
In `@wait/exec_test.go`:
- Around line 69-76: Replace the current mock setup that uses Return with a
clearer RunAndReturn call: on the mock `target` where you call On("Exec",
mock.Anything, mock.Anything, mock.Anything) switch from .Return(func(ctx
context.Context, cmd []string, opts ...tcexec.ProcessOption) (int, io.Reader,
error) { ... }) to .RunAndReturn(func(ctx context.Context, cmd []string, opts
...tcexec.ProcessOption) (int, io.Reader, error) { ... }); keep the same
function signature and the dynamic successAfter logic so the mock executes that
function at call time instead of relying on Return's special-case behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 3d29c677-cdf8-4ba1-9558-0d9d20e64ca1
📒 Files selected for processing (3)
wait/exec_test.gowait/exit_test.gowait/health_test.go
Return() with a function literal does not execute the function; it returns the function object itself as the mock value. Use RunAndReturn() so the provided function is invoked on each call. Signed-off-by: Mateen Anjum <mateenali66@gmail.com>
Replace Return(func...) with RunAndReturn(func...) in TestExecStrategyWaitUntilReady_MultipleChecks. With Return, the function is returned as the value rather than being invoked on each call. RunAndReturn ensures the mock executes the function dynamically, which is the correct pattern for time-dependent behavior. Signed-off-by: Mateen Anjum <mateenali66@gmail.com>
Part of #2765. Replaces the manual StrategyTarget mock structs in exec_test.go, exit_test.go, and health_test.go with the generated mockStrategyTarget from strategytarget_mock_test.go. Tests moved to package wait_test for consistency with the generated mock.