Skip to content

Commit 2750391

Browse files
abhaymathur21ekzhusonichi
authored
Updated code_utils.py & local_commandline_code_executor.py (powershell to pwsh) (#1710)
* 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]> --------- Co-authored-by: Eric Zhu <[email protected]> Co-authored-by: Chi Wang <[email protected]>
1 parent c7d9fef commit 2750391

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

autogen/code_utils.py

+27-3
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,37 @@ def timeout_handler(signum, frame):
213213
raise TimeoutError("Timed out!")
214214

215215

216+
def get_powershell_command():
217+
try:
218+
result = subprocess.run(["powershell", "$PSVersionTable.PSVersion.Major"], capture_output=True, text=True)
219+
if result.returncode == 0:
220+
return "powershell"
221+
222+
except FileNotFoundError:
223+
# This means that 'powershell' command is not found so now we try looking for 'pwsh'
224+
try:
225+
result = subprocess.run(
226+
["pwsh", "-Command", "$PSVersionTable.PSVersion.Major"], capture_output=True, text=True
227+
)
228+
if result.returncode == 0:
229+
return "pwsh"
230+
231+
except FileNotFoundError:
232+
print("Neither powershell nor pwsh is installed.")
233+
return None
234+
235+
236+
powershell_command = get_powershell_command()
237+
238+
216239
def _cmd(lang):
217-
if lang.startswith("python") or lang in ["bash", "sh", "powershell"]:
240+
if lang.startswith("python") or lang in ["bash", "sh", powershell_command]:
218241
return lang
219242
if lang in ["shell"]:
220243
return "sh"
221-
if lang in ["ps1"]:
222-
return "powershell"
244+
if lang in ["ps1", "pwsh", "powershell"]:
245+
return powershell_command
246+
223247
raise NotImplementedError(f"{lang} not recognized in code execution")
224248

225249

autogen/coding/local_commandline_code_executor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def execute_code_blocks(self, code_blocks: List[CodeBlock]) -> CommandlineCodeRe
156156
)
157157
filename_uuid = uuid.uuid4().hex
158158
filename = None
159-
if lang in ["bash", "shell", "sh"]:
159+
if lang in ["bash", "shell", "sh", "pwsh", "powershell", "ps1"]:
160160
filename = f"{filename_uuid}.{lang}"
161161
exitcode, logs, _ = execute_code(
162162
code=code,

test/test_code_utils.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import os
22
import tempfile
33
import unittest
4-
4+
from unittest.mock import patch
5+
import sys
56
import pytest
7+
from io import StringIO
68

79
import autogen
810
from autogen.code_utils import (
@@ -11,6 +13,7 @@
1113
execute_code,
1214
extract_code,
1315
improve_code,
16+
get_powershell_command,
1417
improve_function,
1518
infer_lang,
1619
is_docker_running,
@@ -553,6 +556,41 @@ def test_non_dict_in_list(self):
553556
content_str(content)
554557

555558

559+
class TestGetPowerShellCommand(unittest.TestCase):
560+
@patch("subprocess.run")
561+
def test_get_powershell_command_powershell(self, mock_subprocess_run):
562+
# Set up the mock to return a successful result for 'powershell'
563+
mock_subprocess_run.return_value.returncode = 0
564+
mock_subprocess_run.return_value.stdout = StringIO("5")
565+
566+
self.assertEqual(get_powershell_command(), "powershell")
567+
568+
@patch("subprocess.run")
569+
def test_get_powershell_command_pwsh(self, mock_subprocess_run):
570+
# Set up the mock to return a successful result for 'pwsh'
571+
mock_subprocess_run.side_effect = [FileNotFoundError, mock_subprocess_run.return_value]
572+
mock_subprocess_run.return_value.returncode = 0
573+
mock_subprocess_run.return_value.stdout = StringIO("7")
574+
575+
self.assertEqual(get_powershell_command(), "pwsh")
576+
577+
@patch("subprocess.run")
578+
def test_get_powershell_command_no_shell(self, mock_subprocess_run):
579+
# Set up the mock to simulate 'powershell' and 'pwsh' not found
580+
mock_subprocess_run.side_effect = [FileNotFoundError, FileNotFoundError]
581+
582+
with patch("sys.stdout", new=StringIO()) as fake_out:
583+
get_powershell_command()
584+
self.assertEqual(fake_out.getvalue().strip(), "Neither powershell nor pwsh is installed.")
585+
586+
@patch("subprocess.run")
587+
def test_get_powershell_command_no_shell_no_output(self, mock_subprocess_run):
588+
# Set up the mock to simulate 'powershell' and 'pwsh' not found without printing error message
589+
mock_subprocess_run.side_effect = [FileNotFoundError, FileNotFoundError]
590+
591+
self.assertIsNone(get_powershell_command())
592+
593+
556594
if __name__ == "__main__":
557595
# test_infer_lang()
558596
test_extract_code()

0 commit comments

Comments
 (0)