File: test_file_msp.py

package info (click to toggle)
pillow 8.1.2%2Bdfsg-0.3%2Bdeb11u2
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 65,628 kB
  • sloc: python: 35,630; ansic: 31,009; makefile: 388; javascript: 114; sh: 77
file content (91 lines) | stat: -rw-r--r-- 2,206 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os

import pytest

from PIL import Image, MspImagePlugin

from .helper import assert_image_equal, hopper

TEST_FILE = "Tests/images/hopper.msp"
EXTRA_DIR = "Tests/images/picins"
YA_EXTRA_DIR = "Tests/images/msp"


def test_sanity(tmp_path):
    test_file = str(tmp_path / "temp.msp")

    hopper("1").save(test_file)

    with Image.open(test_file) as im:
        im.load()
        assert im.mode == "1"
        assert im.size == (128, 128)
        assert im.format == "MSP"


def test_invalid_file():
    invalid_file = "Tests/images/flower.jpg"

    with pytest.raises(SyntaxError):
        MspImagePlugin.MspImageFile(invalid_file)


def test_bad_checksum():
    # Arrange
    # This was created by forcing Pillow to save with checksum=0
    bad_checksum = "Tests/images/hopper_bad_checksum.msp"

    # Act / Assert
    with pytest.raises(SyntaxError):
        MspImagePlugin.MspImageFile(bad_checksum)


def test_open_windows_v1():
    # Arrange
    # Act
    with Image.open(TEST_FILE) as im:

        # Assert
        assert_image_equal(im, hopper("1"))
        assert isinstance(im, MspImagePlugin.MspImageFile)


def _assert_file_image_equal(source_path, target_path):
    with Image.open(source_path) as im:
        with Image.open(target_path) as target:
            assert_image_equal(im, target)


@pytest.mark.skipif(
    not os.path.exists(EXTRA_DIR), reason="Extra image files not installed"
)
def test_open_windows_v2():

    files = (
        os.path.join(EXTRA_DIR, f)
        for f in os.listdir(EXTRA_DIR)
        if os.path.splitext(f)[1] == ".msp"
    )
    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():
    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):
    # Arrange
    im = hopper()
    filename = str(tmp_path / "temp.msp")

    # Act/Assert
    with pytest.raises(OSError):
        im.save(filename)