From 64d0da9f0ccc69617453a19b4753d6884ea03dff Mon Sep 17 00:00:00 2001 From: QianEric Date: Sun, 22 Nov 2026 20:14:05 +0800 Subject: [PATCH] fix(changelog): report an unwritable changelog file `cz changelog --file path/to/CHANGELOG.md` crashed with a traceback when the file could not be written, for example when the parent directory does not exist: $ cz changelog --file /nope/CHANGELOG.md Traceback (most recent call last): File "commitizen/commands/changelog.py", line 166, in _write_changelog with smart_open( File "commitizen/git.py", line 206, in smart_open return open(name, mode, encoding=encoding) FileNotFoundError: [Errno 2] No such file or directory: '/nope/CHANGELOG.md' Every other command reports such a problem with a dedicated exception and exit code. This change adds `UnableToWriteChangelog` with the new `ExitCode.CHANGELOG_WRITE_FAILED` (33) and raises it from `_write_changelog`: $ cz changelog --file /nope/CHANGELOG.md Unable to write the changelog file '/nope/CHANGELOG.md': [Errno 2] No such file or directory: '/nope/CHANGELOG.md' $ echo $? 33 Unchanged: a writable `--file` path writes the changelog, `--dry-run` does not touch the filesystem, and the in-place `CHANGELOG.md` flow keeps working. Tests: `test_changelog_unwritable_file` in `tests/commands/test_changelog_command.py` covers the case (fails with the traceback before the change). Assisted By: AI --- commitizen/commands/changelog.py | 14 ++++++++--- commitizen/exceptions.py | 7 ++++++ tests/commands/test_changelog_command.py | 32 ++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index abe03a9241..58a34737bd 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -15,6 +15,7 @@ NoRevisionError, NotAGitProjectError, NotAllowed, + UnableToWriteChangelog, ) from commitizen.git import GitTag, smart_open from commitizen.tags import TagRules @@ -163,9 +164,16 @@ def _find_incremental_rev(self, latest_version: str, tags: Iterable[GitTag]) -> def _write_changelog( self, changelog_out: str, lines: list[str], changelog_meta: changelog.Metadata ) -> None: - with smart_open( - self.file_name, "w", encoding=self.config.settings["encoding"] - ) as changelog_file: + try: + changelog_handle = smart_open( + self.file_name, "w", encoding=self.config.settings["encoding"] + ) + except OSError as error: + raise UnableToWriteChangelog( + f"Unable to write the changelog file '{self.file_name}': {error}" + ) from error + + with changelog_handle as changelog_file: partial_changelog: str | None = None if self.incremental: new_lines = changelog.incremental_build( diff --git a/commitizen/exceptions.py b/commitizen/exceptions.py index d4f0d8f2de..257b09e446 100644 --- a/commitizen/exceptions.py +++ b/commitizen/exceptions.py @@ -40,6 +40,7 @@ class ExitCode(IntEnum): CONFIG_FILE_NOT_FOUND = 30 CONFIG_FILE_IS_EMPTY = 31 COMMIT_MESSAGE_LENGTH_LIMIT_EXCEEDED = 32 + CHANGELOG_WRITE_FAILED = 33 @classmethod def from_str(cls, value: str) -> ExitCode: @@ -226,6 +227,12 @@ class GitCommandError(CommitizenException): exit_code = ExitCode.GIT_COMMAND_ERROR +class UnableToWriteChangelog(CommitizenException): + """The changelog file could not be written""" + + exit_code = ExitCode.CHANGELOG_WRITE_FAILED + + class InvalidManualVersion(CommitizenException): """Manually provided version is invalid""" diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 8922b256f9..9ae98be064 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -6,6 +6,7 @@ from typing import TYPE_CHECKING import pytest +from unittest.mock import Mock from jinja2 import FileSystemLoader from commitizen import git @@ -18,6 +19,7 @@ NoRevisionError, NotAGitProjectError, NotAllowed, + UnableToWriteChangelog, ) if TYPE_CHECKING: @@ -183,6 +185,36 @@ def test_changelog_replacing_unreleased_using_incremental( @pytest.mark.usefixtures("tmp_commitizen_project") @pytest.mark.freeze_time("2022-08-14") +@pytest.mark.usefixtures("in_repo_root") +def test_changelog_unwritable_file(tmp_path: Path): + blocker = tmp_path / "blocker" + blocker.write_text("not a directory", encoding="utf-8") + + mock_config = Mock() + mock_path = Mock(spec=Path) + mock_path.parent = tmp_path + mock_config.path = mock_path + mock_config.settings = { + "name": "cz_conventional_commits", + "changelog_file": "CHANGELOG.md", + "encoding": "utf-8", + "tag_format": "$version", + "legacy_tag_formats": [], + "ignored_tag_formats": [], + "incremental": False, + "changelog_merge_prerelease": False, + } + changelog = Changelog( + mock_config, + {"file_name": str(blocker / "CHANGELOG.md"), "unreleased_version": "1.0.1"}, + ) + + with pytest.raises(UnableToWriteChangelog) as error: + changelog._write_changelog("changelog body", [], Mock()) + + assert "Unable to write the changelog file" in str(error.value) + + def test_changelog_is_persisted_using_incremental( changelog_path: Path, file_regression: FileRegressionFixture,