Skip to content

Commit d0bacdb

Browse files
authored
Merge branch 'BoboTiG:main' into feat-xshmgetimage
2 parents 3753257 + a94b4da commit d0bacdb

File tree

2 files changed

+26
-38
lines changed

2 files changed

+26
-38
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ addopts = """
140140
--showlocals
141141
--strict-markers
142142
-r fE
143-
-vvv
143+
-v
144144
--cov=src/mss
145145
--cov-report=term-missing:skip-covered
146146
"""

src/tests/test_tools.py

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,58 @@
22
Source: https://github.com/BoboTiG/python-mss.
33
"""
44

5-
import hashlib
6-
import zlib
7-
from collections.abc import Callable
5+
from __future__ import annotations
6+
7+
import io
88
from pathlib import Path
9+
from typing import TYPE_CHECKING
910

1011
import pytest
1112

12-
from mss.base import MSSBase
1313
from mss.tools import to_png
1414

15+
if TYPE_CHECKING:
16+
from collections.abc import Callable
17+
18+
from mss.base import MSSBase
19+
1520
WIDTH = 10
1621
HEIGHT = 10
17-
MD5SUM = "ee1b645cc989cbfc48e613b395a929d3d79a922b77b9b38e46647ff6f74acef5"
22+
23+
24+
def assert_is_valid_png(*, raw: bytes | None = None, file: Path | None = None) -> None:
25+
Image = pytest.importorskip("PIL.Image", reason="PIL module not available.") # noqa: N806
26+
27+
assert bool(Image.open(io.BytesIO(raw) if raw is not None else file).tobytes())
28+
try:
29+
Image.open(io.BytesIO(raw) if raw is not None else file).verify()
30+
except Exception: # noqa: BLE001
31+
pytest.fail(reason="invalid PNG data")
1832

1933

2034
def test_bad_compression_level(mss_impl: Callable[..., MSSBase]) -> None:
21-
with mss_impl(compression_level=42) as sct, pytest.raises(zlib.error):
35+
with mss_impl(compression_level=42) as sct, pytest.raises(Exception, match="Bad compression level"):
2236
sct.shot()
2337

2438

25-
def test_compression_level(mss_impl: Callable[..., MSSBase]) -> None:
26-
data = b"rgb" * WIDTH * HEIGHT
27-
output = Path(f"{WIDTH}x{HEIGHT}.png")
28-
29-
with mss_impl() as sct:
30-
to_png(data, (WIDTH, HEIGHT), level=sct.compression_level, output=output)
31-
32-
assert hashlib.sha256(output.read_bytes()).hexdigest() == MD5SUM
33-
34-
35-
@pytest.mark.parametrize(
36-
("level", "checksum"),
37-
[
38-
(0, "547191069e78eef1c5899f12c256dd549b1338e67c5cd26a7cbd1fc5a71b83aa"),
39-
(1, "841665ec73b641dfcafff5130b497f5c692ca121caeb06b1d002ad3de5c77321"),
40-
(2, "b11107163207f68f36294deb3f8e6b6a5a11399a532917bdd59d1d5f1117d4d0"),
41-
(3, "31278bad8c1c077c715ac4f3b497694a323a71a87c5ff8bdc7600a36bd8d8c96"),
42-
(4, "8f7237e1394d9ddc71fcb1fa4a2c2953087562ef6eac85d32d8154b61b287fb0"),
43-
(5, "83a55f161bad2d511b222dcd32059c9adf32c3238b65f9aa576f19bc0a6c8fec"),
44-
(6, "ee1b645cc989cbfc48e613b395a929d3d79a922b77b9b38e46647ff6f74acef5"),
45-
(7, "85f8d1b01cef926c111b194229bd6c01e2a65b18b4dd902293698e6de8f4e9ac"),
46-
(8, "85f8d1b01cef926c111b194229bd6c01e2a65b18b4dd902293698e6de8f4e9ac"),
47-
(9, "85f8d1b01cef926c111b194229bd6c01e2a65b18b4dd902293698e6de8f4e9ac"),
48-
],
49-
)
50-
def test_compression_levels(level: int, checksum: str) -> None:
39+
@pytest.mark.parametrize("level", range(10))
40+
def test_compression_level(level: int) -> None:
5141
data = b"rgb" * WIDTH * HEIGHT
5242
raw = to_png(data, (WIDTH, HEIGHT), level=level)
5343
assert isinstance(raw, bytes)
54-
sha256 = hashlib.sha256(raw).hexdigest()
55-
assert sha256 == checksum
44+
assert_is_valid_png(raw=raw)
5645

5746

5847
def test_output_file() -> None:
5948
data = b"rgb" * WIDTH * HEIGHT
6049
output = Path(f"{WIDTH}x{HEIGHT}.png")
6150
to_png(data, (WIDTH, HEIGHT), output=output)
62-
6351
assert output.is_file()
64-
assert hashlib.sha256(output.read_bytes()).hexdigest() == MD5SUM
52+
assert_is_valid_png(file=output)
6553

6654

6755
def test_output_raw_bytes() -> None:
6856
data = b"rgb" * WIDTH * HEIGHT
6957
raw = to_png(data, (WIDTH, HEIGHT))
7058
assert isinstance(raw, bytes)
71-
assert hashlib.sha256(raw).hexdigest() == MD5SUM
59+
assert_is_valid_png(raw=raw)

0 commit comments

Comments
 (0)