Skip to content

Commit

Permalink
feat(commands/changelog): add config file options for start_rev and i…
Browse files Browse the repository at this point in the history
…ncremental
  • Loading branch information
Jan Willhaus committed Nov 4, 2020
1 parent 14b6c38 commit 6fba3ab
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
8 changes: 6 additions & 2 deletions commitizen/commands/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
2 changes: 2 additions & 0 deletions commitizen/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
50 changes: 50 additions & 0 deletions tests/commands/test_changelog_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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"
4 changes: 4 additions & 0 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand Down

0 comments on commit 6fba3ab

Please sign in to comment.