Skip to content

Commit

Permalink
Rework changes to be testable.
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickfournier committed Dec 31, 2022
1 parent b99198b commit 2628392
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
38 changes: 23 additions & 15 deletions pelican/plugins/image_process/image_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,27 @@ def process_picture(soup, img, group, settings, derivative):
img.insert_before(s["element"])


def try_open_image(path):
try:
i = Image.open(path)
except UnidentifiedImageError:
logger.warning(
"%s Source image %s is not supported by Pillow.",
LOG_PREFIX,
path,
)
return None
except FileNotFoundError:
logger.warning(
"%s Source image %s not found.",
LOG_PREFIX,
path,
)
return None

return i


def process_image(image, settings):
# Set default value for 'IMAGE_PROCESS_FORCE'.
if "IMAGE_PROCESS_FORCE" not in settings:
Expand All @@ -686,21 +707,8 @@ def process_image(image, settings):
):
logger.debug("{} Processing {} -> {}".format(LOG_PREFIX, image[0], image[1]))

try:
i = Image.open(image[0])
except UnidentifiedImageError:
logger.warning(
"%s Source image %s is not supported by Pillow.",
LOG_PREFIX,
image[0],
)
return
except FileNotFoundError:
logger.warning(
"%s Source image %s not found.",
LOG_PREFIX,
image[0],
)
i = try_open_image(image[0])
if i is None:
return

for step in image[2]:
Expand Down
24 changes: 7 additions & 17 deletions pelican/plugins/image_process/test_image_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
ExifTool,
compute_paths,
harvest_images_in_fragment,
is_img_identifiable,
try_open_image,
process_image,
)

Expand Down Expand Up @@ -118,11 +118,6 @@ def test_all_transforms(tmp_path, transform_id, transform_params, image_path):
],
)
def test_path_normalization(mocker, orig_src, orig_img, new_src, new_img):
# Allow non-existing images to be processed:
mocker.patch(
"pelican.plugins.image_process.image_process.is_img_identifiable",
lambda img_filepath: True,
)
# Silence image transforms.
process = mocker.patch("pelican.plugins.image_process.image_process.process_image")

Expand Down Expand Up @@ -439,11 +434,6 @@ def test_path_normalization(mocker, orig_src, orig_img, new_src, new_img):
],
)
def test_picture_generation(mocker, orig_tag, new_tag, call_args):
# Allow non-existing images to be processed:
mocker.patch(
"pelican.plugins.image_process.image_process.is_img_identifiable",
lambda img_filepath: True,
)
process = mocker.patch("pelican.plugins.image_process.image_process.process_image")

settings = get_settings(
Expand Down Expand Up @@ -558,19 +548,19 @@ def test_copy_exif_tags(tmp_path, image_path, copy_tags):
assert tag not in actual_tags.keys()


def test_is_img_identifiable():
def test_try_open_image():
for test_image in TEST_IMAGES:
assert is_img_identifiable(test_image)
assert try_open_image(test_image)

assert not is_img_identifiable("image/that/does/not/exist.png")
assert not try_open_image("image/that/does/not/exist.png")

assert not is_img_identifiable(TEST_DATA.joinpath("folded_puzzle.png"))
assert not is_img_identifiable(TEST_DATA.joinpath("minimal.svg"))
assert not try_open_image(TEST_DATA.joinpath("folded_puzzle.png"))
assert not try_open_image(TEST_DATA.joinpath("minimal.svg"))

img = {"src": "https://upload.wikimedia.org/wikipedia/commons/3/34/Exemple.png"}
settings = get_settings(IMAGE_PROCESS_DIR="derivatives")
path = compute_paths(img, settings, derivative="thumb")
assert not is_img_identifiable(path.source)
assert not try_open_image(path.source)


def generate_test_images():
Expand Down

0 comments on commit 2628392

Please sign in to comment.