Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamline test skipping based on supported features #4434

Merged
merged 3 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions Tests/check_j2k_leaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@

from PIL import Image

from .helper import PillowTestCase, is_win32
from .helper import PillowTestCase, is_win32, skip_unless_feature

# Limits for testing the leak
mem_limit = 1024 * 1048576
stack_size = 8 * 1048576
iterations = int((mem_limit / stack_size) * 2)
codecs = dir(Image.core)
test_file = "Tests/images/rgb_trns_ycbc.jp2"


@unittest.skipIf(is_win32(), "requires Unix or macOS")
@skip_unless_feature("jpg_2000")
class TestJpegLeaks(PillowTestCase):
def setUp(self):
if "jpeg2k_encoder" not in codecs or "jpeg2k_decoder" not in codecs:
self.skipTest("JPEG 2000 support not available")

def test_leak_load(self):
from resource import setrlimit, RLIMIT_AS, RLIMIT_STACK

Expand Down
7 changes: 6 additions & 1 deletion Tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from io import BytesIO

import pytest
from PIL import Image, ImageMath
from PIL import Image, ImageMath, features

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -172,6 +172,11 @@ def skip_known_bad_test(msg=None):
pytest.skip(msg or "Known bad test")


def skip_unless_feature(feature):
reason = "%s not available" % feature
return pytest.mark.skipif(not features.check(feature), reason=reason)


class PillowTestCase(unittest.TestCase):
def delete_tempfile(self, path):
try:
Expand Down
12 changes: 6 additions & 6 deletions Tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import pytest
from PIL import features

from .helper import skip_unless_feature

try:
from PIL import _webp

HAVE_WEBP = True
except ImportError:
HAVE_WEBP = False
pass


def test_check():
Expand All @@ -21,18 +21,18 @@ def test_check():
assert features.check_feature(feature) == features.check(feature)


@pytest.mark.skipif(not HAVE_WEBP, reason="WebP not available")
@skip_unless_feature("webp")
def test_webp_transparency():
assert features.check("transp_webp") != _webp.WebPDecoderBuggyAlpha()
assert features.check("transp_webp") == _webp.HAVE_TRANSPARENCY


@pytest.mark.skipif(not HAVE_WEBP, reason="WebP not available")
@skip_unless_feature("webp")
def test_webp_mux():
assert features.check("webp_mux") == _webp.HAVE_WEBPMUX


@pytest.mark.skipif(not HAVE_WEBP, reason="WebP not available")
@skip_unless_feature("webp")
def test_webp_anim():
assert features.check("webp_anim") == _webp.HAVE_WEBPANIM

Expand Down
14 changes: 5 additions & 9 deletions Tests/test_file_eps.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import io
import unittest

from PIL import EpsImagePlugin, Image
from PIL import EpsImagePlugin, Image, features

from .helper import PillowTestCase, assert_image_similar, hopper
from .helper import PillowTestCase, assert_image_similar, hopper, skip_unless_feature

HAS_GHOSTSCRIPT = EpsImagePlugin.has_ghostscript()

Expand Down Expand Up @@ -67,7 +67,7 @@ def test_cmyk(self):
cmyk_image.load()
self.assertEqual(cmyk_image.mode, "RGB")

if "jpeg_decoder" in dir(Image.core):
if features.check("jpg"):
with Image.open("Tests/images/pil_sample_rgb.jpg") as target:
assert_image_similar(cmyk_image, target, 10)

Expand Down Expand Up @@ -114,11 +114,9 @@ def test_image_mode_not_supported(self):
self.assertRaises(ValueError, im.save, tmpfile)

@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
@skip_unless_feature("zlib")
def test_render_scale1(self):
# We need png support for these render test
codecs = dir(Image.core)
if "zip_encoder" not in codecs or "zip_decoder" not in codecs:
self.skipTest("zip/deflate support not available")

# Zero bounding box
with Image.open(file1) as image1_scale1:
Expand All @@ -137,11 +135,9 @@ def test_render_scale1(self):
assert_image_similar(image2_scale1, image2_scale1_compare, 10)

