Files
Claude-BugHunter/engine/test_windows_encoding.py
dbb9a590e5 fix(engine): utf-8 encoding for all file/console I/O (Windows cp1252 crash) (#191)
Fixes #189. On Windows (and any non-UTF-8 locale) text-mode open() inherits the
code page (cp1252), so writing the engine's '->' / em-dash characters to the log,
arsenal map, or report crashed with UnicodeEncodeError, and reading them back
could mojibake.

- Pin encoding="utf-8" on every text-mode open() in engine/ (state.py, engine.py,
  memory.py, scope.py) and the subprocess-reading tests.
- Reconfigure stdout/stderr to utf-8 in engine main() so console output of the same
  characters does not crash the CLI run.
- Make Engagement.log()'s print non-fatal on a non-UTF-8 console: logging is a side
  channel and must never crash the run; the full line is already in the utf-8 log.
- Parent-side decode of child engine output in the mock subprocess tests pinned to
  utf-8 (the two tests #189 reports failing on Windows).

New engine/test_windows_encoding.py reproduces the crash deterministically on any
host by forcing an ASCII default (PYTHONUTF8=0 PYTHONCOERCECLOCALE=0 LC_ALL=C), so
the regression is caught on the existing ubuntu CI without a windows runner.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-20 21:23:22 +05:30

36 lines
1.4 KiB
Python

"""Regression test for issue #190 (engine UnicodeEncodeError on non-UTF-8 locales).
On Windows the default code page is cp1252, so text-mode open() calls without an
explicit encoding crash when the engine writes '->' / em-dash characters to the
log, arsenal map, or report. We can reproduce that deterministically on any host
by forcing an ASCII default encoding (PYTHONUTF8=0 + PYTHONCOERCECLOCALE=0 +
LC_ALL=C), which is what Windows hits with cp1252.
"""
import os
import subprocess
import sys
import tempfile
import unittest
_ENGINE = os.path.dirname(os.path.abspath(__file__))
_REPO = os.path.dirname(_ENGINE)
_ASCII_ENV = dict(os.environ, PYTHONUTF8="0", PYTHONCOERCECLOCALE="0", LC_ALL="C")
class WindowsEncodingTests(unittest.TestCase):
def test_mock_hunt_run_survives_ascii_locale(self):
"""Full --mock --hunt flow must finish without a UnicodeEncodeError."""
with tempfile.TemporaryDirectory() as base:
r = subprocess.run(
[sys.executable, "engine/engine.py",
"--scope", "engine/engagement.example.json",
"--base", base, "--mock", "--hunt"],
cwd=_REPO, env=_ASCII_ENV, capture_output=True, text=True, encoding="utf-8")
self.assertEqual(r.returncode, 0,
f"engine crashed under ASCII locale:\n{r.stderr}")
self.assertNotIn("UnicodeEncodeError", r.stderr)
if __name__ == "__main__":
unittest.main()