Pillow/Tests/test_file_msp.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

92 lines
2.3 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
import os
from pathlib import Path
import pytest
2017-02-23 13:20:25 +03:00
from PIL import Image, MspImagePlugin
from .helper import assert_image_equal, assert_image_equal_tofile, hopper
2017-01-30 09:21:48 +03:00
2014-09-18 15:48:07 +04:00
TEST_FILE = "Tests/images/hopper.msp"
2017-01-30 09:21:48 +03:00
EXTRA_DIR = "Tests/images/picins"
2017-02-21 16:37:27 +03:00
YA_EXTRA_DIR = "Tests/images/msp"
2014-09-18 15:48:07 +04:00
def test_sanity(tmp_path: Path) -> None:
2020-02-23 00:03:01 +03:00
test_file = str(tmp_path / "temp.msp")
2014-06-10 13:10:47 +04:00
2020-02-23 00:03:01 +03:00
hopper("1").save(test_file)
2014-06-10 13:10:47 +04:00
2020-02-23 00:03:01 +03:00
with Image.open(test_file) as im:
im.load()
assert im.mode == "1"
assert im.size == (128, 128)
assert im.format == "MSP"
2014-06-10 13:10:47 +04:00
2015-07-03 09:22:56 +03:00
def test_invalid_file() -> None:
2020-02-23 00:03:01 +03:00
invalid_file = "Tests/images/flower.jpg"
2015-07-03 08:03:25 +03:00
2020-02-23 00:03:01 +03:00
with pytest.raises(SyntaxError):
MspImagePlugin.MspImageFile(invalid_file)
2017-01-29 23:28:41 +03:00
def test_bad_checksum() -> None:
2020-02-23 00:03:01 +03:00
# Arrange
# This was created by forcing Pillow to save with checksum=0
bad_checksum = "Tests/images/hopper_bad_checksum.msp"
2014-09-18 15:48:07 +04:00
2020-02-23 00:03:01 +03:00
# Act / Assert
with pytest.raises(SyntaxError):
MspImagePlugin.MspImageFile(bad_checksum)
2017-01-30 09:21:48 +03:00
2017-02-21 16:37:27 +03:00
def test_open_windows_v1() -> None:
2020-02-23 00:03:01 +03:00
# Arrange
# Act
with Image.open(TEST_FILE) as im:
# Assert
assert_image_equal(im, hopper("1"))
assert isinstance(im, MspImagePlugin.MspImageFile)
2017-02-21 16:37:27 +03:00
2020-02-23 00:03:01 +03:00
def _assert_file_image_equal(source_path, target_path) -> None:
2020-02-23 00:03:01 +03:00
with Image.open(source_path) as im:
assert_image_equal_tofile(im, target_path)
2020-02-23 00:03:01 +03:00
@pytest.mark.skipif(
not os.path.exists(EXTRA_DIR), reason="Extra image files not installed"
)
def test_open_windows_v2() -> None:
2020-02-23 00:03:01 +03:00
files = (
os.path.join(EXTRA_DIR, f)
for f in os.listdir(EXTRA_DIR)
if os.path.splitext(f)[1] == ".msp"
2019-06-13 18:54:11 +03:00
)
2020-02-23 00:03:01 +03:00
for path in files:
_assert_file_image_equal(path, path.replace(".msp", ".png"))
@pytest.mark.skipif(
not os.path.exists(YA_EXTRA_DIR), reason="Even More Extra image files not installed"
)
def test_msp_v2() -> None:
2020-02-23 00:03:01 +03:00
for f in os.listdir(YA_EXTRA_DIR):
if ".MSP" not in f:
continue
path = os.path.join(YA_EXTRA_DIR, f)
_assert_file_image_equal(path, path.replace(".MSP", ".png"))
def test_cannot_save_wrong_mode(tmp_path: Path) -> None:
2020-02-23 00:03:01 +03:00
# Arrange
im = hopper()
filename = str(tmp_path / "temp.msp")
# Act/Assert
with pytest.raises(OSError):
2020-02-23 00:03:01 +03:00
im.save(filename)