Repository: geekan/MetaGPT
File: tests/metagpt/actions/test_write_prd.py (Python)

Code before:
@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"
    await context.repo.docs.prd.save("1.txt", PRD_SAMPLE)
    await context.repo.docs.save(filename=REQUIREMENT_FILENAME, content=NEW_REQUIREMENT_SAMPLE)

    action = WritePRD(context=context)
    prd = await action.run(Message(content=NEW_REQUIREMENT_SAMPLE, instruct_content=None))
    logger.info(NEW_REQUIREMENT_SAMPLE)
    logger.info(prd)

    # Assert the prd is not None or empty
    assert prd is not None
    assert prd.content != ""
    assert "Refined Requirements" in prd.content


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

    context.src_workspace = context.git_repo.workdir / context.git_repo.workdir.name

    await context.repo.with_src_path(context.src_workspace).srcs.save(
        filename="main.py", content='if __name__ == "__main__":\nmain()'
    )
    requirements = "Please fix the bug in the code."
    await context.repo.docs.save(filename=REQUIREMENT_FILENAME, content=requirements)
    action = WritePRD(context=context)

    prd = await action.run(Message(content=requirements, instruct_content=None))
    logger.info(prd)

    # Assert the prd is not None or empty
    assert prd is not None
    assert prd.content != ""


if __name__ == "__main__":
    pytest.main([__file__, "-s"])


Review comment (refactor):
the pattern `git_dir = Path(__file__).parent / f"unittest/{uuid.uuid4().hex}"` appears for 5 times in this PR.
most of them, including the code after the pattern are mostly redundant.
refactor them.

Code after:
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)

    action = WritePRD(context=context)
    prd = await action.run(Message(content=NEW_REQUIREMENT_SAMPLE, instruct_content=None))
    logger.info(NEW_REQUIREMENT_SAMPLE)
    logger.info(prd)

    # Assert the prd is not None or empty
    assert prd is not None
    assert prd.content != ""
    assert "Refined Requirements" in prd.content


@pytest.mark.asyncio
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(
        filename="main.py", content='if __name__ == "__main__":\nmain()'
    )
    requirements = "Please fix the bug in the code."
    await context.repo.docs.save(filename=REQUIREMENT_FILENAME, content=requirements)
    action = WritePRD(context=context)

    prd = await action.run(Message(content=requirements, instruct_content=None))
    logger.info(prd)

    # Assert the prd is not None or empty
    assert prd is not None
    assert prd.content != ""


if __name__ == "__main__":
    pytest.main([__file__, "-s"])