From e0675b31ade7fb206514e76654a3789fd5bb1361 Mon Sep 17 00:00:00 2001 From: Daniel Vergara Date: Mon, 18 Nov 2019 14:07:09 -0600 Subject: [PATCH] =?UTF-8?q?feat(Commands/commit):=20add=20=C2=B4--dry-run?= =?UTF-8?q?=C2=B4=20flag=20to=20the=20Commit=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Generate a commit message without doing anything closes #56 --- commitizen/cli.py | 7 ++++++- commitizen/commands/commit.py | 6 ++++++ tests/test_commands.py | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/commitizen/cli.py b/commitizen/cli.py index 620707c588..805e1fb3d3 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -45,7 +45,12 @@ "name": ["--retry"], "action": "store_true", "help": "retry last commit", - } + }, + { + "name": "--dry-run", + "action": "store_true", + "help": "show output to stdout, no commit, no modified files", + }, ], }, { diff --git a/commitizen/commands/commit.py b/commitizen/commands/commit.py index 8b438da4d7..c67a381f18 100644 --- a/commitizen/commands/commit.py +++ b/commitizen/commands/commit.py @@ -63,6 +63,12 @@ def __call__(self): m = self.prompt_commit_questions() out.info(f"\n{m}\n") + + dry_run: bool = self.arguments.get("dry_run") + + if dry_run: + raise SystemExit(NOTHING_TO_COMMIT) + c = git.commit(m) if c.err: diff --git a/tests/test_commands.py b/tests/test_commands.py index bd08c3da8f..3206d28002 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -82,6 +82,23 @@ def test_commit_retry_works(mocker): assert not os.path.isfile(temp_file) +@pytest.mark.usefixtures("staging_is_clean") +def test_commit_command_with_dry_run_option(mocker): + prompt_mock = mocker = mocker.patch("questionary.prompt") + prompt_mock.return_value = { + "prefix": "feat", + "subject": "user created", + "scope": "", + "is_breaking_change": False, + "body": "closes #57", + "footer": "", + } + + with pytest.raises(SystemExit): + commit_cmd = commands.Commit(config, {"dry_run": True}) + commit_cmd() + + def test_commit_when_nothing_to_commit(mocker): is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean") is_staging_clean_mock.return_value = True