From 6ffff7c95706e3a39e82fa0362e825aab21cbb26 Mon Sep 17 00:00:00 2001 From: Robert Collins Date: Thu, 13 Mar 2014 17:34:51 +1300 Subject: Factor run_cmd out of the base class. I want to reuse this for factoring out some fixtures, so make it reusable. Change-Id: I7035a514adf889a8ebaadff9ae873341804fe099 --- pbr/tests/base.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/pbr/tests/base.py b/pbr/tests/base.py index 778b026..f18dadc 100644 --- a/pbr/tests/base.py +++ b/pbr/tests/base.py @@ -122,12 +122,19 @@ class BaseTestCase(testtools.TestCase, testresources.ResourcedTestCase): working copy--returns the stdout and stderr streams and the exit code from the subprocess. """ + return _run_cmd([cmd] + list(args), cwd=self.package_dir) - os.chdir(self.package_dir) - p = subprocess.Popen([cmd] + list(args), stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - streams = tuple(s.decode('latin1').strip() for s in p.communicate()) - for line in streams: - print(line) - return (streams) + (p.returncode,) +def _run_cmd(args, cwd): + """Run the command args in cwd. + + :param args: The command to run e.g. ['git', 'status'] + :param cwd: The directory to run the comamnd in. + :return: ((stdout, stderr), returncode) + """ + p = subprocess.Popen( + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd) + streams = tuple(s.decode('latin1').strip() for s in p.communicate()) + for content in streams: + print(content) + return (streams) + (p.returncode,) -- cgit v1.2.1