diff options
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/test.py | 75 |
1 files changed, 57 insertions, 18 deletions
diff --git a/src/test/test.py b/src/test/test.py index 92bf20a..e8c5ab4 100644 --- a/src/test/test.py +++ b/src/test/test.py @@ -6,16 +6,21 @@ # By: charles <me@cacharle.xyz> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/09/27 11:36:32 by charles #+# #+# # -# Updated: 2020/09/28 12:23:40 by cacharle ### ########.fr # +# Updated: 2020/09/28 14:53:40 by cacharle ### ########.fr # # # # ############################################################################ # +import os import subprocess import config import test.philo as philo +class ShouldFailError(Exception): + pass + + class Test: _tests = [] _exec_path = None @@ -49,20 +54,21 @@ class Test: Test._tests.append(self) def run(self): - if self._error_cmd is not None: - argv = [Test._exec_path, *self._error_cmd] + try: + self._run_tested() + except ShouldFailError as e: + self._print_fail("not failed: " + str(e)) + except philo.FormatError as e: + self._print_fail("format: " + str(e)) + except philo.LogError as e: + self._print_fail("log: " + str(e)) + # except Time else: - argv = [ - Test._exec_path, - str(self._philo_num), - str(self._timeout_die), - str(self._timeout_eat), - str(self._timeout_sleep) - ] - if self._meal_num is not None: - argv.append(str(self._meal_num)) + self._print_pass() + + def _run_tested(self): process = subprocess.Popen( - argv, + self._argv(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT ) @@ -70,7 +76,7 @@ class Test: self._check_error(process) else: self._check_output(process.stdout) - process.wait(timeout=config.TIMEOUT) + process.wait(timeout=config.TIMEOUT) def _check_output(self, stream): table = philo.Table(self._philo_num, self._timeout_eat) @@ -80,9 +86,42 @@ class Test: table.check() def _check_error(self, process): - line_num = len([None for _ in process.stdout]) - if line_num != 1: - raise ValueError("you should have an error message") + try: + out, _ = process.communicate(timeout=config.TIMEOUT_ERROR) + except subprocess.TimeoutExpired: + raise ShouldFailError("no error message") if process.returncode == 0: - raise ValueError("you should have a non zero status code") + raise ShouldFailError("non zero status code: {}".format(process.returncode)) + if out.decode().count('\n') != 1: + raise ShouldFailError("no error message") + + RED_CHARS = "\033[31m" + GREEN_CHARS = "\033[32m" + BOLD_CHARS = "\033[1m" + CLOSE_CHARS = "\033[0m" + + def _argv(self, basename=False): + exec_path = os.path.basename(Test._exec_path) if basename else Test._exec_path + if self._error_cmd is not None: + return [exec_path, *self._error_cmd] + else: + argv = [ + exec_path, + str(self._philo_num), + str(self._timeout_die), + str(self._timeout_eat), + str(self._timeout_sleep) + ] + if self._meal_num is not None: + argv.append(str(self._meal_num)) + return argv + + def _print_fail(self, msg): + print("{}[FAIL] {}: {}{}".format(Test.RED_CHARS, self._argv_str, msg, Test.CLOSE_CHARS)) + + def _print_pass(self): + print("{}[PASS] {}{}".format(Test.GREEN_CHARS, self._argv_str, Test.CLOSE_CHARS)) + @property + def _argv_str(self): + return ' '.join(self._argv(basename=True)) |
