I implemented a Click CLI that will run subprocess processes, but send their stdout to stderr, so that stdout only contains the command's specific output, e.g.
@click.command()
def cli():
subprocess.run(["echo", "hello world"], stdout=sys.stderr)
click.echo("result")
And I want to test that "hello world" goes to stderr and "result" goes to stdout. Specifically, if I removed the stdout=sys.stderr parameter, I want my test to fail.
def test_foo():
runner = CliRunner(mix_stderr=False)
result = runner.invoke(cli, catch_exceptions=False)
assert result.stdout == "result"
assert result.stderr == "hello world"
This doesn't work though, because it sets sys.stderr to a handle without a file descriptor, which causes subprocess to fail:
# Assuming file-like object
> c2pwrite = stdout.fileno()
E io.UnsupportedOperation: fileno
Is this a Click bug, or is there a workaround, or is this just not supported? I would like to avoid writing a full integration test that calls my CLI via subprocess instead of CliRunner.