From 6fba3ab3c9e40b85aa4e7be39207ae44d8a54be3 Mon Sep 17 00:00:00 2001 From: Jan Willhaus Date: Wed, 4 Nov 2020 13:17:44 +0100 Subject: [PATCH] feat(commands/changelog): add config file options for start_rev and incremental --- commitizen/commands/changelog.py | 8 +++- commitizen/defaults.py | 2 + tests/commands/test_changelog_command.py | 50 ++++++++++++++++++++++++ tests/test_conf.py | 4 ++ 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index e177062b5..62d175d27 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -29,11 +29,15 @@ def __init__(self, config: BaseConfig, args): self.config: BaseConfig = config self.cz = factory.commiter_factory(self.config) - self.start_rev = args.get("start_rev") + self.start_rev = args.get("start_rev") or self.config.settings.get( + "changelog_start_rev" + ) self.file_name = args.get("file_name") or self.config.settings.get( "changelog_file" ) - self.incremental = args["incremental"] + self.incremental = args["incremental"] or self.config.settings.get( + "changelog_incremental" + ) self.dry_run = args["dry_run"] self.unreleased_version = args["unreleased_version"] self.change_type_map = ( diff --git a/commitizen/defaults.py b/commitizen/defaults.py index 8c871840e..97854453e 100644 --- a/commitizen/defaults.py +++ b/commitizen/defaults.py @@ -11,6 +11,8 @@ "tag_format": None, # example v$version "bump_message": None, # bumped v$current_version to $new_version "changelog_file": "CHANGELOG.md", + "changelog_incremental": False, + "changelog_start_rev": None, } MAJOR = "MAJOR" diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 09a96dfd7..d52d157c7 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -20,6 +20,11 @@ def changelog_path() -> str: return os.path.join(os.getcwd(), "CHANGELOG.md") +@pytest.fixture() +def config_path() -> str: + return os.path.join(os.getcwd(), "pyproject.toml") + + @pytest.mark.usefixtures("tmp_commitizen_project") def test_changelog_on_empty_project(mocker): testargs = ["cz", "changelog", "--dry-run"] @@ -389,3 +394,48 @@ def test_breaking_change_content_v1(mocker, capsys): "## Unreleased\n\n### Feat\n\n- **users**: email pattern corrected\n\n" "### BREAKING CHANGE\n\n- migrate by renaming user to users\n\n" ) + + +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_changelog_config_flag_increment(mocker, changelog_path, config_path): + + with open(config_path, "a") as f: + f.write("changelog_incremental = true\n") + with open(changelog_path, "a") as f: + f.write("\nnote: this should be persisted using increment\n") + + create_file_and_commit("feat: add new output") + + testargs = ["cz", "changelog"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + + with open(changelog_path, "r") as f: + out = f.read() + + assert "this should be persisted using increment" in out + + +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_changelog_config_start_rev_option(mocker, capsys, config_path): + + # create commit and tag + create_file_and_commit("feat: new file") + testargs = ["cz", "bump", "--yes"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + capsys.readouterr() + + create_file_and_commit("feat: after 0.2.0") + create_file_and_commit("feat: after 0.2") + + with open(config_path, "a") as f: + f.write('changelog_start_rev = "0.2.0"\n') + + testargs = ["cz", "changelog", "--dry-run"] + mocker.patch.object(sys, "argv", testargs) + with pytest.raises(DryRunExit): + cli.main() + + out, _ = capsys.readouterr() + assert out == "## Unreleased\n\n### Feat\n\n- after 0.2\n- after 0.2.0\n\n" diff --git a/tests/test_conf.py b/tests/test_conf.py index 241f2117d..ba621bd63 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -32,6 +32,8 @@ "version_files": ["commitizen/__version__.py", "pyproject.toml"], "style": [["pointer", "reverse"], ["question", "underline"]], "changelog_file": "CHANGELOG.md", + "changelog_incremental": False, + "changelog_start_rev": None, } _new_settings = { @@ -42,6 +44,8 @@ "version_files": ["commitizen/__version__.py", "pyproject.toml"], "style": [["pointer", "reverse"], ["question", "underline"]], "changelog_file": "CHANGELOG.md", + "changelog_incremental": False, + "changelog_start_rev": None, } _read_settings = {