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

Code before:
    if "Aborting" in result.output:
        assert False
    else:
        # After running, there will be new commit
        cur_tag = subprocess.run(["git", "describe", "--tags"], capture_output=True, text=True).stdout.strip()
        if cur_tag == "base":
            assert False
        else:
            assert True
            if subprocess.run(["git", "show-ref", "--verify", "--quiet", f"refs/tags/{tag_name}"]).returncode == 0:
                tag_name += str(int(time.time()))
            try:
                subprocess.run(["git", "tag", tag_name], check=True)
            except subprocess.CalledProcessError as e:
                raise e


def get_incremental_dev_result(idea, project_name, use_review=True):
    project_path = TEST_DATA_PATH / "incremental_dev_project" / project_name
    # Check if the project path exists
    if not project_path.exists():
        # If the project does not exist, extract the project file
        try:
            if os.name == "nt":
                subprocess.run(["tar", "-xf", f"{project_path}.zip", "-C", str(project_path.parent)], check=True)
            else:
                subprocess.run(["unzip", f"{project_path}.zip", "-d", str(project_path.parent)], check=True)
            logger.info(f"Extracted project {project_name} successfully.")
        except subprocess.CalledProcessError as e:
            # If the extraction fails, throw an exception
            raise Exception(f"Failed to extract project {project_name}. Error: {e}")

    check_or_create_base_tag(project_path)
    args = [idea, "--inc", "--project-path", project_path, "--n-round", "20"]
    if not use_review:
        args.append("--no-code-review")
    result = runner.invoke(app, args)
    return result


def check_or_create_base_tag(project_path):
    # Change the current working directory to the specified project path
    os.chdir(project_path)

    # Initialize a Git repository
    subprocess.run(["git", "init"], check=True)

    # Check if the 'base' tag exists
    check_base_tag_cmd = ["git", "show-ref", "--verify", "--quiet", "refs/tags/base"]
    if subprocess.run(check_base_tag_cmd).returncode == 0:
        has_base_tag = True

Review comment (suggestion):
I think a generic unzip function, or something like that, should be provided. This function should check if the command exists and then execute the command, a process that is not system-agnostic, but the command provided

Code after:
    logger.info(result.output)
    if "Aborting" in result.output:
        assert False
    else:
        # After running, there will be new commit
        cur_tag = subprocess.run(["git", "describe", "--tags"], capture_output=True, text=True).stdout.strip()
        if cur_tag == "base":
            assert False
        else:
            assert True
            if subprocess.run(["git", "show-ref", "--verify", "--quiet", f"refs/tags/{tag_name}"]).returncode == 0:
                tag_name += str(int(time.time()))
            try:
                subprocess.run(["git", "tag", tag_name], check=True)
            except subprocess.CalledProcessError as e:
                raise e


def get_incremental_dev_result(idea, project_name, use_review=True):
    project_path = TEST_DATA_PATH / "incremental_dev_project" / project_name
    # Check if the project path exists
    if not project_path.exists():
        # If the project does not exist, extract the project file
        try:
            if shutil.which("unzip"):
                subprocess.run(["unzip", f"{project_path}.zip", "-d", str(project_path.parent)], check=True)
            elif shutil.which("tar"):
                subprocess.run(["tar", "-xf", f"{project_path}.zip", "-C", str(project_path.parent)], check=True)
            logger.info(f"Extracted project {project_name} successfully.")
        except FileNotFoundError as e:
            raise FileNotFoundError(f"Neither 'unzip' nor 'tar' command found. Error: {e}")
        except subprocess.CalledProcessError as e:
            raise Exception(f"Failed to extract project {project_name}. Error: {e}")

    check_or_create_base_tag(project_path)
    args = [idea, "--inc", "--project-path", project_path, "--n-round", "20"]
    if not use_review:
        args.append("--no-code-review")
    result = runner.invoke(app, args)
    return result


def check_or_create_base_tag(project_path):
    # Change the current working directory to the specified project path
    os.chdir(project_path)

    # Initialize a Git repository
    subprocess.run(["git", "init"], check=True)

    # Check if the 'base' tag exists
    check_base_tag_cmd = ["git", "show-ref", "--verify", "--quiet", "refs/tags/base"]