From d82c6de8dbb3b870cc424cedf8a2741b3be92b22 Mon Sep 17 00:00:00 2001 From: Abhay Mathur Date: Fri, 23 Feb 2024 09:11:33 +0530 Subject: [PATCH] Updating code_utils.py to solve issue #1747 (#1758) * Update code_utils.py Updated the powershell command to pwsh * Update code_utils.py added a split to handle powershell in the first condition as well * Update local_commandline_code_executor.py added "pwsh" as a command option in lang variable * Update autogen/coding/local_commandline_code_executor.py Co-authored-by: Eric Zhu * Update code_utils.py * Update code_utils.py fixed formatting * Update code_utils.py defined a function to detect whether 'powershell' or 'pwsh' works and accordingly use the one that works * Update code_utils.py fixed formatting * Update and rename test_code.py to test_code_utils.py added a unit test for get_powershell_command function in code_utils.py * Update test_code_utils.py fixed formatting * Update test_code_utils.py fixed formatting * Update autogen/code_utils.py Co-authored-by: Chi Wang * solved issue #1747 * updated unit test * fixed formatting * fixed formatting * fixed formatting * fixed a bug * removed extra return None and removed redundant comments --------- Co-authored-by: Eric Zhu Co-authored-by: Chi Wang --- autogen/code_utils.py | 3 ++- test/test_code_utils.py | 22 +++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/autogen/code_utils.py b/autogen/code_utils.py index 3a7c67c136d6..4abd28449251 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 74efd9ddc222..be6c611661f4 100644 --- a/test/test_code_utils.py +++ b/test/test_code_utils.py @@ -575,20 +575,24 @@ def test_get_powershell_command_pwsh(self, mock_subprocess_run): 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__":