Skip to content

chore(wait): replace manual StrategyTarget mocks with generated mockery mocks#3603

Open
mateenali66 wants to merge 4 commits intotestcontainers:mainfrom
mateenali66:feat/issue-2765-generated-mocks-wait
Open

chore(wait): replace manual StrategyTarget mocks with generated mockery mocks#3603
mateenali66 wants to merge 4 commits intotestcontainers:mainfrom
mateenali66:feat/issue-2765-generated-mocks-wait

Conversation

@mateenali66
Copy link
Copy Markdown

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.

…y mocks

Signed-off-by: Mateen Anjum <mateenali66@gmail.com>
@mateenali66 mateenali66 requested a review from a team as a code owner March 24, 2026 20:12
@netlify
Copy link
Copy Markdown

netlify bot commented Mar 24, 2026

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit 728129a
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-go/deploys/69ca2312d73fdf0008979a75
😎 Deploy Preview https://deploy-preview-3603--testcontainers-go.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 24, 2026

Walkthrough

Tests in the wait package were refactored to remove local hand-rolled stubs and use a centralized newMockStrategyTarget(t) based on testify/mock; packages changed to wait_test, constructor calls updated to wait.*, and mock behavior is configured via On/RunAndReturn expectations.

Changes

Cohort / File(s) Summary
Wait strategy exec tests
wait/exec_test.go
Removed mockExecTarget stub; replaced with newMockStrategyTarget(t) from testify/mock; moved exec behavior into target.On("Exec", ...).Return(...) and .RunAndReturn(...); removed unused Docker/container-related test helpers/imports.
Wait strategy exit tests
wait/exit_test.go
Switched package waitpackage wait_test; removed local exitStrategyTarget stub; introduced newMockStrategyTarget(t) and mocked State responses; updated constructor calls to wait.NewExitStrategy() and adjusted imports to testify/mock.
Wait strategy health tests
wait/health_test.go
Switched to external test package; removed healthStrategyTarget stub and replaced with newMockStrategyTarget(t); programmed State returns (including dynamic RunAndReturn for delayed state changes); updated calls to wait.NewHealthStrategy() and cleaned imports.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

chore

Poem

🐇 I hopped through tests, so neat and small,

Stubs tucked away, mocks answer the call.
Expectations set with careful art,
One tidy target, a cleaner start. 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description check ✅ Passed The description is directly related to the changeset, explaining the replacement of manual mocks with generated ones and the test package reorganization.
Title check ✅ Passed The title describes the main change—replacing manual StrategyTarget mocks with generated mockery mocks—which aligns with the actual modifications across all three test files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
wait/exec_test.go (1)

69-76: Consider using RunAndReturn() 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

📥 Commits

Reviewing files that changed from the base of the PR and between ea9748b and 99389dc.

📒 Files selected for processing (3)
  • wait/exec_test.go
  • wait/exit_test.go
  • wait/health_test.go

mateenali66 and others added 3 commits March 24, 2026 16:56
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>
@mateenali66 mateenali66 changed the title test(wait): replace manual StrategyTarget mocks with generated mockery mocks chore(wait): replace manual StrategyTarget mocks with generated mockery mocks Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant