Skip to content

Commit f122ce7

Browse files
authored
Fix formatting and checking parent dir of conda exe (#218)
1 parent 52b4e23 commit f122ce7

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

crates/pet-conda/src/environments.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ pub fn get_conda_installation_used_to_create_conda_env(env_path: &Path) -> Optio
136136
if let Some(conda_dir) = get_conda_dir_from_cmd(line) {
137137
if is_conda_install(&conda_dir) {
138138
return Some(conda_dir);
139+
} else {
140+
// Possible this is a directory such as `C:\Users\donja\miniconda3\Scripts`
141+
// We try to remove `Scripts` or `bin` from the path in the `get_conda_dir_from_cmd`.
142+
// However if there are other directories such as `condabin` or others we are not aware of, lets try.
143+
if let Some(conda_dir) = conda_dir.parent() {
144+
if is_conda_install(conda_dir) {
145+
return Some(conda_dir.into());
146+
}
147+
}
139148
}
140149
}
141150
}
@@ -233,6 +242,7 @@ fn get_conda_dir_from_cmd(cmd_line: String) -> Option<PathBuf> {
233242
if let Some(conda_dir) = cmd_line.file_name() {
234243
if conda_dir.to_string_lossy().to_lowercase() == "bin"
235244
|| conda_dir.to_string_lossy().to_lowercase() == "scripts"
245+
|| conda_dir.to_string_lossy().to_lowercase() == "condabin"
236246
{
237247
if let Some(conda_dir) = cmd_line.parent() {
238248
// Ensure the casing of the paths are correct.
@@ -285,7 +295,8 @@ fn is_conda_env_name_in_cmd(cmd_line: String, name: &str) -> bool {
285295
// # cmd: /Users/donjayamanne/miniconda3/bin/conda create -n conda1
286296
// # cmd_line: "# cmd: /usr/bin/conda create -p ./prefix-envs/.conda1 python=3.12 -y"
287297
// Look for "-n <name>" in the command line
288-
cmd_line.contains(format!("-n {:?}", name).as_str())
298+
cmd_line.contains(format!("-n {}", name).as_str())
299+
|| cmd_line.contains(format!("--name {}", name).as_str())
289300
}
290301

291302
pub fn get_activation_command(
@@ -349,4 +360,23 @@ mod tests {
349360
PathBuf::from("/Users/donjayamanne/.pyenv/versions/mambaforge-22.11.1-3")
350361
);
351362
}
363+
364+
#[test]
365+
#[cfg(unix)]
366+
fn verify_conda_env_name() {
367+
let mut line = "# cmd: /Users/donjayamanne/.pyenv/versions/mambaforge-22.11.1-3/lib/python3.10/site-packages/conda/__main__.py create --yes --name .conda python=3.12";
368+
assert!(is_conda_env_name_in_cmd(line.to_string(), ".conda"));
369+
370+
let mut line = "# cmd: /Users/donjayamanne/.pyenv/versions/mambaforge-22.11.1-3/lib/python3.10/site-packages/conda/__main__.py create --yes -n .conda python=3.12";
371+
assert!(is_conda_env_name_in_cmd(line.to_string(), ".conda"));
372+
373+
line = "# cmd: /Users/donjayamanne/.pyenv/versions/mambaforge-22.11.1-3/lib/python3.10/site-packages/conda/__main__.py create --yes --name .conda python=3.12";
374+
assert!(!is_conda_env_name_in_cmd(line.to_string(), "base"));
375+
376+
line = "# cmd: /Users/donjayamanne/.pyenv/versions/mambaforge-22.11.1-3/lib/python3.10/site-packages/conda/__main__.py create --yes -p .conda python=3.12";
377+
assert!(!is_conda_env_name_in_cmd(line.to_string(), "base"));
378+
379+
line = "# cmd: /Users/donjayamanne/.pyenv/versions/mambaforge-22.11.1-3/lib/python3.10/site-packages/conda/__main__.py create --yes -p .conda python=3.12";
380+
assert!(!is_conda_env_name_in_cmd(line.to_string(), ".conda"));
381+
}
352382
}

0 commit comments

Comments
 (0)