From 3c9b4f60ab53a36bb71a6ff83a2db00daca06486 Mon Sep 17 00:00:00 2001 From: isidorajovanovic Date: Fri, 21 Jun 2024 22:10:55 +0200 Subject: [PATCH] added test to improve coverage for _save func --- Tests/test_file_spider.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Tests/test_file_spider.py b/Tests/test_file_spider.py index d8bf33f80..3f8ffa77b 100644 --- a/Tests/test_file_spider.py +++ b/Tests/test_file_spider.py @@ -4,6 +4,9 @@ import tempfile import warnings from io import BytesIO from pathlib import Path +from unittest.mock import MagicMock, patch +import unittest +import struct import pytest @@ -162,3 +165,30 @@ def test_odd_size() -> None: data.seek(0) with Image.open(data) as im2: assert_image_equal(im, im2) + + +def test_seek_no_frame() -> None: + with Image.open(TEST_FILE) as im: + im.istack = 1 + im.seek(0) + + +def test_save_small_header(): + width, height = 10, 10 + im = Image.new("F", (width, height)) + + fp = BytesIO() + + corrupted_header = [b'\x00' * 4] * 22 + + with patch("PIL.SpiderImagePlugin.makeSpiderHeader", return_value=corrupted_header): + try: + # Attempt to save the image + im.save(fp, format="SPIDER") + except OSError as e: + # Check if the correct exception is raised + assert str(e) == "Error creating Spider header" + else: + # If no exception is raised, the test has failed + assert False, "Expected an OSError due to corrupted header" +