diff --git a/autogen/code_utils.py b/autogen/code_utils.py index 6f781468484..da7b1d52736 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -220,7 +220,6 @@ def get_powershell_command(): result = subprocess.run(["powershell", "$PSVersionTable.PSVersion.Major"], capture_output=True, text=True) if result.returncode == 0: return "powershell" - except (FileNotFoundError, NotADirectoryError): # This means that 'powershell' command is not found so now we try looking for 'pwsh' try: @@ -229,22 +228,30 @@ def get_powershell_command(): ) if result.returncode == 0: return "pwsh" - - except (FileNotFoundError, NotADirectoryError): - if WIN32: - logging.warning("Neither powershell nor pwsh is installed but it is a Windows OS") - return None - - -powershell_command = get_powershell_command() + except FileExistsError as e: + raise FileNotFoundError( + "Neither powershell.exe nor pwsh.exe is present in the system. " + "Please install PowerShell and try again. " + f"Original error: {e}" + ) + except NotADirectoryError as e: + raise NotADirectoryError( + "PowerShell is either not installed or its path is not given " + "properly in the environment variable PATH. Please check the " + "path and try again. " + f"Original error: {e}" + ) + except PermissionError as e: + raise PermissionError(f"No permission to run powershell. Original error: {e}") def _cmd(lang: str) -> str: - if lang.startswith("python") or lang in ["bash", "sh", powershell_command]: + if lang.startswith("python") or lang in ["bash", "sh"]: return lang if lang in ["shell"]: return "sh" if lang in ["ps1", "pwsh", "powershell"]: + powershell_command = get_powershell_command() return powershell_command raise NotImplementedError(f"{lang} not recognized in code execution") diff --git a/test/test_code_utils.py b/test/test_code_utils.py index e3909848188..1d45a9b4e6d 100755 --- a/test/test_code_utils.py +++ b/test/test_code_utils.py @@ -577,24 +577,16 @@ def test_get_powershell_command_pwsh(self, mock_subprocess_run): self.assertEqual(get_powershell_command(), "pwsh") @patch("subprocess.run") - @patch("logging.warning") - def test_get_powershell_command_windows_no_shell(self, mock_logging_warning, mock_subprocess_run): - # Set up the mock to simulate 'powershell' and 'pwsh' not found + def test_get_powershell_command_not_found(self, mock_subprocess_run): mock_subprocess_run.side_effect = [FileNotFoundError, FileNotFoundError] - - with patch("autogen.code_utils.WIN32", True): - self.assertIsNone(get_powershell_command()) - mock_logging_warning.assert_called_once_with( - "Neither powershell nor pwsh is installed but it is a Windows OS" - ) + with self.assertRaises(FileNotFoundError): + get_powershell_command() @patch("subprocess.run") - def test_get_powershell_command_no_windows_no_shell(self, mock_subprocess_run): - # Set up the mock to simulate 'powershell' and 'pwsh' not found - mock_subprocess_run.side_effect = FileNotFoundError - # Mock WIN32 to False - with patch("autogen.code_utils.WIN32", False): - self.assertIsNone(get_powershell_command()) + def test_get_powershell_command_no_permission(self, mock_subprocess_run): + mock_subprocess_run.side_effect = [PermissionError, FileNotFoundError] + with self.assertRaises(PermissionError): + get_powershell_command() if __name__ == "__main__":