Skip to content

fix(pipe): strip ANSI before the pytest filter in pipe mode - #4465

Merged
aeppling merged 3 commits into
rtk-ai:developfrom
DockSky:fix/pipe-pytest-strip-ansi
Oct 7, 2026
Merged

aeppling merged 3 commits into
rtk-ai:developfrom
DockSky:fix/pipe-pytest-strip-ansi

Conversation

@DockSky

@DockSky DockSky commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Problem

rtk pipe --filter pytest reports a false "Pytest: No tests collected" when pytest's output contains ANSI color codes.

Tool mode (rtk pytest) strips ANSI before calling filter_pytest_output (see the strip_ansi(raw) call in pytest_cmd::run), but the pipe path in resolve_filter passes stdin straight to the filter, so a colored summary line is invisible to the parser.

Repro:

printf 'tests/x.py \033[32m.\033[0m [100%%]\n\n\033[32m===== \033[1m12 passed\033[0m \033[32m in 21.90s\033[0m =====\033[0m\n' | rtk pipe --filter pytest
# Pytest: No tests collected   <-- wrong, 12 tests passed

This matters for agents: pytest writes color even when piped (auto-detection is off in many CI/venv setups), so a compacted run reports no tests at all and pushes users to bypass RTK with raw output.

Fix

Route the pipe path through a small pytest_wrapper that applies strip_ansi first, mirroring tool mode. No filter logic changes.

Testing

  • New regression test test_pipe_pytest_strips_ansi_before_parsing feeds a colored summary through resolve_filter("pytest") and asserts the parsed counts.
  • Existing pytest filter tests unchanged and passing (cargo test strips_ansi: 8 passed).
  • Verified end-to-end against a live agent pipeline: colored pytest output now compacts to Pytest: 3 passed in 1.50s instead of Pytest: No tests collected.

A colored pytest summary was reported as 'Pytest: No tests collected'
because resolve_filter passed raw stdin to filter_pytest_output while
tool mode (run) strips ANSI first. Regression test included.
@CLAassistant

CLAassistant commented Oct 6, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working pytest pipe-mode labels Oct 6, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟢 Risk low

Summary

Fixes a bug where rtk pipe --filter pytest falsely reports no tests collected when pytest output contains ANSI color codes, because the pipe path skipped the ANSI stripping that tool mode performs. The fix adds a pytest_wrapper that strips ANSI before invoking the existing filter_pytest_output parser, with a new regression test.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Analyzed automatically by wshm · This is an automated analysis, not a human review.

@aeppling aeppling left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reporting and contributing!

The fix works, but it is at the wrong level: it adds a second call site that knows filter_pytest_output cannot handle ANSI, instead of removing that requirement. The filter still mis-parses for any future caller, and that hidden precondition is exactly how this bug happened (tool mode knew, pipe mode did not). cargo has the same structure and is broken through the pipe today.

The repo's pattern is filter_phpunit_output: the parser strips for itself, so no caller can get it wrong. Same fix here:

pub(crate) fn filter_pytest_output(output: &str) -> String {
    // pytest colors its summary even when piped (force-color CI/venv setups), and the
    // `N passed` / FAILURES anchors below never match through ANSI codes. The parser owns
    // the stripping, as `filter_phpunit_output` does, so no caller carries a precondition.
    let cleaned = strip_ansi(output);
    let output = cleaned.as_str();
    ...

Then drop the pre-strip in pytest_cmd::run's closure (its broken-run branch keeps one: truncate(strip_ansi(raw).trim(), ...), it uses the cleaned text too), and revert the pipe_cmd.rs hunk to the plain function pointer. Your test stays exactly as written and passes unchanged.

Move the strip out of callers (tool-mode closure and the pipe wrapper)
into the parser itself, as filter_phpunit_output already does, so no
caller can bypass the precondition that caused the pipe-mode bug.
The broken-run passthrough keeps its own strip. Pipe route reverts to
the plain function pointer. Regression test unchanged.
@DockSky

DockSky commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review, agreed on all points. The caller-side strip was me patching the symptom; the parser owning the precondition is the right shape.

Pushed in a249a90:

  • filter_pytest_output now strips ANSI itself (with a comment mirroring your rationale), so the tool-mode closure and any future caller carry no precondition.
  • pytest_cmd::run's closure drops its pre-strip; the broken-run passthrough keeps its own strip_ansi(raw).trim() as you specified.
  • pipe_cmd.rs reverted to the plain function pointer.
  • Regression test untouched and passing against the new structure, along with the full pytest filter suite (25 passed locally).

Happy to send a follow-up for the cargo pipe path with the same shape if that's useful. Otherwise I'll leave that to a separate issue/PR.

@aeppling

aeppling commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

@DockSky all green

thanks for contributing to RTK ;)

@aeppling
aeppling merged commit 8533612 into rtk-ai:develop Oct 7, 2026
11 checks passed
@rtk-release-bot rtk-release-bot Bot mentioned this pull request Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working pipe-mode pytest

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants