Skip to content

Commit 26def49

Browse files
committed
Add test to reproduce Windows cp1252 encoding error (#55)
This test reproduces the UnicodeEncodeError that occurs on Windows CI when logging a message containing an emoji (🎂) on main.py:81. The test simulates Windows console behavior by: - Creating a stream with cp1252 encoding (Windows default) - Attempting to log the emoji message - Verifying that UnicodeEncodeError is captured in stderr This allows reproduction of the Windows-specific encoding issue on Linux without requiring actual Windows environment. Related to #55
1 parent 42f4178 commit 26def49

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

tests/test_logging.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import io
12
import logging
23
import sys
34

@@ -102,3 +103,54 @@ def test_setup_logger_no_formatter_for_info():
102103
"%(asctime)s" not in handler.formatter._fmt
103104
or handler.formatter._fmt is None
104105
)
106+
107+
108+
def test_emoji_logging_with_cp1252_encoding(capsys, caplog):
109+
"""Test that logging emojis with cp1252 encoding raises UnicodeEncodeError.
110+
111+
This reproduces the Windows CI error where the console uses cp1252 encoding
112+
which cannot handle Unicode emojis like 🎂.
113+
114+
Error from Windows CI:
115+
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f382'
116+
in position 0: character maps to <undefined>
117+
"""
118+
from mxdev.logging import logger
119+
120+
# Clear any existing handlers
121+
root = logging.getLogger()
122+
root.handlers.clear()
123+
124+
# Create a stream with cp1252 encoding (simulating Windows console)
125+
# Use errors='strict' to ensure it raises on unencodable characters
126+
stream = io.TextIOWrapper(
127+
io.BytesIO(),
128+
encoding='cp1252',
129+
errors='strict',
130+
line_buffering=True
131+
)
132+
133+
# Set up handler with the cp1252 stream
134+
handler = logging.StreamHandler(stream)
135+
handler.setLevel(logging.INFO)
136+
root.addHandler(handler)
137+
root.setLevel(logging.INFO)
138+
139+
# This is the exact emoji from main.py:81 that causes the issue
140+
emoji_message = "🎂 You are now ready for: pip install -r requirements-mxdev.txt"
141+
142+
# When logging fails due to encoding, Python's logging module catches
143+
# the error and prints it to stderr via handleError(), but doesn't raise
144+
logger.info(emoji_message)
145+
146+
# Capture stderr to check for the encoding error
147+
captured = capsys.readouterr()
148+
149+
# Verify the UnicodeEncodeError was logged to stderr
150+
assert "UnicodeEncodeError" in captured.err
151+
assert "charmap" in captured.err or "cp1252" in captured.err
152+
assert "\\U0001f382" in captured.err or "U0001f382" in captured.err
153+
assert emoji_message in captured.err
154+
155+
# Clean up
156+
root.handlers.clear()

0 commit comments

Comments
 (0)