diff --git a/metagpt/tools/libs/editor.py b/metagpt/tools/libs/editor.py index c49d33a6e..b297c9989 100644 --- a/metagpt/tools/libs/editor.py +++ b/metagpt/tools/libs/editor.py @@ -17,6 +17,7 @@ from metagpt.const import DEFAULT_WORKSPACE_ROOT from metagpt.tools.libs.index_repo import DEFAULT_MIN_TOKEN_COUNT, IndexRepo from metagpt.tools.libs.linter import Linter from metagpt.tools.tool_registry import register_tool +from metagpt.utils.common import awrite from metagpt.utils.file import File from metagpt.utils.report import EditorReporter @@ -348,7 +349,7 @@ class Editor(BaseModel): output += self._print_window(self.current_file, self.current_line, self.window) return output - def create_file(self, filename: str) -> str: + async def create_file(self, filename: str) -> str: """Creates and opens a new file with the given name. Args: @@ -358,10 +359,7 @@ class Editor(BaseModel): if filename.exists(): raise FileExistsError(f"File '{filename}' already exists.") - if not filename.parent.exists(): - os.makedirs(filename.parent, exist_ok=True) - with filename.open("w") as file: - file.write("\n") + await awrite(filename, "\n") self.open_file(filename) return f"[File {filename} created.]" diff --git a/tests/metagpt/tools/libs/test_editor.py b/tests/metagpt/tools/libs/test_editor.py index 81964440a..8d6e923af 100644 --- a/tests/metagpt/tools/libs/test_editor.py +++ b/tests/metagpt/tools/libs/test_editor.py @@ -270,9 +270,10 @@ def test_create_file_unexist_path(): editor.create_file("/unexist/path/a.txt") -def test_create_file(temp_file_path): +@pytest.mark.asyncio +async def test_create_file(temp_file_path): editor = Editor() - result = editor.create_file(str(temp_file_path)) + result = await editor.create_file(str(temp_file_path)) expected = f"[File {temp_file_path} created.]" assert result.split("\n") == expected.split("\n")