|
1 | 1 | import inspect
|
2 | 2 | import sys
|
| 3 | +from typing import Tuple |
3 | 4 | from unittest.mock import MagicMock
|
4 | 5 |
|
5 | 6 | import pytest
|
@@ -528,3 +529,88 @@ def test_bump_with_changelog_to_stdout_dry_run_arg(mocker, capsys, changelog_pat
|
528 | 529 | assert out.startswith("#")
|
529 | 530 | assert "this should appear in stdout with dry-run enabled" in out
|
530 | 531 | 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, |
| 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