Skip to content

Commit c508fb0

Browse files
committed
test: add unit tests for conda environment name retrieval in various scenarios
1 parent a8f0352 commit c508fb0

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

crates/pet-conda/src/environments.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,4 +498,83 @@ mod tests {
498498
// Cleanup
499499
let _ = std::fs::remove_dir_all(&temp_dir);
500500
}
501+
502+
/// Test that environments under conda_dir/envs/ return the folder name.
503+
/// This is the most common case for named conda environments.
504+
#[test]
505+
fn env_under_conda_dir_returns_folder_name() {
506+
// Create a temp directory simulating conda_dir/envs/myenv structure
507+
let temp_dir = std::env::temp_dir().join("pet_test_env_under_conda");
508+
let conda_dir = temp_dir.join("miniconda3");
509+
let env_path = conda_dir.join("envs").join("myenv");
510+
let conda_meta_dir = env_path.join("conda-meta");
511+
std::fs::create_dir_all(&conda_meta_dir).unwrap();
512+
513+
// When env is under conda_dir/envs/, name should be the folder name
514+
let name = get_conda_env_name(&env_path, &env_path, &Some(conda_dir));
515+
assert_eq!(
516+
name,
517+
Some("myenv".to_string()),
518+
"Env under conda_dir/envs/ should return folder name"
519+
);
520+
521+
// Cleanup
522+
let _ = std::fs::remove_dir_all(&temp_dir);
523+
}
524+
525+
/// Test that external env with no history file returns None for name.
526+
/// This ensures safe path-based activation when we can't determine how it was created.
527+
#[test]
528+
fn external_env_without_history_returns_none_name() {
529+
// Create a temp directory simulating an external conda env without history
530+
let temp_dir = std::env::temp_dir().join("pet_test_external_no_history");
531+
let conda_meta_dir = temp_dir.join("myenv").join("conda-meta");
532+
std::fs::create_dir_all(&conda_meta_dir).unwrap();
533+
// Note: NOT creating a history file
534+
535+
let env_path = temp_dir.join("myenv");
536+
// conda_dir is known but env is NOT under it (external environment)
537+
let conda_dir = Some(std::path::PathBuf::from("/some/other/conda"));
538+
539+
let name = get_conda_env_name(&env_path, &env_path, &conda_dir);
540+
assert!(
541+
name.is_none(),
542+
"External env without history should return None for safe path-based activation, got {:?}",
543+
name
544+
);
545+
546+
// Cleanup
547+
let _ = std::fs::remove_dir_all(&temp_dir);
548+
}
549+
550+
/// Test that external env with history but no -n/-p flags returns None.
551+
/// Edge case: history exists but create command doesn't clearly indicate name or path based.
552+
#[test]
553+
fn external_env_with_ambiguous_history_returns_none_name() {
554+
// Create a temp directory simulating an external conda env
555+
let temp_dir = std::env::temp_dir().join("pet_test_external_ambiguous");
556+
let conda_meta_dir = temp_dir.join("myenv").join("conda-meta");
557+
std::fs::create_dir_all(&conda_meta_dir).unwrap();
558+
559+
// Write a history file without -n or -p (edge case, maybe cloned env)
560+
let history_file = conda_meta_dir.join("history");
561+
std::fs::write(
562+
&history_file,
563+
"# some other history content\n# no create command here\n",
564+
)
565+
.unwrap();
566+
567+
let env_path = temp_dir.join("myenv");
568+
let conda_dir = Some(std::path::PathBuf::from("/some/other/conda"));
569+
570+
let name = get_conda_env_name(&env_path, &env_path, &conda_dir);
571+
assert!(
572+
name.is_none(),
573+
"External env with ambiguous history should return None, got {:?}",
574+
name
575+
);
576+
577+
// Cleanup
578+
let _ = std::fs::remove_dir_all(&temp_dir);
579+
}
501580
}

0 commit comments

Comments
 (0)