Skip to content

Commit

Permalink
More readable message (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
fzovpec authored Apr 22, 2024
1 parent 2d95918 commit 7eeb5c9
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 72 deletions.
35 changes: 19 additions & 16 deletions chat_client_check/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,25 @@ 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)
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)
Expand All @@ -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)
Expand Down Expand Up @@ -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')
Expand Down
30 changes: 22 additions & 8 deletions dns_check/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,41 @@
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)
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)

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)
Expand Down
37 changes: 25 additions & 12 deletions http_server_check/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
46 changes: 26 additions & 20 deletions server_check/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down
35 changes: 19 additions & 16 deletions unreliable_chat_check/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,25 @@ 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)
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)
Expand All @@ -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)
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 7eeb5c9

Please sign in to comment.