Skip to content

Commit

Permalink
Update test_main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss authored May 19, 2024
1 parent 6fe0635 commit 46deef7
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
from sys import version_info
from warnings import filterwarnings

import pytest

from puremagic.main import PureError, what

filterwarnings("ignore", category=DeprecationWarning, module="imghdr")
try: # imghdr was removed from the standard library in Python 3.13
filterwarnings("ignore", category=DeprecationWarning)
from imghdr import what as imghdr_what
except ModuleNotFoundError:
imghdr_what = None

import pytest

from puremagic.main import PureError, what

files = "bmp exr gif jpg pbm pgm png ppm ras rgb tif webp xbm".split()
files = "bmp gif jpg png tif webp".split() # TODO: (cclauss) Add the remaining files
# file_tests = sorted(test_func.__name__[5:] for test_func in imghdr.tests)
# file_tests = "bmp exr gif jpg pbm pgm png ppm ras rgb tif webp xbm".split()
file_tests = "bmp gif jpg png tif webp".split() # TODO: (cclauss) Add the remaining files


@pytest.mark.skipif(imghdr_what is None, reason="imghdr was removed from the standard library in Python 3.13")
@pytest.mark.parametrize("file", files)
@pytest.mark.parametrize("file", file_tests)
def test_what_from_file(file, h=None):
"""Run each test with a path string and a pathlib.Path."""
file = f"test/resources/images/test.{file}"
assert what(file, h) == imghdr_what(file, h)
file = Path(file).resolve()
Expand Down Expand Up @@ -68,7 +70,7 @@ def test_what_from_file(file, h=None):
@pytest.mark.skipif(imghdr_what is None, reason="imghdr was removed from the standard library in Python 3.13")
@pytest.mark.parametrize("expected, h", string_tests)
def test_what_from_string(expected, h):
if isinstance(h, str):
if isinstance(h, str): # In imgdir.what() h must be bytes, not str.
h = bytes.fromhex(h)
assert imghdr_what(None, h) == what(None, h) == expected

Expand All @@ -86,7 +88,7 @@ def test_what_from_string_py311(expected, h):
These tests fail with imghdr on Python < 3.11.
TODO: (cclauss) Document these imghdr fails on Python < 3.11
"""
if isinstance(h, str):
if isinstance(h, str): # In imgdir.what() h must be bytes, not str.
h = bytes.fromhex(h)
assert what(None, h) == expected
if version_info < (3, 11): # TODO: Document these imghdr fails
Expand Down

0 comments on commit 46deef7

Please sign in to comment.