Skip to content

Commit 59846f2

Browse files
Akay7Apkawa
authored andcommitted
fix: image_save image with type Path throw NotImplementError
#7
1 parent f7d3cd6 commit 59846f2

File tree

5 files changed

+46
-10
lines changed

5 files changed

+46
-10
lines changed

pytest_image_diff/_types.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pathlib import Path
1+
import pathlib
22
from typing import BinaryIO, Union, Tuple, Optional, TYPE_CHECKING
33
from typing_extensions import Literal, Protocol
44

@@ -7,7 +7,8 @@
77
if TYPE_CHECKING:
88
from .plugin import DiffCompareResult
99

10-
PathOrFileType = Union[str, bytes, Path, BinaryIO]
10+
PathType = Union[str, pathlib.Path]
11+
PathOrFileType = Union[PathType, bytes, BinaryIO]
1112
ImageFileType = Union[Image, PathOrFileType]
1213
ImageSize = Tuple[int, int]
1314

pytest_image_diff/helpers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from _pytest import junitxml
1010
from _pytest.fixtures import FixtureRequest
1111

12-
from pytest_image_diff._types import ImageFileType
12+
from pytest_image_diff._types import ImageFileType, PathType
1313

1414

1515
def build_filename(
@@ -78,10 +78,10 @@ def get_test_info(
7878
return TestInfo.get_test_info(request, suffix, prefix)
7979

8080

81-
def image_save(image: ImageFileType, path: typing.Union[str, pathlib.Path]) -> None:
81+
def image_save(image: ImageFileType, path: PathType) -> None:
8282
if isinstance(image, Image):
8383
image.save(path)
84-
elif isinstance(image, str):
84+
elif isinstance(image, (str, pathlib.Path)):
8585
if not os.path.exists(image):
8686
raise ValueError("Image maybe path. Path not exists!")
8787
shutil.copyfile(image, str(path))
@@ -91,6 +91,9 @@ def image_save(image: ImageFileType, path: typing.Union[str, pathlib.Path]) -> N
9191
shutil.copyfileobj(
9292
image, f
9393
) # type: ignore # Workaround for python/mypy#8962
94+
elif isinstance(image, bytes):
95+
with open(path, "wb") as f:
96+
f.write(image)
9497
else:
9598
raise NotImplementedError()
9699

pytest_image_diff/plugin.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
from _pytest.python import Function
77
from _pytest.runner import CallInfo
88

9-
from ._types import ImageFileType, ImageRegressionCallableType, ImageDiffCallableType
9+
from ._types import (
10+
ImageFileType,
11+
ImageRegressionCallableType,
12+
ImageDiffCallableType,
13+
PathType,
14+
)
1015
from .helpers import get_test_info, build_filename, image_save, ensure_dirs, temp_file
1116
from .image_diff import _diff
1217

@@ -45,23 +50,23 @@ def image_diff_throw_exception() -> bool:
4550

4651

4752
@pytest.fixture(scope="session")
48-
def image_diff_root(request: FixtureRequest) -> str:
53+
def image_diff_root(request: FixtureRequest) -> PathType:
4954
"""
5055
Root path for storing diff images. By default - `request.config.rootdir`
5156
"""
5257
return str(request.config.rootdir) # type: ignore
5358

5459

5560
@pytest.fixture(scope="session") # pragma: no cover
56-
def image_diff_dir(image_diff_root: str) -> str:
61+
def image_diff_dir(image_diff_root: str) -> PathType:
5762
"""
5863
Path for store diff images. by default - '{image_diff_root}.tests/image_diff/'
5964
"""
6065
return os.path.join(image_diff_root, ".tests/image_diff/")
6166

6267

6368
@pytest.fixture(scope="session")
64-
def image_diff_reference_dir(image_diff_root: str) -> str:
69+
def image_diff_reference_dir(image_diff_root: str) -> PathType:
6570
"""
6671
Path for store reference images
6772
"""

tests/conftest.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import os
2+
import pathlib
23

34
import pytest
45
from _pytest.fixtures import FixtureRequest
56

7+
from pytest_image_diff._types import PathType
8+
69

710
@pytest.fixture(scope="session")
8-
def image_diff_root(request: FixtureRequest) -> str:
11+
def image_diff_root(request: FixtureRequest) -> PathType:
912
"""
1013
Root path for storing diff images. By default - `request.config.rootdir`
1114
"""
1215
return os.path.join(request.config.rootdir, "tests") # type: ignore
16+
17+
18+
@pytest.fixture(scope="session")
19+
def image_diff_dir(image_diff_root: str) -> PathType:
20+
"""
21+
Path for store diff images. by default - '{image_diff_root}.tests/image_diff/'
22+
"""
23+
return pathlib.Path(image_diff_root) / ".tests/image_diff/"

tests/test_diff.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# -*- coding: utf-8 -*-
2+
import pathlib
23
import random
34
import string
5+
import sys
6+
import tempfile
7+
from typing import Union
48

59
import pytest
610
from PIL import Image, ImageDraw
711

12+
from pytest_image_diff.helpers import image_save
13+
814

915
def make_test_image(text="Hello world", size=(100, 30)) -> Image:
1016
img = Image.new("RGB", size, color=(73, 109, 137))
@@ -15,6 +21,16 @@ def make_test_image(text="Hello world", size=(100, 30)) -> Image:
1521
return img
1622

1723

24+
@pytest.mark.skipif(sys.platform != "linux", reason="PermissionError in windows")
25+
def test_compare(image_diff):
26+
image: Union[Image, str, bytes] = make_test_image()
27+
tf = tempfile.NamedTemporaryFile(suffix=".jpeg")
28+
# FIXME PermissionError: in windows
29+
image_save(image, tf.name)
30+
image2: Union[Image, str, bytes] = pathlib.Path(tf.name)
31+
image_diff(image, image2)
32+
33+
1834
def test_initial_diff(image_diff, image_diff_dir):
1935
image = make_test_image()
2036
suffix = "".join(random.choices(string.ascii_letters, k=10))

0 commit comments

Comments
 (0)