Skip to content

WIP: Add capture mode to CliRunner and revert default fileno exposure#3391

Draft
kdeldycke wants to merge 1 commit intopallets:mainfrom
kdeldycke:fix-3384-clirunner-fd-capture
Draft

WIP: Add capture mode to CliRunner and revert default fileno exposure#3391
kdeldycke wants to merge 1 commit intopallets:mainfrom
kdeldycke:fix-3384-clirunner-fd-capture

Conversation

@kdeldycke
Copy link
Copy Markdown
Collaborator

@kdeldycke kdeldycke commented Apr 29, 2026

This follows up on #3244 which worked great to fix #2865 where a user was bitten CliRunner wholesale substitution of stdout and stderr by its own wrappers to capture a CLI I/O. Unfortunately this broke tests for users bypassing the bypass and duplicating standard streams is duplicated with os.dup2. This is what was reported in #3384.

To fix this tension, we extend the solution introduced in #3244 by adding a capture mode parameter to the CliRunner. This complete what we started in #3244 by mirroring Pytest capsys and capfd modes.

Modes have the advantage of flexibility to address each user's own needs, while establishing a contract of expectations. I updated the docs to explain each mode and their expectations.

I also reverted back to the behavior of CliRunner pre-8.3.3 release.

Also note that this pave the way to fix #737 in the future by extending _FDCapture by opening the possibility of adding a tee mode.

@kdeldycke kdeldycke added this to the 8.4.0 milestone Apr 29, 2026
@kdeldycke kdeldycke marked this pull request as draft April 29, 2026 10:28
@kdeldycke kdeldycke linked an issue Apr 29, 2026 that may be closed by this pull request
@kdeldycke kdeldycke added bug f:test runner feature: cli test runner labels Apr 29, 2026
@kdeldycke kdeldycke changed the title WIP: Add capture parameter to CliRunner and revert default-mode fileno exposure WIP: Add capture mode to CliRunner and revert default fileno exposure Apr 29, 2026
@kdeldycke kdeldycke linked an issue Apr 29, 2026 that may be closed by this pull request
@kdeldycke kdeldycke linked an issue Apr 29, 2026 that may be closed by this pull request
@kdeldycke kdeldycke force-pushed the fix-3384-clirunner-fd-capture branch from b1a4103 to aad0fd6 Compare April 29, 2026 11:09
@davidism
Copy link
Copy Markdown
Member

How does this interact with pytest's capturing modes?

@kdeldycke
Copy link
Copy Markdown
Collaborator Author

kdeldycke commented Apr 30, 2026

How does this interact with pytest's capturing modes?

Oh right. That's an excellent question. I went to the mode route with 2 distinctive cases because I could not figure out a magic way to auto-guess what was the user's intention. And if Pytest had already solve this issue this way it's probably because there is no silver bullet.

Still, I did not tested this proposal within the different modes of Pytest itself. I'll add a couple of unit tests to try the matrix of Pytest's capsys and capfd modes with CliRunner's sys/fd modes to see how's that behaving.

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