Skip to content

Commit 95a68e5

Browse files
Make venv fallback test robust against VIRTUAL_ENV (#128)
1 parent abd270d commit 95a68e5

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

crates/djls-project/src/lib.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,6 @@ mod tests {
277277
original_value,
278278
}
279279
}
280-
281-
fn clear(key: &'a str) -> Self {
282-
let original_value = env::var(key).ok();
283-
env::remove_var(key);
284-
Self {
285-
key,
286-
original_value,
287-
}
288-
}
289280
}
290281

291282
impl Drop for VirtualEnvGuard<'_> {
@@ -333,8 +324,11 @@ mod tests {
333324
let project_dir = tempdir().unwrap();
334325
let project_venv_prefix = create_mock_venv(&project_dir.path().join(".venv"), None);
335326

336-
// Clear VIRTUAL_ENV just in case
337-
let _guard = VirtualEnvGuard::clear("VIRTUAL_ENV");
327+
// Set VIRTUAL_ENV to something known to be invalid, rather than clearing.
328+
// This prevents the test runner's VIRTUAL_ENV (e.g., from Nox) from interfering.
329+
let invalid_virtual_env_path = project_dir.path().join("non_existent_virtual_env");
330+
let _guard =
331+
VirtualEnvGuard::set("VIRTUAL_ENV", invalid_virtual_env_path.to_str().unwrap());
338332

339333
// Provide an invalid explicit path
340334
let invalid_path = project_dir.path().join("non_existent_venv");
@@ -392,8 +386,10 @@ mod tests {
392386
let project_dir = tempdir().unwrap();
393387
let venv_prefix = create_mock_venv(&project_dir.path().join(".venv"), None);
394388

395-
// Ensure VIRTUAL_ENV is not set
396-
let _guard = VirtualEnvGuard::clear("VIRTUAL_ENV");
389+
// Set VIRTUAL_ENV to something known to be invalid to ensure it's ignored.
390+
let invalid_virtual_env_path = project_dir.path().join("non_existent_venv_proj_found");
391+
let _guard =
392+
VirtualEnvGuard::set("VIRTUAL_ENV", invalid_virtual_env_path.to_str().unwrap());
397393

398394
let env = PythonEnvironment::new(project_dir.path(), None)
399395
.expect("Should find environment in project .venv");
@@ -408,7 +404,10 @@ mod tests {
408404
let dot_venv_prefix = create_mock_venv(&project_dir.path().join(".venv"), None);
409405
let _venv_prefix = create_mock_venv(&project_dir.path().join("venv"), None); // Should be ignored if .venv found first
410406

411-
let _guard = VirtualEnvGuard::clear("VIRTUAL_ENV");
407+
// Set VIRTUAL_ENV to something known to be invalid to ensure it's ignored.
408+
let invalid_virtual_env_path = project_dir.path().join("non_existent_venv_priority");
409+
let _guard =
410+
VirtualEnvGuard::set("VIRTUAL_ENV", invalid_virtual_env_path.to_str().unwrap());
412411

413412
let env =
414413
PythonEnvironment::new(project_dir.path(), None).expect("Should find environment");
@@ -422,8 +421,11 @@ mod tests {
422421
fn test_system_python_fallback() {
423422
let project_dir = tempdir().unwrap();
424423

425-
// Ensure no explicit path, no VIRTUAL_ENV, no project venvs
426-
let _guard = VirtualEnvGuard::clear("VIRTUAL_ENV");
424+
// Set VIRTUAL_ENV to something known to be invalid to ensure it's ignored.
425+
let invalid_virtual_env_path =
426+
project_dir.path().join("non_existent_venv_sys_fallback");
427+
let _guard =
428+
VirtualEnvGuard::set("VIRTUAL_ENV", invalid_virtual_env_path.to_str().unwrap());
427429
// We don't create any venvs in project_dir
428430

429431
// This test assumes `which python` works and points to a standard layout
@@ -447,8 +449,10 @@ mod tests {
447449
fn test_no_python_found() {
448450
let project_dir = tempdir().unwrap();
449451

450-
// Ensure no explicit path, no VIRTUAL_ENV, no project venvs
451-
let _guard = VirtualEnvGuard::clear("VIRTUAL_ENV");
452+
// Ensure no explicit path, no project venvs, and set VIRTUAL_ENV to invalid.
453+
let invalid_virtual_env_path = project_dir.path().join("non_existent_venv_no_python");
454+
let _guard =
455+
VirtualEnvGuard::set("VIRTUAL_ENV", invalid_virtual_env_path.to_str().unwrap());
452456

453457
// To *ensure* system fallback fails, we'd need to manipulate PATH,
454458
// which is tricky and platform-dependent. Instead, we test the scenario

0 commit comments

Comments
 (0)