From 545393c3f6c89bcc7e41fd2dd61d0b524472fe8e Mon Sep 17 00:00:00 2001 From: Ben Finney Date: Wed, 7 Oct 2015 16:55:52 +1100 Subject: [PATCH] =?utf8?q?Use=20=E2=80=98io.BytesIO=E2=80=99=20for=20strea?= =?utf8?q?ms=20of=20=E2=80=98SubprocessDouble=E2=80=99.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- test/helper.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/test/helper.py b/test/helper.py index 1c11e86..d175616 100644 --- a/test/helper.py +++ b/test/helper.py @@ -41,6 +41,7 @@ else: import os import os.path +import io import shutil import tempfile import pwd @@ -1402,9 +1403,12 @@ class SubprocessDouble(TestDoubleWithRegistry): self.set_popen_double() - self.stdin_double = FileDouble() - self.stdout_double = FileDouble() - self.stderr_double = FileDouble() + stream_class = SubprocessDouble.stream_class + for stream_name in ['stdin', 'stdout', 'stderr']: + fake_file = stream_class() + file_double = FileDouble(fake_file=fake_file) + stream_double_name = '{name}_double'.format(name=stream_name) + setattr(self, stream_double_name, file_double) super(SubprocessDouble, self).__init__(*args, **kwargs) @@ -1446,17 +1450,26 @@ class SubprocessDouble(TestDoubleWithRegistry): result = tuple(self.argv) return result - def set_stdin_content(self, text): + stream_class = io.BytesIO + stream_encoding = "utf-8" + + def set_stdin_content(self, text, bytes_encoding=stream_encoding): """ Set the content of the `stdin` stream for this double. """ - self.stdin_double.fake_file = StringIO(text) + content = text.encode(bytes_encoding) + fake_file = self.stream_class(content) + self.stdin_double.fake_file = fake_file - def set_stdout_content(self, text): + def set_stdout_content(self, text, bytes_encoding=stream_encoding): """ Set the content of the `stdout` stream for this double. """ - self.stdout_double.fake_file = StringIO(text) + content = text.encode(bytes_encoding) + fake_file = self.stream_class(content) + self.stdout_double.fake_file = fake_file - def set_stderr_content(self, text): + def set_stderr_content(self, text, bytes_encoding=stream_encoding): """ Set the content of the `stderr` stream for this double. """ - self.stderr_double.fake_file = StringIO(text) + content = text.encode(bytes_encoding) + fake_file = self.stream_class(content) + self.stderr_double.fake_file = fake_file def make_fake_subprocess_scenarios(path=None): -- 2.11.4.GIT