fix(tests): fix Windows test failures with click 8.2 encoding#2
fix(tests): fix Windows test failures with click 8.2 encoding#2bobo-xxx wants to merge 1 commit into
Conversation
Set PYTHONIOENCODING=utf-8 environment variable for Windows platform to ensure proper Unicode handling when tests output Unicode characters through click.echo(). Fixes encoding issues that cause UnicodeEncodeError on Windows when tests output Unicode characters.
There was a problem hiding this comment.
Pull request overview
Adjusts the test environment to avoid Windows CP1252 stdout/stderr encoding errors triggered by Unicode output in Click 8.2+.
Changes:
- Set
PYTHONIOENCODING=utf-8intox.inifor test environments. - Added a Windows-only autouse session fixture in
tests/conftest.pyintended to force UTF-8 I/O behavior during tests.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
tox.ini |
Forces UTF-8 I/O encoding for tox-driven test runs to prevent Windows Unicode encoding failures. |
tests/conftest.py |
Adds a session autouse fixture attempting to mitigate Windows encoding issues during pytest execution. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if sys.platform == "win32": | ||
| os.environ["PYTHONIOENCODING"] = "utf-8" | ||
|
|
||
|
|
There was a problem hiding this comment.
Setting PYTHONIOENCODING inside a pytest fixture won’t reconfigure the already-created sys.stdout/sys.stderr for the current Python process (Python only uses PYTHONIOENCODING at interpreter startup). This means the fixture is unlikely to prevent the UnicodeEncodeError coming from click.echo() writing to stdout on Windows when running python -m pytest. Consider either (a) relying solely on tox/CI to set PYTHONIOENCODING before Python starts and remove this fixture, or (b) in this fixture, reconfigure sys.stdout/sys.stderr to UTF-8 (and optionally only set the env var via setdefault for subprocesses).
| if sys.platform == "win32": | |
| os.environ["PYTHONIOENCODING"] = "utf-8" | |
| if sys.platform != "win32": | |
| return | |
| os.environ.setdefault("PYTHONIOENCODING", "utf-8") | |
| for stream in (sys.stdout, sys.stderr): | |
| if stream is not None and hasattr(stream, "reconfigure"): | |
| stream.reconfigure(encoding="utf-8") |
Fixes Windows test failures related to click 8.2 encoding issues.
Problem
Windows uses CP1252 encoding by default, which cannot handle Unicode characters output by
click.echo(). This causes test failures on Windows when using click 8.2+ with error messages like:Solution
Set
PYTHONIOENCODING=utf-8environment variable to ensure proper Unicode handling:fix_windows_encodingfixture that setsPYTHONIOENCODING=utf-8at module level for Windows platform during test collectionsetenv = PYTHONIOENCODING=utf-8for all test environmentsChanges
fix_windows_encodingautouse session fixture intests/conftest.pyPYTHONIOENCODING=utf-8viasetenvin tox.ini testenv configurationFixes encoding issues that cause UnicodeEncodeError on Windows when tests output Unicode characters through click.echo().