Skip to content

Commit 0e1a641

Browse files
committed
fix: Add fallback to home directory for pyenv version detection on Windows and implement corresponding tests
1 parent bfbd44d commit 0e1a641

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

crates/pet-pyenv/src/manager.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ fn get_pyenv_manager_version(
8585
) -> Option<String> {
8686
// In windows, the version is stored in the `.pyenv/.version` file
8787

88-
let pyenv_dir = get_pyenv_dir(environment)?;
88+
// Try env var path first, then fall back to home directory
89+
let pyenv_dir = get_pyenv_dir(environment)
90+
.or_else(|| get_home_pyenv_dir(environment)?.parent().map(PathBuf::from))?;
91+
8992
let mut version_file = pyenv_dir.join(".version");
9093
if !version_file.exists() {
9194
// We might have got the path `~/.pyenv/pyenv-win`

crates/pet-pyenv/tests/pyenv_test.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,63 @@
33

44
mod common;
55

6+
#[test]
7+
#[cfg(windows)]
8+
fn gets_pyenv_manager_version_without_env_vars() {
9+
use crate::common::{create_test_environment, resolve_test_path};
10+
use pet_conda::Conda;
11+
use pet_core::{
12+
manager::{EnvManager, EnvManagerType},
13+
Locator,
14+
};
15+
use pet_pyenv::PyEnv;
16+
use pet_reporter::{cache::CacheReporter, collect};
17+
use std::{collections::HashMap, sync::Arc};
18+
19+
// Test that pyenv-win version detection works when PYENV/PYENV_ROOT env vars are not set
20+
// by falling back to the home directory path (~/.pyenv/.version)
21+
let home = resolve_test_path(&["windows", "pyenv_no_env_vars", "user_home"]);
22+
let pyenv_bin = resolve_test_path(&[
23+
"windows",
24+
"pyenv_no_env_vars",
25+
"user_home",
26+
".pyenv",
27+
"pyenv-win",
28+
"bin",
29+
]);
30+
31+
// Create environment WITHOUT pyenv/pyenv_root env vars, but provide the bin path
32+
// via known_global_search_locations (simulates pyenv being on PATH)
33+
let environment =
34+
create_test_environment(HashMap::new(), Some(home.clone()), vec![pyenv_bin], None);
35+
36+
let conda = Arc::new(Conda::from(&environment));
37+
let locator = PyEnv::from(&environment, conda);
38+
let reporter = Arc::new(collect::create_reporter());
39+
locator.find(&CacheReporter::new(reporter.clone()));
40+
41+
let managers = reporter.managers.lock().unwrap().clone();
42+
43+
// Should find the pyenv manager with version from ~/.pyenv/.version
44+
assert_eq!(managers.len(), 1);
45+
46+
let expected_manager = EnvManager {
47+
executable: resolve_test_path(&[
48+
"windows",
49+
"pyenv_no_env_vars",
50+
"user_home",
51+
".pyenv",
52+
"pyenv-win",
53+
"bin",
54+
"pyenv.exe",
55+
]),
56+
version: Some("3.5.0".to_string()),
57+
tool: EnvManagerType::Pyenv,
58+
};
59+
assert_eq!(expected_manager.version, managers[0].version);
60+
assert_eq!(expected_manager.tool, managers[0].tool);
61+
}
62+
663
#[test]
764
#[cfg(unix)]
865
fn does_not_find_any_pyenv_envs() {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.5.0

crates/pet-pyenv/tests/windows/pyenv_no_env_vars/user_home/.pyenv/pyenv-win/bin/pyenv.exe

Whitespace-only changes.

crates/pet-pyenv/tests/windows/pyenv_no_env_vars/user_home/.pyenv/pyenv-win/versions/3.11.0/python.exe

Whitespace-only changes.

0 commit comments

Comments
 (0)