Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code_utils.py powershell command bugfix in MacOS #1963

Merged
merged 21 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
ekzhu marked this conversation as resolved.
Show resolved Hide resolved
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
Loading