Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions commitizen/commands/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
NoRevisionError,
NotAGitProjectError,
NotAllowed,
UnableToWriteChangelog,
)
from commitizen.git import GitTag, smart_open
from commitizen.tags import TagRules
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions commitizen/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"""

Expand Down
32 changes: 32 additions & 0 deletions tests/commands/test_changelog_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import TYPE_CHECKING

import pytest
from unittest.mock import Mock
from jinja2 import FileSystemLoader

from commitizen import git
Expand All @@ -18,6 +19,7 @@
NoRevisionError,
NotAGitProjectError,
NotAllowed,
UnableToWriteChangelog,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -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,
Expand Down
Loading