Skip to content

Commit

Permalink
Updating code_utils.py to solve issue microsoft#1747 (microsoft#1758)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>

* solved issue microsoft#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 <[email protected]>
Co-authored-by: Chi Wang <[email protected]>
  • Loading branch information
3 people authored Feb 23, 2024
1 parent bf69f15 commit d82c6de
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
3 changes: 2 additions & 1 deletion autogen/code_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
22 changes: 13 additions & 9 deletions test/test_code_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down

0 comments on commit d82c6de

Please sign in to comment.