import sys

import pytest

from commitizen import cli, cmd, git
from tests.utils import create_file_and_commit


@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_command(mocker):
    # MINOR
    create_file_and_commit("feat: new file")

    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()

    tag_exists = git.tag_exist("0.2.0")
    assert tag_exists is True

    # PATCH
    create_file_and_commit("fix: username exception")

    testargs = ["cz", "bump"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()

    tag_exists = git.tag_exist("0.2.1")
    assert tag_exists is True

    # PRERELEASE
    create_file_and_commit("feat: location")

    testargs = ["cz", "bump", "--prerelease", "alpha"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()

    tag_exists = git.tag_exist("0.3.0a0")
    assert tag_exists is True

    # PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
    testargs = ["cz", "bump"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()

    tag_exists = git.tag_exist("0.3.0")
    assert tag_exists is True

    # MAJOR
    create_file_and_commit(
        "feat: new user interface\n\nBREAKING CHANGE: age is no longer supported"
    )

    testargs = ["cz", "bump"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()

    tag_exists = git.tag_exist("1.0.0")
    assert tag_exists is True


def test_bump_when_bumpping_is_not_support(mocker, capsys, tmpdir):
    with tmpdir.as_cwd():
        with open("./pyproject.toml", "w") as f:
            f.write("[tool.commitizen]\n" 'version="0.1.0"')

        cmd.run("git init")
        create_file_and_commit(
            "feat: new user interface\n\nBREAKING CHANGE: age is no longer supported"
        )

        testargs = ["cz", "-n", "cz_jira", "bump", "--yes"]
        mocker.patch.object(sys, "argv", testargs)

        with pytest.raises(SystemExit):
            cli.main()

        _, err = capsys.readouterr()
        assert "'cz_jira' rule does not support bump" in err


@pytest.mark.usefixtures("tmp_git_project")
def test_bump_is_not_specify(mocker, capsys):
    mocker.patch.object(sys, "argv", ["cz", "bump"])

    with pytest.raises(SystemExit):
        cli.main()

    expected_error_message = (
        "[NO_VERSION_SPECIFIED]\n"
        "Check if current version is specified in config file, like:\n"
        "version = 0.4.3\n"
    )

    _, err = capsys.readouterr()
    assert expected_error_message in err


@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_when_not_new_commit(mocker, capsys):
    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)

    with pytest.raises(SystemExit):
        cli.main()

    expected_error_message = "[NO_COMMITS_FOUND]\n" "No new commits found."
    _, err = capsys.readouterr()
    assert expected_error_message in err

@@ -1419,16 +1419,1145 @@
  True%0A%0A%0A
+@pytest.mark.usefixtures(%22tmp_commitizen_project%22)%0Adef test_bump_on_git_with_hooks_no_verify_disabled(mocker, capsys):%0A    cmd.run(%22mkdir .git/hooks%22)%0A    with open(%22.git/hooks/pre-commit%22, %22w%22) as f:%0A        f.write(%22#!/usr/bin/env bash%5Cn%22 'echo %220.1.0%22')%0A    cmd.run(%22chmod +x .git/hooks/pre-commit%22)%0A%0A    # MINOR%0A    create_file_and_commit(%22feat: new file%22)%0A%0A    testargs = %5B%22cz%22, %22bump%22, %22--yes%22%5D%0A    mocker.patch.object(sys, %22argv%22, testargs)%0A%0A    with pytest.raises(SystemExit):%0A        cli.main()%0A%0A    _, err = capsys.readouterr()%0A    assert 'git.commit errror: %220.1.0%22' in err%0A%0A%0A@pytest.mark.usefixtures(%22tmp_commitizen_project%22)%0Adef test_bump_on_git_with_hooks_no_verify_enabled(mocker):%0A    cmd.run(%22mkdir .git/hooks%22)%0A    with open(%22.git/hooks/pre-commit%22, %22w%22) as f:%0A        f.write(%22#!/usr/bin/env bash%5Cn%22 'echo %220.1.0%22')%0A    cmd.run(%22chmod +x .git/hooks/pre-commit%22)%0A%0A    # MINOR%0A    create_file_and_commit(%22feat: new file%22)%0A%0A    testargs = %5B%22cz%22, %22bump%22, %22--yes%22, %22--no-verify%22%5D%0A    mocker.patch.object(sys, %22argv%22, testargs)%0A    cli.main()%0A%0A    tag_exists = git.tag_exist(%220.2.0%22)%0A    assert tag_exists is True%0A%0A%0A
 def test

add test for --no-verify option