Skip to content
Snippets Groups Projects
Commit 4ef570a3 authored by Philippe Pepiot's avatar Philippe Pepiot
Browse files

Introduce a check_output() method on BaseTestSuite

This is used to run given command with a controlled environment and handle
generic behavior regarding expected return code.

Use it in _single_execute() for Time and Track base classes.
parent fe1db153
No related branches found
No related tags found
No related merge requests found
......@@ -173,6 +173,33 @@
'-T', '{node|short}', '-r', '.'],
env=self.environ).strip()
def check_output(self, *args, **kwargs):
"""Helper to run commands
Run given command in a subprocess
Optional expected_return_code (default 0) is used to control whenever
we expect the command should exit.
If the command succeeded with expected_return_code = 0, return the output
If the command succeeded with expected_return_code != 0, raise RuntimeError
If the command fail with expected_return_code, return None, else raise
original subprocess.CalledProcessError exception.
"""
env = kwargs.pop('env', self.environ)
expected_return_code = kwargs.pop('expected_return_code', 0)
cmd = list(args)
try:
output = subprocess.check_output(cmd, env=env, **kwargs)
except subprocess.CalledProcessError as exc:
if exc.returncode == expected_return_code:
# failed as we expected
return None
raise
else:
if expected_return_code != 0:
raise RuntimeError('unexpected return code 0 for {}'.format(cmd))
return output
def setup(self, repo_name, *args, **kwargs):
venv = os.path.abspath(os.path.join(os.path.dirname(sys.executable), ".."))
self.repo_name = repo_name
......@@ -222,14 +249,9 @@
def _single_execute(self, cmd):
before = time.time()
try:
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT,
env=self.environ)
after = time.time()
duration = after - before
return duration
except subprocess.CalledProcessError as e:
raise
self.check_output(*cmd, stderr=subprocess.STDOUT)
after = time.time()
return after - before
class BaseTimeTestSuite(BaseTestSuite):
......@@ -252,17 +274,8 @@
] + list(args)
return cmd
def _single_execute(self, cmd, expected_return_code=0):
try:
subprocess.check_output(cmd, stderr=subprocess.STDOUT,
env=self.environ)
except subprocess.CalledProcessError as exc:
if exc.returncode != expected_return_code:
print(exc.output)
raise
else:
if expected_return_code != 0:
raise RuntimeError('unexpected return code 0 for %s' % (cmd,))
def _single_execute(self, cmd, **kwargs):
self.check_output(*cmd, **kwargs)
class BaseNChangesetsTestSuite(BaseTimeTestSuite):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment