|
1 | 1 | import os
|
2 | 2 | import tempfile
|
3 | 3 | import unittest
|
4 |
| - |
| 4 | +from unittest.mock import patch |
| 5 | +import sys |
5 | 6 | import pytest
|
| 7 | +from io import StringIO |
6 | 8 |
|
7 | 9 | import autogen
|
8 | 10 | from autogen.code_utils import (
|
|
11 | 13 | execute_code,
|
12 | 14 | extract_code,
|
13 | 15 | improve_code,
|
| 16 | + get_powershell_command, |
14 | 17 | improve_function,
|
15 | 18 | infer_lang,
|
16 | 19 | is_docker_running,
|
@@ -553,6 +556,41 @@ def test_non_dict_in_list(self):
|
553 | 556 | content_str(content)
|
554 | 557 |
|
555 | 558 |
|
| 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 | + |
556 | 594 | if __name__ == "__main__":
|
557 | 595 | # test_infer_lang()
|
558 | 596 | test_extract_code()
|
|
0 commit comments