Skip to content

Commit aece597

Browse files
committed
test: conda binary discovery tests for PATH and condabin locations
1 parent c24f327 commit aece597

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed

crates/pet-conda/tests/environment_locations_test.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,113 @@ fn list_conda_envs_discovers_base_from_another_child_env() {
9595
]
9696
);
9797
}
98+
99+
/// Test that get_known_conda_install_locations discovers conda installations from PATH
100+
/// when no explicit conda_executable is provided. This is important for discovering
101+
/// conda installations on mapped drives and other non-standard locations.
102+
/// Fixes https://github.com/microsoft/python-environment-tools/issues/194
103+
#[cfg(unix)]
104+
#[test]
105+
fn discovers_conda_install_from_path() {
106+
use common::{create_test_environment, resolve_test_path};
107+
use pet_conda::env_variables::EnvVariables;
108+
use pet_conda::environment_locations::get_known_conda_install_locations;
109+
use std::collections::HashMap;
110+
111+
// Set up PATH to include the conda bin directory (simulating conda on a mapped drive)
112+
let anaconda_bin = resolve_test_path(&["unix", "anaconda3-2023.03", "bin"]);
113+
let path_value = anaconda_bin.to_string_lossy().to_string();
114+
115+
let mut vars = HashMap::new();
116+
vars.insert("PATH".to_string(), path_value);
117+
118+
let env = create_test_environment(vars, None, vec![], None);
119+
let env_vars = EnvVariables::from(&env);
120+
121+
// Call get_known_conda_install_locations without an explicit conda_executable
122+
let locations = get_known_conda_install_locations(&env_vars, &None);
123+
124+
// The anaconda3-2023.03 install should be discovered from PATH
125+
let expected_conda_install = resolve_test_path(&["unix", "anaconda3-2023.03"]);
126+
assert!(
127+
locations.contains(&expected_conda_install),
128+
"Expected {:?} to be in {:?}",
129+
expected_conda_install,
130+
locations
131+
);
132+
}
133+
134+
/// Test that get_known_conda_install_locations discovers conda installations from condabin in PATH.
135+
/// This simulates the typical Windows Miniforge/Anaconda setup where condabin is added to PATH.
136+
/// Fixes https://github.com/microsoft/python-environment-tools/issues/194
137+
#[cfg(unix)]
138+
#[test]
139+
fn discovers_conda_install_from_condabin_in_path() {
140+
use common::{create_test_environment, resolve_test_path};
141+
use pet_conda::env_variables::EnvVariables;
142+
use pet_conda::environment_locations::get_known_conda_install_locations;
143+
use std::collections::HashMap;
144+
145+
// Set up PATH to include the condabin directory (typical Miniforge/Anaconda setup on Windows)
146+
let anaconda_condabin = resolve_test_path(&["unix", "anaconda3-2023.03", "condabin"]);
147+
let path_value = anaconda_condabin.to_string_lossy().to_string();
148+
149+
let mut vars = HashMap::new();
150+
vars.insert("PATH".to_string(), path_value);
151+
152+
let env = create_test_environment(vars, None, vec![], None);
153+
let env_vars = EnvVariables::from(&env);
154+
155+
// Call get_known_conda_install_locations without an explicit conda_executable
156+
let locations = get_known_conda_install_locations(&env_vars, &None);
157+
158+
// The anaconda3-2023.03 install should be discovered from PATH via condabin
159+
let expected_conda_install = resolve_test_path(&["unix", "anaconda3-2023.03"]);
160+
assert!(
161+
locations.contains(&expected_conda_install),
162+
"Expected {:?} to be in {:?}",
163+
expected_conda_install,
164+
locations
165+
);
166+
}
167+
168+
/// Test that when an explicit conda_executable is provided, PATH lookup is skipped.
169+
/// This ensures we don't do unnecessary work when the user has configured a conda path.
170+
#[cfg(unix)]
171+
#[test]
172+
fn skips_path_lookup_when_conda_executable_provided() {
173+
use common::{create_test_environment, resolve_test_path};
174+
use pet_conda::env_variables::EnvVariables;
175+
use pet_conda::environment_locations::get_known_conda_install_locations;
176+
use std::collections::HashMap;
177+
178+
// Set up PATH to include a conda directory
179+
let anaconda_bin = resolve_test_path(&["unix", "anaconda3-2023.03", "bin"]);
180+
let path_value = anaconda_bin.to_string_lossy().to_string();
181+
182+
let mut vars = HashMap::new();
183+
vars.insert("PATH".to_string(), path_value);
184+
185+
let env = create_test_environment(vars, None, vec![], None);
186+
let env_vars = EnvVariables::from(&env);
187+
188+
// Provide an explicit conda_executable
189+
let conda_executable = Some(resolve_test_path(&[
190+
"unix",
191+
"anaconda3-2023.03",
192+
"bin",
193+
"conda",
194+
]));
195+
196+
// Call get_known_conda_install_locations with an explicit conda_executable
197+
let locations = get_known_conda_install_locations(&env_vars, &conda_executable);
198+
199+
// The conda install should still be discovered (from the explicit path, not PATH)
200+
let expected_conda_install = resolve_test_path(&["unix", "anaconda3-2023.03"]);
201+
assert!(
202+
locations.contains(&expected_conda_install),
203+
"Expected {:?} to be in {:?}",
204+
expected_conda_install,
205+
locations
206+
);
207+
}

