Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions tests/unit/commands/test_policy_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import json
import re
import textwrap
import unicodedata
from pathlib import Path
Expand Down Expand Up @@ -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[ ... <letter>) 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;
Expand Down
Loading