Make venv fallback test robust against VIRTUAL_ENV (#128)

This commit is contained in:
Josh Thomas 2025-04-30 22:49:42 -05:00 committed by GitHub
parent abd270daeb
commit 95a68e5f3a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -277,15 +277,6 @@ mod tests {
original_value,
}
}
fn clear(key: &'a str) -> Self {
let original_value = env::var(key).ok();
env::remove_var(key);
Self {
key,
original_value,
}
}
}
impl Drop for VirtualEnvGuard<'_> {
@ -333,8 +324,11 @@ mod tests {
let project_dir = tempdir().unwrap();
let project_venv_prefix = create_mock_venv(&project_dir.path().join(".venv"), None);
// Clear VIRTUAL_ENV just in case
let _guard = VirtualEnvGuard::clear("VIRTUAL_ENV");
// Set VIRTUAL_ENV to something known to be invalid, rather than clearing.
// This prevents the test runner's VIRTUAL_ENV (e.g., from Nox) from interfering.
let invalid_virtual_env_path = project_dir.path().join("non_existent_virtual_env");
let _guard =
VirtualEnvGuard::set("VIRTUAL_ENV", invalid_virtual_env_path.to_str().unwrap());
// Provide an invalid explicit path
let invalid_path = project_dir.path().join("non_existent_venv");
@ -392,8 +386,10 @@ mod tests {
let project_dir = tempdir().unwrap();
let venv_prefix = create_mock_venv(&project_dir.path().join(".venv"), None);
// Ensure VIRTUAL_ENV is not set
let _guard = VirtualEnvGuard::clear("VIRTUAL_ENV");
// Set VIRTUAL_ENV to something known to be invalid to ensure it's ignored.
let invalid_virtual_env_path = project_dir.path().join("non_existent_venv_proj_found");
let _guard =
VirtualEnvGuard::set("VIRTUAL_ENV", invalid_virtual_env_path.to_str().unwrap());
let env = PythonEnvironment::new(project_dir.path(), None)
.expect("Should find environment in project .venv");
@ -408,7 +404,10 @@ mod tests {
let dot_venv_prefix = create_mock_venv(&project_dir.path().join(".venv"), None);
let _venv_prefix = create_mock_venv(&project_dir.path().join("venv"), None); // Should be ignored if .venv found first
let _guard = VirtualEnvGuard::clear("VIRTUAL_ENV");
// Set VIRTUAL_ENV to something known to be invalid to ensure it's ignored.
let invalid_virtual_env_path = project_dir.path().join("non_existent_venv_priority");
let _guard =
VirtualEnvGuard::set("VIRTUAL_ENV", invalid_virtual_env_path.to_str().unwrap());
let env =
PythonEnvironment::new(project_dir.path(), None).expect("Should find environment");
@ -422,8 +421,11 @@ mod tests {
fn test_system_python_fallback() {
let project_dir = tempdir().unwrap();
// Ensure no explicit path, no VIRTUAL_ENV, no project venvs
let _guard = VirtualEnvGuard::clear("VIRTUAL_ENV");
// Set VIRTUAL_ENV to something known to be invalid to ensure it's ignored.
let invalid_virtual_env_path =
project_dir.path().join("non_existent_venv_sys_fallback");
let _guard =
VirtualEnvGuard::set("VIRTUAL_ENV", invalid_virtual_env_path.to_str().unwrap());
// We don't create any venvs in project_dir
// This test assumes `which python` works and points to a standard layout
@ -447,8 +449,10 @@ mod tests {
fn test_no_python_found() {
let project_dir = tempdir().unwrap();
// Ensure no explicit path, no VIRTUAL_ENV, no project venvs
let _guard = VirtualEnvGuard::clear("VIRTUAL_ENV");
// Ensure no explicit path, no project venvs, and set VIRTUAL_ENV to invalid.
let invalid_virtual_env_path = project_dir.path().join("non_existent_venv_no_python");
let _guard =
VirtualEnvGuard::set("VIRTUAL_ENV", invalid_virtual_env_path.to_str().unwrap());
// To *ensure* system fallback fails, we'd need to manipulate PATH,
// which is tricky and platform-dependent. Instead, we test the scenario