Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
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
20 changes: 20 additions & 0 deletions tests/commands/test_commit_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ 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")
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})()
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