Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(agent, forge): Move relevant tests from autogpt to forge #7247

Merged
merged 15 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "autogpt/tests/vcr_cassettes"]
path = autogpt/tests/vcr_cassettes
[submodule "forge/tests/vcr_cassettes"]
path = forge/tests/vcr_cassettes
url = https://github.com/Significant-Gravitas/Auto-GPT-test-cassettes
2 changes: 1 addition & 1 deletion autogpt/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions autogpt/pyproject.toml
Pwuts marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ pytest-integration = "*"
pytest-mock = "*"
pytest-recording = "*"
pytest-xdist = "*"
vcrpy = { git = "https://github.com/Significant-Gravitas/vcrpy.git", rev = "master" }


[build-system]
Expand All @@ -111,4 +110,4 @@ ignore = ["../forge/**"]


[tool.pytest.ini_options]
markers = ["slow", "requires_openai_api_key", "requires_huggingface_api_key"]
markers = ["slow"]
Pwuts marked this conversation as resolved.
Show resolved Hide resolved
1 change: 0 additions & 1 deletion autogpt/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

pytest_plugins = [
"tests.integration.agent_factory",
"tests.vcr",
]


Expand Down
34 changes: 34 additions & 0 deletions forge/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import uuid
from pathlib import Path

import pytest

from forge.file_storage.base import FileStorage, FileStorageConfiguration
from forge.file_storage.local import LocalFileStorage

pytest_plugins = [
"tests.vcr",
]


@pytest.fixture()
def tmp_project_root(tmp_path: Path) -> Path:
return tmp_path


@pytest.fixture()
def app_data_dir(tmp_project_root: Path) -> Path:
dir = tmp_project_root / "data"
dir.mkdir(parents=True, exist_ok=True)
return dir


@pytest.fixture()
def storage(app_data_dir: Path) -> FileStorage:
storage = LocalFileStorage(
FileStorageConfiguration(
root=Path(f"{app_data_dir}/{str(uuid.uuid4())}"), restrict_to_root=False
)
)
storage.initialize()
return storage
4 changes: 2 additions & 2 deletions forge/forge/agent_protocol/agent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@


@pytest.fixture
def agent(test_workspace: Path):
def agent(tmp_project_root: Path):
db = AgentDB("sqlite:///test.db")
config = FileStorageConfiguration(root=test_workspace)
config = FileStorageConfiguration(root=tmp_project_root)
workspace = LocalFileStorage(config)
return ProtocolAgent(db, workspace)

Expand Down
Empty file.
11 changes: 8 additions & 3 deletions forge/forge/components/code_executor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from .code_executor import CodeExecutionError, CodeExecutorComponent
from .code_executor import (
CodeExecutionError,
CodeExecutorComponent,
is_docker_available,
we_are_running_in_a_docker_container,
)

