diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2017-02-07 10:31:07 -0800 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2017-02-07 10:45:55 -0800 |
commit | 6fe90ec16662c6760453fc9857ba4e06bb1045bd (patch) | |
tree | e7a6960082ae67cf98bcbebfac1ac7682c9dc6ef | |
parent | 4e24022be56f5cc072f2f536959ada9e8b0ecda7 (diff) |
alot/helper: simplify call_cmd and fix test
This both fixes a test that failed (since stderr wasn't being set) and
simplifies the function to only have one path that uses a ternary.
-rw-r--r-- | alot/helper.py | 23 | ||||
-rw-r--r-- | tests/helper_test.py | 2 |
2 files changed, 9 insertions, 16 deletions
diff --git a/alot/helper.py b/alot/helper.py index 2e523c88..7214de38 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # Copyright (C) 2011-2012 Patrick Totzke <patricktotzke@gmail.com> +# Copyright © 2017 Dylan Baker # This file is released under the GNU GPL, version 3 or a later revision. # For further details see the COPYING file from __future__ import absolute_import @@ -272,22 +273,16 @@ def call_cmd(cmdlist, stdin=None): :return: triple of stdout, stderr, return value of the shell command :rtype: str, str, int """ - - out, err, ret = '', '', 0 try: - if stdin: - proc = subprocess.Popen(cmdlist, stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - out, err = proc.communicate(stdin) - ret = proc.poll() - else: - try: - out = subprocess.check_output(cmdlist) - except subprocess.CalledProcessError as e: - err = e.output - ret = e.returncode + proc = subprocess.Popen( + cmdlist, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + stdin=subprocess.PIPE if stdin is not None else None) + out, err = proc.communicate(stdin) + ret = proc.returncode except OSError as e: + out = b'' err = e.strerror ret = e.errno diff --git a/tests/helper_test.py b/tests/helper_test.py index a2a2b925..78cba84d 100644 --- a/tests/helper_test.py +++ b/tests/helper_test.py @@ -362,8 +362,6 @@ class TestCallCmd(unittest.TestCase): # know for certain it should *not* return 0 self.assertNotEqual(code, 0) - # This fails because stderr is not recorded correctly - @unittest.expectedFailure def test_bad_argument(self): out, err, code = helper.call_cmd(['cat', '-Y']) self.assertEqual(out, u'') |