Skip to content

[Test Improver] fix(tests): strip ANSI escape sequences in _ascii_only helper (test_policy_status.py) - #977

Closed
Daniel Meppiel (danielmeppiel) wants to merge 1 commit into
mainfrom
test-assist/fix-policy-status-ansi-24975296739-53298e7dea0d296d
Closed

Daniel Meppiel (danielmeppiel) wants to merge 1 commit into
mainfrom
test-assist/fix-policy-status-ansi-24975296739-53298e7dea0d296d

Conversation

@danielmeppiel

Copy link
Copy Markdown
Collaborator

🤖 Test Improver — automated AI assistant

Problem

tests/unit/commands/test_policy_status.py::TestStatusFoundOutcome::test_renders_found_outcome and 6 sibling tests were failing with:

AssertionError: assert False
 where False = _ascii_only('\x1b[3m   APM Policy Status   \x1b[0m\n┏━━━...')
```

The `_ascii_only` helper returned `False` for any character whose `unicodedata.category` starts with `"C"` (control characters). This caught `\x1b` (U+001B, ESC), the opening byte of every ANSI CSI escape sequence Rich emits for italic/bold terminal styling — even when `NO_COLOR=1` is set in the environment (`NO_COLOR` suppresses colour codes but **not** all styling codes such as `\x1b[3m` italic or `\x1b[1m` bold).

## Approach

Strip CSI escape sequences (`\x1b[...letter`) from the rendered output **before** the character loop in `_ascii_only`.

The **intent** of the helper is to detect non-ASCII Unicode *content* characters we author (emojis, smart-quotes, unexpected symbols) that would violate the repo's cross-platform encoding rule. ANSI escape sequences are generated by Rich at render time, not authored in source strings — they are acceptable in terminal output and should not trigger the guard.

## Changes

- `tests/unit/commands/test_policy_status.py`: add `import re`; rewrite `_ascii_only` to strip CSI sequences before checking each character; update docstring.

## Test Status

| Suite | Before | After |
|---|---|---|
| `test_policy_status.py` | 33 pass / **7 fail** | **40 pass / 0 fail** |
| Full unit suite | 5476 pass / **1 fail (first hit)** | **5516 pass / 0 fail** |

```
uv run pytest tests/unit/commands/test_policy_status.py -q
# 40 passed in 0.41s

uv run pytest tests/unit tests/test_console.py -q
# 5516 passed, 1 warning, 26 subtests passed in 19.07s

Trade-offs

  • The regex r"\x1b\[[0-9;]*[A-Za-z]" covers standard CSI sequences. Any non-CSI escape bytes still present after stripping (e.g. a stray bare \x1b) would be caught as a control character, preserving the safety net.
  • Box-drawing-character allowlist in the function is unchanged.

Generated by Daily Test Improver · ● 2.8M · ◷

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/daily-test-improver.md@b87234850bf9664d198f28a02df0f937d0447295

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[ ... <letter>) 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>
@danielmeppiel Daniel Meppiel (danielmeppiel) added automation Legacy classification name; prefer type/automation. Retained while active writers are migrated. testing Legacy classification name; prefer area/testing. Retained for history. labels Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automation Legacy classification name; prefer type/automation. Retained while active writers are migrated. testing Legacy classification name; prefer area/testing. Retained for history.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant