coding-alt/AutoGPT
0
1"""Smoke test for the autogpt package."""2import os3import subprocess4import sys5 6import pytest7 8from autogpt.commands.file_operations import delete_file, read_file9 10 11@pytest.mark.integration_test12def test_write_file() -> None:13 """14 Test case to check if the write_file command can successfully write 'Hello World' to a file15 named 'hello_world.txt'.16 17 Read the current ai_settings.yaml file and store its content.18 """19 env_vars = {"MEMORY_BACKEND": "no_memory", "TEMPERATURE": "0"}20 ai_settings = None21 if os.path.exists("ai_settings.yaml"):22 with open("ai_settings.yaml", "r") as f:23 ai_settings = f.read()24 os.remove("ai_settings.yaml")25 26 try:27 if os.path.exists("hello_world.txt"):28 # Clean up any existing 'hello_world.txt' file before testing.29 delete_file("hello_world.txt")30 # Prepare input data for the test.31 input_data = """write_file-GPT32an AI designed to use the write_file command to write 'Hello World' into a file named "hello_world.txt" and then use the task_complete command to complete the task.33Use the write_file command to write 'Hello World' into a file named "hello_world.txt".34Use the task_complete command to complete the task.35Do not use any other commands.36 37y -538EOF"""39 command = f"{sys.executable} -m autogpt"40 41 # Execute the script with the input data.42 process = subprocess.Popen(43 command,44 stdin=subprocess.PIPE,45 shell=True,46 env={**os.environ, **env_vars},47 )48 process.communicate(input_data.encode())49 50 # Read the content of the 'hello_world.txt' file created during the test.51 content = read_file("hello_world.txt")52 finally:53 if ai_settings:54 # Restore the original ai_settings.yaml file.55 with open("ai_settings.yaml", "w") as f:56 f.write(ai_settings)57 58 # Check if the content of the 'hello_world.txt' file is equal to 'Hello World'.59 assert content == "Hello World", f"Expected 'Hello World', got {content}"60 