@@ -653,4 +653,88 @@ mod tests {
653653 "canonicalize() would resolve to target, but path() does not"
654654 ) ;
655655 }
656+
657+ /// Test for https://github.com/microsoft/python-environment-tools/issues/151
658+ /// Verifies that refresh with searchKind (e.g., "Venv") still finds environments
659+ /// in workspace directories, not just global locations.
660+ ///
661+ /// The bug was that when searchKind was provided, workspace_directories was cleared,
662+ /// preventing discovery of workspace-based environments like venvs.
663+ #[ test]
664+ fn test_search_kind_finds_workspace_venvs ( ) {
665+ use super :: { find_and_report_envs, SearchScope } ;
666+ use crate :: locators:: create_locators;
667+ use pet_conda:: Conda ;
668+ use pet_core:: os_environment:: EnvironmentApi ;
669+ use pet_core:: python_environment:: PythonEnvironmentKind ;
670+ use pet_core:: Configuration ;
671+ use pet_poetry:: Poetry ;
672+ use pet_reporter:: collect;
673+ use std:: sync:: Arc ;
674+
675+ let tmp = TempDir :: new ( ) . expect ( "Failed to create temp dir" ) ;
676+ let workspace = tmp. path ( ) . to_path_buf ( ) ;
677+
678+ // Create a venv structure in the workspace
679+ let venv_dir = workspace. join ( ".venv" ) ;
680+ #[ cfg( windows) ]
681+ let bin_dir = venv_dir. join ( "Scripts" ) ;
682+ #[ cfg( unix) ]
683+ let bin_dir = venv_dir. join ( "bin" ) ;
684+ fs:: create_dir_all ( & bin_dir) . expect ( "Failed to create bin dir" ) ;
685+
686+ // Create pyvenv.cfg (required for venv detection)
687+ fs:: write (
688+ venv_dir. join ( "pyvenv.cfg" ) ,
689+ "home = /usr/bin\n version = 3.11.0\n " ,
690+ )
691+ . expect ( "Failed to create pyvenv.cfg" ) ;
692+
693+ // Create python executable
694+ #[ cfg( windows) ]
695+ let python_exe = bin_dir. join ( "python.exe" ) ;
696+ #[ cfg( unix) ]
697+ let python_exe = bin_dir. join ( "python" ) ;
698+ fs:: write ( & python_exe, "fake python" ) . expect ( "Failed to create python exe" ) ;
699+
700+ // Set up locators and configuration
701+ let environment = EnvironmentApi :: new ( ) ;
702+ let conda_locator = Arc :: new ( Conda :: from ( & environment) ) ;
703+ let poetry_locator = Arc :: new ( Poetry :: from ( & environment) ) ;
704+ let locators = create_locators ( conda_locator, poetry_locator, & environment) ;
705+
706+ let config = Configuration {
707+ workspace_directories : Some ( vec ! [ workspace. clone( ) ] ) ,
708+ ..Default :: default ( )
709+ } ;
710+ for locator in locators. iter ( ) {
711+ locator. configure ( & config) ;
712+ }
713+
714+ let reporter = Arc :: new ( collect:: create_reporter ( ) ) ;
715+
716+ // Call find_and_report_envs with SearchScope::Global(Venv)
717+ // This simulates the behavior when refresh is called with searchKind: "Venv"
718+ find_and_report_envs (
719+ reporter. as_ref ( ) ,
720+ config,
721+ & locators,
722+ & environment,
723+ Some ( SearchScope :: Global ( PythonEnvironmentKind :: Venv ) ) ,
724+ ) ;
725+
726+ let environments = reporter. environments . lock ( ) . unwrap ( ) ;
727+
728+ // The venv should be discovered even when searching by kind
729+ let venv_found = environments. iter ( ) . any ( |env| {
730+ env. kind == Some ( PythonEnvironmentKind :: Venv )
731+ && env. prefix . as_ref ( ) . map ( |p| p == & venv_dir) . unwrap_or ( false )
732+ } ) ;
733+
734+ assert ! (
735+ venv_found,
736+ "Venv in workspace should be found when searching by kind. Found environments: {:?}" ,
737+ * environments
738+ ) ;
739+ }
656740}
0 commit comments