# HG changeset patch # User Philippe Pepiot <philippe.pepiot@logilab.fr> # Date 1526549545 -7200 # Thu May 17 11:32:25 2018 +0200 # Node ID 4ef570a34bb7dd5aaeaca73b040a79279b634ed1 # Parent fe1db153c773dd119303f9d28fa65a3f350be882 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. diff --git a/benchmarks/utils.py b/benchmarks/utils.py --- a/benchmarks/utils.py +++ b/benchmarks/utils.py @@ -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):