Skip to content

Commit

Permalink
Add additional test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fdintino committed Sep 19, 2024
1 parent 0ef3c88 commit b312c20
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions Tests/test_file_avif.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,21 @@ def has_alpha_premultiplied(im_bytes):


class TestUnsupportedAvif:
def test_unsupported(self):
if features.check("avif"):
AvifImagePlugin.SUPPORTED = False
def test_unsupported(self, monkeypatch):
monkeypatch.setattr(AvifImagePlugin, "SUPPORTED", False)

try:
file_path = "Tests/images/avif/hopper.avif"
pytest.warns(
UserWarning,
lambda: pytest.raises(UnidentifiedImageError, Image.open, file_path),
)
finally:
AvifImagePlugin.SUPPORTED = features.check("avif")
file_path = "Tests/images/avif/hopper.avif"
pytest.warns(
UserWarning,
lambda: pytest.raises(UnidentifiedImageError, Image.open, file_path),
)

def test_unsupported_open(self, monkeypatch):
monkeypatch.setattr(AvifImagePlugin, "SUPPORTED", False)

file_path = "Tests/images/avif/hopper.avif"
with pytest.raises(SyntaxError):
AvifImagePlugin.AvifImageFile(file_path)


@skip_unless_feature("avif")
Expand Down Expand Up @@ -198,6 +201,27 @@ def test_AvifDecoder_with_invalid_args(self):
with pytest.raises(TypeError):
_avif.AvifDecoder()

def test_encoder_finish_none_error(self, monkeypatch, tmp_path):
"""Save should raise an OSError if AvifEncoder.finish returns None"""

class _mock_avif:
class AvifEncoder:
def __init__(self, *args, **kwargs):
pass

def add(self, *args, **kwargs):
pass

def finish(self):
return None

monkeypatch.setattr(AvifImagePlugin, "_avif", _mock_avif)

im = Image.new("RGB", (150, 150))
test_file = str(tmp_path / "temp.avif")
with pytest.raises(OSError):
im.save(test_file)

def test_no_resource_warning(self, tmp_path):
with Image.open(TEST_AVIF_FILE) as image:
temp_file = str(tmp_path / "temp.avif")
Expand Down

0 comments on commit b312c20

Please sign in to comment.