You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_commandline_code_executor.py 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # File based from: https://github.com/microsoft/autogen/blob/main/test/coding/test_commandline_code_executor.py
  2. # Credit to original authors
  3. import sys
  4. import tempfile
  5. from pathlib import Path
  6. import pytest
  7. from agnext.components.code_executor import LocalCommandLineCodeExecutor, CodeBlock
  8. UNIX_SHELLS = ["bash", "sh", "shell"]
  9. WINDOWS_SHELLS = ["ps1", "pwsh", "powershell"]
  10. PYTHON_VARIANTS = ["python", "Python", "py"]
  11. def test_execute_code() -> None:
  12. with tempfile.TemporaryDirectory() as temp_dir:
  13. executor = LocalCommandLineCodeExecutor(work_dir=temp_dir)
  14. # Test single code block.
  15. code_blocks = [CodeBlock(code="import sys; print('hello world!')", language="python")]
  16. code_result = executor.execute_code_blocks(code_blocks)
  17. assert code_result.exit_code == 0 and "hello world!" in code_result.output and code_result.code_file is not None
  18. # Test multiple code blocks.
  19. code_blocks = [
  20. CodeBlock(code="import sys; print('hello world!')", language="python"),
  21. CodeBlock(code="a = 100 + 100; print(a)", language="python"),
  22. ]
  23. code_result = executor.execute_code_blocks(code_blocks)
  24. assert (
  25. code_result.exit_code == 0
  26. and "hello world!" in code_result.output
  27. and "200" in code_result.output
  28. and code_result.code_file is not None
  29. )
  30. # Test bash script.
  31. if sys.platform not in ["win32"]:
  32. code_blocks = [CodeBlock(code="echo 'hello world!'", language="bash")]
  33. code_result = executor.execute_code_blocks(code_blocks)
  34. assert code_result.exit_code == 0 and "hello world!" in code_result.output and code_result.code_file is not None
  35. # Test running code.
  36. file_lines = ["import sys", "print('hello world!')", "a = 100 + 100", "print(a)"]
  37. code_blocks = [CodeBlock(code="\n".join(file_lines), language="python")]
  38. code_result = executor.execute_code_blocks(code_blocks)
  39. assert (
  40. code_result.exit_code == 0
  41. and "hello world!" in code_result.output
  42. and "200" in code_result.output
  43. and code_result.code_file is not None
  44. )
  45. # Check saved code file.
  46. with open(code_result.code_file) as f:
  47. code_lines = f.readlines()
  48. for file_line, code_line in zip(file_lines, code_lines):
  49. assert file_line.strip() == code_line.strip()
  50. def test_commandline_code_executor_timeout() -> None:
  51. with tempfile.TemporaryDirectory() as temp_dir:
  52. executor = LocalCommandLineCodeExecutor(timeout=1, work_dir=temp_dir)
  53. code_blocks = [CodeBlock(code="import time; time.sleep(10); print('hello world!')", language="python")]
  54. code_result = executor.execute_code_blocks(code_blocks)
  55. assert code_result.exit_code and "Timeout" in code_result.output
  56. def test_local_commandline_code_executor_restart() -> None:
  57. executor = LocalCommandLineCodeExecutor()
  58. with pytest.warns(UserWarning, match=r".*No action is taken."):
  59. executor.restart()
  60. def test_invalid_relative_path() -> None:
  61. executor = LocalCommandLineCodeExecutor()
  62. code = """# filename: /tmp/test.py
  63. print("hello world")
  64. """
  65. result = executor.execute_code_blocks([CodeBlock(code=code, language="python")])
  66. assert result.exit_code == 1 and "Filename is not in the workspace" in result.output
  67. def test_valid_relative_path() -> None:
  68. with tempfile.TemporaryDirectory() as temp_dir_str:
  69. temp_dir = Path(temp_dir_str)
  70. executor = LocalCommandLineCodeExecutor(work_dir=temp_dir)
  71. code = """# filename: test.py
  72. print("hello world")
  73. """
  74. result = executor.execute_code_blocks([CodeBlock(code=code, language="python")])
  75. assert result.exit_code == 0
  76. assert "hello world" in result.output
  77. assert result.code_file is not None
  78. assert "test.py" in result.code_file
  79. assert (temp_dir / Path("test.py")).resolve() == Path(result.code_file).resolve()
  80. assert (temp_dir / Path("test.py")).exists()

This is a mirror of AutoGen from GitHub. AutoGen is a framework that enables the development of LLM applications using multiple agents that can converse with each other to solve tasks.