Repository navigation
fix(pipe): strip ANSI before the pytest filter in pipe mode - #4465
Conversation
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.
📊 Automated PR Analysis
SummaryFixes a bug where Review Checklist
Analyzed automatically by wshm · This is an automated analysis, not a human review. |
aeppling
left a comment
There was a problem hiding this comment.
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.
|
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:
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. |
|
@DockSky all green thanks for contributing to RTK ;) |
Problem
rtk pipe --filter pytestreports a false "Pytest: No tests collected" when pytest's output contains ANSI color codes.Tool mode (
rtk pytest) strips ANSI before callingfilter_pytest_output(see thestrip_ansi(raw)call inpytest_cmd::run), but the pipe path inresolve_filterpasses stdin straight to the filter, so a colored summary line is invisible to the parser.Repro:
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_wrapperthat appliesstrip_ansifirst, mirroring tool mode. No filter logic changes.Testing
test_pipe_pytest_strips_ansi_before_parsingfeeds a colored summary throughresolve_filter("pytest")and asserts the parsed counts.cargo test strips_ansi: 8 passed).Pytest: 3 passed in 1.50sinstead ofPytest: No tests collected.