Skip to content

Commit

Permalink
Fix issues with file reading and writing with Python code
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-megarad committed Jun 6, 2023
1 parent dafbd11 commit 90686ed
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
15 changes: 12 additions & 3 deletions autogpt/commands/execute_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from autogpt.commands.command import command
from autogpt.config import Config
from autogpt.logs import logger
from autogpt.setup import CFG


@command("execute_python_file", "Execute Python File", '"filename": "<filename>"')
Expand All @@ -21,17 +22,25 @@ def execute_python_file(filename: str, config: Config) -> str:
Returns:
str: The output of the file
"""
logger.info(f"Executing file '{filename}'")
logger.info(
f"Executing python file '{filename}' in working directory '{CFG.workspace_path}'"
)

if not filename.endswith(".py"):
return "Error: Invalid file type. Only .py files are allowed."

if not os.path.isfile(filename):
return f"Error: File '{filename}' does not exist."
# Mimic the response that you get from the command line so that it's easier to identify
return (
f"python: can't open file '{filename}': [Errno 2] No such file or directory"
)

if we_are_running_in_a_docker_container():
result = subprocess.run(
["python", filename], capture_output=True, encoding="utf8"
["python", filename],
capture_output=True,
encoding="utf8",
cwd=CFG.workspace_path,
)
if result.returncode == 0:
return result.stdout
Expand Down
2 changes: 1 addition & 1 deletion autogpt/commands/file_operations_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def is_file_binary_fn(file_path: str):

def read_textual_file(file_path: str, logger: logs.Logger) -> str:
if not os.path.isfile(file_path):
raise FileNotFoundError(f"{file_path} not found!")
raise FileNotFoundError(f"read_file: {file_path}: no such file or directory")
is_binary = is_file_binary_fn(file_path)
file_extension = os.path.splitext(file_path)[1].lower()
parser = extension_to_parser.get(file_extension)
Expand Down
17 changes: 12 additions & 5 deletions tests/integration/test_execute_code.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import random
import string
import tempfile
from typing import Callable

import pytest
from pytest_mock import MockerFixture
Expand All @@ -10,12 +11,12 @@


@pytest.fixture
def config_allow_execute(config: Config, mocker: MockerFixture):
def config_allow_execute(config: Config, mocker: MockerFixture) -> Callable:
yield mocker.patch.object(config, "execute_local_commands", True)


@pytest.fixture
def python_test_file(config: Config, random_string):
def python_test_file(config: Config, random_string) -> Callable:
temp_file = tempfile.NamedTemporaryFile(dir=config.workspace_path, suffix=".py")
temp_file.write(str.encode(f"print('Hello {random_string}!')"))
temp_file.flush()
Expand All @@ -34,17 +35,23 @@ def test_execute_python_file(python_test_file: str, random_string: str, config):
assert result.replace("\r", "") == f"Hello {random_string}!\n"


def test_execute_python_file_invalid(config):
def test_execute_python_file_invalid(config: Config):
assert all(
s in sut.execute_python_file("not_python", config).lower()
for s in ["error:", "invalid", ".py"]
)


def test_execute_python_file_not_found(config: Config):
assert all(
s in sut.execute_python_file("notexist.py", config).lower()
for s in ["error:", "does not exist"]
for s in [
"python: can't open file 'notexist.py'",
"[errno 2] no such file or directory",
]
)


def test_execute_shell(config_allow_execute, random_string, config):
def test_execute_shell(config_allow_execute: bool, random_string: str, config: Config):
result = sut.execute_shell(f"echo 'Hello {random_string}!'", config)
assert f"Hello {random_string}!" in result
8 changes: 8 additions & 0 deletions tests/unit/test_file_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@ def test_read_file(
assert content.replace("\r", "") == file_content


def test_read_file_not_found(config: Config):
content = file_ops.read_file("does_not_exist.txt", config)
assert (
content.replace("\r", "")
== "Error: read_file: does_not_exist.txt: no such file or directory"
)


def test_write_to_file(test_file_path: Path, config):
new_content = "This is new content.\n"
file_ops.write_to_file(str(test_file_path), new_content, config)
Expand Down

0 comments on commit 90686ed

Please sign in to comment.