8000 [worker:coverage] test(sandbox2): add tests for Sandbox::new by gominimal-aw-bot[bot] · Pull Request #250 · gominimal/minimal · GitHub
[go: up one dir, main page]

Skip to content

[worker:coverage] test(sandbox2): add tests for Sandbox::new - #250

Closed
gominimal-aw-bot[bot] wants to merge 2 commits into
mainfrom
chore/sandbox2-new-coverage-0d344bb00323cefd
Closed

gominimal-aw-bot[bot] wants to merge 2 commits into
mainfrom
chore/sandbox2-new-coverage-0d344bb00323cefd

Conversation

@gominimal-aw-bot
@gominimal-aw-bot gominimal-aw-bot Bot commented May 28, 2026 •
Copy link
Copy Markdown
Contributor

Exercise three previously uncovered entry paths in Sandbox::new (issue #243, regions=214, whole-function coverage = 0).

What was changed

Added three #[test] cases to the existing tests module in crates/sandbox2/src/lib.rs:

Test Path covered
sandbox_new_isolated_empty_creates_dirs WdSetup::Isolated with no rootfs/inputs — creates rootfs/, build/, state/ dirs
sandbox_new_bound_dir_creates_rootfs WdSetup::BoundDir — shadow cwd tree created under rootfs/
sandbox_new_cs_build_env_inert_when_mirror_absent MINIMAL_INTERNAL_CS_BUILD=1 with no cs-mirror path — succeeds (inert branch)

A make_sandbox_base() helper creates synth/ and synth/usr/ so that hardlink_dir_contents(synth to rootfs) populates rootfs/usr/ before Sandbox::new tries to place the usr/lib64 to lib symlink. Each test uses tempfile::TempDir for automatic cleanup.

Verification: cargo test -p sandbox2 (5 tests: 5 ok), cargo fmt --all --check, and cargo clippy -p sandbox2 --all-targets all pass.

Closes #243

Generated by Chore-issue worker agent (side-repo-ops operator) · ● 70.6M · ◷

Summary by CodeRabbit

  • Tests
    • Improved test coverage for sandbox initialization: verifies correct sandbox directory layout creation, handling of isolated and bound-directory configurations, and preservation of current working directory semantics inside the sandbox.
    • Added tests to ensure behavior remains stable when a minimal internal build mode is toggled via environment variables.

Note: These are internal testing improvements with no user-visible changes.

Review Change Stack

Exercise three previously uncovered entry paths in Sandbox::new
(issue #243, regions=214, whole-function coverage = 0).

Three #[test] cases added to the existing tests module in lib.rs:

| Test | Path covered |
|---|---|
| sandbox_new_isolated_empty_creates_dirs | WdSetup::Isolated with no rootfs/inputs → creates rootfs, build, state dirs |
| sandbox_new_bound_dir_creates_rootfs | WdSetup::BoundDir → shadow cwd tree created in rootfs |
| sandbox_new_cs_build_env_inert_when_mirror_absent | MINIMAL_INTERNAL_CS_BUILD=1 but cs-mirror absent → succeeds (inert branch) |

Each test uses tempfile::TempDir for cleanup. The make_sandbox_base()
helper creates synth/ and synth/usr/ so that hardlink_dir_contents
populates rootfs/usr/ before Sandbox::new tries to create the
usr/lib64 → lib symlink.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@coderabbitai
coderabbitai Bot commented May 28, 2026 •
Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f77f294d-2be4-41cb-a762-b06269c1dfc7

📥 Commits

Reviewing files that changed from the base of the PR and between a098595 and 4390097.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (2)
  • crates/sandbox2/Cargo.toml
  • crates/sandbox2/src/lib.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/sandbox2/src/lib.rs

📝 Walkthrough

Walkthrough

Adds test-only dev-dependency and three unit tests plus a test helper to exercise Sandbox::new: verifies filesystem layout for Isolated and BoundDir configs and that MINIMAL_INTERNAL_CS_BUILD=1 is inert when the convention mirror path is absent.

Changes

Sandbox::new unit tests

Layer / File(s) Summary
Test helper and dev deps
crates/sandbox2/Cargo.toml, crates/sandbox2/src/lib.rs
Adds serial_test as a dev-dependency, imports serial_test::serial, and adds make_sandbox_base() which creates temporary synth/ and synth/usr/ needed by tests.
Isolated config test
crates/sandbox2/src/lib.rs
Adds a unit test that calls Sandbox::new with WdSetup::Isolated { working_inputs: vec![] } and asserts rootfs/, build/, and /state (including state/home) exist.
BoundDir config test
crates/sandbox2/src/lib.rs
Adds a unit test that calls Sandbox::new with WdSetup::BoundDir and asserts the host cwd shadow tree is created inside the sandbox rootfs.
MINIMAL_INTERNAL_CS_BUILD env-var test
crates/sandbox2/src/lib.rs
Adds a serialized test that sets MINIMAL_INTERNAL_CS_BUILD=1, verifies Sandbox::new still succeeds when the convention mirror path is absent, and restores the env var via a drop guard.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • gominimal/minimal#234: Modifies sandbox cs-mirror hardlinking behavior that the new tests exercise.
  • gominimal/minimal#228: Related changes around MINIMAL_INTERNAL_CS_BUILD handling exercised by the new tests.
  • gominimal/minimal#188: Alters Sandbox::new initialization and directory layout that these tests validate.

Suggested reviewers

  • msample
  • twitchyliquid64
  • mitodrummer

Poem

🐰 I dug a tiny test-filled den,
To prove Sandbox::new behaves again,
With synth made neat,
And envs kept sweet,
Now roots and shadows pass the pen.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically summarizes the main change: adding test coverage for Sandbox::new with appropriate test categorization.
Linked Issues check ✅ Passed The PR successfully implements all coding requirements from issue #243: three test cases for Sandbox::new (Isolated, BoundDir, MINIMAL_INTERNAL_CS_BUILD), tempfile::TempDir usage, serial_test for test safety, and environment variable restoration.
Out of Scope Changes check ✅ Passed All changes are directly aligned with issue #243 requirements: test cases for Sandbox::new, helper function for test setup, and serial_test dependency for safe env-var mutation.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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

@coderabbitai coderabbitai Bot left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/sandbox2/src/lib.rs`:
- Line 1016: Replace the call to unwrap() on cwd.path().strip_prefix("/") with
expect(...) to document the asserted invariant; change the expression that
assigns rel (the let rel = cwd.path().strip_prefix("/").unwrap() line) to use
expect with a short message like "cwd.path() is absolute and must start with
'/'" so the reason the strip_prefix cannot fail is recorded at the call site.
- Around line 1030-1034: The test currently unsafely sets
MINIMAL_INTERNAL_CS_BUILD with an incorrect SAFETY comment and risks race
conditions; change the test to run serially (e.g., add #[serial] or acquire a
crate-level global Mutex) around the section that mutates the env var, remove
the unnecessary unsafe on std::env::set_var/remove_var, and wrap the mutation in
an RAII guard type that saves the previous env state and restores it in Drop so
that MINIMAL_INTERNAL_CS_BUILD is always restored even if Sandbox::new panics;
locate the code around the calls to std::env::set_var / std::env::remove_var and
the Sandbox::new(...) invocation and apply these changes.
🪄 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: 9f0b6119-e3e3-4229-9d60-89037be4c663

📥 Commits

Reviewing files that changed from the base of the PR and between 86b3505 and a098595.

📒 Files selected for processing (1)
  • crates/sandbox2/src/lib.rs

Comment thread crates/sandbox2/src/lib.rs Resolved
Comment thread crates/sandbox2/src/lib.rs Outdated
Comment on lines +1030 to +1034
// SAFETY: single-threaded test; no other thread reads this variable concurrently.
unsafe { std::env::set_var("MINIMAL_INTERNAL_CS_BUILD", "1") };
let result = Sandbox::new(base.path().to_path_buf(), config, ());
// Always restore the variable, even on failure.
unsafe { std::env::remove_var("MINIMAL_INTERNAL_CS_BUILD") };
@coderabbitai coderabbitai Bot May 28, 2026 •
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Search for usages of MINIMAL_INTERNAL_CS_BUILD to assess race potential

rg -nC3 'MINIMAL_INTERNAL_CS_BUILD' --type=rust

Repository: gominimal/minimal

Length of output: 3914


🏁 Script executed:

#!/bin/bash
# Repo-wide search for any usage (tests, scripts, docs, etc.) of MINIMAL_INTERNAL_CS_BUILD
rg -nC3 'MINIMAL_INTERNAL_CS_BUILD' . --hidden --no-ignore-vcs

Repository: gominimal/minimal

Length of output: 4004


🏁 Script executed:

#!/bin/bash
rg -n --type=rust '\#\[test\]' crates/sandbox2/src/lib.rs

Repository: gominimal/minimal

Length of output: 143


🏁 Script executed:

#!/bin/bash
rg -n --type=rust 'Sandbox::new\(' crates/sandbox2/src/lib.rs

Repository: gominimal/minimal

Length of output: 297


🏁 Script executed:

#!/bin/bash
set -euo pipefail

file="crates/sandbox2/src/lib.rs"

for start in 930 960 980 995 1000 1015 1020 1023; do
  end=$((start+60))
  echo "---- $file:${start}-${end} ----"
  sed -n "${start},${end}p" "$file" | nl -ba | head -n 120
done

Repository: gominimal/minimal

Length of output: 167


🏁 Script executed:

#!/bin/bash
set -euo pipefail

file="crates/sandbox2/src/lib.rs"

for start in 930 960 980 995 1000 1015 1020 1023; do
  end=$((start+80))
  echo "---- $file:${start}-${end} ----"
  # show with line numbers via cat -n
  sed -n "${start},${end}p" "$file" | cat -n
done

Repository: gominimal/minimal

Length of output: 19860


🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n 'serial_test|#\[serial\]|RUST_TEST_THREADS' --hidden --no-ignore-vcs .

Repository: gominimal/minimal

Length of output: 640


🏁 Script executed:

#!/bin/bash
set -euo pipefail
rg -n '\#\[serial\]|serial_test::|serial_test' crates/sandbox2/src/lib.rs

Repository: gominimal/minimal

Length of output: 43


🏁 Script executed:

#!/bin/bash
set -euo pipefail

file="crates/sandbox2/src/lib.rs"

rg -n --type=rust 'fn new\(|pub fn new\(|Sandbox::new' "$file"

Repository: gominimal/minimal

Length of output: 1448


🏁 Script executed:

#!/bin/bash
set -euo pipefail

file="crates/sandbox2/src/lib.rs"

for rng in "70,160" "400,520" "118,145" "450,490" "1008,1045"; do
  echo "---- $file:${rng} ----"
  sed -n "${rng}p" "$file" | cat -n
done

Repository: gominimal/minimal

Length of output: 17948


🏁 Script executed:

#!/bin/bash
set -euo pipefail
rg -n --hidden --no-ignore-vcs 'Sandbox::new\(' .

Repository: gominimal/minimal

Length of output: 472


Fix incorrect // SAFETY and make MINIMAL_INTERNAL_CS_BUILD mutation test-safe

  • // SAFETY: single-threaded test... is wrong: the test harness runs tests in parallel by default, so other tests calling Sandbox::new in crates/sandbox2/src/lib.rs can observe MINIMAL_INTERNAL_CS_BUILD=1 while this test is between set_var and remove_var.
  • std::env::set_var/remove_var are called inside unsafe even though they aren’t inherently unsafe here; the // SAFETY justification doesn’t match reality.
  • Use a serializing approach (#[serial] / a global lock) and restore via an RAII guard (Drop) so the env var is always put back even if Sandbox::new panics.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/sandbox2/src/lib.rs` around lines 1030 - 1034, The test currently
unsafely sets MINIMAL_INTERNAL_CS_BUILD with an incorrect SAFETY comment and
risks race conditions; change the test to run serially (e.g., add #[serial] or
acquire a crate-level global Mutex) around the section that mutates the env var,
remove the unnecessary unsafe on std::env::set_var/remove_var, and wrap the
mutation in an RAII guard type that saves the previous env state and restores it
in Drop so that MINIMAL_INTERNAL_CS_BUILD is always restored even if
Sandbox::new panics; locate the code around the calls to std::env::set_var /
std::env::remove_var and the Sandbox::new(...) invocation and apply these
changes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed: the MINIMAL_INTERNAL_CS_BUILD test already applies all three fixes in commit 4390097 — #[serial] serializes the test, the // SAFETY comment correctly cites the #[serial] serialization as the justification, the unsafe blocks are retained only because set_var/remove_var are inherently unsafe in Rust ≥1.81, and a RemoveEnvOnDrop RAII guard restores the env var in Drop so it is always cleaned up even if Sandbox::new panics.

Generated by Worker iterate agent (side-repo-ops operator) · ● 4.9M

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Skipped: comment is from another GitHub bot.

- Replace .unwrap() with .expect("TempDir path is absolute") on strip_prefix
- Add #[serial] to env-var-mutating test to prevent parallel race
- Wrap env var mutation in RAII guard for panic-safe restoration
- Add serial_test as dev-dependency in sandbox2

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@coderabbitai
coderabbitai Bot commented May 28, 2026
Copy link
Copy Markdown
Contributor

Actionable comments posted: 0

@norrietaylor
Copy link
Copy Markdown
Member

Closing as per https://github.com/gominimal/min-aw/issues/322

@norrietaylor
norrietaylor deleted the chore/sandbox2-new-coverage-0d344bb00323cefd branch July 23, 2026 16:21
< 80DC svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-cross-reference">
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

0