Move sandbox manager tests to use a test directory and to clean up after#25005
Move sandbox manager tests to use a test directory and to clean up after#25005
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly improves the hygiene and reliability of the sandbox manager integration tests. By introducing a temporary workspace that is automatically created and cleaned up, it ensures that test runs do not leave behind residual files, addressing a known issue of file leakage and maintaining a clean development environment. This change enhances test isolation and prevents unintended side effects on the file system. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request updates the SandboxManager integration tests to use a temporary directory for the workspace, improving test isolation and ensuring proper cleanup. A critical issue was identified in the test skipping logic: because the manager is initialized within a beforeAll block, it remains undefined during the test discovery phase when the skip condition is evaluated. This prevents the tests from being correctly skipped on unsupported platforms.
| let workspace: string; | ||
| let manager: SandboxManager; | ||
|
|
||
| beforeAll(() => { | ||
| workspace = fs.mkdtempSync(path.join(os.tmpdir(), 'sandbox-integration-')); | ||
| manager = createSandboxManager({ enabled: true }, { workspace }); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| if (workspace && fs.existsSync(workspace)) { | ||
| fs.rmSync(workspace, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| // Skip if we are on an unsupported platform or if it's a NoopSandboxManager | ||
| const shouldSkip = | ||
| const shouldSkip = () => | ||
| manager instanceof NoopSandboxManager || | ||
| manager instanceof LocalSandboxManager || | ||
| !ensureSandboxAvailable(); | ||
|
|
||
| describe.skipIf(shouldSkip)('Cross-platform Sandbox Behavior', () => { | ||
| describe.skipIf(shouldSkip())('Cross-platform Sandbox Behavior', () => { |
There was a problem hiding this comment.
The current implementation of shouldSkip as a function called during test suite discovery (line 167) will always see manager as undefined because beforeAll has not yet executed. This causes the instanceof checks to evaluate to false regardless of the actual manager type, meaning the test suite will not be skipped even when using a NoopSandboxManager or LocalSandboxManager. This can lead to test failures in environments where sandboxing is not supported or disabled.
Since createSandboxManager and fs.mkdtempSync are synchronous and necessary for determining whether to skip the suite, they should be initialized at the describe block level. This ensures the skip condition is correctly evaluated while still using a temporary directory for the tests.
const workspace = fs.mkdtempSync(path.join(os.tmpdir(), 'sandbox-integration-'));
const manager: SandboxManager = createSandboxManager({ enabled: true }, { workspace });
afterAll(() => {
if (workspace && fs.existsSync(workspace)) {
fs.rmSync(workspace, { recursive: true, force: true });
}
});
// Skip if we are on an unsupported platform or if it's a NoopSandboxManager
const shouldSkip =
manager instanceof NoopSandboxManager ||
manager instanceof LocalSandboxManager ||
!ensureSandboxAvailable();
describe.skipIf(shouldSkip)('Cross-platform Sandbox Behavior', () => {References
- For test environment setup, it is acceptable to use synchronous operations (e.g., fs.*Sync, execSync) within an async function. This approach is preferred for its simplicity and readability when the setup is sequential and not a performance bottleneck.
- When creating temporary files or directories in global temporary directories (e.g., /tmp), use fs.mkdtempSync() to generate securely named, uniquely named temporary directories. This mitigates symlink attacks where an attacker could pre-create a symlink with a predictable name to truncate arbitrary files.
Summary
Modifies the sandbox manager tests to use a temporary test directory and cleanup afterwards.
Details
This fixes the issue where the sandbox manager integration tests are orphaning files packages/core/.gitignore and packages/core/.geminiignore during a test run.
Related Issues
Fixes #25003
How to Validate
npm run testwithout change will result in these files.npm run testwith this change will no longer drop these files.Pre-Merge Checklist