Skip to content

Commit 1ab3be0

Browse files
committed
fix: make stdout UTF-8 for Windows runners
Emoji and check marks (✔ ✖ ❌) cannot be encoded by the default cp1252 codec on Windows, crashing the action. Reconfigure stdout/stderr to UTF-8, matching commit-check core's _reconfigure_io.
1 parent 82f1f1d commit 1ab3be0

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎main.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ def env_flag(name: str, default: str = "false") -> bool:
4040
return os.getenv(name, default).lower() == "true"
4141

4242

43+
def _reconfigure_io() -> None:
44+
"""Reconfigure stdout/stderr to UTF-8 so emoji and check marks never
45+
crash on runners with legacy encodings (e.g. cp1252 on Windows)."""
46+
for stream in (sys.stdout, sys.stderr):
47+
if hasattr(stream, "reconfigure"):
48+
stream.reconfigure(encoding="utf-8", errors="replace")
49+
50+
4351
MESSAGE_ENABLED = env_flag("MESSAGE")
4452
BRANCH_ENABLED = env_flag("BRANCH")
4553
AUTHOR_NAME_ENABLED = env_flag("AUTHOR_NAME")
@@ -646,6 +654,7 @@ def log_error_and_exit(ret_code: int, results: list[ScopeResult]) -> None:
646654

647655
def main():
648656
"""Main function to run commit-check and render all output surfaces."""
657+
_reconfigure_io()
649658
log_env_vars()
650659

651660
ret_code, results = run_commit_check()

‎main_test.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,40 @@ def test_missing_uses_default(self):
8181
self.assertTrue(main.env_flag("FEATURE_FLAG", default="true"))
8282

8383

84+
class TestReconfigureIo(unittest.TestCase):
85+
def test_reconfigures_streams_to_utf8(self):
86+
class FakeStream:
87+
def __init__(self):
88+
self.reconfigured = None
89+
90+
def reconfigure(self, **kwargs):
91+
self.reconfigured = kwargs
92+
93+
fake_out = FakeStream()
94+
fake_err = FakeStream()
95+
with (
96+
patch.object(sys, "stdout", fake_out),
97+
patch.object(sys, "stderr", fake_err),
98+
):
99+
main._reconfigure_io()
100+
self.assertEqual(
101+
fake_out.reconfigured, {"encoding": "utf-8", "errors": "replace"}
102+
)
103+
self.assertEqual(
104+
fake_err.reconfigured, {"encoding": "utf-8", "errors": "replace"}
105+
)
106+
107+
def test_streams_without_reconfigure_are_ignored(self):
108+
class NoopStream:
109+
pass
110+
111+
with (
112+
patch.object(sys, "stdout", NoopStream()),
113+
patch.object(sys, "stderr", NoopStream()),
114+
):
115+
main._reconfigure_io() # should not raise
116+
117+
84118
class TestBuildCheckArgs(unittest.TestCase):
85119
def test_all_true(self):
86120
with (

0 commit comments

Comments
 (0)