test_agent.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import asyncio
  2. import os
  3. import shutil
  4. import subprocess
  5. import pytest
  6. from opendevin.core.main import main
  7. workspace_base = os.getenv('WORKSPACE_BASE')
  8. @pytest.mark.skipif(
  9. os.getenv('AGENT') == 'CodeActAgent'
  10. and os.getenv('SANDBOX_TYPE').lower() == 'exec',
  11. reason='CodeActAgent does not support exec sandbox since exec sandbox is NOT stateful',
  12. )
  13. def test_write_simple_script():
  14. task = "Write a shell script 'hello.sh' that prints 'hello'. Do not ask me for confirmation at any point."
  15. asyncio.run(main(task))
  16. # Verify the script file exists
  17. script_path = os.path.join(workspace_base, 'hello.sh')
  18. assert os.path.exists(script_path), 'The file "hello.sh" does not exist'
  19. # Run the script and capture the output
  20. result = subprocess.run(['bash', script_path], capture_output=True, text=True)
  21. # Verify the output from the script
  22. assert (
  23. result.stdout.strip() == 'hello'
  24. ), f'Expected output "hello", but got "{result.stdout.strip()}"'
  25. @pytest.mark.skipif(
  26. os.getenv('AGENT') == 'CodeActAgent'
  27. and os.getenv('SANDBOX_TYPE').lower() == 'exec',
  28. reason='CodeActAgent does not support exec sandbox since exec sandbox is NOT stateful',
  29. )
  30. @pytest.mark.skipif(
  31. os.getenv('AGENT') == 'SWEAgent',
  32. reason='SWEAgent is not capable of this task right now',
  33. )
  34. @pytest.mark.skipif(
  35. os.getenv('SANDBOX_TYPE') == 'local',
  36. reason='local sandbox shows environment-dependent absolute path for pwd command',
  37. )
  38. def test_edits():
  39. # Move workspace artifacts to workspace_base location
  40. source_dir = os.path.join(os.path.dirname(__file__), 'workspace/test_edits/')
  41. files = os.listdir(source_dir)
  42. for file in files:
  43. dest_file = os.path.join(workspace_base, file)
  44. if os.path.exists(dest_file):
  45. os.remove(dest_file)
  46. shutil.copy(os.path.join(source_dir, file), dest_file)
  47. # Execute the task
  48. task = 'Fix typos in bad.txt. Do not ask me for confirmation at any point.'
  49. asyncio.run(main(task))
  50. # Verify bad.txt has been fixed
  51. text = """This is a stupid typo.
  52. Really?
  53. No more typos!
  54. Enjoy!
  55. """
  56. with open(os.path.join(workspace_base, 'bad.txt'), 'r') as f:
  57. content = f.read()
  58. assert content.strip() == text.strip()
  59. @pytest.mark.skipif(
  60. os.getenv('AGENT') != 'CodeActAgent',
  61. reason='currently only CodeActAgent defaults to have IPython (Jupyter) execution',
  62. )
  63. @pytest.mark.skipif(
  64. os.getenv('SANDBOX_TYPE') != 'ssh',
  65. reason='Currently, only ssh sandbox supports stateful tasks',
  66. )
  67. def test_ipython():
  68. # Execute the task
  69. task = "Use Jupyter IPython to write a text file containing 'hello world' to '/workspace/test.txt'. Do not ask me for confirmation at any point."
  70. controller = asyncio.run(main(task))
  71. asyncio.run(controller.close())
  72. # Verify the file exists
  73. file_path = os.path.join(workspace_base, 'test.txt')
  74. assert os.path.exists(file_path), 'The file "test.txt" does not exist'
  75. # Verify the file contains the expected content
  76. with open(file_path, 'r') as f:
  77. content = f.read()
  78. assert (
  79. content.strip() == 'hello world'
  80. ), f'Expected content "hello world", but got "{content.strip()}"'