Pillow/Tests/test_features.py

143 lines
3.9 KiB
Python
Raw Normal View History

Add __main__.py to output basic format and support information To help debug and show supported formats, users can run: $ python -m PIL to get basic format and support information about the installed version of Pillow. The new feature works as follows: $ python -m PIL -------------------------------------------------------------------- Pillow 6.1.0.dev0 -------------------------------------------------------------------- Python modules loaded from .../Pillow/src/PIL Binary modules loaded from .../Pillow/src/PIL -------------------------------------------------------------------- Python 3.7.3 (default, May 11 2019, 00:38:04) [GCC 9.1.1 20190503 (Red Hat 9.1.1-1)] -------------------------------------------------------------------- --- PIL CORE support ok --- TKINTER support ok --- FREETYPE2 support ok --- LITTLECMS2 support ok --- WEBP support ok --- WEBP Transparency support ok --- WEBPMUX support ok --- WEBP Animation support ok --- JPEG support ok --- OPENJPEG (JPEG2000) support ok --- ZLIB (PNG/ZIP) support ok --- LIBTIFF support ok --- RAQM (Bidirectional Text) support ok -------------------------------------------------------------------- BLP Extensions: .blp Features: open -------------------------------------------------------------------- BMP image/bmp Extensions: .bmp Features: open, save -------------------------------------------------------------------- BUFR Extensions: .bufr Features: open, save -------------------------------------------------------------------- …
2019-05-25 21:11:33 +03:00
import io
2020-06-15 16:32:30 +03:00
import re
Add __main__.py to output basic format and support information To help debug and show supported formats, users can run: $ python -m PIL to get basic format and support information about the installed version of Pillow. The new feature works as follows: $ python -m PIL -------------------------------------------------------------------- Pillow 6.1.0.dev0 -------------------------------------------------------------------- Python modules loaded from .../Pillow/src/PIL Binary modules loaded from .../Pillow/src/PIL -------------------------------------------------------------------- Python 3.7.3 (default, May 11 2019, 00:38:04) [GCC 9.1.1 20190503 (Red Hat 9.1.1-1)] -------------------------------------------------------------------- --- PIL CORE support ok --- TKINTER support ok --- FREETYPE2 support ok --- LITTLECMS2 support ok --- WEBP support ok --- WEBP Transparency support ok --- WEBPMUX support ok --- WEBP Animation support ok --- JPEG support ok --- OPENJPEG (JPEG2000) support ok --- ZLIB (PNG/ZIP) support ok --- LIBTIFF support ok --- RAQM (Bidirectional Text) support ok -------------------------------------------------------------------- BLP Extensions: .blp Features: open -------------------------------------------------------------------- BMP image/bmp Extensions: .bmp Features: open, save -------------------------------------------------------------------- BUFR Extensions: .bufr Features: open, save -------------------------------------------------------------------- …
2019-05-25 21:11:33 +03:00
2020-01-27 14:46:52 +03:00
import pytest
2015-05-15 00:33:17 +03:00
from PIL import features
from .helper import skip_unless_feature
2017-05-12 00:34:21 +03:00
try:
from PIL import _webp
2018-10-02 11:36:07 +03:00
except ImportError:
pass
2017-09-01 14:05:40 +03:00
2015-05-15 00:33:17 +03:00
2020-01-27 14:46:52 +03:00
def test_check():
# Check the correctness of the convenience function
for module in features.modules:
assert features.check_module(module) == features.check(module)
for codec in features.codecs:
assert features.check_codec(codec) == features.check(codec)
for feature in features.features:
assert features.check_feature(feature) == features.check(feature)
2020-06-15 16:32:30 +03:00
def test_version():
# Check the correctness of the convenience function
# and the format of version numbers
def test(name, function):
version = features.version(name)
if not features.check(name):
assert version is None
else:
assert function(name) == version
if name != "PIL":
assert version is None or re.search(r"\d+(\.\d+)*$", version)
for module in features.modules:
test(module, features.version_module)
for codec in features.codecs:
test(codec, features.version_codec)
for feature in features.features:
test(feature, features.version_feature)
@skip_unless_feature("webp")
2020-01-27 14:46:52 +03:00
def test_webp_transparency():
assert features.check("transp_webp") != _webp.WebPDecoderBuggyAlpha()
assert features.check("transp_webp") == _webp.HAVE_TRANSPARENCY
@skip_unless_feature("webp")
2020-01-27 14:46:52 +03:00
def test_webp_mux():
assert features.check("webp_mux") == _webp.HAVE_WEBPMUX
@skip_unless_feature("webp")
2020-01-27 14:46:52 +03:00
def test_webp_anim():
assert features.check("webp_anim") == _webp.HAVE_WEBPANIM
2020-06-15 16:32:30 +03:00
@skip_unless_feature("libjpeg_turbo")
def test_libjpeg_turbo_version():
assert re.search(r"\d+\.\d+\.\d+$", features.version("libjpeg_turbo"))
@skip_unless_feature("libimagequant")
def test_libimagequant_version():
assert re.search(r"\d+\.\d+\.\d+$", features.version("libimagequant"))
2020-01-27 14:46:52 +03:00
def test_check_modules():
for feature in features.modules:
assert features.check_module(feature) in [True, False]
2020-06-15 16:32:30 +03:00
def test_check_codecs():
2020-01-27 14:46:52 +03:00
for feature in features.codecs:
assert features.check_codec(feature) in [True, False]
def test_check_warns_on_nonexistent():
with pytest.warns(UserWarning) as cm:
has_feature = features.check("typo")
assert has_feature is False
assert str(cm[-1].message) == "Unknown feature 'typo'."
2020-01-27 14:46:52 +03:00
def test_supported_modules():
assert isinstance(features.get_supported_modules(), list)
assert isinstance(features.get_supported_codecs(), list)
assert isinstance(features.get_supported_features(), list)
assert isinstance(features.get_supported(), list)
def test_unsupported_codec():
# Arrange
codec = "unsupported_codec"
# Act / Assert
with pytest.raises(ValueError):
features.check_codec(codec)
2020-06-15 16:32:30 +03:00
with pytest.raises(ValueError):
features.version_codec(codec)
2020-01-27 14:46:52 +03:00
def test_unsupported_module():
# Arrange
module = "unsupported_module"
# Act / Assert
with pytest.raises(ValueError):
features.check_module(module)
2020-06-15 16:32:30 +03:00
with pytest.raises(ValueError):
features.version_module(module)
2020-01-27 14:46:52 +03:00
def test_pilinfo():
buf = io.StringIO()
features.pilinfo(buf)
out = buf.getvalue()
lines = out.splitlines()
assert lines[0] == "-" * 68
assert lines[1].startswith("Pillow ")
assert lines[2].startswith("Python ")
lines = lines[3:]
while lines[0].startswith(" "):
lines = lines[1:]
assert lines[0] == "-" * 68
assert lines[1].startswith("Python modules loaded from ")
assert lines[2].startswith("Binary modules loaded from ")
assert lines[3] == "-" * 68
jpeg = (
"\n"
+ "-" * 68
+ "\n"
+ "JPEG image/jpeg\n"
+ "Extensions: .jfif, .jpe, .jpeg, .jpg\n"
+ "Features: open, save\n"
+ "-" * 68
+ "\n"
)
assert jpeg in out