From 0fa67dde2de2703fc3998907975d5a2041a1c688 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Thu, 23 Jun 2022 15:43:19 +0200 Subject: [PATCH] Cleaned the usage of return codes --- modules/internal_io.py | 18 +++++++++++------- modules/spidey_test.py | 20 ++++++++++++++------ modules/test_interface.py | 7 ++++--- modules/test_template.py | 29 ++++++++++++++++++++++------- 4 files changed, 51 insertions(+), 23 deletions(-) diff --git a/modules/internal_io.py b/modules/internal_io.py index c42822e..8804dbb 100644 --- a/modules/internal_io.py +++ b/modules/internal_io.py @@ -5,13 +5,17 @@ Internal interfacting objects. from collections import namedtuple -global_status = namedtuple('global_status', 'total_tests, tests_finished, tests_failed, tests_running, tests_skipped', defaults=(0, 0, 0, 0, 0)) +global_status = namedtuple( + 'global_status', + 'total_tests, tests_finished, test_success, tests_failed, tests_running, tests_skipped', + defaults=(0, 0, 0, 0, 0, 0) +) test_status = namedtuple('test_status', 'name, status, message', defaults=(None, None, None)) return_code_dict = { - 2: "The test was not run", - 3: "The test is running", - 0: "The test is a success", - 1: "Test has failed", - 4: "Test found problems", + "skipped": "The test was not run", + "running": "The test is running", + "success": "The test is a success", + "failure": "Test has failed", + "problems": "Test found problems", + "idle": "Test is idle", } - diff --git a/modules/spidey_test.py b/modules/spidey_test.py index 3da8a46..1003efd 100644 --- a/modules/spidey_test.py +++ b/modules/spidey_test.py @@ -18,6 +18,15 @@ class SpideyTest(test_template.TestTemplate): """ super().__init__(args, agnpath, test_data, queue) + def slowdown(self): + """ + This function is only used for testing. + :return: + """ + print("Spidey test is running slowly...") + import random, time + time.sleep(random.randint(1, 5)) + def run_test(self): """ Counts the number of lines in both index.js files. If it's more than one, it's almost certain your discord @@ -25,22 +34,21 @@ class SpideyTest(test_template.TestTemplate): :return: Whether any of the index.js is more than one line :rtype: bool """ - import random, time - # Wait a random amount of time to make sure the files are loaded - time.sleep(random.randint(1, 5)) + + self.slowdown() # This function is only used for testing paths = self.get_path() self.is_infected = False lines = 0 with open(os.path.join(paths[0], "index.js")) as f: for line in f: - lines += 1 + lines += 1 # TODO: you don't actually need to count the lines, you can just check if it's more than 1 if lines > 1: self.is_infected = True lines = 0 with open(os.path.join(paths[1], "index.js")) as f: for line in f: - lines += 1 + lines += 1 # TODO: you don't actually need to count the lines, you can just check if it's more than 1 if lines > 1: self.is_infected = True @@ -52,7 +60,7 @@ class SpideyTest(test_template.TestTemplate): "do also consider a clean windows reinstall.") print("Spidey test finished") - self.set_status(0) + self.set_status("success") status = self.get_status() self.queue.append(status) return self.get_status_code() diff --git a/modules/test_interface.py b/modules/test_interface.py index 956fee3..1be178c 100644 --- a/modules/test_interface.py +++ b/modules/test_interface.py @@ -167,17 +167,18 @@ class TestRunner: nb_tests_skipped = 0 for test in self.finished_tests: print(f"Test {test.name} finished with status {test.status}") - if test.status == 0: + if test.status == "success": nb_tests_success += 1 - elif test.status == 1: + elif test.status == "failure": nb_tests_failure += 1 - elif test.status == 2: + elif test.status == "skipped": nb_tests_skipped += 1 nb_tests_ran = nb_tests_success + nb_tests_failure + nb_tests_skipped nb_test_running = nb_tests - nb_tests_ran return global_status( total_tests=nb_test_running, + test_success=nb_tests_success, tests_finished=nb_tests_ran, tests_failed=nb_tests_failure, tests_running=nb_test_running, diff --git a/modules/test_template.py b/modules/test_template.py index 88dcd5f..9d10d92 100644 --- a/modules/test_template.py +++ b/modules/test_template.py @@ -22,9 +22,18 @@ class TestTemplate: self.main_path = agnpath.main_path self.test_data = test_data self.is_infected = False - self.status_code = 0 + self.status_code = "idle" self.status = "Test not yet initialized." self.queue = queue + self.set_status("idle") + + def run_test(self): + """ + The main runner function, must be implemented by the child class. + During the execution of the test, the status code and status MUST be updated. + """ + self.set_status("running", "The test will begin shortly, please wait...") + self.set_status("sucess", "The test was a success but kinda not a success.") def name(self): """ @@ -36,33 +45,39 @@ class TestTemplate: def get_status_code(self): """ :return: Status code at the time of call. - :rtype: int + :rtype: str """ return self.status_code def get_status(self): """ :return: Status at the time of call. - :rtype: str + :rtype: internal_io.test_status """ return internal_io.test_status(self.name(), self.status_code, self.status) @staticmethod def get_status_from_code(code): """ + :param code: The status code to get the status from. + :type code: str :return: Description of the status code :rtype: str """ return internal_io.return_code_dict[code] - def set_status(self, statuscode: int): + def set_status(self, statuscode: str, message: str = None): """ Sets a new status code. :param statuscode: Status code to set - :type statuscode: int + :param message: Message to set + :type statuscode: str :return: new status code - :rtype: int + :rtype: str """ self.status_code = statuscode - self.status = self.get_status_from_code(statuscode) + if message is not None: + self.status = message + else: + self.status = self.get_status_from_code(statuscode) return self.status_code