Repository navigation
fix(changelog): read and write the exported template as UTF-8 - #2138
Open
stella-h-choi wants to merge 5 commits into
Open
stella-h-choi wants to merge 5 commits into
stella-h-choi wants to merge 5 commits into
Conversation
`cz changelog --export-template` used the locale encoding, while Jinja loads the template as UTF-8, so a non-ASCII template failed to export where the locale is not UTF-8 (for example EUC-KR or cp949). Fixes commitizen-tools#2136 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2138 +/- ##
=======================================
Coverage 98.29% 98.29%
=======================================
Files 61 61
Lines 2874 2874
=======================================
Hits 2825 2825
Misses 49 49 ☔ View full report in Codecov by Harness. |
Windows writes text files with CRLF line endings, so compare the decoded text instead of the raw bytes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Undo the byte copy and the uv.lock change that slipped into the previous commit. The test now compares decoded text, so the UTF-8 read and write pass on Windows as well. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Reading and writing in text mode translates newlines on Windows, so the exported file ended up with CRLF. Copy the bytes instead and compare bytes in the test again. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This reverts commit f978122, which was pushed by mistake. The UTF-8 read and write with the text comparison already pass on Windows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
cz changelog --export-templateread the template and wrote the copy in the locale encoding. Jinja loads the same template as UTF-8, so a template that renders could fail to export where the locale is not UTF-8 (for example EUC-KR on Linux or cp949 on Korean Windows):_export_templatenow reads and writes the file as UTF-8, so the exported file has the same bytes as the template.Each changed line:
commitizen/commands/changelog.py:read_text(encoding="utf-8")decodes the template the way Jinja's loader does, instead of with the locale encoding.commitizen/commands/changelog.py:write_text(text, encoding="utf-8")writes the copy back in UTF-8. Without it, the read succeeds and the write fails on characters the locale cannot encode (UnicodeEncodeError: 'cp949' codec can't encode character '✨').tests/commands/test_changelog_command.py: the new test patchesio.text_encoding, which pathlib uses to pick the encoding when none is given, to returncp949. This simulates a non-UTF-8 locale on any OS. The template has Korean text, whose UTF-8 bytes cp949 cannot decode, and a character cp949 cannot encode, so the test fails if either line is missing.Fixes #2136
Checklist
Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code (claude-opus-5-5) following the guidelines
Code Changes
uv run poe alllocally to ensure this change passes linter check and testsExpected Behavior
cz changelog --export-templatewrites a file with the same bytes as the template, whatever the locale encoding is.Steps to Test This Pull Request
uv run pytest tests/commands/test_changelog_command.py -k non_utf8_locale. It fails on master with theUnicodeDecodeErrorabove, fails with only the read fixed with aUnicodeEncodeError, and passes with both lines.ko_KR.EUC-KRlocale generated, run the steps fromcz changelog --export-templateraises UnicodeDecodeError on a non-ASCII template where the locale is not UTF-8 #2136 in an empty directory.cz changelog --export-template out.mdexits 0 andcmp my.md.j2 out.mdreports no difference.Additional Context
#1854 changed these two lines among others and was closed without a reproducing test. #2119 fixed
cz commit --editandcz initthe same way and did not touch them.🤖 Generated with Claude Code