mirror of https://github.com/astral-sh/uv
Clear the env vars for tests
Currently, it's possible to break our test suite by having an env var set that influences uv, either a `UV_*` var, or something more generic such as the XDG env vars. We previously fixed them env-var-by-env-var as we discovered. By clearing the env var for subcommands entirely, we can invert this to only include the env vars we care about. Notable limitations are that this only affects tests that use `TestContext::add_shared_env` (default, can be opted-out) and we still pass a (modified) `PATH`.
This commit is contained in:
parent
83635a6c45
commit
1860947930
|
|
@ -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<P: AsRef<Path>>(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")
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue