From d4dba5602bbc424a2292b68b6a036ed65c14cbe3 Mon Sep 17 00:00:00 2001 From: Harry Date: Tue, 4 Nov 2014 09:27:10 +0000 Subject: [PATCH] handle nonzero return codes --- procs.py | 6 ++++-- tests/test_procs.py | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/procs.py b/procs.py index 9d7ddaf..867878b 100644 --- a/procs.py +++ b/procs.py @@ -9,6 +9,7 @@ class Process(object): self.environ = {} self.cwd = None self._stdout = None + self._returncode = None def set_command(self, command): @@ -39,7 +40,8 @@ class Process(object): @property def returncode(self): - return 0 + if self._returncode is not None: + return self._returncode def run(self): self._subprocess = subprocess.Popen( @@ -48,7 +50,7 @@ class Process(object): stdin=subprocess.PIPE, stdout=subprocess.PIPE, ) - self._subprocess.wait() + self._returncode = self._subprocess.wait() self._stdout = self._subprocess.stdout.read().decode() diff --git a/tests/test_procs.py b/tests/test_procs.py index 4e329fb..0d055b4 100644 --- a/tests/test_procs.py +++ b/tests/test_procs.py @@ -14,3 +14,11 @@ def test_single_proc(): assert ls.returncode == 0 assert ls.stdout == u'file1\nfile2\nfile3\n' + +def test_returncode(): + assert not os.path.exists('/bin/nosuchcommand') + + p = Process('/bin/nosuchcommand') + p.run() + assert p.returncode == 127 +