Skip to content

Commit 4af68a0

Browse files
committed
test(test_bump_command.py): ensure that CHANGELOG.md and version files get committed
1 parent fd6ce5d commit 4af68a0

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

commitizen/git.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ def get_commits(
126126
return git_commits
127127

128128

129+
def get_filenames_in_commit(git_reference: str = ""):
130+
"""Get the list of files that were committed in the requested git reference.
131+
132+
:param git_reference: a git reference as accepted by `git show`, default: the last commit
133+
134+
:returns: file names committed in the last commit by default or inside the passed git reference
135+
"""
136+
c = cmd.run(f"git show --name-only --pretty=format: {git_reference}")
137+
if c.return_code == 0:
138+
return c.out.strip().split("\n")
139+
else:
140+
raise GitCommandError(c.err)
141+
142+
129143
def get_tags(dateformat: str = "%Y-%m-%d") -> List[GitTag]:
130144
inner_delimiter = "---inner_delimiter---"
131145
formatter = (

tests/commands/test_bump_command.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import inspect
22
import sys
3+
from typing import Tuple
34
from unittest.mock import MagicMock
45

56
import pytest
@@ -528,3 +529,88 @@ def test_bump_with_changelog_to_stdout_dry_run_arg(mocker, capsys, changelog_pat
528529
assert out.startswith("#")
529530
assert "this should appear in stdout with dry-run enabled" in out
530531
assert "0.2.0" in out
532+
533+
534+
@pytest.mark.parametrize(
535+
"version_filepath, version_regex, version_file_content",
536+
[
537+
pytest.param(
538+
"pyproject.toml",
539+
"pyproject.toml:^version",
540+
"""
541+
[tool.poetry]
542+
name = "my_package"
543+
version = "0.1.0"
544+
""",
545+
id="version in pyproject.toml with regex",
546+
),
547+
pytest.param(
548+
"pyproject.toml",
549+
"pyproject.toml",
550+
"""
551+
[tool.poetry]
552+
name = "my_package"
553+
version = "0.1.0"
554+
""",
555+
id="version in pyproject.toml without regex",
556+
),
557+
pytest.param(
558+
"__init__.py",
559+
"__init__.py:^__version__",
560+
"""
561+
'''This is a test file.'''
562+
__version__ = "0.1.0"
563+
""",
564+
id="version in __init__.py with regex",
565+
),
566+
],
567+
)
568+
@pytest.mark.parametrize(
569+
"cli_bump_changelog_args",
570+
[
571+
("cz", "bump", "--changelog", "--yes"),
572+
(
573+
"cz",
574+
"bump",
575+
"--changelog",
576+
"--changelog-to-stdout",
577+
"--annotated",
578+
"--check-consistency",
579+
"--yes",
580+
),
581+
],
582+
ids=lambda cmd_tuple: " ".join(cmd_tuple),
583+
)
584+
def test_bump_changelog_command_commits_untracked_changelog_and_version_files(
585+
tmp_commitizen_project: pytest.TempdirFactory,
586+
mocker,
587+
cli_bump_changelog_args: Tuple[str, ...],
588+
version_filepath: str,
589+
version_regex: str,
590+
version_file_content: str,
591+
):
592+
"""Ensure that changelog always gets committed, no matter what version file or cli options get passed.
593+
594+
Steps:
595+
- Append the version file's name and regex commitizen configuration lines to `pyproject.toml`.
596+
- Append to or create the version file.
597+
- Add a commit of type fix to be eligible for a version bump.
598+
- Call commitizen main cli and assert that the `CHANGELOG.md` and the version file were committed.
599+
"""
600+
601+
with tmp_commitizen_project.join("pyproject.toml").open(
602+
mode="a"
603+
) as commitizen_config:
604+
commitizen_config.write(f"version_files = [\n" f"'{version_regex}'\n]")
605+
606+
with tmp_commitizen_project.join(version_filepath).open(mode="a+") as version_file:
607+
version_file.write(version_file_content)
608+
609+
create_file_and_commit("fix: some test commit")
610+
611+
mocker.patch.object(sys, "argv", cli_bump_changelog_args)
612+
cli.main()
613+
614+
commit_file_names = git.get_filenames_in_commit()
615+
assert "CHANGELOG.md" in commit_file_names
616+
assert version_filepath in commit_file_names

0 commit comments

Comments
 (0)