From b312c20ccabd97daa4c291b6794323f7e897d621 Mon Sep 17 00:00:00 2001 From: Frankie Dintino Date: Wed, 18 Sep 2024 21:29:42 -0400 Subject: [PATCH] Add additional test coverage --- Tests/test_file_avif.py | 46 +++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/Tests/test_file_avif.py b/Tests/test_file_avif.py index 8c9293aa4d6..7474172ab55 100644 --- a/Tests/test_file_avif.py +++ b/Tests/test_file_avif.py @@ -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") @@ -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")