# pyright: reportWildcardImportFromLibrary=false from behave import * # noqa: F403 import re import os try: from steps.common import Case, ThumbValidator except ModuleNotFoundError: from common import Case, ThumbValidator @then(u'stdout was') # noqa: F405 def step_stdout_was(context): case: Case = context.case text: str = context.text expected_lines = [] substitution_dict = { "path-to-large-thumb": os.path.join( context.case.thumb_dir, "large", context.case.thumb_name ), "hash-of-thumb": case.infile_hash, "input-path": case.infile } for line in text.strip().splitlines(): line = line.format(**substitution_dict) expected_lines.append(line) result = case.result assert result is not None actual_lines = result.stdout.strip().splitlines() try: assert len(expected_lines) == len(actual_lines), \ "Expected are {} lines, but {} lines were returned".format( len(expected_lines), len(actual_lines), ) for expected, actual in zip(expected_lines, actual_lines): assert re.match(expected, actual), \ "{} does not match {} as expected".format( actual, expected ) except AssertionError as e: print("Expected:") print("\n".join(expected_lines)) print("Actual:") print("\n".join(actual_lines)) raise e @then(u'the path to the {size} thumb is returned') # noqa: F405 def step_path_to_thumb_is_returned(context, size): case: Case = context.case result = case.result assert result is not None expected_dir = os.path.join( context.case.thumb_dir, size, context.case.thumb_name ) assert result.stdout.strip() == expected_dir, \ "Unexpected amt output.\nactual: {}\nexpected: {}".format( result.stdout.strip(), expected_dir ) @then(u'the path to the input file is returned') # noqa: F405 def step_path_to_input_file_is_returned(context): case: Case = context.case result = case.result assert result is not None assert result.stdout.strip() == case.infile, \ "Unexpected amt output.\nactual: {}\nexpected: {}".format( result.stdout.strip(), case.infile ) @then(u'nothing is returned on stdout') # noqa: F405 def step_nothing_is_returned(context): case: Case = context.case result = case.result assert result is not None assert result.stdout.strip() == "", \ "expected no stdout, but '{}' has been returned".format(context.result.stdout) @then(u'allmytoes terminates with exit code {code}') # noqa: F405 def step_terminates_with_exit_code(context, code): case: Case = context.case result = case.result assert result is not None assert result.returncode == int(code.strip()), \ "exit code was expected to be {} but is {}".format(code, result.returncode) @then(u'the thumbs have not been changed') # noqa: F405 def step_thumbs_have_not_been_changed(context): case: Case = context.case for path, expected_mtime in case.prepared_thumbs_mtimes.items(): actual_mtime = os.stat(path).st_mtime_ns assert expected_mtime == actual_mtime, "File " @then(u'the {size} thumb is a PNG image with max size {x}') # noqa: F405 def step_valid_thumb_with_deviating_size_exist(context, size, x): case: Case = context.case ThumbValidator.validate_single_thumb_for_specific_size(case, size, int(x)) @then(u'the {from_size} thumb is a symlink to the {target_size} thumb') # noqa: F405 def step_thumb_is_symlink(context, from_size, target_size): case: Case = context.case ThumbValidator.validate_one_thumb_is_symlink_to_anther(case, from_size, target_size) @then(u'valid thumbs for all sizes exist') # noqa: F405 def step_valid_thumbs_exist(context): case: Case = context.case ThumbValidator.validate_case(case) @then(u'valid thumb exist for size {size}') # noqa: F405 def step_valid_thumb_for_size_exists(context, size): case: Case = context.case ThumbValidator.validate_case(case, size) @then(u'no thumb exists for sizes other than {existing_size}') # noqa: F405 def step_no_thumb_exists_other_than_one_size(context, existing_size): case: Case = context.case assert existing_size in case.sizes for size, path in zip(case.sizes, case.thumb_paths): if existing_size == size: assert os.path.isfile(path) else: assert not os.path.exists(path) @then(u'the thumb is a {size} thumbnail of {image}') # noqa: F405 def step_thumb_of_specific_file(context, size: str, image: str): case: Case = context.case expected = os.path.join(context.image_template_path, image) ThumbValidator.validate_single_thumb_for_specific_size( case, size, infile=expected, )