From 7eeb5c9cfad61c8d57281837274788648012245a Mon Sep 17 00:00:00 2001 From: Gleb <39743818+fzovpec@users.noreply.github.com> Date: Mon, 22 Apr 2024 09:41:06 +0200 Subject: [PATCH] More readable message (#66) --- chat_client_check/check.py | 35 ++++++++++++++------------ dns_check/check.py | 30 ++++++++++++++++------ http_server_check/check.py | 37 ++++++++++++++++++--------- server_check/check.py | 46 +++++++++++++++++++--------------- unreliable_chat_check/check.py | 35 ++++++++++++++------------ 5 files changed, 111 insertions(+), 72 deletions(-) diff --git a/chat_client_check/check.py b/chat_client_check/check.py index bff1770..6047af7 100644 --- a/chat_client_check/check.py +++ b/chat_client_check/check.py @@ -20,6 +20,17 @@ def generate_name(): def generate_message(min_len=32, max_len=64): return ''.join(random.choice(string.ascii_letters) for _ in range(random.randint(min_len, max_len))) +def get_last_printed_line(output_buffer): + last_printed_line = '[EMPTY LINE. PROGRAM DID NOT PRODUCE ANY OUTPUT]' + lines = output_buffer.split('\n') + + for line in reversed(lines): + if line.strip(): + last_printed_line = line + break + + return last_printed_line + def handle_pexpect(child_process, processes_to_terminate, expect_string, output_buffer, step, timeout=1, display_expect_string=''): try: child_process.expect(expect_string, timeout=timeout) @@ -27,13 +38,7 @@ def handle_pexpect(child_process, processes_to_terminate, expect_string, output_ except TimeoutException: output_buffer += child_process.before - last_printed_line = '[EMPTY LINE. PROGRAM DID NOT PRODUCE ANY OUTPUT]' - lines = output_buffer.split('\n') - - for line in reversed(lines): - if line.strip(): - last_printed_line = line - break + last_printed_line = get_last_printed_line(output_buffer) for process in processes_to_terminate: process.terminate(force=True) @@ -43,7 +48,13 @@ def handle_pexpect(child_process, processes_to_terminate, expect_string, output_ raise TestException(f'unexpected output at step {step}!\nExpected output:\n\n{expect_string}\n\nActual output (the last printed line): \n\n{last_printed_line}\n\nTotal program output:\n\n{output_buffer}') except EndOfFileException: - output_buffer += child_process.before + child_process.after + if type(child_process.before) == 'str': + output_buffer += child_process.before + + if type(child_process.after) == 'str': + output_buffer += child_process.after + + last_printed_line = get_last_printed_line(output_buffer) for process in processes_to_terminate: process.terminate(force=True) @@ -381,14 +392,6 @@ def execute(self, disable_colors): else: print(f'[ \u2713 ] {self.test_id}. {self.test_msg}. Success!') - except TypeError as e: # originates from pexpect .before if script terminates. except for more readable error message - if not disable_colors: - print(f'\033[91m[ x ] \033[30m{self.test_id}. {self.test_msg} \033[91mFailed! \033[30m The list of tags is {tags_string} \nYour client did not start, connected to a wrong server port or did not keep running\033[0m') - else: - print(f'[ x ] {self.test_id}. {self.test_msg} Failed! The list of tags is {tags_string} \nYour client did not start, connected to a wrong server port or did not keep running') - - success = False - except Exception as e: if not disable_colors: print(f'\033[91m[ x ] \033[30m{self.test_id}. {self.test_msg} \033[91mFailed! \033[30m The list of tags is {tags_string} \nError message is {e} \033[0m') diff --git a/dns_check/check.py b/dns_check/check.py index 7fb1833..3d98c8a 100644 --- a/dns_check/check.py +++ b/dns_check/check.py @@ -12,6 +12,17 @@ class TestException(Exception): pass +def get_last_printed_line(output_buffer): + last_printed_line = '[EMPTY LINE. PROGRAM DID NOT PRODUCE ANY OUTPUT]' + lines = output_buffer.split('\n') + + for line in reversed(lines): + if line.strip(): + last_printed_line = line + break + + return last_printed_line + def handle_pexpect(child_process, processes_to_terminate, expect_string, output_buffer, step, timeout=1, display_expect_string=''): try: child_process.expect(expect_string, timeout=timeout) @@ -19,20 +30,23 @@ def handle_pexpect(child_process, processes_to_terminate, expect_string, output_ except TimeoutException: output_buffer += child_process.before - last_printed_line = '[EMPTY LINE. PROGRAM DID NOT PRODUCE ANY OUTPUT]' - lines = output_buffer.split('\n') - - for line in reversed(lines): - if line.strip(): - last_printed_line = line - break + last_printed_line = get_last_printed_line(output_buffer) for process in processes_to_terminate: process.terminate(force=True) + if display_expect_string: + expect_string = display_expect_string + raise TestException(f'unexpected output at step {step}!\nExpected output:\n\n{expect_string}\n\nActual output (the last printed line): \n\n{last_printed_line}\n\nTotal program output:\n\n{output_buffer}') except EndOfFileException: - output_buffer += child_process.before + child_process.after + if type(child_process.before) == 'str': + output_buffer += child_process.before + + if type(child_process.after) == 'str': + output_buffer += child_process.after + + last_printed_line = get_last_printed_line(output_buffer) for process in processes_to_terminate: process.terminate(force=True) diff --git a/http_server_check/check.py b/http_server_check/check.py index 9966009..805afe3 100644 --- a/http_server_check/check.py +++ b/http_server_check/check.py @@ -23,25 +23,46 @@ def generate_message(min_len=32, max_len=64): class TestException(Exception): pass -def handle_pexpect(child_process, processes_to_terminate, expect_string, output_buffer, step, timeout=1): +def get_last_printed_line(output_buffer): + last_printed_line = '[EMPTY LINE. PROGRAM DID NOT PRODUCE ANY OUTPUT]' + lines = output_buffer.split('\n') + + for line in reversed(lines): + if line.strip(): + last_printed_line = line + break + + return last_printed_line + +def handle_pexpect(child_process, processes_to_terminate, expect_string, output_buffer, step, timeout=1, display_expect_string=''): try: child_process.expect(expect_string, timeout=timeout) output_buffer += child_process.before + child_process.after except TimeoutException: output_buffer += child_process.before + last_printed_line = get_last_printed_line(output_buffer) for process in processes_to_terminate: process.terminate(force=True) - raise TestException(f'unexpected output at step {step}!\nExpected output:\n\n{expect_string}\n\nActual output: \n\n{child_process.before}\n\nTotal program output:\n\n{output_buffer}') + if display_expect_string: + expect_string = display_expect_string + + raise TestException(f'unexpected output at step {step}!\nExpected output:\n\n{expect_string}\n\nActual output (the last printed line): \n\n{last_printed_line}\n\nTotal program output:\n\n{output_buffer}') except EndOfFileException: - output_buffer += child_process.before + child_process.after + if type(child_process.before) == 'str': + output_buffer += child_process.before + + if type(child_process.after) == 'str': + output_buffer += child_process.after + + last_printed_line = get_last_printed_line(output_buffer) for process in processes_to_terminate: process.terminate(force=True) - raise TestException(f'program has unexpectidly terminated at step {step}!\nExpected output:\n\n{expect_string}\n\nProgram\'s last output: \n\n{child_process.before}\n\nTotal program output:\n\n{output_buffer}') + raise TestException(f'program has unexpectidly terminated at step {step}!\nExpected output:\n\n{expect_string}\n\nProgram\'s last printed line: \n\n{last_printed_line}\n\nTotal program output:\n\n{output_buffer}') return output_buffer @@ -324,14 +345,6 @@ def execute(self, disable_colors=False): print(f'\033[92m[ \u2713 ] \033[30m{self.test_id}. {self.test_msg}. \033[92mSuccess! \033[0m') else: print(f'[ \u2713 ] {self.test_id}. {self.test_msg}. Success! ') - - except TypeError as e: # originates from pexpect .before if script terminates. except for more readable error message - success = False - - if not disable_colors: - print(f'\033[91m[ x ] \033[30m{self.test_id}. {self.test_msg} \033[91mFailed! \033[30m The list of tags is {tags_string} \nYour server did not start \033[0m') - else: - print(f'[ x ] {self.test_id}. {self.test_msg} Failed! The list of tags is {tags_string} \nYour server did not start') except Exception as e: success = False diff --git a/server_check/check.py b/server_check/check.py index 07484ef..add4708 100644 --- a/server_check/check.py +++ b/server_check/check.py @@ -21,27 +21,41 @@ def generate_message(min_len=32, max_len=64): class TestException(Exception): pass -def handle_pexpect(child_process, processes_to_terminate, expect_string, output_buffer, step, timeout=1): +def get_last_printed_line(output_buffer): + last_printed_line = '[EMPTY LINE. PROGRAM DID NOT PRODUCE ANY OUTPUT] - the client did not produce any output which means it could not connect to the server. Please check you are starting your server at a correct address \'127.0.0.1\' and port 5378' + lines = output_buffer.split('\n') + + for line in reversed(lines): + if line.strip(): + last_printed_line = line + break + + return last_printed_line + +def handle_pexpect(child_process, processes_to_terminate, expect_string, output_buffer, step, timeout=1, display_expect_string=''): try: child_process.expect(expect_string, timeout=timeout) output_buffer += child_process.before + child_process.after except TimeoutException: output_buffer += child_process.before - last_printed_line = '[EMPTY LINE. PROGRAM DID NOT PRODUCE ANY OUTPUT]' - lines = output_buffer.split('\n') - - for line in reversed(lines): - if line.strip(): - last_printed_line = line - break + last_printed_line = get_last_printed_line(output_buffer) for process in processes_to_terminate: process.terminate(force=True) - raise TestException(f'unexpected client output at the step {step}!\nExpected output:\n\n{expect_string}\n\nActual output (the last printed line): \n\n{last_printed_line}\n\nTotal program output:\n\n{output_buffer}') + if display_expect_string: + expect_string = display_expect_string + + raise TestException(f'unexpected output at step {step}!\nExpected output:\n\n{expect_string}\n\nActual output (the last printed line): \n\n{last_printed_line}\n\nTotal program output:\n\n{output_buffer}') except EndOfFileException: - output_buffer += child_process.before + child_process.after + if type(child_process.before) == 'str': + output_buffer += child_process.before + + if type(child_process.after) == 'str': + output_buffer += child_process.after + + last_printed_line = get_last_printed_line(output_buffer) for process in processes_to_terminate: process.terminate(force=True) @@ -322,9 +336,9 @@ def execute(self, disable_colors=False): server_process, _ = start_server() except: if not disable_colors: - print(f'\033[91m[ x ] \033[30m{self.test_id}. {self.test_msg} \033[91mFailed! \033[30m The list of tags is {tags_string} \nError message is the server did not start. Please make sure your server prints Server is on on startup \033[0m') + print(f'\033[91m[ x ] \033[30m{self.test_id}. {self.test_msg} \033[91mFailed! \033[30m The list of tags is {tags_string} \nError message is the server did not start. Please make sure your server prints \'Server is on\' on startup \033[0m') else: - print(f'[ x ] {self.test_id}. {self.test_msg} Failed! The list of tags is {tags_string} \nError message is {e} \nThe server output is {server_process.before}') + print(f'[ x ] {self.test_id}. {self.test_msg} Failed! The list of tags is {tags_string} \nError message is the server did not start. Please make sure your server prints \'Server is on\' on startup') return False @@ -336,14 +350,6 @@ def execute(self, disable_colors=False): else: print(f'[ \u2713 ] {self.test_id}. {self.test_msg}. Success!') - except TypeError as e: # originates from pexpect .before if script terminates. except for more readable error message - success = False - - if not disable_colors: - print(f'\033[91m[ x ] \033[30m{self.test_id}. {self.test_msg} \033[91mFailed! \033[30m The list of tags is {tags_string} \nYour server did not start or did not keep running\033[0m') - else: - print(f'[ x ] {self.test_id}. {self.test_msg} Failed! The list of tags is {tags_string} \nYour server did not start or did not keep running') - except Exception as e: try: server_process.expect(pexpect.EOF, timeout=0) diff --git a/unreliable_chat_check/check.py b/unreliable_chat_check/check.py index 50bbeac..61500c6 100644 --- a/unreliable_chat_check/check.py +++ b/unreliable_chat_check/check.py @@ -21,6 +21,17 @@ def generate_name(): def generate_message(min_len=32, max_len=64): return ''.join(random.choice(string.ascii_letters) for _ in range(random.randint(min_len, max_len))) +def get_last_printed_line(output_buffer): + last_printed_line = '[EMPTY LINE. PROGRAM DID NOT PRODUCE ANY OUTPUT]' + lines = output_buffer.split('\n') + + for line in reversed(lines): + if line.strip(): + last_printed_line = line + break + + return last_printed_line + def handle_pexpect(child_process, processes_to_terminate, expect_string, output_buffer, step, timeout=1, display_expect_string=''): try: child_process.expect(expect_string, timeout=timeout) @@ -28,13 +39,7 @@ def handle_pexpect(child_process, processes_to_terminate, expect_string, output_ except TimeoutException: output_buffer += child_process.before - last_printed_line = '[EMPTY LINE. PROGRAM DID NOT PRODUCE ANY OUTPUT]' - lines = output_buffer.split('\n') - - for line in reversed(lines): - if line.strip(): - last_printed_line = line - break + last_printed_line = get_last_printed_line(output_buffer) for process in processes_to_terminate: process.terminate(force=True) @@ -44,7 +49,13 @@ def handle_pexpect(child_process, processes_to_terminate, expect_string, output_ raise TestException(f'unexpected output at step {step}!\nExpected output:\n\n{expect_string}\n\nActual output (the last printed line): \n\n{last_printed_line}\n\nTotal program output:\n\n{output_buffer}') except EndOfFileException: - output_buffer += child_process.before + child_process.after + if type(child_process.before) == 'str': + output_buffer += child_process.before + + if type(child_process.after) == 'str': + output_buffer += child_process.after + + last_printed_line = get_last_printed_line(output_buffer) for process in processes_to_terminate: process.terminate(force=True) @@ -427,14 +438,6 @@ def execute(self, disable_colors=False): else: print(f'[ \u2713 ] {self.test_id}. {self.test_msg}. Success!') - except TypeError as e: # originates from pexpect .before if script terminates. except for more readable error message - if not disable_colors: - print(f'\033[91m[ x ] \033[30m{self.test_id}. {self.test_msg} \033[91mFailed! \033[30m The list of tags is {tags_string} \nYour client did not start or connected to a wrong server port \033[0m') - else: - print(f'[ x ] {self.test_id}. {self.test_msg} Failed! The list of tags is {tags_string} \nYour client did not start or connected to a wrong server port') - - success = False - except Exception as e: if not disable_colors: print(f'\033[91m[ x ] \033[30m{self.test_id}. {self.test_msg} \033[91mFailed! \033[30m The list of tags is {tags_string} \nError message is {e} \033[0m')