|
1 | 1 | from pathlib import Path
|
2 | 2 | import sys
|
3 | 3 | import tempfile
|
| 4 | +import uuid |
4 | 5 | import pytest
|
5 | 6 | from autogen.agentchat.conversable_agent import ConversableAgent
|
6 | 7 | from autogen.code_utils import is_docker_running
|
7 | 8 | from autogen.coding.base import CodeBlock, CodeExecutor
|
8 | 9 | from autogen.coding.factory import CodeExecutorFactory
|
9 | 10 | from autogen.coding.docker_commandline_code_executor import DockerCommandLineCodeExecutor
|
10 | 11 | from autogen.coding.local_commandline_code_executor import LocalCommandLineCodeExecutor
|
11 |
| -from autogen.oai.openai_utils import config_list_from_json |
12 | 12 |
|
13 |
| -from conftest import MOCK_OPEN_AI_API_KEY, skip_openai, skip_docker |
| 13 | +from conftest import MOCK_OPEN_AI_API_KEY, skip_docker |
14 | 14 |
|
15 | 15 | if skip_docker or not is_docker_running():
|
16 | 16 | classes_to_test = [LocalCommandLineCodeExecutor]
|
17 | 17 | else:
|
18 | 18 | classes_to_test = [LocalCommandLineCodeExecutor, DockerCommandLineCodeExecutor]
|
19 | 19 |
|
| 20 | +UNIX_SHELLS = ["bash", "sh", "shell"] |
| 21 | +WINDOWS_SHELLS = ["ps1", "pwsh", "powershell"] |
| 22 | + |
20 | 23 |
|
21 | 24 | @pytest.mark.parametrize("cls", classes_to_test)
|
22 | 25 | def test_is_code_executor(cls) -> None:
|
@@ -218,3 +221,31 @@ def test_valid_relative_path(cls) -> None:
|
218 | 221 | assert "test.py" in result.code_file
|
219 | 222 | assert (temp_dir / "test.py").resolve() == Path(result.code_file).resolve()
|
220 | 223 | assert (temp_dir / "test.py").exists()
|
| 224 | + |
| 225 | + |
| 226 | +@pytest.mark.parametrize("cls", classes_to_test) |
| 227 | +@pytest.mark.parametrize("lang", WINDOWS_SHELLS + UNIX_SHELLS) |
| 228 | +def test_silent_pip_install(cls, lang: str) -> None: |
| 229 | + # Ensure that the shell is supported. |
| 230 | + lang = "ps1" if lang in ["powershell", "pwsh"] else lang |
| 231 | + |
| 232 | + if sys.platform in ["win32"] and lang in UNIX_SHELLS: |
| 233 | + pytest.skip("Linux shells are not supported on Windows.") |
| 234 | + elif sys.platform not in ["win32"] and lang in WINDOWS_SHELLS: |
| 235 | + pytest.skip("Windows shells are not supported on Unix.") |
| 236 | + |
| 237 | + error_exit_code = 0 if sys.platform in ["win32"] else 1 |
| 238 | + |
| 239 | + executor = cls(timeout=600) |
| 240 | + |
| 241 | + code = "pip install matplotlib numpy" |
| 242 | + code_blocks = [CodeBlock(code=code, language=lang)] |
| 243 | + code_result = executor.execute_code_blocks(code_blocks) |
| 244 | + assert code_result.exit_code == 0 and code_result.output.strip() == "" |
| 245 | + |
| 246 | + none_existing_package = uuid.uuid4().hex |
| 247 | + |
| 248 | + code = f"pip install matplotlib_{none_existing_package}" |
| 249 | + code_blocks = [CodeBlock(code=code, language=lang)] |
| 250 | + code_result = executor.execute_code_blocks(code_blocks) |
| 251 | + assert code_result.exit_code == error_exit_code and "ERROR: " in code_result.output |
0 commit comments