From e0f0cbbd5e75df407e00630466be289695d79fcc Mon Sep 17 00:00:00 2001 From: naisanzaa Date: Fri, 17 May 2024 23:55:35 +0800 Subject: [PATCH] run: ensure stdout and stderr are bytes --- automon/helpers/subprocessWrapper/run.py | 10 ++++++++-- .../helpers/subprocessWrapper/tests/test_text.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 automon/helpers/subprocessWrapper/tests/test_text.py diff --git a/automon/helpers/subprocessWrapper/run.py b/automon/helpers/subprocessWrapper/run.py index 84cd3f61..40f77539 100644 --- a/automon/helpers/subprocessWrapper/run.py +++ b/automon/helpers/subprocessWrapper/run.py @@ -60,7 +60,10 @@ def set_command(self, command: str) -> bool: return False @property - def stdout(self): + def stdout(self) -> bytes: + if type(self._stdout) is str: + return str(self._stdout).encode() + return self._stdout @property @@ -68,7 +71,10 @@ def stdout_lines(self): return self.stdout.decode().splitlines() @property - def stderr(self): + def stderr(self) -> bytes: + if type(self._stderr) is str: + return str(self._stderr).encode() + return self._stderr @property diff --git a/automon/helpers/subprocessWrapper/tests/test_text.py b/automon/helpers/subprocessWrapper/tests/test_text.py new file mode 100644 index 00000000..f2d4d199 --- /dev/null +++ b/automon/helpers/subprocessWrapper/tests/test_text.py @@ -0,0 +1,14 @@ +import unittest + +from automon.helpers.subprocessWrapper import Run + + +class TestRun(unittest.TestCase): + def test_text(self): + run = Run() + run.run('ls', text=True) + self.assertEqual(type(run.stdout), bytes) + + +if __name__ == '__main__': + unittest.main()