From b0682d5a1439a1a479aa42d6d703cb276884493f Mon Sep 17 00:00:00 2001 From: Alexandre Date: Sun, 18 Sep 2022 12:14:58 +0200 Subject: [PATCH 1/5] feat(commit): added a --allow-empty flag to commit command --- commitizen/commands/commit.py | 4 +++- tests/commands/test_commit_command.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/commitizen/commands/commit.py b/commitizen/commands/commit.py index 13e61abe6d..79aec74de6 100644 --- a/commitizen/commands/commit.py +++ b/commitizen/commands/commit.py @@ -64,7 +64,9 @@ def prompt_commit_questions(self) -> str: def __call__(self): dry_run: bool = self.arguments.get("dry_run") - if git.is_staging_clean() and not dry_run: + allow_empty: bool = self.arguments.get("allow_empty") + + 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") diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py index 8544833c8f..4f9b295cc5 100644 --- a/tests/commands/test_commit_command.py +++ b/tests/commands/test_commit_command.py @@ -38,7 +38,7 @@ def test_commit(config, mocker): success_mock = mocker.patch("commitizen.out.success") commands.Commit(config, {})() - success_mock.assert_called_once() + success_mock.assert_called_once() @pytest.mark.usefixtures("staging_is_clean") From 78fff8399234a4303e850937b4fdcb647f37fd0e Mon Sep 17 00:00:00 2001 From: Alexandre Date: Sun, 18 Sep 2022 19:48:53 +0200 Subject: [PATCH 2/5] fix(commit): Fixed typo error in the cli configuration file --- commitizen/cli.py | 5 +++++ commitizen/commands/commit.py | 14 +++++++++----- tests/commands/test_commit_command.py | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/commitizen/cli.py b/commitizen/cli.py index 264bddb2ff..921424114f 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -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", + }, ], }, { diff --git a/commitizen/commands/commit.py b/commitizen/commands/commit.py index 79aec74de6..376850d615 100644 --- a/commitizen/commands/commit.py +++ b/commitizen/commands/commit.py @@ -62,10 +62,14 @@ def prompt_commit_questions(self) -> str: return cz.message(answers) def __call__(self): - dry_run: bool = self.arguments.get("dry_run") - + 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 or allow_empty): raise NothingToCommitError("No files added to staging!") @@ -84,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) diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py index 4f9b295cc5..8544833c8f 100644 --- a/tests/commands/test_commit_command.py +++ b/tests/commands/test_commit_command.py @@ -38,7 +38,7 @@ def test_commit(config, mocker): success_mock = mocker.patch("commitizen.out.success") commands.Commit(config, {})() - success_mock.assert_called_once() + success_mock.assert_called_once() @pytest.mark.usefixtures("staging_is_clean") From a4c9a1eddd4848920784773356e36eb7c7422dbe Mon Sep 17 00:00:00 2001 From: Alexandre Date: Sun, 18 Sep 2022 22:22:16 +0200 Subject: [PATCH 3/5] test: Testing allow-empty flag for cz commit command --- tests/commands/test_commit_command.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py index 8544833c8f..a706857c4d 100644 --- a/tests/commands/test_commit_command.py +++ b/tests/commands/test_commit_command.py @@ -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") + 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") From 6493ff3c879bae06b8bae4bb5ca2fe628383c39b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Marc=C3=A9?= Date: Fri, 4 Nov 2022 22:40:09 +0100 Subject: [PATCH 4/5] test(allow_empty): Added parameters for asserting the command arguments --- tests/commands/test_commit_command.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py index a706857c4d..195646998e 100644 --- a/tests/commands/test_commit_command.py +++ b/tests/commands/test_commit_command.py @@ -69,6 +69,8 @@ def test_commit_allow_empty(config, mocker): 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() From 4f9c725d4d80945ed8d71b0c44ffbf81ed66b4a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Marc=C3=A9?= Date: Sat, 5 Nov 2022 18:14:03 +0100 Subject: [PATCH 5/5] style: runned flake8 formatting --- tests/commands/test_commit_command.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py index 195646998e..f4ace3d382 100644 --- a/tests/commands/test_commit_command.py +++ b/tests/commands/test_commit_command.py @@ -70,7 +70,7 @@ def test_commit_allow_empty(config, mocker): commands.Commit(config, {"allow_empty": True})() - commit_mock.assert_called_with('feat: user created\n\ncloses #21', "--allow-empty") + commit_mock.assert_called_with("feat: user created\n\ncloses #21", "--allow-empty") success_mock.assert_called_once()