This document describes the test suite for cargox, particularly focusing on sandboxing and directory isolation guarantees.
split_spec_without_version- Verifies parsing of crate names without version specifierssplit_spec_with_version- Verifies parsing of crate names with@versionsyntaxsplit_spec_rejects_empty- Ensures invalid specs are rejected
These tests verify that command-line arguments are correctly parsed and that arguments intended for the target binary are not intercepted by cargox.
Critical regression prevention tests for ensuring cargox bat --help shows bat's help, not cargox's help:
-
test_help_flag_passed_to_binary- Most critical test: Verifies that flags like--helpafter the crate spec are passed to the target binary and not intercepted by cargox's argument parser. Ensures we don't regress the fix for this common use case. -
test_cargox_help_still_works- Verifies thatcargox --help(without a crate spec) still shows cargox's own help text. -
test_cargox_flags_before_crate_spec- Verifies that flags before the crate spec (likecargox --force bat) are correctly parsed by cargox. -
test_bin_flag_parsing- Verifies that the--binflag works correctly and that subsequent arguments are passed to the binary.
parse_args_separates_binary_args_correctly- Unit test demonstrating the difference between standard clap parsing and custom argument separationparse_args_handles_bin_flag- Verifies--binflag parsingparse_args_handles_force_flag- Verifies-f/--forceflag parsing
These tests verify that cargox uses the correct, sandboxed installation directories:
Ensures that when CARGOX_INSTALL_DIR is set, it takes precedence over all other directory determination logic.
Critical sandboxing test: Verifies that CARGO_INSTALL_ROOT is completely ignored, even when set. This ensures we don't accidentally use the user's standard Cargo install location.
Verifies that when no override is set, we use platform-appropriate XDG directories:
- macOS:
~/Library/Application Support/cargox - Linux:
~/.local/share/cargox - Windows:
%APPDATA%\cargox
Directory isolation is enforced by refusing to execute binaries outside the sandboxed install directory (see the Execution Guard tests below).
Unit test in executor.rs that ensures binaries located inside the sandboxed install directory are allowed to run.
Unit test in executor.rs that ensures we refuse to execute binaries that live outside the sandboxed install directory, preventing delegation to system-wide paths.
Verifies that the sanitize_cargo_env function exists and compiles correctly. The actual environment sanitization is tested through integration tests since we can't directly inspect a Command's environment.
The function removes these variables before calling cargo install or cargo-binstall:
CARGO_INSTALL_ROOTCARGO_HOMECARGO_BUILD_TARGET_DIRCARGO_TARGET_DIRBINSTALL_INSTALL_PATHRUSTUP_HOMERUSTUP_TOOLCHAIN
# Run all tests
cargo test
# Run tests with output
cargo test -- --nocapture
# Run tests serially (useful for env var tests)
cargo test -- --test-threads=1
# Run a specific test
cargo test test_name
# Run tests in release mode
cargo test --releaseThe test suite ensures these critical sandboxing guarantees:
-
Directory Isolation:
cargoxnever checks standard Cargo directories like~/.cargo/bin,~/.local/bin, or/usr/local/bin -
Environment Variable Isolation: Cargo-related environment variables are completely ignored when determining install locations
-
Installation Isolation: When installing packages, all Cargo environment variables are removed to prevent any leakage
-
Predictable Behavior: The install directory is always deterministic based on platform XDG directories or explicit override
-
No Cross-Contamination: Binaries installed via
cargoxcannot interfere with binaries installed via standardcargo install
Some tests use unsafe blocks for env::set_var and env::remove_var because these functions can cause undefined behavior if called while other threads are reading environment variables. In our test suite, this is acceptable because:
- Tests that modify environment variables run serially (not in parallel)
- Environment modifications are always cleaned up before test completion
- The modifications are only for testing purposes and don't affect production code