user commom.awrite in Editr.create_file

This commit is contained in:
黄伟韬 2024-09-29 14:29:10 +08:00
parent 58907d0dbb
commit b7ef5d6718
2 changed files with 6 additions and 7 deletions

View file

@ -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.]"

View file

@ -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")