Skip to content

[Test Improver] fix(tests): strip ANSI escape codes in test_policy_status._ascii_only#1011

Draft
danielmeppiel wants to merge 1 commit intomainfrom
test-assist/fix-policy-status-ansi-v2-b99295174bf25aa3
Draft

[Test Improver] fix(tests): strip ANSI escape codes in test_policy_status._ascii_only#1011
danielmeppiel wants to merge 1 commit intomainfrom
test-assist/fix-policy-status-ansi-v2-b99295174bf25aa3

Conversation

@danielmeppiel
Copy link
Copy Markdown
Collaborator

🤖 Test Improver — automated AI assistant

Problem

test_policy_status.py had 7 consistently failing tests. The _ascii_only() helper was returning False because Rich outputs ANSI CSI escape sequences (\x1b[3m italic, \x1b[1m bold, \x1b[0m reset) even inside Click's CliRunner, which does not set a pseudo-TTY. The ESC byte (\x1b, codepoint 0x1B) is categorised as Cc (control character) by unicodedata, so the existing guard correctly rejects it — but incorrectly flags Rich's styling codes as a violation.

Failures:

FAILED test_policy_status.py::TestStatusFoundOutcome::test_renders_found_outcome
FAILED test_policy_status.py::TestStatusAbsentOutcome::test_renders_absent_cleanly
FAILED test_policy_status.py::TestStatusCachedStaleOutcome::test_renders_stale_with_refresh_error
FAILED test_policy_status.py::TestStatusAsciiOnly::test_renderings_are_ascii_safe[found-...]
FAILED test_policy_status.py::TestStatusAsciiOnly::test_renderings_are_ascii_safe[absent-...]
FAILED test_policy_status.py::TestStatusAsciiOnly::test_renderings_are_ascii_safe[cached_stale-...]
FAILED test_policy_status.py::TestStatusAsciiOnly::test_renderings_are_ascii_safe[disabled-...]
```

## Approach

- Add `_strip_ansi()` helper using `re.sub(r"\x1b\[[0-9;]*[A-Za-z]", "", text)` — strips CSI escape sequences only, leaving actual text intact.
- Call `_strip_ansi()` at the top of `_ascii_only()` before iteration.
- The intent of `_ascii_only` is to catch non-ASCII *authored* strings (emojis, smart quotes) in CLI output — Rich's ANSI styling is not authored text and should be excluded from the check.

## Test Status

| Suite | Before | After |
|---|---|---|
| `tests/unit/commands/test_policy_status.py` | 7 failed, 33 passed | **40 passed** |
| Full unit suite | blocked at first failure | **6564 passed** |

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

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

Trade-offs

  • Minimal change: one import (re), one compiled regex, one helper function.
  • The regex \x1b\[[0-9;]*[A-Za-z] covers standard CSI sequences; OSC/DCS sequences are not stripped but don't appear in Rich table output.

Generated by Daily Test Improver · ● 1.9M ·

To install this agentic workflow, run

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

Rich emits ANSI CSI sequences (\x1b[3m italic, \x1b[1m bold) even when
NO_COLOR is unset in the CliRunner environment. The _ascii_only helper
was checking raw bytes, causing 7 tests to fail with False negatives.

Fix: strip ANSI sequences via regex before the ASCII check, and add
_strip_ansi() as a reusable helper in the test module.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@danielmeppiel danielmeppiel added automation Deprecated: use type/automation. Kept for issue history; will be removed in milestone 0.10.0. testing Deprecated: use area/testing. Kept for issue history; will be removed in milestone 0.10.0. labels Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automation Deprecated: use type/automation. Kept for issue history; will be removed in milestone 0.10.0. testing Deprecated: use area/testing. Kept for issue history; will be removed in milestone 0.10.0.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant