Repository navigation
[worker:coverage] test(sandbox2): add behavior tests for Sandbox::new - #259
Conversation
Test three observable contracts of Sandbox::new, not directory layout: - sandbox_new_rejects_mapped_file_in_rootfs: SandboxMapped::File in config.rootfs must return Err(Error::MappedFile(_)). Files cannot be hardlinked at the rootfs level; only directories are valid. - sandbox_new_derives_state_dir_from_base: when no state_dir is configured, sandbox.state_dir must equal base_dir/state. Callers rely on this to bind-mount /state at the correct host path. - sandbox_new_honours_explicit_state_dir: when with_state_dir() is used, sandbox.state_dir must equal the caller-supplied path verbatim. - sandbox_new_minenv_socket_is_connectable: after construction, the Unix socket at run/minenv_sock must accept connections, confirming the channel listener thread is running. No directory-existence assertions are made; all four tests assert the function's observable contract per the test authoring standards. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
📝 WalkthroughWalkthroughThis PR extends test coverage for ChangesSandbox::new test coverage
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (5)
crates/sandbox2/src/lib.rs (5)
1029-1034: ⚡ Quick winUse
expect()instead ofunwrap()for consistency.Line 1034 uses
.unwrap()while line 1036 correctly uses.expect(). Applyexpect()consistently per coding guidelines.🔧 Suggested improvement
- let sandbox = Sandbox::new(base, config, ()).unwrap(); + let sandbox = Sandbox::new(base, config, ()) + .expect("Sandbox::new should succeed for socket test");🤖 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 1029 - 1034, In the test function sandbox_new_minenv_socket_is_connectable replace the use of .unwrap() on the Sandbox::new(base, config, ()) call with .expect(...) to match the existing style; update the call in sandbox_new_minenv_socket_is_connectable to use Sandbox::new(...).expect("failed to create Sandbox in sandbox_new_minenv_socket_is_connectable") (or a similar descriptive message) so it is consistent with the other expect() usage.
967-976: ⚡ Quick winUse
expect()instead ofunwrap()for clearer test failures.The helper uses
.unwrap()at lines 972 and 974. As per coding guidelines, preferexpect("reason")to provide context when panics occur during test setup.🔧 Suggested improvement
fn make_base_with_synth() -> (tempfile::TempDir, PathBuf) { - let tmp = tempfile::TempDir::new().unwrap(); + let tmp = tempfile::TempDir::new().expect("failed to create temp directory for test"); let base = tmp.path().to_path_buf(); - fs::create_dir_all(base.join("synth").join("usr")).unwrap(); + fs::create_dir_all(base.join("synth").join("usr")) + .expect("failed to create synth/usr test structure"); (tmp, base) }🤖 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 967 - 976, In make_base_with_synth(), replace the two .unwrap() calls with .expect(...) so test failures show context: change tempfile::TempDir::new().unwrap() to .expect("create tempdir in make_base_with_synth") and change fs::create_dir_all(...).unwrap() to .expect("create synth/usr directory in make_base_with_synth"); reference tmp, base, tempfile::TempDir::new, and fs::create_dir_all when making the edits.
1014-1019: ⚡ Quick winUse
expect()instead ofunwrap().Lines 1016 and 1019 use
.unwrap(). As per coding guidelines, useexpect("reason")for clearer failure context.🔧 Suggested improvement
- let state_tmp = tempfile::TempDir::new().unwrap(); + let state_tmp = tempfile::TempDir::new().expect("failed to create state temp directory"); let state_path = state_tmp.path().to_path_buf(); let config = Config::new("test-state-explicit").with_state_dir(state_path.clone()); - let sandbox = Sandbox::new(base, config, ()).unwrap(); + let sandbox = Sandbox::new(base, config, ()) + .expect("Sandbox::new should succeed with explicit state_dir");🤖 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 1014 - 1019, In sandbox_new_honours_explicit_state_dir replace the two uses of .unwrap() with .expect(...) to provide clear failure context: use state_tmp creation like tempfile::TempDir::new().expect("failed to create temporary state dir for sandbox_new_honours_explicit_state_dir") and replace Sandbox::new(base, config, ()).unwrap() with Sandbox::new(base, config, ()).expect("failed to create Sandbox in sandbox_new_honours_explicit_state_dir"); this keeps the same behavior but gives actionable error messages and references the functions make_base_with_synth, tempfile::TempDir::new, and Sandbox::new for locating the calls.
999-1002: ⚡ Quick winUse
expect()instead ofunwrap().Line 1002 uses
.unwrap(). As per coding guidelines, useexpect("reason")for clearer failure messages.🔧 Suggested improvement
- let sandbox = Sandbox::new(base.clone(), config, ()).unwrap(); + let sandbox = Sandbox::new(base.clone(), config, ()) + .expect("Sandbox::new should succeed with minimal config");🤖 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 999 - 1002, In the test function sandbox_new_derives_state_dir_from_base replace the unsafe .unwrap() call on Sandbox::new(base.clone(), config, ()) with .expect("failed to create Sandbox in sandbox_new_derives_state_dir_from_base") so test failures print a clear message; locate the call to Sandbox::new in that function (and any similar uses in the same test) and swap unwrap() for expect(...) with a short descriptive reason referencing Sandbox creation.
982-988: ⚡ Quick winUse
expect()instead ofunwrap().Line 983 uses
.unwrap(). As per coding guidelines, useexpect("reason")to clarify intent and provide better panic messages.🔧 Suggested improvement
- let tmp = tempfile::TempDir::new().unwrap(); + let tmp = tempfile::TempDir::new().expect("failed to create temp directory");🤖 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 982 - 988, In the test function sandbox_new_rejects_mapped_file_in_rootfs the call to tempfile::TempDir::new().unwrap() should use expect() with a clear message instead of unwrap(); replace the unwrap() on the TempDir creation with expect("failed to create temp dir for sandbox_new_rejects_mapped_file_in_rootfs") (or similar) so failures produce a helpful panic message when creating base in Sandbox::new.
🤖 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.
Nitpick comments:
In `@crates/sandbox2/src/lib.rs`:
- Around line 1029-1034: In the test function
sandbox_new_minenv_socket_is_connectable replace the use of .unwrap() on the
Sandbox::new(base, config, ()) call with .expect(...) to match the existing
style; update the call in sandbox_new_minenv_socket_is_connectable to use
Sandbox::new(...).expect("failed to create Sandbox in
sandbox_new_minenv_socket_is_connectable") (or a similar descriptive message) so
it is consistent with the other expect() usage.
- Around line 967-976: In make_base_with_synth(), replace the two .unwrap()
calls with .expect(...) so test failures show context: change
tempfile::TempDir::new().unwrap() to .expect("create tempdir in
make_base_with_synth") and change fs::create_dir_all(...).unwrap() to
.expect("create synth/usr directory in make_base_with_synth"); reference tmp,
base, tempfile::TempDir::new, and fs::create_dir_all when making the edits.
- Around line 1014-1019: In sandbox_new_honours_explicit_state_dir replace the
two uses of .unwrap() with .expect(...) to provide clear failure context: use
state_tmp creation like tempfile::TempDir::new().expect("failed to create
temporary state dir for sandbox_new_honours_explicit_state_dir") and replace
Sandbox::new(base, config, ()).unwrap() with Sandbox::new(base, config,
()).expect("failed to create Sandbox in
sandbox_new_honours_explicit_state_dir"); this keeps the same behavior but gives
actionable error messages and references the functions make_base_with_synth,
tempfile::TempDir::new, and Sandbox::new for locating the calls.
- Around line 999-1002: In the test function
sandbox_new_derives_state_dir_from_base replace the unsafe .unwrap() call on
Sandbox::new(base.clone(), config, ()) with .expect("failed to create Sandbox in
sandbox_new_derives_state_dir_from_base") so test failures print a clear
message; locate the call to Sandbox::new in that function (and any similar uses
in the same test) and swap unwrap() for expect(...) with a short descriptive
reason referencing Sandbox creation.
- Around line 982-988: In the test function
sandbox_new_rejects_mapped_file_in_rootfs the call to
tempfile::TempDir::new().unwrap() should use expect() with a clear message
instead of unwrap(); replace the unwrap() on the TempDir creation with
expect("failed to create temp dir for
sandbox_new_rejects_mapped_file_in_rootfs") (or similar) so failures produce a
helpful panic message when creating base in Sandbox::new.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 82551142-94ac-4a94-be98-a1b448794ecd
📒 Files selected for processing (1)
crates/sandbox2/src/lib.rs
Exercise four observable contracts of
sandbox2::Sandbox::new(issue #243,regions=214, whole-function coverage = 0).What was changed
Added four
#[test]cases to the existing#[cfg(test)] mod testsincrates/sandbox2/src/lib.rs, plus amake_base_with_synth()test-setup helper:sandbox_new_rejects_mapped_file_in_rootfsSandboxMapped::Fileinconfig.rootfs→Err(Error::MappedFile(_))sandbox_new_derives_state_dir_from_basestate_dirconfigured →sandbox.state_dir == base_dir/statesandbox_new_honours_explicit_state_dirwith_state_dir(p)→sandbox.state_dir == psandbox_new_minenv_socket_is_connectablerun/minenv_sockaccepts aUnixStream::connectWhy these tests, not directory-existence tests
PR
#250was closed because it asserted thatbuild/,state/, andstate/home/exist — change-detector tests that restate the constructor body. Per the test authoring standards, these are banned.These four tests assert the function's observable contract:
state_dirfield value thatnew_containeruses to bind-mount/state(caller-visible field).No directory-existence assertions are made.
Setup helper
make_base_with_synth()createsbase_dir/synth/usr/— the minimum structure needed forhardlink_dir_contentsand theusr/lib64 → libsymlink step to succeed. This is test setup, not assertion.Closes
#243Verification
cargo test -p sandbox2— 6 tests: 6 ok.cargo fmt --all --check— no diff.cargo clippy --workspace --all-targets— no warnings (exit 0).Summary by CodeRabbit