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

Fix up some pytest style issues #6968

Merged
merged 7 commits into from
Mar 3, 2023
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
21 changes: 11 additions & 10 deletions Tests/test_core_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,14 @@ def test_units(self):
Image._apply_env_variables({"PILLOW_BLOCK_SIZE": "2m"})
assert Image.core.get_block_size() == 2 * 1024 * 1024

def test_warnings(self):
pytest.warns(
UserWarning, Image._apply_env_variables, {"PILLOW_ALIGNMENT": "15"}
)
pytest.warns(
UserWarning, Image._apply_env_variables, {"PILLOW_BLOCK_SIZE": "1024"}
)
pytest.warns(
UserWarning, Image._apply_env_variables, {"PILLOW_BLOCKS_MAX": "wat"}
)
@pytest.mark.parametrize(
"var",
(
{"PILLOW_ALIGNMENT": "15"},
{"PILLOW_BLOCK_SIZE": "1024"},
{"PILLOW_BLOCKS_MAX": "wat"},
),
)
def test_warnings(self, var):
with pytest.warns(UserWarning):
Image._apply_env_variables(var)
10 changes: 5 additions & 5 deletions Tests/test_decompression_bomb.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ def test_warning(self):
Image.MAX_IMAGE_PIXELS = 128 * 128 - 1
assert Image.MAX_IMAGE_PIXELS == 128 * 128 - 1

def open():
with pytest.warns(Image.DecompressionBombWarning):
with Image.open(TEST_FILE):
pass

pytest.warns(Image.DecompressionBombWarning, open)

def test_exception(self):
# Set limit to trigger exception on the test file
Image.MAX_IMAGE_PIXELS = 64 * 128 - 1
Expand Down Expand Up @@ -87,15 +85,17 @@ def test_enlarge_crop(self):
# same decompression bomb warnings on them.
with hopper() as src:
box = (0, 0, src.width * 2, src.height * 2)
pytest.warns(Image.DecompressionBombWarning, src.crop, box)
with pytest.warns(Image.DecompressionBombWarning):
src.crop(box)

def test_crop_decompression_checks(self):
im = Image.new("RGB", (100, 100))

for value in ((-9999, -9999, -9990, -9990), (-999, -999, -990, -990)):
assert im.crop(value).size == (9, 9)

pytest.warns(Image.DecompressionBombWarning, im.crop, (-160, -160, 99, 99))
with pytest.warns(Image.DecompressionBombWarning):
im.crop((-160, -160, 99, 99))

with pytest.raises(Image.DecompressionBombError):
im.crop((-99909, -99990, 99999, 99999))
16 changes: 4 additions & 12 deletions Tests/test_file_apng.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,11 @@ def test_apng_chunk_errors():
with Image.open("Tests/images/apng/chunk_no_actl.png") as im:
assert not im.is_animated

def open():
with pytest.warns(UserWarning):
with Image.open("Tests/images/apng/chunk_multi_actl.png") as im:
im.load()
akx marked this conversation as resolved.
Show resolved Hide resolved
assert not im.is_animated

pytest.warns(UserWarning, open)

with Image.open("Tests/images/apng/chunk_actl_after_idat.png") as im:
assert not im.is_animated

Expand All @@ -287,21 +285,17 @@ def open():


def test_apng_syntax_errors():
def open_frames_zero():
with pytest.warns(UserWarning):
with Image.open("Tests/images/apng/syntax_num_frames_zero.png") as im:
assert not im.is_animated
with pytest.raises(OSError):
im.load()

pytest.warns(UserWarning, open_frames_zero)

def open_frames_zero_default():
with pytest.warns(UserWarning):
with Image.open("Tests/images/apng/syntax_num_frames_zero_default.png") as im:
assert not im.is_animated
im.load()

pytest.warns(UserWarning, open_frames_zero_default)

# we can handle this case gracefully
exception = None
with Image.open("Tests/images/apng/syntax_num_frames_low.png") as im:
Expand All @@ -316,13 +310,11 @@ def open_frames_zero_default():
im.seek(im.n_frames - 1)
im.load()

def open():
with pytest.warns(UserWarning):
with Image.open("Tests/images/apng/syntax_num_frames_invalid.png") as im:
assert not im.is_animated
im.load()

pytest.warns(UserWarning, open)


