Skip to content

Commit

Permalink
code_utils.py powershell command bugfix in MacOS (microsoft#1963)
Browse files Browse the repository at this point in the history
* macos bugfix

* logs permissionerror warning only if lang is powershell command

* condensed the if statements down

* Update code_utils.py

* fixed formatting

* handled powershell_command = None case

* bugfix

* raising exceptions instead of logging warnings

* code formatting fixed

* removed return sh statement

* fixed code formatting

* update get_powershell_command

* Update code_utils.py

fixed code format

---------

Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com>
  • Loading branch information
abhaymathur21 and ekzhu committed Mar 18, 2024
1 parent 9b56ce5 commit 9fff924
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
27 changes: 17 additions & 10 deletions autogen/code_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,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:
Expand All @@ -227,22 +226,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):
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")
Expand Down
22 changes: 7 additions & 15 deletions test/test_code_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down

0 comments on commit 9fff924

Please sign in to comment.