crates/pet-conda/tests/manager_test.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,84 @@ fn does_not_find_conda_env_for_bogus_dirs() {
5050

5151
assert!(CondaManager::from(&path).is_none());
5252
}
53+
54+
/// Test that find_conda_binary finds conda from the PATH environment variable.
55+
/// This is important for discovering conda installations on mapped drives and
56+
/// other non-standard locations (fixes https://github.com/microsoft/python-environment-tools/issues/194).
57+
#[cfg(unix)]
58+
#[test]
59+
fn finds_conda_binary_from_path() {
60+
use common::{create_test_environment, resolve_test_path};
61+
use pet_conda::env_variables::EnvVariables;
62+
use pet_conda::manager::find_conda_binary;
63+
use std::collections::HashMap;
64+
65+
let anaconda_bin = resolve_test_path(&["unix", "anaconda3-2023.03", "bin"]);
66+
let path_value = anaconda_bin.to_string_lossy().to_string();
67+
68+
let mut vars = HashMap::new();
69+
vars.insert("PATH".to_string(), path_value);
70+
71+
let env = create_test_environment(vars, None, vec![], None);
72+
let env_vars = EnvVariables::from(&env);
73+
74+
let conda_binary = find_conda_binary(&env_vars);
75+
76+
assert!(conda_binary.is_some());
77+
assert_eq!(
78+
conda_binary.unwrap(),
79+
resolve_test_path(&["unix", "anaconda3-2023.03", "bin", "conda"])
80+
);
81+
}
82+
83+
/// Test that find_conda_binary also works when conda is in the condabin directory
84+
/// (common on Windows with Miniforge/Anaconda where condabin is added to PATH).
85+
#[cfg(unix)]
86+
#[test]
87+
fn finds_conda_binary_from_condabin_path() {
88+
use common::{create_test_environment, resolve_test_path};
89+
use pet_conda::env_variables::EnvVariables;
90+
use pet_conda::manager::find_conda_binary;
91+
use std::collections::HashMap;
92+
93+
let anaconda_condabin = resolve_test_path(&["unix", "anaconda3-2023.03", "condabin"]);
94+
let path_value = anaconda_condabin.to_string_lossy().to_string();
95+
96+
let mut vars = HashMap::new();
97+
vars.insert("PATH".to_string(), path_value);
98+
99+
let env = create_test_environment(vars, None, vec![], None);
100+
let env_vars = EnvVariables::from(&env);
101+
102+
let conda_binary = find_conda_binary(&env_vars);
103+
104+
assert!(conda_binary.is_some());
105+
assert_eq!(
106+
conda_binary.unwrap(),
107+
resolve_test_path(&["unix", "anaconda3-2023.03", "condabin", "conda"])
108+
);
109+
}
110+
111+
/// Test that find_conda_binary returns None when conda is not on PATH.
112+
#[cfg(unix)]
113+
#[test]
114+
fn does_not_find_conda_binary_when_not_on_path() {
115+
use common::{create_test_environment, resolve_test_path};
116+
use pet_conda::env_variables::EnvVariables;
117+
use pet_conda::manager::find_conda_binary;
118+
use std::collections::HashMap;
119+
120+
// Use a path that doesn't have conda
121+
let some_other_path = resolve_test_path(&["unix", "bogus_directory"]);
122+
let path_value = some_other_path.to_string_lossy().to_string();
123+
124+
let mut vars = HashMap::new();
125+
vars.insert("PATH".to_string(), path_value);
126+
127+
let env = create_test_environment(vars, None, vec![], None);
128+
let env_vars = EnvVariables::from(&env);
129+
130+
let conda_binary = find_conda_binary(&env_vars);
131+
132+
assert!(conda_binary.is_none());
133+
}

0 commit comments

Comments
 (0)