2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
import os
|
2024-01-31 12:12:58 +03:00
|
|
|
from pathlib import Path
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2017-02-23 13:20:25 +03:00
|
|
|
from PIL import Image, MspImagePlugin
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2021-02-21 14:15:56 +03:00
|
|
|
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
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2024-01-31 12:12:58 +03: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
|
|
|
|
2024-01-31 12:12:58 +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
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +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
|
|
|
|
2024-01-31 12:12:58 +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
|
|
|
|
2024-01-31 12:12:58 +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:
|
2021-02-21 14:15:56 +03:00
|
|
|
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"
|
|
|
|
)
|
2024-01-31 12:12:58 +03:00
|
|
|
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"
|
|
|
|
)
|
2024-01-31 12:12:58 +03:00
|
|
|
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"))
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
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
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError):
|
2020-02-23 00:03:01 +03:00
|
|
|
im.save(filename)
|