Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion commitizen/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Command(NamedTuple):
err: str
stdout: bytes
stderr: bytes
return_code: int


def run(cmd: str) -> Command:
Expand All @@ -18,4 +19,5 @@ def run(cmd: str) -> Command:
stdin=subprocess.PIPE,
)
stdout, stderr = process.communicate()
return Command(stdout.decode(), stderr.decode(), stdout, stderr)
return_code = process.returncode
return Command(stdout.decode(), stderr.decode(), stdout, stderr, return_code)
4 changes: 2 additions & 2 deletions commitizen/commands/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ def __call__(self): # noqa: C901

self.config.set_key("version", new_version.public)
c = git.commit(message, args=self._get_commit_args())
if c.err:
if c.return_code != 0:
raise BumpCommitFailedError(f'git.commit error: "{c.err.strip()}"')
c = git.tag(new_tag_version)
if c.err:
if c.return_code != 0:
raise BumpTagFailedError(c.err)
out.success("Done!")

Expand Down
5 changes: 2 additions & 3 deletions commitizen/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __call__(self):

c = git.commit(m)

if c.err:
if c.return_code != 0:
out.error(c.err)

# Create commit backup
Expand All @@ -86,10 +86,9 @@ def __call__(self):

if "nothing added" in c.out or "no changes added to commit" in c.out:
out.error(c.out)
elif c.err:
out.error(c.err)
else:
with contextlib.suppress(FileNotFoundError):
os.remove(self.temp_file)
out.write(c.err)
out.write(c.out)
out.success("Commit successful!")
28 changes: 24 additions & 4 deletions tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from commitizen import cli, cmd, git
from commitizen.exceptions import (
BumpCommitFailedError,
BumpTagFailedError,
CurrentVersionNotFoundError,
DryRunExit,
ExpectedExit,
Expand Down Expand Up @@ -71,6 +71,7 @@ def test_bump_command(mocker):

@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_on_git_with_hooks_no_verify_disabled(mocker):
"""Bump commit without --no-verify"""
cmd.run("mkdir .git/hooks")
with open(".git/hooks/pre-commit", "w") as f:
f.write("#!/usr/bin/env bash\n" 'echo "0.1.0"')
Expand All @@ -82,10 +83,29 @@ def test_bump_on_git_with_hooks_no_verify_disabled(mocker):
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)

with pytest.raises(BumpCommitFailedError) as excinfo:
cli.main()
cli.main()

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


@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_tag_exists_raises_exception(mocker):
cmd.run("mkdir .git/hooks")
with open(".git/hooks/post-commit", "w") as f:
f.write("#!/usr/bin/env bash\n" "exit 9")
cmd.run("chmod +x .git/hooks/post-commit")

# MINOR
create_file_and_commit("feat: new file")
git.tag("0.2.0")

assert 'git.commit error: "0.1.0"' in str(excinfo.value)
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)

with pytest.raises(BumpTagFailedError) as excinfo:
cli.main()
assert "fatal: tag '0.2.0' already exists" in str(excinfo.value)


@pytest.mark.usefixtures("tmp_commitizen_project")
Expand Down
8 changes: 4 additions & 4 deletions tests/commands/test_commit_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_commit(config, mocker):
}

commit_mock = mocker.patch("commitizen.git.commit")
commit_mock.return_value = cmd.Command("success", "", "", "")
commit_mock.return_value = cmd.Command("success", "", "", "", 0)
success_mock = mocker.patch("commitizen.out.success")

commands.Commit(config, {})()
Expand All @@ -44,7 +44,7 @@ def test_commit(config, mocker):
@pytest.mark.usefixtures("staging_is_clean")
def test_commit_retry_fails_no_backup(config, mocker):
commit_mock = mocker.patch("commitizen.git.commit")
commit_mock.return_value = cmd.Command("success", "", "", "")
commit_mock.return_value = cmd.Command("success", "", "", "", 0)

with pytest.raises(NoCommitBackupError) as excinfo:
commands.Commit(config, {"retry": True})()
Expand All @@ -65,7 +65,7 @@ def test_commit_retry_works(config, mocker):
}

commit_mock = mocker.patch("commitizen.git.commit")
commit_mock.return_value = cmd.Command("", "error", "", "")
commit_mock.return_value = cmd.Command("", "error", "", "", 9)
error_mock = mocker.patch("commitizen.out.error")

with pytest.raises(CommitError):
Expand All @@ -79,7 +79,7 @@ def test_commit_retry_works(config, mocker):

# Previous commit failed, so retry should pick up the backup commit
# commit_mock = mocker.patch("commitizen.git.commit")
commit_mock.return_value = cmd.Command("success", "", "", "")
commit_mock.return_value = cmd.Command("success", "", "", "", 0)
success_mock = mocker.patch("commitizen.out.success")

commands.Commit(config, {"retry": True})()
Expand Down