@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
@skip_unless_feature("zlib")
def test_render_scale2(self):
# We need png support for these render test
codecs = dir(Image.core)
if "zip_encoder" not in codecs or "zip_decoder" not in codecs:
self.skipTest("zip/deflate support not available")

# Zero bounding box
with Image.open(file1) as image1_scale2:
Expand Down
17 changes: 2 additions & 15 deletions Tests/test_file_gif.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from io import BytesIO

import pytest
from PIL import GifImagePlugin, Image, ImageDraw, ImagePalette
from PIL import GifImagePlugin, Image, ImageDraw, ImagePalette, features

from .helper import (
PillowTestCase,
Expand All @@ -13,15 +13,6 @@
netpbm_available,
)

try:
from PIL import _webp

HAVE_WEBP = True
except ImportError:
HAVE_WEBP = False

codecs = dir(Image.core)

# sample gif stream
TEST_GIF = "Tests/images/hopper.gif"

Expand All @@ -30,10 +21,6 @@


class TestFileGif(PillowTestCase):
def setUp(self):
if "gif_encoder" not in codecs or "gif_decoder" not in codecs:
self.skipTest("gif support not available") # can this happen?

def test_sanity(self):
with Image.open(TEST_GIF) as im:
im.load()
Expand Down Expand Up @@ -562,7 +549,7 @@ def test_background(self):

self.assertEqual(reread.info["background"], im.info["background"])

if HAVE_WEBP and _webp.HAVE_WEBPANIM:
if features.check("webp") and features.check("webp_anim"):
with Image.open("Tests/images/hopper.webp") as im:
self.assertIsInstance(im.info["background"], tuple)
im.save(out)
Expand Down
13 changes: 3 additions & 10 deletions Tests/test_file_jpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@
djpeg_available,
hopper,
is_win32,
skip_unless_feature,
unittest,
)

codecs = dir(Image.core)

TEST_FILE = "Tests/images/hopper.jpg"


@skip_unless_feature("jpg")
class TestFileJpeg(PillowTestCase):
def setUp(self):
if "jpeg_encoder" not in codecs or "jpeg_decoder" not in codecs:
self.skipTest("jpeg support not available")

def roundtrip(self, im, **options):
out = BytesIO()
im.save(out, "JPEG", **options)
Expand Down Expand Up @@ -687,11 +683,8 @@ def test_photoshop_malformed_and_multiple(self):


@unittest.skipUnless(is_win32(), "Windows only")
@skip_unless_feature("jpg")
class TestFileCloseW32(PillowTestCase):
def setUp(self):
if "jpeg_encoder" not in codecs or "jpeg_decoder" not in codecs:
self.skipTest("jpeg support not available")

def test_fd_leak(self):
tmpfile = self.tempfile("temp.jpg")

Expand Down
8 changes: 2 additions & 6 deletions Tests/test_file_jpeg2k.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
assert_image_similar,
is_big_endian,
on_ci,
skip_unless_feature,
)

codecs = dir(Image.core)

test_card = Image.open("Tests/images/test-card.png")
test_card.load()

Expand All @@ -21,11 +20,8 @@
# 'Not enough memory to handle tile data'


@skip_unless_feature("jpg_2000")
class TestFileJpeg2k(PillowTestCase):
def setUp(self):
if "jpeg2k_encoder" not in codecs or "jpeg2k_decoder" not in codecs:
self.skipTest("JPEG 2000 support not available")

def roundtrip(self, im, **options):
out = BytesIO()
im.save(out, "JPEG2000", **options)
Expand Down
14 changes: 4 additions & 10 deletions Tests/test_file_libtiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from collections import namedtuple
from ctypes import c_float

from PIL import Image, ImageFilter, TiffImagePlugin, TiffTags, features
from PIL import Image, ImageFilter, TiffImagePlugin, TiffTags

from .helper import (
PillowTestCase,
Expand All @@ -15,16 +15,14 @@
assert_image_similar,
assert_image_similar_tofile,
hopper,
skip_unless_feature,
)

logger = logging.getLogger(__name__)


@skip_unless_feature("libtiff")
class LibTiffTestCase(PillowTestCase):
def setUp(self):
if not features.check("libtiff"):
self.skipTest("tiff support not available")

