diff --git a/metagpt/utils/project_repo.py b/metagpt/utils/project_repo.py index c7ddffb71..e2c7bb0e2 100644 --- a/metagpt/utils/project_repo.py +++ b/metagpt/utils/project_repo.py @@ -133,7 +133,7 @@ class ProjectRepo(FileRepository): code_files = self.with_src_path(path=git_workdir / git_workdir.name).srcs.all_files if not code_files: return False - return True + return bool(code_files) def with_src_path(self, path: str | Path) -> ProjectRepo: try: diff --git a/tests/conftest.py b/tests/conftest.py index f2379a833..efd782417 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,6 +12,7 @@ import logging import os import re import uuid +from pathlib import Path from typing import Callable import aiohttp.web @@ -270,3 +271,11 @@ def mermaid_mocker(aiohttp_mocker, mermaid_rsp_cache): aiohttp_mocker.rsp_cache = mermaid_rsp_cache aiohttp_mocker.check_funcs = check_funcs yield check_funcs + + +@pytest.fixture +def git_dir(): + """Fixture to get the unittest directory.""" + git_dir = Path(__file__).parent / f"unittest/{uuid.uuid4().hex}" + git_dir.mkdir(parents=True, exist_ok=True) + return git_dir diff --git a/tests/metagpt/actions/test_summarize_code.py b/tests/metagpt/actions/test_summarize_code.py index 3cfe7ca81..5e3057dee 100644 --- a/tests/metagpt/actions/test_summarize_code.py +++ b/tests/metagpt/actions/test_summarize_code.py @@ -6,8 +6,6 @@ @File : test_summarize_code.py @Modifiled By: mashenquan, 2023-12-6. Unit test for summarize_code.py """ -import uuid -from pathlib import Path import pytest @@ -178,10 +176,7 @@ class Snake: @pytest.mark.skip @pytest.mark.asyncio -async def test_summarize_code(context): - git_dir = Path(__file__).parent / f"unittest/{uuid.uuid4().hex}" - git_dir.mkdir(parents=True, exist_ok=True) - +async def test_summarize_code(context, git_dir): context.src_workspace = context.git_repo.workdir / "src" await context.repo.docs.system_design.save(filename="1.json", content=DESIGN_CONTENT) await context.repo.docs.task.save(filename="1.json", content=TASK_CONTENT) diff --git a/tests/metagpt/actions/test_write_code.py b/tests/metagpt/actions/test_write_code.py index b5b6e4c52..1709e1f5b 100644 --- a/tests/metagpt/actions/test_write_code.py +++ b/tests/metagpt/actions/test_write_code.py @@ -7,7 +7,6 @@ @Modifiled By: mashenquan, 2023-12-6. According to RFC 135 """ import json -import uuid from pathlib import Path import pytest @@ -25,6 +24,17 @@ from tests.data.incremental_dev_project.mock import ( from tests.metagpt.actions.mock_markdown import TASKS_2, WRITE_CODE_PROMPT_SAMPLE +def setup_inc_workdir(context, inc: bool = False): + """setup incremental workdir for testing""" + context.src_workspace = context.git_repo.workdir / "src" + if inc: + context.config.inc = inc + context.repo.old_workspace = context.repo.git_repo.workdir / "old" + context.config.project_path = "old" + + return context + + @pytest.mark.asyncio async def test_write_code(context): # Prerequisites @@ -89,13 +99,9 @@ async def test_write_code_deps(context): @pytest.mark.asyncio -async def test_write_refined_code(context): +async def test_write_refined_code(context, git_dir): # Prerequisites - git_dir = Path(__file__).parent / f"unittest/{uuid.uuid4().hex}" - git_dir.mkdir(parents=True, exist_ok=True) - context.config.inc = True - - context.src_workspace = context.git_repo.workdir / "src" + context = setup_inc_workdir(context, inc=True) await context.repo.docs.system_design.save(filename="1.json", content=json.dumps(REFINED_DESIGN_JSON)) await context.repo.docs.task.save(filename="1.json", content=json.dumps(REFINED_TASK_JSON)) await context.repo.docs.code_plan_and_change.save( @@ -103,7 +109,6 @@ async def test_write_refined_code(context): ) # old_workspace contains the legacy code - context.repo.old_workspace = context.repo.git_repo.workdir / "old" await context.repo.with_src_path(context.repo.old_workspace).srcs.save( filename="game.py", content=CodeParser.parse_code(block="", text=REFINED_CODE_INPUT_SAMPLE) ) @@ -126,8 +131,7 @@ async def test_write_refined_code(context): @pytest.mark.asyncio async def test_get_codes(context): # Prerequisites - context.src_workspace = context.git_repo.workdir / "src" - context.repo.old_workspace = context.repo.git_repo.workdir / "old" + context = setup_inc_workdir(context, inc=True) for filename in ["game.py", "ui.py"]: await context.repo.with_src_path(context.src_workspace).srcs.save( filename=filename, content=f"# {filename}\nnew code ..." diff --git a/tests/metagpt/actions/test_write_code_plan_and_change_an.py b/tests/metagpt/actions/test_write_code_plan_and_change_an.py index f564a677b..5c262b4b7 100644 --- a/tests/metagpt/actions/test_write_code_plan_and_change_an.py +++ b/tests/metagpt/actions/test_write_code_plan_and_change_an.py @@ -6,8 +6,6 @@ @File : test_write_code_plan_and_change_an.py """ import json -import uuid -from pathlib import Path import pytest from openai._models import BaseModel @@ -32,6 +30,7 @@ from tests.data.incremental_dev_project.mock import ( REFINED_TASK_JSON, TASK_SAMPLE, ) +from tests.metagpt.actions.test_write_code import setup_inc_workdir def mock_code_plan_and_change(): @@ -39,19 +38,12 @@ def mock_code_plan_and_change(): @pytest.mark.asyncio -async def test_write_code_plan_and_change_an(mocker, context): - # Prerequisites - git_dir = Path(__file__).parent / f"unittest/{uuid.uuid4().hex}" - git_dir.mkdir(parents=True, exist_ok=True) - context.config.inc = True +async def test_write_code_plan_and_change_an(mocker, context, git_dir): + context = setup_inc_workdir(context, inc=True) + await context.repo.docs.prd.save(filename="2.json", content=json.dumps(REFINED_PRD_JSON)) + await context.repo.docs.system_design.save(filename="2.json", content=json.dumps(REFINED_DESIGN_JSON)) + await context.repo.docs.task.save(filename="2.json", content=json.dumps(REFINED_TASK_JSON)) - context.src_workspace = context.git_repo.workdir / "src" - await context.repo.docs.prd.save(filename="1.json", content=json.dumps(REFINED_PRD_JSON)) - await context.repo.docs.system_design.save(filename="1.json", content=json.dumps(REFINED_DESIGN_JSON)) - await context.repo.docs.task.save(filename="1.json", content=json.dumps(REFINED_TASK_JSON)) - - context.config.project_path = "old" - context.repo.old_workspace = context.repo.git_repo.workdir / "old" await context.repo.with_src_path(context.repo.old_workspace).srcs.save( filename="game.py", content=CodeParser.parse_code(block="", text=REFINED_CODE_INPUT_SAMPLE) ) @@ -67,9 +59,9 @@ async def test_write_code_plan_and_change_an(mocker, context): code_plan_and_change_context = CodePlanAndChangeContext( requirement="New requirement", - prd_filename="1.json", - design_filename="1.json", - task_filename="1.json", + prd_filename="2.json", + design_filename="2.json", + task_filename="2.json", ) node = await WriteCodePlanAndChange(i_context=code_plan_and_change_context, context=context).run() @@ -96,12 +88,8 @@ async def test_refine_code(mocker): @pytest.mark.asyncio -async def test_get_old_code(context): - git_dir = Path(__file__).parent / f"unittest/{uuid.uuid4().hex}" - git_dir.mkdir(parents=True, exist_ok=True) - - context.config.project_path = "old" - context.repo.old_workspace = context.repo.git_repo.workdir / "old" +async def test_get_old_code(context, git_dir): + context = setup_inc_workdir(context, inc=True) await context.repo.with_src_path(context.repo.old_workspace).srcs.save( filename="game.py", content=REFINED_CODE_INPUT_SAMPLE ) diff --git a/tests/metagpt/actions/test_write_prd.py b/tests/metagpt/actions/test_write_prd.py index 0a2739975..43aa336b7 100644 --- a/tests/metagpt/actions/test_write_prd.py +++ b/tests/metagpt/actions/test_write_prd.py @@ -6,8 +6,6 @@ @File : test_write_prd.py @Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, replace `handle` with `run`. """ -import uuid -from pathlib import Path import pytest @@ -19,6 +17,7 @@ from metagpt.roles.role import RoleReactMode from metagpt.schema import Message from metagpt.utils.common import any_to_str from tests.data.incremental_dev_project.mock import NEW_REQUIREMENT_SAMPLE, PRD_SAMPLE +from tests.metagpt.actions.test_write_code import setup_inc_workdir @pytest.mark.asyncio @@ -39,11 +38,8 @@ async def test_write_prd(new_filename, context): @pytest.mark.asyncio -async def test_write_prd_inc(new_filename, context): - git_dir = Path(__file__).parent / f"unittest/{uuid.uuid4().hex}" - git_dir.mkdir(parents=True, exist_ok=True) - - context.src_workspace = context.git_repo.workdir / "src" +async def test_write_prd_inc(new_filename, context, git_dir): + context = setup_inc_workdir(context, inc=True) await context.repo.docs.prd.save("1.txt", PRD_SAMPLE) await context.repo.docs.save(filename=REQUIREMENT_FILENAME, content=NEW_REQUIREMENT_SAMPLE) @@ -59,10 +55,7 @@ async def test_write_prd_inc(new_filename, context): @pytest.mark.asyncio -async def test_fix_debug(new_filename, context): - git_dir = Path(__file__).parent / f"unittest/{uuid.uuid4().hex}" - git_dir.mkdir(parents=True, exist_ok=True) - +async def test_fix_debug(new_filename, context, git_dir): context.src_workspace = context.git_repo.workdir / context.git_repo.workdir.name await context.repo.with_src_path(context.src_workspace).srcs.save(