CliRunner isolation streams have name and mode
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jethro Cao · committed by David Lord · parent 10103e3 · Browse files · 3 files·+40 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+40 added−8 removed
- tests:+11 added−0 removed
- other:+29 added−8 removed
Provenance
- Commit
7d7809f- Tree
cbfa69b- Author
Jethro Cao <jethrocao@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
10103e3
3 files changed+40 added−8 removed
modified:src/click/testing.py+26 added-8 removed
38 unmodified lines39 def __repr__(self):40 return repr(self._input)41 42 43+class _NamedTextIOWrapper(io.TextIOWrapper):44+ def __init__(self, buffer, name=None, mode=None, **kwargs):45+ super().__init__(buffer, **kwargs)46+ self._name = name47+ self._mode = mode48+49+ @property50+ def name(self):51+ return self._name52+53+ @property54+ def mode(self):55+ return self._mode56+57+58 def make_input_stream(input, charset):59 # Is already an input stream.60 if hasattr(input, "read"):61 rv = _find_binary_reader(input)138 unmodified lines200 201 if self.echo_stdin:202 input = EchoingStdin(input, bytes_output)203 189- input = io.TextIOWrapper(input, encoding=self.charset)190- sys.stdout = io.TextIOWrapper(bytes_output, encoding=self.charset)204+ sys.stdin = input = _NamedTextIOWrapper(205+ input, encoding=self.charset, name="<stdin>", mode="r"206+ )207+ sys.stdout = _NamedTextIOWrapper(208+ bytes_output, encoding=self.charset, name="<stdout>", mode="w"209+ )210 192- if not self.mix_stderr:193- bytes_error = io.BytesIO()194- sys.stderr = io.TextIOWrapper(bytes_error, encoding=self.charset)195-211 if self.mix_stderr:212 sys.stderr = sys.stdout213+ else:214+ bytes_error = io.BytesIO()215+ sys.stderr = _NamedTextIOWrapper(216+ bytes_error, encoding=self.charset, name="<stderr>", mode="w"217+ )218 199- sys.stdin = input200-219 def visible_input(prompt=None):220 sys.stdout.write(prompt or "")221 val = input.readline().rstrip("\r\n")222 sys.stdout.write(f"{val}\n")175 unmodified linesmodified:tests/test_testing.py+11 added
306 unmodified lines307 result = runner.invoke(cli, standalone_mode=False)308 assert result.output == "ok\n"309 assert result.return_value == "Hello, World!"310 assert result.exit_code == 0311+312+313+def test_file_stdin_attrs(runner):314+ @click.command()315+ @click.argument("f", type=click.File())316+ def cli(f):317+ click.echo(f.name)318+ click.echo(f.mode, nl=False)319+320+ result = runner.invoke(cli, ["-"])321+ assert result.output == "<stdin>\nr"modified:CHANGES.rst+3 added
77 unmodified lines78 - A context's ``show_default`` parameter defaults to the value from79 the parent context. :issue:`1565`80 - ``click.style()`` can output 256 and RGB color codes. Most modern81 terminals support these codes. :pr:`1429`82+- When using ``CliRunner.invoke()``, the replaced ``stdin`` file has83+ ``name`` and ``mode`` attributes. This lets ``File`` options with84+ the ``-`` value match non-testing behavior. :issue:`1064`85 86 87 Version 7.1.288 -------------635 unmodified lines