|
17 | 17 | BumpTagFailedError, |
18 | 18 | CommitizenException, |
19 | 19 | CurrentVersionNotFoundError, |
| 20 | + DirtyWorkingTreeError, |
20 | 21 | DryRunExit, |
21 | 22 | ExitCode, |
22 | 23 | ExpectedExit, |
@@ -1708,3 +1709,117 @@ def test_bump_allow_no_commit_issue( |
1708 | 1709 | util.run_cli( |
1709 | 1710 | "bump", "--allow-no-commit", "--prerelease", "rc" |
1710 | 1711 | ) # Should not fail when changelog generation runs with no new commits |
| 1712 | + |
| 1713 | + |
| 1714 | +@pytest.mark.usefixtures("tmp_commitizen_project") |
| 1715 | +def test_bump_no_allow_dirty_aborts_with_uncommitted_changes(util: UtilFixture): |
| 1716 | + # Arrange |
| 1717 | + util.create_file_and_commit("feat: new file", filename="tracked.txt") |
| 1718 | + Path("tracked.txt").write_text("work in progress") |
| 1719 | + |
| 1720 | + # Act / Assert |
| 1721 | + with pytest.raises(DirtyWorkingTreeError, match=r"\[DIRTY_WORKING_TREE\]"): |
| 1722 | + util.run_cli("bump", "--yes", "--no-allow-dirty") |
| 1723 | + |
| 1724 | + assert git.tag_exist("0.2.0") is False |
| 1725 | + assert 'version="0.1.0"' in Path("pyproject.toml").read_text() |
| 1726 | + assert Path("tracked.txt").read_text() == "work in progress" |
| 1727 | + |
| 1728 | + |
| 1729 | +@pytest.mark.usefixtures("tmp_commitizen_project") |
| 1730 | +def test_bump_no_allow_dirty_lists_dirty_files(util: UtilFixture): |
| 1731 | + # Arrange |
| 1732 | + util.create_file_and_commit("feat: new file", filename="tracked.txt") |
| 1733 | + Path("tracked.txt").write_text("work in progress") |
| 1734 | + |
| 1735 | + # Act |
| 1736 | + with pytest.raises(DirtyWorkingTreeError) as exc_info: |
| 1737 | + util.run_cli("bump", "--yes", "--no-allow-dirty") |
| 1738 | + |
| 1739 | + # Assert |
| 1740 | + assert exc_info.value.exit_code == ExitCode.DIRTY_WORKING_TREE |
| 1741 | + assert " tracked.txt\n" in exc_info.value.message |
| 1742 | + |
| 1743 | + |
| 1744 | +def test_bump_allow_dirty_false_in_config_aborts( |
| 1745 | + tmp_commitizen_project, util: UtilFixture |
| 1746 | +): |
| 1747 | + # Arrange |
| 1748 | + with (tmp_commitizen_project / "pyproject.toml").open("a", encoding="utf-8") as f: |
| 1749 | + f.write("allow_dirty = false\n") |
| 1750 | + util.create_file_and_commit("feat: new file", filename="tracked.txt") |
| 1751 | + Path("tracked.txt").write_text("work in progress") |
| 1752 | + |
| 1753 | + # Act / Assert |
| 1754 | + with pytest.raises(DirtyWorkingTreeError): |
| 1755 | + util.run_cli("bump", "--yes") |
| 1756 | + |
| 1757 | + assert git.tag_exist("0.2.0") is False |
| 1758 | + |
| 1759 | + |
| 1760 | +def test_bump_allow_dirty_flag_overrides_config( |
| 1761 | + tmp_commitizen_project, util: UtilFixture |
| 1762 | +): |
| 1763 | + # Arrange |
| 1764 | + with (tmp_commitizen_project / "pyproject.toml").open("a", encoding="utf-8") as f: |
| 1765 | + f.write("allow_dirty = false\n") |
| 1766 | + util.create_file_and_commit("feat: new file", filename="tracked.txt") |
| 1767 | + Path("tracked.txt").write_text("work in progress") |
| 1768 | + |
| 1769 | + # Act |
| 1770 | + util.run_cli("bump", "--yes", "--allow-dirty") |
| 1771 | + |
| 1772 | + # Assert |
| 1773 | + assert git.tag_exist("0.2.0") is True |
| 1774 | + |
| 1775 | + |
| 1776 | +@pytest.mark.usefixtures("tmp_commitizen_project") |
| 1777 | +def test_bump_dirty_tree_is_allowed_by_default(util: UtilFixture): |
| 1778 | + """Existing behavior is preserved: pending changes land in the bump commit.""" |
| 1779 | + # Arrange |
| 1780 | + util.create_file_and_commit("feat: new file", filename="tracked.txt") |
| 1781 | + Path("tracked.txt").write_text("work in progress") |
| 1782 | + |
| 1783 | + # Act |
| 1784 | + util.run_cli("bump", "--yes") |
| 1785 | + |
| 1786 | + # Assert |
| 1787 | + assert git.tag_exist("0.2.0") is True |
| 1788 | + assert "tracked.txt" in git.get_filenames_in_commit() |
| 1789 | + |
| 1790 | + |
| 1791 | +@pytest.mark.usefixtures("tmp_commitizen_project") |
| 1792 | +def test_bump_no_allow_dirty_ignores_untracked_files(util: UtilFixture): |
| 1793 | + # Arrange |
| 1794 | + util.create_file_and_commit("feat: new file") |
| 1795 | + Path("untracked.txt").write_text("not part of the repo") |
| 1796 | + |
| 1797 | + # Act |
| 1798 | + util.run_cli("bump", "--yes", "--no-allow-dirty") |
| 1799 | + |
| 1800 | + # Assert |
| 1801 | + assert git.tag_exist("0.2.0") is True |
| 1802 | + assert "untracked.txt" not in git.get_filenames_in_commit() |
| 1803 | + |
| 1804 | + |
| 1805 | +@pytest.mark.parametrize( |
| 1806 | + ("extra_args", "expected_exception"), |
| 1807 | + [ |
| 1808 | + (["--dry-run"], DryRunExit), |
| 1809 | + (["--get-next"], DryRunExit), |
| 1810 | + (["--version-files-only"], ExpectedExit), |
| 1811 | + ], |
| 1812 | +) |
| 1813 | +@pytest.mark.usefixtures("tmp_commitizen_project") |
| 1814 | +def test_bump_no_allow_dirty_skipped_when_no_commit_is_made( |
| 1815 | + util: UtilFixture, |
| 1816 | + extra_args: list[str], |
| 1817 | + expected_exception: type[Exception], |
| 1818 | +): |
| 1819 | + # Arrange |
| 1820 | + util.create_file_and_commit("feat: new file", filename="tracked.txt") |
| 1821 | + Path("tracked.txt").write_text("work in progress") |
| 1822 | + |
| 1823 | + # Act / Assert |
| 1824 | + with pytest.raises(expected_exception): |
| 1825 | + util.run_cli("bump", "--yes", "--no-allow-dirty", *extra_args) |
0 commit comments