From b38ad263568cfc89147e5c7c9d355b0607ae1e18 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Mon, 28 Sep 2020 15:01:53 +0200 Subject: Added test result reporting --- src/config.py | 7 ++++-- src/main.py | 17 ++++++++++++- src/test/test.py | 75 ++++++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 78 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/config.py b/src/config.py index f718296..ebe7a94 100644 --- a/src/config.py +++ b/src/config.py @@ -6,7 +6,7 @@ # By: charles +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/09/27 11:05:38 by charles #+# #+# # -# Updated: 2020/09/28 12:06:05 by cacharle ### ########.fr # +# Updated: 2020/09/28 14:54:02 by cacharle ### ########.fr # # # # ############################################################################ # @@ -24,7 +24,10 @@ BUILD_BEFORE = True BUILD_CMD = "make --no-print-directory -C {path}" # Timeout for non infinite test -TIMEOUT = 2 +TIMEOUT = 1 + +# Timeout for error test +TIMEOUT_ERROR = 0.2 # Timeout for infinite test INFINITE_TIMEOUT = 1 diff --git a/src/main.py b/src/main.py index 13b2d41..17d1bf8 100755 --- a/src/main.py +++ b/src/main.py @@ -51,6 +51,8 @@ def main(): sys.exit(1) Test.new_error([]) + Test.new_error(["a", "a", "a", "a"]) + Test.new_error(["aaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "a", "a", "a"]) Test.new_error(["10"]) Test.new_error(["10", "10"]) Test.new_error(["10", "10", "10"]) @@ -61,7 +63,20 @@ def main(): Test.new_error(["10", "10", "10", "-1"]) Test.new_error(["10", "10", "10", "10", "-1"]) - # Test(error_cmd=[str(config.UINT_MAX + 1), "10", "10", "10"], error=True) + Test.new_error([str(config.UINT_MAX + 1), "10", "10", "10"]) + Test.new_error(["10", str(config.UINT_MAX + 1), "10", "10"]) + Test.new_error(["10", "10", str(config.UINT_MAX + 1), "10"]) + Test.new_error(["10", "10", "10", str(config.UINT_MAX + 1)]) + Test.new_error(["10", "10", "10", "10", str(config.UINT_MAX + 1)]) + + Test.new_error([str(-config.UINT_MAX), "10", "10", "10"]) + Test.new_error(["10", str(-config.UINT_MAX), "10", "10"]) + Test.new_error(["10", "10", str(-config.UINT_MAX), "10"]) + Test.new_error(["10", "10", "10", str(-config.UINT_MAX)]) + Test.new_error(["10", "10", "10", "10", str(-config.UINT_MAX)]) + + Test.new_error(["0", "100", "100", "100"]) + Test.new_error(["1", "100", "100", "100"]) # Test(10, 100, 100, 10) 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 +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # 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)) -- cgit