mirror of
https://github.com/joshuadavidthomas/django-language-server.git
synced 2025-08-03 17:48:20 +00:00
Make venv fallback test robust against VIRTUAL_ENV
(#128)
This commit is contained in:
parent
abd270daeb
commit
95a68e5f3a
1 changed files with 22 additions and 18 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue