Removed unittest mock

This commit is contained in:
Andrew Murray 2020-03-28 22:10:58 +11:00
parent 7b628a5ef6
commit a2ab6ac649

View File

@ -3,8 +3,9 @@ import distutils.version
import os import os
import re import re
import shutil import shutil
import sys
from contextlib import contextmanager
from io import BytesIO from io import BytesIO
from unittest import mock
import pytest import pytest
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
@ -25,6 +26,23 @@ FONT_SIZE = 20
TEST_TEXT = "hey you\nyou are awesome\nthis looks awkward" TEST_TEXT = "hey you\nyou are awesome\nthis looks awkward"
@contextmanager
def mock_patch(target, property, new_value):
created = not hasattr(target, property)
if not created:
original_value = getattr(target, property)
setattr(target, property, new_value)
try:
yield
finally:
if created:
delattr(target, property)
else:
setattr(target, property, original_value)
@skip_unless_feature("freetype2") @skip_unless_feature("freetype2")
class TestImageFont(PillowTestCase): class TestImageFont(PillowTestCase):
LAYOUT_ENGINE = ImageFont.LAYOUT_BASIC LAYOUT_ENGINE = ImageFont.LAYOUT_BASIC
@ -460,7 +478,7 @@ class TestImageFont(PillowTestCase):
def _test_fake_loading_font(self, path_to_fake, fontname): def _test_fake_loading_font(self, path_to_fake, fontname):
# Make a copy of FreeTypeFont so we can patch the original # Make a copy of FreeTypeFont so we can patch the original
free_type_font = copy.deepcopy(ImageFont.FreeTypeFont) free_type_font = copy.deepcopy(ImageFont.FreeTypeFont)
with mock.patch.object(ImageFont, "_FreeTypeFont", free_type_font, create=True): with mock_patch(ImageFont, "_FreeTypeFont", free_type_font):
def loadable_font(filepath, size, index, encoding, *args, **kwargs): def loadable_font(filepath, size, index, encoding, *args, **kwargs):
if filepath == path_to_fake: if filepath == path_to_fake:
@ -471,7 +489,7 @@ class TestImageFont(PillowTestCase):
filepath, size, index, encoding, *args, **kwargs filepath, size, index, encoding, *args, **kwargs
) )
with mock.patch.object(ImageFont, "FreeTypeFont", loadable_font): with mock_patch(ImageFont, "FreeTypeFont", loadable_font):
font = ImageFont.truetype(fontname) font = ImageFont.truetype(fontname)
# Make sure it's loaded # Make sure it's loaded
name = font.getname() name = font.getname()
@ -482,9 +500,12 @@ class TestImageFont(PillowTestCase):
# A lot of mocking here - this is more for hitting code and # A lot of mocking here - this is more for hitting code and
# catching syntax like errors # catching syntax like errors
font_directory = "/usr/local/share/fonts" font_directory = "/usr/local/share/fonts"
with mock.patch("sys.platform", "linux"): with mock_patch(sys, "platform", "linux"):
patched_env = {"XDG_DATA_DIRS": "/usr/share/:/usr/local/share/"} created = "XDG_DATA_DIRS" not in os.environ
with mock.patch.dict(os.environ, patched_env): if not created:
original_value = os.environ["XDG_DATA_DIRS"]
os.environ["XDG_DATA_DIRS"] = "/usr/share/:/usr/local/share/"
try:
def fake_walker(path): def fake_walker(path):
if path == font_directory: if path == font_directory:
@ -502,7 +523,7 @@ class TestImageFont(PillowTestCase):
] ]
return [(path, [], ["some_random_font.ttf"])] return [(path, [], ["some_random_font.ttf"])]
with mock.patch("os.walk", fake_walker): with mock_patch(os, "walk", fake_walker):
# Test that the font loads both with and without the # Test that the font loads both with and without the
# extension # extension
self._test_fake_loading_font( self._test_fake_loading_font(
@ -521,13 +542,18 @@ class TestImageFont(PillowTestCase):
self._test_fake_loading_font( self._test_fake_loading_font(
font_directory + "/Duplicate.ttf", "Duplicate" font_directory + "/Duplicate.ttf", "Duplicate"
) )
finally:
if created:
del os.environ["XDG_DATA_DIRS"]
else:
os.environ["XDG_DATA_DIRS"] = original_value
@pytest.mark.skipif(is_win32(), reason="requires Unix or macOS") @pytest.mark.skipif(is_win32(), reason="requires Unix or macOS")
def test_find_macos_font(self): def test_find_macos_font(self):
# Like the linux test, more cover hitting code rather than testing # Like the linux test, more cover hitting code rather than testing
# correctness. # correctness.
font_directory = "/System/Library/Fonts" font_directory = "/System/Library/Fonts"
with mock.patch("sys.platform", "darwin"): with mock_patch(sys, "platform", "darwin"):
def fake_walker(path): def fake_walker(path):
if path == font_directory: if path == font_directory:
@ -545,7 +571,7 @@ class TestImageFont(PillowTestCase):
] ]
return [(path, [], ["some_random_font.ttf"])] return [(path, [], ["some_random_font.ttf"])]
with mock.patch("os.walk", fake_walker): with mock_patch(os, "walk", fake_walker):
self._test_fake_loading_font(font_directory + "/Arial.ttf", "Arial.ttf") self._test_fake_loading_font(font_directory + "/Arial.ttf", "Arial.ttf")
self._test_fake_loading_font(font_directory + "/Arial.ttf", "Arial") self._test_fake_loading_font(font_directory + "/Arial.ttf", "Arial")
self._test_fake_loading_font(font_directory + "/Single.otf", "Single") self._test_fake_loading_font(font_directory + "/Single.otf", "Single")