Skip to content

A programming framework for agentic AI

Updating code_utils.py to solve issue #1747 (#1758)

Abhay Mathura646df2d025b
diff --git a/autogen/code_utils.py b/autogen/code_utils.py
index 3a7c67c13..4abd28449 100644
--- a/autogen/code_utils.py
+++ b/autogen/code_utils.py
@@ -229,7 +229,8 @@ def get_powershell_command():
return "pwsh"
except FileNotFoundError:
- print("Neither powershell nor pwsh is installed.")
+ if WIN32:
+ logging.warning("Neither powershell nor pwsh is installed but it is a Windows OS")
return None
diff --git a/test/test_code_utils.py b/test/test_code_utils.py
index 74efd9ddc..be6c61166 100644
--- a/test/test_code_utils.py
+++ b/test/test_code_utils.py
@@ -575,20 +575,24 @@ class TestGetPowerShellCommand(unittest.TestCase):
self.assertEqual(get_powershell_command(), "pwsh")
@patch("subprocess.run")
- def test_get_powershell_command_no_shell(self, mock_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
mock_subprocess_run.side_effect = [FileNotFoundError, FileNotFoundError]
- with patch("sys.stdout", new=StringIO()) as fake_out:
- get_powershell_command()
- self.assertEqual(fake_out.getvalue().strip(), "Neither powershell nor pwsh is installed.")
+ 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"
+ )
@patch("subprocess.run")
- def test_get_powershell_command_no_shell_no_output(self, mock_subprocess_run):
- # Set up the mock to simulate 'powershell' and 'pwsh' not found without printing error message
- mock_subprocess_run.side_effect = [FileNotFoundError, FileNotFoundError]
-
- self.assertIsNone(get_powershell_command())
+ 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())
if __name__ == "__main__":