Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/allow empty commit #592

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
"action": "store_true",
"help": "Sign off the commit",
},
{
"name": "--allow-empty",
"action": "store_true",
"help": "Allow to create commit on an empty staging",
},
],
},
{
Expand Down
14 changes: 10 additions & 4 deletions commitizen/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ def prompt_commit_questions(self) -> str:
return cz.message(answers)

def __call__(self):
args = []
allow_empty: bool = self.arguments.get("allow_empty")

if allow_empty:
args.append("--allow-empty")

dry_run: bool = self.arguments.get("dry_run")

if git.is_staging_clean() and not dry_run:
if git.is_staging_clean() and not (dry_run or allow_empty):
raise NothingToCommitError("No files added to staging!")

retry: bool = self.arguments.get("retry")
Expand All @@ -82,9 +88,9 @@ def __call__(self):
signoff: bool = self.arguments.get("signoff")

if signoff:
c = git.commit(m, "-s")
else:
c = git.commit(m)
args.append("-s")

c = git.commit(m, *args)

if c.return_code != 0:
out.error(c.err)
Expand Down
22 changes: 22 additions & 0 deletions tests/commands/test_commit_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ def test_commit_retry_fails_no_backup(config, mocker):
assert NoCommitBackupError.message in str(excinfo.value)


@pytest.mark.usefixtures("staging_is_clean")
def test_commit_allow_empty(config, mocker):
prompt_mock = mocker.patch("questionary.prompt")
Dranaxel marked this conversation as resolved.
Show resolved Hide resolved
prompt_mock.return_value = {
"prefix": "feat",
"subject": "user created",
"scope": "",
"is_breaking_change": False,
"body": "closes #21",
"footer": "",
}

commit_mock = mocker.patch("commitizen.git.commit")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dranaxel Thanks for correcting me. Yes, you're right. In that case, we probably should assert whether this function is called with --allow-empty. This can be done by something likeassert_called_with.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry i'm not sure about what you mean/how to use assert_called_with. Are you suggesting to assert the flag allow-empty was here when le command was launched ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert --allow-empty is actually passed into commitizen.git.commit when the operation above is executed

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

commands.Commit(config, {"allow_empty": True})()

commit_mock.assert_called_with("feat: user created\n\ncloses #21", "--allow-empty")
success_mock.assert_called_once()


@pytest.mark.usefixtures("staging_is_clean")
def test_commit_retry_works(config, mocker):
prompt_mock = mocker.patch("questionary.prompt")
Expand Down