def _assert_noerr(self, im):
"""Helper tests that assert basic sanity about the g4 tiff reading"""
# 1 bit
Expand Down Expand Up @@ -727,13 +725,9 @@ def test_16bit_RGBa_tiff(self):

assert_image_equal_tofile(im, "Tests/images/tiff_16bit_RGBa_target.png")

@skip_unless_feature("jpg")
def test_gimp_tiff(self):
# Read TIFF JPEG images from GIMP [@PIL168]

codecs = dir(Image.core)
if "jpeg_decoder" not in codecs:
self.skipTest("jpeg support not available")

filename = "Tests/images/pil168.tif"
with Image.open(filename) as im:
self.assertEqual(im.mode, "RGB")
Expand Down
6 changes: 3 additions & 3 deletions Tests/test_file_mic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from PIL import Image, ImagePalette, features
from PIL import Image, ImagePalette

from .helper import assert_image_similar, hopper
from .helper import assert_image_similar, hopper, skip_unless_feature

try:
from PIL import MicImagePlugin
Expand All @@ -15,7 +15,7 @@

pytestmark = [
pytest.mark.skipif(not olefile_installed, reason="olefile package not installed"),
pytest.mark.skipif(not features.check("libtiff"), reason="libtiff not installed"),
skip_unless_feature("libtiff"),
]


Expand Down
8 changes: 2 additions & 6 deletions Tests/test_file_mpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
import pytest
from PIL import Image

from .helper import assert_image_similar, is_pypy
from .helper import assert_image_similar, is_pypy, skip_unless_feature

test_files = ["Tests/images/sugarshack.mpo", "Tests/images/frozenpond.mpo"]


def setup_module():
codecs = dir(Image.core)
if "jpeg_encoder" not in codecs or "jpeg_decoder" not in codecs:
pytest.skip("jpeg support not available")
pytestmark = skip_unless_feature("jpg")


def frame_roundtrip(im, **options):
Expand Down
26 changes: 5 additions & 21 deletions Tests/test_file_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,9 @@
is_big_endian,
is_win32,
on_ci,
skip_unless_feature,
)

try:
from PIL import _webp

HAVE_WEBP = True
except ImportError:
HAVE_WEBP = False

codecs = dir(Image.core)


# sample png stream

TEST_PNG_FILE = "Tests/images/hopper.png"
Expand Down Expand Up @@ -63,11 +54,8 @@ def roundtrip(im, **options):
return Image.open(out)


@skip_unless_feature("zlib")
class TestFilePng(PillowTestCase):
def setUp(self):
if "zip_encoder" not in codecs or "zip_decoder" not in codecs:
self.skipTest("zip/deflate support not available")

def get_chunks(self, filename):
chunks = []
with open(filename, "rb") as fp:
Expand Down Expand Up @@ -632,9 +620,8 @@ def test_exif_argument(self):
with Image.open(test_file) as reloaded:
self.assertEqual(reloaded.info["exif"], b"Exif\x00\x00exifstring")

@unittest.skipUnless(
HAVE_WEBP and _webp.HAVE_WEBPANIM, "WebP support not installed with animation"
)
@skip_unless_feature("webp")
@skip_unless_feature("webp_anim")
def test_apng(self):
with Image.open("Tests/images/iss634.apng") as im:
self.assertEqual(im.get_format_mimetype(), "image/apng")
Expand All @@ -645,14 +632,11 @@ def test_apng(self):


@unittest.skipIf(is_win32(), "requires Unix or macOS")
@skip_unless_feature("zlib")
class TestTruncatedPngPLeaks(PillowLeakTestCase):
mem_limit = 2 * 1024 # max increase in K
iterations = 100 # Leak is 56k/iteration, this will leak 5.6megs

def setUp(self):
if "zip_encoder" not in codecs or "zip_decoder" not in codecs:
self.skipTest("zip/deflate support not available")

def test_leak_load(self):
with open("Tests/images/hopper.png", "rb") as f:
DATA = BytesIO(f.read(16 * 1024))
Expand Down