diff options
author | Jethro Cao <jethrocao@gmail.com> | 2020-03-11 13:58:08 +0800 |
---|---|---|
committer | David Lord <davidism@gmail.com> | 2020-08-13 23:17:27 -0700 |
commit | 7d7809fcd0940c9a272b5aaeba3ec96bc8b682c1 (patch) | |
tree | cbfa69bff1a82b44b435c516a9a81fb686351b9c /src/click/testing.py | |
parent | 10103e3835a381185f786745c0281237f0b238ae (diff) | |
download | click-7d7809fcd0940c9a272b5aaeba3ec96bc8b682c1.tar.gz |
CliRunner isolation streams have name and mode
Diffstat (limited to 'src/click/testing.py')
-rw-r--r-- | src/click/testing.py | 34 |
1 files changed, 26 insertions, 8 deletions
diff --git a/src/click/testing.py b/src/click/testing.py index d7e5856..9443556 100644 --- a/src/click/testing.py +++ b/src/click/testing.py @@ -40,6 +40,21 @@ class EchoingStdin: return repr(self._input) +class _NamedTextIOWrapper(io.TextIOWrapper): + def __init__(self, buffer, name=None, mode=None, **kwargs): + super().__init__(buffer, **kwargs) + self._name = name + self._mode = mode + + @property + def name(self): + return self._name + + @property + def mode(self): + return self._mode + + def make_input_stream(input, charset): # Is already an input stream. if hasattr(input, "read"): @@ -186,17 +201,20 @@ class CliRunner: if self.echo_stdin: input = EchoingStdin(input, bytes_output) - input = io.TextIOWrapper(input, encoding=self.charset) - sys.stdout = io.TextIOWrapper(bytes_output, encoding=self.charset) - - if not self.mix_stderr: - bytes_error = io.BytesIO() - sys.stderr = io.TextIOWrapper(bytes_error, encoding=self.charset) + sys.stdin = input = _NamedTextIOWrapper( + input, encoding=self.charset, name="<stdin>", mode="r" + ) + sys.stdout = _NamedTextIOWrapper( + bytes_output, encoding=self.charset, name="<stdout>", mode="w" + ) if self.mix_stderr: sys.stderr = sys.stdout - - sys.stdin = input + else: + bytes_error = io.BytesIO() + sys.stderr = _NamedTextIOWrapper( + bytes_error, encoding=self.charset, name="<stderr>", mode="w" + ) def visible_input(prompt=None): sys.stdout.write(prompt or "") |