@pytest.mark.parametrize(
"test_file",
Expand Down
3 changes: 2 additions & 1 deletion Tests/test_file_dcx.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def open():
im = Image.open(TEST_FILE)
im.load()

pytest.warns(ResourceWarning, open)
with pytest.warns(ResourceWarning):
open()


def test_closed_file():
Expand Down
3 changes: 2 additions & 1 deletion Tests/test_file_fli.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def open():
im = Image.open(static_test_file)
im.load()

pytest.warns(ResourceWarning, open)
with pytest.warns(ResourceWarning):
open()


def test_closed_file():
Expand Down
6 changes: 4 additions & 2 deletions Tests/test_file_gif.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def open():
im = Image.open(TEST_GIF)
im.load()

pytest.warns(ResourceWarning, open)
with pytest.warns(ResourceWarning):
open()


def test_closed_file():
Expand Down Expand Up @@ -1087,7 +1088,8 @@ def test_rgb_transparency(tmp_path):
im = Image.new("RGB", (1, 1))
im.info["transparency"] = b""
ims = [Image.new("RGB", (1, 1))]
pytest.warns(UserWarning, im.save, out, save_all=True, append_images=ims)
with pytest.warns(UserWarning):
im.save(out, save_all=True, append_images=ims)

with Image.open(out) as reloaded:
assert "transparency" not in reloaded.info
Expand Down
4 changes: 1 addition & 3 deletions Tests/test_file_ico.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,10 @@ def test_save_append_images(tmp_path):
def test_unexpected_size():
# This image has been manually hexedited to state that it is 16x32
# while the image within is still 16x16
def open():
with pytest.warns(UserWarning):
with Image.open("Tests/images/hopper_unexpected.ico") as im:
assert im.size == (16, 16)

pytest.warns(UserWarning, open)


def test_draw_reloaded(tmp_path):
with Image.open(TEST_ICO_FILE) as im:
Expand Down
3 changes: 2 additions & 1 deletion Tests/test_file_im.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def open():
im = Image.open(TEST_IM)
im.load()

pytest.warns(ResourceWarning, open)
with pytest.warns(ResourceWarning):
open()


def test_closed_file():
Expand Down
3 changes: 2 additions & 1 deletion Tests/test_file_mpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def open():
im = Image.open(test_files[0])
im.load()

pytest.warns(ResourceWarning, open)
with pytest.warns(ResourceWarning):
open()


def test_closed_file():
Expand Down
3 changes: 2 additions & 1 deletion Tests/test_file_psd.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def open():
im = Image.open(test_file)
im.load()

pytest.warns(ResourceWarning, open)
with pytest.warns(ResourceWarning):
open()


def test_closed_file():
Expand Down
3 changes: 2 additions & 1 deletion Tests/test_file_spider.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def open():
im = Image.open(TEST_FILE)
im.load()

pytest.warns(ResourceWarning, open)
with pytest.warns(ResourceWarning):
open()


def test_closed_file():
Expand Down
4 changes: 1 addition & 3 deletions Tests/test_file_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ def test_sanity(codec, test_path, format):

@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
def test_unclosed_file():
def open():
with pytest.warns(ResourceWarning):
TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg")

pytest.warns(ResourceWarning, open)


def test_close():
with warnings.catch_warnings():
Expand Down
4 changes: 3 additions & 1 deletion Tests/test_file_tga.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ def test_save_id_section(tmp_path):

# Save with custom id section greater than 255 characters
id_section = b"Test content" * 25
pytest.warns(UserWarning, lambda: im.save(out, id_section=id_section))
with pytest.warns(UserWarning):
im.save(out, id_section=id_section)

with Image.open(out) as test_im:
assert test_im.info["id_section"] == id_section[:255]

Expand Down
6 changes: 4 additions & 2 deletions Tests/test_file_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def open():
im = Image.open("Tests/images/multipage.tiff")
im.load()

pytest.warns(ResourceWarning, open)
with pytest.warns(ResourceWarning):
open()

def test_closed_file(self):
with warnings.catch_warnings():
Expand Down Expand Up @@ -231,7 +232,8 @@ def test_invalid_file(self):
def test_bad_exif(self):
with Image.open("Tests/images/hopper_bad_exif.jpg") as i:
# Should not raise struct.error.
pytest.warns(UserWarning, i._getexif)
with pytest.warns(UserWarning):
i._getexif()

def test_save_rgba(self, tmp_path):
im = hopper("RGBA")
Expand Down
8 changes: 5 additions & 3 deletions Tests/test_file_tiff_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ def test_empty_metadata():
head = f.read(8)
info = TiffImagePlugin.ImageFileDirectory(head)
# Should not raise struct.error.
pytest.warns(UserWarning, info.load, f)
with pytest.warns(UserWarning):
info.load(f)


def test_iccprofile(tmp_path):
Expand Down Expand Up @@ -418,11 +419,12 @@ def test_too_many_entries():
ifd = TiffImagePlugin.ImageFileDirectory_v2()

# 277: ("SamplesPerPixel", SHORT, 1),
ifd._tagdata[277] = struct.pack("hh", 4, 4)
ifd._tagdata[277] = struct.pack("<hh", 4, 4)
ifd.tagtype[277] = TiffTags.SHORT

# Should not raise ValueError.
pytest.warns(UserWarning, lambda: ifd[277])
with pytest.warns(UserWarning):
assert ifd[277] == 4


def test_tag_group_data():
Expand Down
5 changes: 4 additions & 1 deletion Tests/test_file_webp.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ def test_unsupported(self):
WebPImagePlugin.SUPPORTED = False

file_path = "Tests/images/hopper.webp"
pytest.warns(UserWarning, lambda: pytest.raises(OSError, Image.open, file_path))
with pytest.warns(UserWarning):
with pytest.raises(OSError):
with Image.open(file_path):
pass

if HAVE_WEBP:
WebPImagePlugin.SUPPORTED = True
Expand Down
37 changes: 18 additions & 19 deletions Tests/test_imagefont.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ def test_rotated_transposed_font(font, orientation):
assert bbox_b[3] == 20 + bbox_a[2] - bbox_a[0]

# text length is undefined for vertical text
pytest.raises(ValueError, draw.textlength, word)
with pytest.raises(ValueError):
draw.textlength(word)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -872,25 +873,23 @@ def test_anchor_invalid(font):
d.font = font

for anchor in ["", "l", "a", "lax", "sa", "xa", "lx"]:
pytest.raises(ValueError, lambda: font.getmask2("hello", anchor=anchor))
pytest.raises(ValueError, lambda: font.getbbox("hello", anchor=anchor))
pytest.raises(ValueError, lambda: d.text((0, 0), "hello", anchor=anchor))
pytest.raises(ValueError, lambda: d.textbbox((0, 0), "hello", anchor=anchor))
pytest.raises(
ValueError, lambda: d.multiline_text((0, 0), "foo\nbar", anchor=anchor)
)
pytest.raises(
ValueError,
lambda: d.multiline_textbbox((0, 0), "foo\nbar", anchor=anchor),
)
with pytest.raises(ValueError):
font.getmask2("hello", anchor=anchor)
with pytest.raises(ValueError):
font.getbbox("hello", anchor=anchor)
with pytest.raises(ValueError):
d.text((0, 0), "hello", anchor=anchor)
with pytest.raises(ValueError):
d.textbbox((0, 0), "hello", anchor=anchor)
with pytest.raises(ValueError):
d.multiline_text((0, 0), "foo\nbar", anchor=anchor)
with pytest.raises(ValueError):
d.multiline_textbbox((0, 0), "foo\nbar", anchor=anchor)
for anchor in ["lt", "lb"]:
pytest.raises(
ValueError, lambda: d.multiline_text((0, 0), "foo\nbar", anchor=anchor)
)
pytest.raises(
ValueError,
lambda: d.multiline_textbbox((0, 0), "foo\nbar", anchor=anchor),
)
with pytest.raises(ValueError):
d.multiline_text((0, 0), "foo\nbar", anchor=anchor)
with pytest.raises(ValueError):
d.multiline_textbbox((0, 0), "foo\nbar", anchor=anchor)


@pytest.mark.parametrize("bpp", (1, 2, 4, 8))
Expand Down
49 changes: 16 additions & 33 deletions Tests/test_imagefontctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,37 +360,20 @@ def test_anchor_invalid_ttb():
d.font = font

for anchor in ["", "l", "a", "lax", "xa", "la", "ls", "ld", "lx"]:
pytest.raises(
ValueError, lambda: font.getmask2("hello", anchor=anchor, direction="ttb")
)
pytest.raises(
ValueError, lambda: font.getbbox("hello", anchor=anchor, direction="ttb")
)
pytest.raises(
ValueError, lambda: d.text((0, 0), "hello", anchor=anchor, direction="ttb")
)
pytest.raises(
ValueError,
lambda: d.textbbox((0, 0), "hello", anchor=anchor, direction="ttb"),
)
pytest.raises(
ValueError,
lambda: d.multiline_text(
(0, 0), "foo\nbar", anchor=anchor, direction="ttb"
),
)
pytest.raises(
ValueError,
lambda: d.multiline_textbbox(
(0, 0), "foo\nbar", anchor=anchor, direction="ttb"
),
)
with pytest.raises(ValueError):
font.getmask2("hello", anchor=anchor, direction="ttb")
with pytest.raises(ValueError):
font.getbbox("hello", anchor=anchor, direction="ttb")
with pytest.raises(ValueError):
d.text((0, 0), "hello", anchor=anchor, direction="ttb")
with pytest.raises(ValueError):
d.textbbox((0, 0), "hello", anchor=anchor, direction="ttb")
with pytest.raises(ValueError):
d.multiline_text((0, 0), "foo\nbar", anchor=anchor, direction="ttb")
with pytest.raises(ValueError):
d.multiline_textbbox((0, 0), "foo\nbar", anchor=anchor, direction="ttb")
# ttb multiline text does not support anchors at all
pytest.raises(
ValueError,
lambda: d.multiline_text((0, 0), "foo\nbar", anchor="mm", direction="ttb"),
)
pytest.raises(
ValueError,
lambda: d.multiline_textbbox((0, 0), "foo\nbar", anchor="mm", direction="ttb"),
)
with pytest.raises(ValueError):
d.multiline_text((0, 0), "foo\nbar", anchor="mm", direction="ttb")
with pytest.raises(ValueError):
d.multiline_textbbox((0, 0), "foo\nbar", anchor="mm", direction="ttb")