Skip to content

Add RichCliRunner for testing Rich prompt/output capture#326

Open
mirs002 wants to merge 2 commits intoewels:mainfrom
mirs002:main
Open

Add RichCliRunner for testing Rich prompt/output capture#326
mirs002 wants to merge 2 commits intoewels:mainfrom
mirs002:main

Conversation

@mirs002
Copy link

@mirs002 mirs002 commented Mar 2, 2026

Summary

  • add rich_click.testing.RichCliRunner, a click.testing.CliRunner subclass
  • rebind Rich's global cached console during CliRunner.isolation() so output goes to Click's isolated stdout/stderr streams
  • restore the previous global Rich console after invocation to avoid leaking test state
  • export RichCliRunner from rich_click
  • add regression tests covering prompt capture behavior with a preconfigured global Rich console

Why

When Rich's global console has already been initialized before CliRunner.invoke(), prompt / console output may go to the pre-existing console stream instead of Click's test-captured streams. This makes asserting Rich prompt output difficult in tests.

This change provides a dedicated runner for Rich-based CLIs that keeps Rich aligned with Click's test isolation.

Closes #232.

@mirs002
Copy link
Author

mirs002 commented Mar 2, 2026

Note on the failing test (3.10, 8.1.*, 14.*) job

Just a heads-up that the one remaining CI failure is unrelated to this PR.

The job fails with an ImportError in conftest.py when importing Typer — before any of the new code in this PR is even loaded:

from typer.testing import CliRunner as TyperCliRunner
...
TypeError: 'type' object is not subscriptable

This is caused by Typer 0.24.x (released recently) dropping support for the Python 3.10 + Click 8.1 combination. The same job was passing on the last push to main on Jan 31 — before Typer 0.24 was released.

You can verify this by re-running the main branch CI — it will reproduce the same failure independently of this PR.

Happy to update the PR once this is resolved upstream or the matrix is adjusted! 🙂

@dwreeves
Copy link
Collaborator

dwreeves commented Mar 2, 2026

Thanks! I'll review when I get a moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: provide modified CliRunner

2 participants