__all__ = [
"ALLOWLIST_CONTROL",
"DENYLIST_CONTROL",
"we_are_running_in_a_docker_container",
"is_docker_available",
"CodeExecutionError",
Pwuts marked this conversation as resolved.
Show resolved Hide resolved
"CodeExecutorComponent",
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
from pathlib import Path

import pytest
from forge.components.code_executor.code_executor import (

from forge.file_storage.base import FileStorage
from forge.utils.exceptions import InvalidArgumentError, OperationNotAllowedError

from . import (
CodeExecutorComponent,
is_docker_available,
we_are_running_in_a_docker_container,
)
from forge.file_storage.base import FileStorage
from forge.utils.exceptions import InvalidArgumentError, OperationNotAllowedError


@pytest.fixture
Expand Down Expand Up @@ -103,6 +105,22 @@ def test_execute_python_file_not_found(code_executor_component: CodeExecutorComp
code_executor_component.execute_python_file(Path("notexist.py"))


def test_execute_shell(
code_executor_component: CodeExecutorComponent, random_string: str
):
code_executor_component.config.shell_command_control = "allowlist"
code_executor_component.config.shell_allowlist = ["echo"]
result = code_executor_component.execute_shell(f"echo 'Hello {random_string}!'")
assert f"Hello {random_string}!" in result


def test_execute_shell_local_commands_not_allowed(
code_executor_component: CodeExecutorComponent, random_string: str
):
with pytest.raises(OperationNotAllowedError, match="not allowed"):
code_executor_component.execute_shell(f"echo 'Hello {random_string}!'")


def test_execute_shell_denylist_should_deny(
code_executor_component: CodeExecutorComponent, random_string: str
):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
from pathlib import Path

import pytest

from forge.agent.base import BaseAgentSettings
from forge.file_storage import FileStorage

from autogpt.agents.agent import Agent
from . import FileManagerComponent


@pytest.fixture()
Expand All @@ -13,8 +15,13 @@ def file_content():


@pytest.fixture
def file_manager_component(agent: Agent):
return agent.file_manager
def file_manager_component(storage: FileStorage):
return FileManagerComponent(
storage,
BaseAgentSettings(
agent_id="TestAgent", name="TestAgent", description="Test Agent description"
),
)


@pytest.fixture()
Expand All @@ -41,36 +48,35 @@ def test_nested_file(storage: FileStorage):
async def test_read_file(
test_file_path: Path,
file_content,
file_manager_component,
agent: Agent,
file_manager_component: FileManagerComponent,
):
await agent.file_manager.workspace.write_file(test_file_path.name, file_content)
await file_manager_component.workspace.write_file(test_file_path.name, file_content)
content = file_manager_component.read_file(test_file_path.name)
assert content.replace("\r", "") == file_content


def test_read_file_not_found(file_manager_component):
def test_read_file_not_found(file_manager_component: FileManagerComponent):
filename = "does_not_exist.txt"
with pytest.raises(FileNotFoundError):
file_manager_component.read_file(filename)


@pytest.mark.asyncio
async def test_write_to_file_relative_path(
test_file_name: Path, file_manager_component, agent: Agent
test_file_name: Path, file_manager_component: FileManagerComponent
):
new_content = "This is new content.\n"
await file_manager_component.write_to_file(test_file_name, new_content)
with open(
agent.file_manager.workspace.get_path(test_file_name), "r", encoding="utf-8"
file_manager_component.workspace.get_path(test_file_name), "r", encoding="utf-8"
) as f:
content = f.read()
assert content == new_content


@pytest.mark.asyncio
async def test_write_to_file_absolute_path(
test_file_path: Path, file_manager_component
test_file_path: Path, file_manager_component: FileManagerComponent
):
new_content = "This is new content.\n"
await file_manager_component.write_to_file(test_file_path, new_content)
Expand All @@ -80,18 +86,18 @@ async def test_write_to_file_absolute_path(


@pytest.mark.asyncio
async def test_list_files(file_manager_component, agent: Agent):
async def test_list_files(file_manager_component: FileManagerComponent):
# Create files A and B
file_a_name = "file_a.txt"
file_b_name = "file_b.txt"
test_directory = Path("test_directory")

await agent.file_manager.workspace.write_file(file_a_name, "This is file A.")
await agent.file_manager.workspace.write_file(file_b_name, "This is file B.")
await file_manager_component.workspace.write_file(file_a_name, "This is file A.")
await file_manager_component.workspace.write_file(file_b_name, "This is file B.")

# Create a subdirectory and place a copy of file_a in it
agent.file_manager.workspace.make_dir(test_directory)
await agent.file_manager.workspace.write_file(
file_manager_component.workspace.make_dir(test_directory)
await file_manager_component.workspace.write_file(
test_directory / file_a_name, "This is file A in the subdirectory."
)

Expand All @@ -101,10 +107,10 @@ async def test_list_files(file_manager_component, agent: Agent):
assert os.path.join(test_directory, file_a_name) in files

# Clean up
agent.file_manager.workspace.delete_file(file_a_name)
agent.file_manager.workspace.delete_file(file_b_name)
agent.file_manager.workspace.delete_file(test_directory / file_a_name)
agent.file_manager.workspace.delete_dir(test_directory)
file_manager_component.workspace.delete_file(file_a_name)
file_manager_component.workspace.delete_file(file_b_name)
file_manager_component.workspace.delete_file(test_directory / file_a_name)
file_manager_component.workspace.delete_dir(test_directory)

# Case 2: Search for a file that does not exist and make sure we don't throw
non_existent_file = "non_existent_file.txt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pytest
from forge.components.git_operations import GitOperationsComponent
from forge.file_storage.base import FileStorage
from forge.utils.exceptions import CommandExecutionError
from git.exc import GitCommandError
from git.repo.base import Repo

from autogpt.agents.agent import Agent
from forge.file_storage.base import FileStorage
from forge.utils.exceptions import CommandExecutionError

from . import GitOperationsComponent


@pytest.fixture
Expand All @@ -14,15 +14,14 @@ def mock_clone_from(mocker):


@pytest.fixture
def git_ops_component(agent: Agent):
return agent.git_ops
def git_ops_component():
return GitOperationsComponent()


def test_clone_auto_gpt_repository(
git_ops_component: GitOperationsComponent,
storage: FileStorage,
mock_clone_from,
agent: Agent,
):
mock_clone_from.return_value = None

Expand All @@ -46,7 +45,6 @@ def test_clone_repository_error(
git_ops_component: GitOperationsComponent,
storage: FileStorage,
mock_clone_from,
agent: Agent,
):
url = "https://github.com/this-repository/does-not-exist.git"
clone_path = storage.get_path("does-not-exist")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
from unittest.mock import patch

import pytest
from PIL import Image
from pydantic import SecretStr

from forge.components.image_gen import ImageGeneratorComponent
from forge.components.image_gen.image_gen import ImageGeneratorConfiguration
from forge.file_storage.base import FileStorage
from forge.llm.providers.openai import OpenAICredentials
from PIL import Image
from pydantic import SecretStr


@pytest.fixture
def image_gen_component(storage: FileStorage):
cred = OpenAICredentials.from_env()
cred = OpenAICredentials(api_key=SecretStr("test"))
Pwuts marked this conversation as resolved.
Show resolved Hide resolved
return ImageGeneratorComponent(storage, openai_credentials=cred)


Expand All @@ -34,7 +35,6 @@ def image_size(request):
return request.param


@pytest.mark.requires_openai_api_key
@pytest.mark.vcr
def test_dalle(
image_gen_component: ImageGeneratorComponent,
Expand All @@ -52,7 +52,6 @@ def test_dalle(
reason="The image is too big to be put in a cassette for a CI pipeline. "
"We're looking into a solution."
)
@pytest.mark.requires_huggingface_api_key
@pytest.mark.parametrize(
"image_model",
["CompVis/stable-diffusion-v1-4", "stabilityai/stable-diffusion-2-1"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import json

import pytest
from forge.components.web.search import WebSearchComponent
from forge.utils.exceptions import ConfigurationError
from googleapiclient.errors import HttpError
from pydantic import SecretStr

from autogpt.agents.agent import Agent
from forge.components.web.search import WebSearchConfiguration
from forge.utils.exceptions import ConfigurationError

from . import WebSearchComponent


@pytest.fixture
def web_search_component(agent: Agent):
agent.web_search.config.google_api_key = SecretStr("test")
agent.web_search.config.google_custom_search_engine_id = SecretStr("test")
return agent.web_search
def web_search_component():
config = WebSearchConfiguration(
google_api_key=SecretStr("test"),
google_custom_search_engine_id=SecretStr("test"),
Pwuts marked this conversation as resolved.
Show resolved Hide resolved
)
return WebSearchComponent(config)


@pytest.mark.parametrize(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
from pathlib import Path

import pytest
from forge.components.web.selenium import BrowsingError, WebSeleniumComponent

from autogpt.agents.agent import Agent
from forge.llm.providers.multi import MultiProvider

from . import BrowsingError, WebSeleniumComponent


@pytest.fixture
def web_selenium_component(agent: Agent):
return agent.web_selenium
def web_selenium_component(app_data_dir: Path):
return WebSeleniumComponent(MultiProvider(), app_data_dir)


@pytest.mark.vcr
@pytest.mark.requires_openai_api_key
@pytest.mark.vc
@pytest.mark.asyncio
async def test_browse_website_nonexistent_url(
web_selenium_component: WebSeleniumComponent, cached_openai_client: None
web_selenium_component: WebSeleniumComponent,
):
url = "https://auto-gpt-thinks-this-website-does-not-exist.com"
question = "How to execute a barrel roll"
Expand Down
8 changes: 0 additions & 8 deletions forge/forge/conftest.py

This file was deleted.

Loading
Loading