diff --git a/crates/uv/tests/it/common/mod.rs b/crates/uv/tests/it/common/mod.rs index 01b805e5b..b26f07c40 100644 --- a/crates/uv/tests/it/common/mod.rs +++ b/crates/uv/tests/it/common/mod.rs @@ -755,7 +755,7 @@ impl TestContext { "Activate with: source $1/[BIN]/activate".to_string(), )); filters.push(( - r"Activate with: source (.*)/bin/activate(?:\.\w+)?".to_string(), + r"Activate with: source (.*)/bin/activate".to_string(), "Activate with: source $1/[BIN]/activate".to_string(), )); @@ -927,11 +927,6 @@ impl TestContext { if activate_venv { command.env(EnvVars::VIRTUAL_ENV, self.venv.as_os_str()); } - - if cfg!(unix) { - // Avoid locale issues in tests - command.env(EnvVars::LC_ALL, "C"); - } } /// Create a `pip compile` command for testing. @@ -1271,6 +1266,9 @@ impl TestContext { command } + /// The path to the Python interpreter in the venv. + /// + /// Don't use this for `Command::new`, use `Self::python_command` instead. pub fn interpreter(&self) -> PathBuf { let venv = &self.venv; if cfg!(unix) { @@ -1544,8 +1542,50 @@ impl TestContext { /// Creates a new `Command` that is intended to be suitable for use in /// all tests, but with the given binary. + /// + /// The command contains only essential env vars to avoid the tests getting + /// influenced by host `UV_*`, XDG, or other environment variables. fn new_command_with(bin: &Path) -> Command { - Command::new(bin) + let mut command = Command::new(bin); + + // Only forward essential environment variables, to avoid influencing behavior and snapshots + // from the host env. + command.env_clear(); + if cfg!(unix) { + // For Unix, we pretend the tests run in a bash for the activate hint, for Windows, shell + // detection assumes PowerShell if `PROMPT` is not set. + command.env("SHELL", "/bin/bash"); + + // Avoid locale issues in tests + command.env(EnvVars::LC_ALL, "C"); + + // STOPSHIP(konsti): This is for pyodide on my machine + for env_var in ["VOLTA_HOME"] { + if let Some(value) = env::var_os(env_var) { + command.env(env_var, value); + } + } + } else if cfg!(windows) { + let env_vars = &[ + "PATH", + "PATHEXT", + "ProgramFiles", + "ProgramFiles(x86)", + "RANDOM", + // https://github.com/rust-lang/rust/issues/114737 + "SYSTEMROOT", + "SystemDrive", + ]; + for env_var in env_vars { + if let Some(system_root) = env::var_os(env_var) { + command.env(env_var, system_root); + } + } + } else { + unimplemented!("Only Unix and Windows are supported"); + } + + command } } @@ -1608,7 +1648,7 @@ pub fn get_python(version: &PythonVersion) -> PathBuf { /// Create a virtual environment at the given path. pub fn create_venv_from_executable>(path: P, cache_dir: &ChildPath, python: &Path) { - assert_cmd::Command::new(get_bin()) + TestContext::new_command_with(&get_bin()) .arg("venv") .arg(path.as_ref().as_os_str()) .arg("--clear") diff --git a/crates/uv/tests/it/pip_install.rs b/crates/uv/tests/it/pip_install.rs index 1255a77a1..f83a4c6d6 100644 --- a/crates/uv/tests/it/pip_install.rs +++ b/crates/uv/tests/it/pip_install.rs @@ -5872,6 +5872,8 @@ fn deptry_gitignore() { uv_snapshot!(context.filters(), context.pip_install() .arg(format!("deptry_reproducer @ {}", source_dist_dir.join("deptry_reproducer-0.1.0.tar.gz").simplified_display())) .arg("--strict") + // Set by the test harness, needed by the test + .env("RUSTUP_TOOLCHAIN", std::env::var("RUSTUP_TOOLCHAIN").unwrap()) .current_dir(source_dist_dir), @r###" success: true exit_code: 0