@@ -388,4 +388,67 @@ mod tests {
388388 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" ;
389389 assert ! ( !is_conda_env_name_in_cmd( line. to_string( ) , ".conda" ) ) ;
390390 }
391+
392+ /// Test that path-based conda environments (created with --prefix) return None for name
393+ /// when conda_dir is unknown. This ensures VS Code uses path-based activation.
394+ /// Regression test for https://github.com/microsoft/python-environment-tools/issues/329
395+ #[ test]
396+ fn path_based_env_returns_none_name_when_conda_dir_unknown ( ) {
397+ // Create a temp directory structure simulating a path-based conda env
398+ let temp_dir = std:: env:: temp_dir ( ) . join ( "pet_test_path_based_env" ) ;
399+ let conda_meta_dir = temp_dir. join ( ".conda" ) . join ( "conda-meta" ) ;
400+ std:: fs:: create_dir_all ( & conda_meta_dir) . unwrap ( ) ;
401+
402+ // Write a history file showing the env was created with --prefix (path-based)
403+ let history_file = conda_meta_dir. join ( "history" ) ;
404+ std:: fs:: write (
405+ & history_file,
406+ "# cmd: /usr/bin/conda create --yes --prefix .conda python=3.12\n " ,
407+ )
408+ . unwrap ( ) ;
409+
410+ let env_path = temp_dir. join ( ".conda" ) ;
411+
412+ // When conda_dir is None and env was created with --prefix, name should be None
413+ let name = get_conda_env_name ( & env_path, & env_path, & None ) ;
414+ assert ! (
415+ name. is_none( ) ,
416+ "Path-based env should return None for name, got {:?}" ,
417+ name
418+ ) ;
419+
420+ // Cleanup
421+ let _ = std:: fs:: remove_dir_all ( & temp_dir) ;
422+ }
423+
424+ /// Test that name-based conda environments (created with -n) return the name
425+ /// even when conda_dir is unknown.
426+ #[ test]
427+ fn name_based_env_returns_name_when_conda_dir_unknown ( ) {
428+ // Create a temp directory structure simulating a name-based conda env
429+ let temp_dir = std:: env:: temp_dir ( ) . join ( "pet_test_name_based_env" ) ;
430+ let conda_meta_dir = temp_dir. join ( "myenv" ) . join ( "conda-meta" ) ;
431+ std:: fs:: create_dir_all ( & conda_meta_dir) . unwrap ( ) ;
432+
433+ // Write a history file showing the env was created with -n (name-based)
434+ let history_file = conda_meta_dir. join ( "history" ) ;
435+ std:: fs:: write (
436+ & history_file,
437+ "# cmd: /usr/bin/conda create -n myenv python=3.12\n " ,
438+ )
439+ . unwrap ( ) ;
440+
441+ let env_path = temp_dir. join ( "myenv" ) ;
442+
443+ // When conda_dir is None but env was created with -n, name should be returned
444+ let name = get_conda_env_name ( & env_path, & env_path, & None ) ;
445+ assert_eq ! (
446+ name,
447+ Some ( "myenv" . to_string( ) ) ,
448+ "Name-based env should return the name"
449+ ) ;
450+
451+ // Cleanup
452+ let _ = std:: fs:: remove_dir_all ( & temp_dir) ;
453+ }
391454}
0 commit comments