test(mcp): restore os.homedir spy after each test to prevent cross-file leak#1043
test(mcp): restore os.homedir spy after each test to prevent cross-file leak#1043sahrizvi wants to merge 1 commit into
Conversation
…le leak The `beforeEach` in `test/mcp/lifecycle.test.ts` installs a `spyOn(os, "homedir")` that returns a fresh `mkdtempSync(... "mcp-lifecycle-home-...")` on every call. Without a matching restore, the spy persists past this file into subsequent files in the same bun worker process. Downstream tests that call `os.homedir()` then get a different random path on every invocation — most visibly the tilde and `$HOME` expansion assertions in `test/permission/next.test.ts`, which see two different `mkdtempSync` paths within the same `expect().toEqual()` call and fail 6 times. The failure was file-order dependent (passes in isolation, fails when the full suite runs in the wrong order), which manifested as a required `CI/TypeScript` check failing intermittently on PRs — including v0.9.3. Fix: add `afterEach(() => mock.restore())` in `lifecycle.test.ts` so the spy is torn down between tests and cannot leak past the file boundary. Blanket `mock.restore()` matches the pattern already used elsewhere in the suite and cleans up any other spies installed during a test. Closes #1042
There was a problem hiding this comment.
Claude Code Review
This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.
Tip: disable this comment in your organization's Code Review settings.
|
👋 This PR was automatically closed by our quality checks. Common reasons:
If you believe this was a mistake, please open an issue explaining your intended contribution and a maintainer will help you. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe MCP lifecycle test imports ChangesMCP test isolation
Estimated code review effort: 1 (Trivial) | ~2 minutes Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration. 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 |
|
👋 This PR was automatically closed by our quality checks. Common reasons:
If you believe this was a mistake, please open an issue explaining your intended contribution and a maintainer will help you. |
1 similar comment
|
👋 This PR was automatically closed by our quality checks. Common reasons:
If you believe this was a mistake, please open an issue explaining your intended contribution and a maintainer will help you. |
Code Review SummaryStatus: No Issues Found | Recommendation: Merge The change adds Files Reviewed (1 file)
Reviewed by glm-5.2 · Input: 50.4K · Output: 6.8K · Cached: 323.3K Review guidance: REVIEW.md from base branch |
🤖 Code Review — OpenCodeReview (Gemini) — No Issues FoundNo supported files changed. |
Issue for this PR
Closes #1042
Type of change
What does this PR do?
The
beforeEachinpackages/opencode/test/mcp/lifecycle.test.ts:250installsspyOn(os, "homedir").mockImplementation(...)and returns a freshmkdtempSync(... "mcp-lifecycle-home-...")on every call. There is no matchingafterEachthat restores the spy. When bun runs the full test suite, the spy persists past this file into any downstream file in the same worker process. The next test that callsos.homedir()gets a different random path per invocation — inside a singleexpect().toEqual(...)call, the value returned to the "actual" side and the value used to build the "expected" side can differ, and the assertion fails on paths that visibly contain themcp-lifecycle-home-prefix from this file.The most visible casualty is
test/permission/next.test.ts, where 6 tilde /$HOME-expansion tests fail intermittently on CI:The failure is file-order dependent — it passes in isolation and only surfaces when the full suite runs in the leaking order.
Fix: import
afterEachfrombun:testand addafterEach(() => mock.restore())right after thebeforeEachblock inlifecycle.test.ts. Blanketmock.restore()tears down theos.homedirspy (and any other spy installed during a test) between tests, so the mock cannot escape the file boundary. This matches the pattern already used elsewhere in the suite and keeps the fix a single import addition plus a 3-line hook.How did you verify your code works?
bun test test/mcp/lifecycle.test.ts— 31 pass, 0 fail (fix's own file still green)bun test test/permission/next.test.ts— 80 pass, 0 fail (previously affected file green in isolation, same as before the fix)bun test test/mcp/lifecycle.test.ts test/permission/next.test.ts— 111 pass, 0 fail (both files together in one bun invocation — the shared-worker scenario that reproduced the leak)bun run typecheck— clean (exit 0)bun run script/upstream/analyze.ts --markers --base origin/main --strict— cleanbun run script/upstream/analyze.ts --branding— 0 leaksScreenshots / recordings
n/a — no UI change.
Checklist
Summary by cubic
Fix flaky tests by restoring the os.homedir spy after each test in packages/opencode/test/mcp/lifecycle.test.ts. Adds afterEach(() => mock.restore()) from
bun:testto stop cross-file leaks that broke tilde/$HOME expansion checks in test/permission/next.test.ts.Written for commit fec5f66. Summary will update on new commits.
Summary by CodeRabbit