diff --git a/tests/unit/commands/test_policy_status.py b/tests/unit/commands/test_policy_status.py index 65a5b14c1..2a4266e6b 100644 --- a/tests/unit/commands/test_policy_status.py +++ b/tests/unit/commands/test_policy_status.py @@ -3,6 +3,7 @@ from __future__ import annotations import json +import re import textwrap import unicodedata from pathlib import Path @@ -68,15 +69,25 @@ def _rich_policy() -> ApmPolicy: def _ascii_only(text: str) -> bool: - """Return True iff every codepoint is printable ASCII (U+0020-U+007E).""" + """Return True iff every codepoint (after stripping ANSI sequences) is + printable ASCII (U+0020-U+007E) or an allowed Rich box-drawing character. + + ANSI escape sequences (e.g. ``\\x1b[1m``, ``\\x1b[0m``) are generated by + Rich for terminal styling. They are stripped before the check because the + intent is to detect *content* characters we author that violate the repo's + cross-platform encoding rule (emojis, smart-quotes, etc.) -- not to ban + Rich's own ANSI rendering codes. + """ + # Strip CSI escape sequences (\x1b[ ... ) produced by Rich. + _ansi = re.compile(r"\x1b\[[0-9;]*[A-Za-z]") + text = _ansi.sub("", text) for ch in text: if ch in ("\n", "\r", "\t"): continue cp = ord(ch) if cp < 0x20 or cp > 0x7E: - # Allow Rich box-drawing characters in *rendered* output -- - # they originate from the Rich library, not our source code. - # The encoding rule applies to source/CLI strings we author. + # Remaining control characters (e.g. stray ESC not part of a CSI + # sequence) are not acceptable. if unicodedata.category(ch).startswith("C"): return False # Box-drawing & similar are tolerated in Rich-rendered output;