From b0a2077767153ac1c83c6d05ec270620bc7e9771 Mon Sep 17 00:00:00 2001 From: Daniel Meppiel <51440732+danielmeppiel@users.noreply.github.com> Date: Mon, 27 Apr 2026 05:46:29 +0200 Subject: [PATCH] fix(tests): strip ANSI escape sequences in _ascii_only helper The test helper _ascii_only in test_policy_status.py was returning False for any control character (category Cc), which includes the ANSI ESC byte (U+001B) emitted by Rich for italic/bold styling even when NO_COLOR=1 is set (NO_COLOR suppresses colour codes but not all styling). Strip CSI escape sequences (\x1b[ ... ) before the character loop, since the intent of _ascii_only is to detect non-ASCII Unicode content we author (emojis, smart-quotes) -- not to ban Rich's own ANSI rendering codes. This restores all 7 previously-failing assertions across TestStatusFoundOutcome, TestStatusNotFound, TestStatusDisabled, and TestStatusAsciiOnly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- tests/unit/commands/test_policy_status.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) 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;