-
-
Notifications
You must be signed in to change notification settings - Fork 343
fix(out): reconfigure stdout to UTF-8 on any non-UTF-8 terminal #1975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bearomorphism
wants to merge
3
commits into
commitizen-tools:master
Choose a base branch
from
bearomorphism:fix/956-cross-platform-stdout-encoding
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| """Tests for ``commitizen.out``. | ||
|
|
||
| Mostly focused on the stdout-encoding helper introduced for #956: the | ||
| function must reconfigure non-UTF-8 streams to UTF-8 with a permissive | ||
| ``errors="replace"`` strategy so commitizen output (emoji, typographic | ||
| quotes) doesn't crash with ``UnicodeEncodeError`` on terminals using | ||
| locale-dependent encodings such as ``cp1252`` (Windows) or | ||
| ``ISO8859-1`` (Linux/macOS). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import io | ||
| from typing import Any | ||
|
|
||
| from commitizen.out import _ensure_utf8_stdout | ||
|
|
||
|
|
||
| class _StubStream(io.TextIOWrapper): | ||
| """Light-weight ``TextIOWrapper`` that records calls to ``reconfigure``. | ||
|
|
||
| Subclassing ``TextIOWrapper`` keeps the ``isinstance`` check in | ||
| ``_ensure_utf8_stdout`` happy without monkey-patching ``sys.stdout``. | ||
| """ | ||
|
|
||
| reconfigure_calls: list[dict[str, Any]] | ||
|
|
||
| def __init__(self, encoding: str) -> None: | ||
| super().__init__(io.BytesIO(), encoding=encoding) | ||
| self.reconfigure_calls = [] | ||
|
|
||
| def reconfigure(self, **kwargs: Any) -> None: | ||
| self.reconfigure_calls.append(kwargs) | ||
| super().reconfigure(**kwargs) | ||
|
|
||
|
|
||
| def test_ensure_utf8_stdout_noop_when_already_utf8(): | ||
| stream = _StubStream(encoding="utf-8") | ||
| _ensure_utf8_stdout(stream) | ||
| assert stream.reconfigure_calls == [] | ||
|
|
||
|
|
||
| def test_ensure_utf8_stdout_noop_for_dashless_utf8_alias(): | ||
| stream = _StubStream(encoding="UTF8") | ||
| _ensure_utf8_stdout(stream) | ||
| assert stream.reconfigure_calls == [] | ||
|
|
||
|
|
||
| def test_ensure_utf8_stdout_reconfigures_iso8859_1_terminal(): | ||
| """Regression test for #956 (Linux/macOS ``LANG=de_CH.ISO8859-1``).""" | ||
| stream = _StubStream(encoding="latin-1") | ||
| _ensure_utf8_stdout(stream) | ||
| assert stream.reconfigure_calls == [{"encoding": "utf-8", "errors": "replace"}] | ||
|
|
||
|
|
||
| def test_ensure_utf8_stdout_reconfigures_windows_cp1252(): | ||
| """Regression test for the historical Windows ``cmd.exe`` case.""" | ||
| stream = _StubStream(encoding="cp1252") | ||
| _ensure_utf8_stdout(stream) | ||
| assert stream.reconfigure_calls == [{"encoding": "utf-8", "errors": "replace"}] | ||
|
|
||
|
|
||
| def test_ensure_utf8_stdout_skips_non_textio_streams(): | ||
| class NotATextIO: | ||
| encoding = "latin-1" | ||
| reconfigure_calls: list[dict[str, Any]] = [] | ||
|
|
||
| def reconfigure(self, **kwargs: Any) -> None: # pragma: no cover - unused | ||
| self.reconfigure_calls.append(kwargs) | ||
|
|
||
| stream = NotATextIO() | ||
| _ensure_utf8_stdout(stream) | ||
| assert stream.reconfigure_calls == [] | ||
|
|
||
|
|
||
| def test_ensure_utf8_stdout_after_reconfigure_can_emit_emoji(): | ||
| """End-to-end: after reconfiguration, writing an emoji must not raise.""" | ||
| stream = _StubStream(encoding="latin-1") | ||
| _ensure_utf8_stdout(stream) | ||
|
|
||
| # Should not raise UnicodeEncodeError; ``errors="replace"`` lets | ||
| # genuinely-unrenderable bytes fall through as ``?`` instead of | ||
| # crashing the whole command. | ||
|
bearomorphism marked this conversation as resolved.
Outdated
|
||
| stream.write("Configuration complete \U0001f680") | ||
| stream.flush() | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.