From 07d587b5438392ac49c386decb60ee9f7afa5b81 Mon Sep 17 00:00:00 2001 From: EdgyEdgemond Date: Fri, 23 Feb 2024 14:42:25 +0000 Subject: [PATCH] dont run post_process if changes declined, dry_run or not commited --- changelog_gen/cli/command.py | 11 +++++++---- release_notes/53.fix | 1 + tests/cli/test_generate.py | 28 +++++++++++++++++----------- 3 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 release_notes/53.fix diff --git a/changelog_gen/cli/command.py b/changelog_gen/cli/command.py index 286e99f..f6ea257 100644 --- a/changelog_gen/cli/command.py +++ b/changelog_gen/cli/command.py @@ -165,10 +165,10 @@ def _gen(cfg: config.Config, version_tag: str | None = None, *, dry_run: bool = typer.echo(w) - _finalise(w, e, version_tag, extension, cfg, dry_run=dry_run) + processed = _finalise(w, e, version_tag, extension, cfg, dry_run=dry_run) post_process = cfg.post_process - if post_process: + if post_process and processed: unique_issues = e.unique_issues(sections) per_issue_post_process(post_process, sorted(unique_issues), version_tag, dry_run=dry_run) @@ -181,7 +181,7 @@ def _finalise( # noqa: PLR0913 cfg: config.Config, *, dry_run: bool, -) -> None: +) -> bool: if dry_run or typer.confirm( f"Write CHANGELOG for suggested version {version_tag}", ): @@ -189,7 +189,7 @@ def _finalise( # noqa: PLR0913 extractor.clean() if dry_run or not cfg.commit: - return + return False Git.add_path(f"CHANGELOG.{extension.value}") # TODO(edgy): Dont add release notes if using commit messages... @@ -198,3 +198,6 @@ def _finalise( # noqa: PLR0913 if cfg.release: BumpVersion.release(version_tag) + return True + + return False diff --git a/release_notes/53.fix b/release_notes/53.fix new file mode 100644 index 0000000..5738675 --- /dev/null +++ b/release_notes/53.fix @@ -0,0 +1 @@ +Only run post_process commands, if changes were actually executed. diff --git a/tests/cli/test_generate.py b/tests/cli/test_generate.py index 6fd8f6c..5832b9d 100644 --- a/tests/cli/test_generate.py +++ b/tests/cli/test_generate.py @@ -96,6 +96,7 @@ def post_process_setup(git_repo): tag = true [changelog_gen] +commit = true post_process = url=https://my-api/{issue_ref}/release auth_env=MY_API_AUTH @@ -579,17 +580,22 @@ def test_generate_dry_run( result = gen_cli_runner.invoke(["--dry-run"]) assert result.exit_code == 0 - assert post_process_mock.call_args_list == [ - mock.call( - PostProcessConfig( - url="https://my-api/{issue_ref}/release", - auth_env="MY_API_AUTH", - ), - ["1", "2", "3", "4"], - "0.1.0", - dry_run=True, - ), - ] + assert post_process_mock.call_count == 0 + + @pytest.mark.usefixtures("git_repo", "_release_notes", "changelog", "post_process_setup") + def test_generate_decline_changes( + self, + gen_cli_runner, + monkeypatch, + ): + monkeypatch.setattr(typer, "confirm", mock.MagicMock(return_value=False)) + post_process_mock = mock.MagicMock() + monkeypatch.setattr(command, "per_issue_post_process", post_process_mock) + + result = gen_cli_runner.invoke([]) + + assert result.exit_code == 0 + assert post_process_mock.call_count == 0 @freeze_time("2022-04-14T16:45:03")