test_agent.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. controller = asyncio.run(main(task))
  16. asyncio.run(controller.close())
  17. # Verify the script file exists
  18. script_path = os.path.join(workspace_base, 'hello.sh')
  19. assert os.path.exists(script_path), 'The file "hello.sh" does not exist'
  20. # Run the script and capture the output
  21. result = subprocess.run(['bash', script_path], capture_output=True, text=True)
  22. # Verify the output from the script
  23. assert (
  24. result.stdout.strip() == 'hello'
  25. ), f'Expected output "hello", but got "{result.stdout.strip()}"'
  26. @pytest.mark.skipif(
  27. os.getenv('AGENT') == 'CodeActAgent'
  28. and os.getenv('SANDBOX_TYPE').lower() == 'exec',
  29. reason='CodeActAgent does not support exec sandbox since exec sandbox is NOT stateful',
  30. )
  31. @pytest.mark.skipif(
  32. os.getenv('AGENT') == 'SWEAgent',
  33. reason='SWEAgent is not capable of this task right now',
  34. )
  35. @pytest.mark.skipif(
  36. os.getenv('SANDBOX_TYPE') == 'local',
  37. reason='local sandbox shows environment-dependent absolute path for pwd command',
  38. )
  39. def test_edits():
  40. # Move workspace artifacts to workspace_base location
  41. source_dir = os.path.join(os.path.dirname(__file__), 'workspace/test_edits/')
  42. files = os.listdir(source_dir)
  43. for file in files:
  44. dest_file = os.path.join(workspace_base, file)
  45. if os.path.exists(dest_file):
  46. os.remove(dest_file)
  47. print('source = ', os.path.join(source_dir, file), ' dest = ', dest_file)
  48. shutil.copy(os.path.join(source_dir, file), dest_file)
  49. # Execute the task
  50. task = 'Fix typos in bad.txt. Do not ask me for confirmation at any point.'
  51. controller = asyncio.run(main(task))
  52. asyncio.run(controller.close())
  53. # Verify bad.txt has been fixed
  54. text = """This is a stupid typo.
  55. Really?
  56. No more typos!
  57. Enjoy!
  58. """
  59. with open(os.path.join(workspace_base, 'bad.txt'), 'r') as f:
  60. content = f.read()
  61. assert content.strip() == text.strip()