Fixed asserting that no warnings were raised

This commit is contained in:
Andrew Murray 2021-02-10 23:37:55 +11:00
parent cc4ed21f19
commit 01be700081
15 changed files with 53 additions and 43 deletions

View File

@ -20,7 +20,7 @@ def test_bad():
either""" either"""
for f in get_files("b"): for f in get_files("b"):
def open(f): with pytest.warns(None) as record:
try: try:
with Image.open(f) as im: with Image.open(f) as im:
im.load() im.load()
@ -28,7 +28,7 @@ def test_bad():
pass pass
# Assert that there is no unclosed file warning # Assert that there is no unclosed file warning
pytest.warns(None, open, f) assert len(record) == 0
def test_questionable(): def test_questionable():

View File

@ -31,20 +31,20 @@ def test_unclosed_file():
def test_closed_file(): def test_closed_file():
def open(): with pytest.warns(None) as record:
im = Image.open(TEST_FILE) im = Image.open(TEST_FILE)
im.load() im.load()
im.close() im.close()
pytest.warns(None, open) assert len(record) == 0
def test_context_manager(): def test_context_manager():
def open(): with pytest.warns(None) as record:
with Image.open(TEST_FILE) as im: with Image.open(TEST_FILE) as im:
im.load() im.load()
pytest.warns(None, open) assert len(record) == 0
def test_invalid_file(): def test_invalid_file():

View File

@ -38,20 +38,20 @@ def test_unclosed_file():
def test_closed_file(): def test_closed_file():
def open(): with pytest.warns(None) as record:
im = Image.open(static_test_file) im = Image.open(static_test_file)
im.load() im.load()
im.close() im.close()
pytest.warns(None, open) assert len(record) == 0
def test_context_manager(): def test_context_manager():
def open(): with pytest.warns(None) as record:
with Image.open(static_test_file) as im: with Image.open(static_test_file) as im:
im.load() im.load()
pytest.warns(None, open) assert len(record) == 0
def test_tell(): def test_tell():

View File

@ -38,20 +38,20 @@ def test_unclosed_file():
def test_closed_file(): def test_closed_file():
def open(): with pytest.warns(None) as record:
im = Image.open(TEST_GIF) im = Image.open(TEST_GIF)
im.load() im.load()
im.close() im.close()
pytest.warns(None, open) assert len(record) == 0
def test_context_manager(): def test_context_manager():
def open(): with pytest.warns(None) as record:
with Image.open(TEST_GIF) as im: with Image.open(TEST_GIF) as im:
im.load() im.load()
pytest.warns(None, open) assert len(record) == 0
def test_invalid_file(): def test_invalid_file():

View File

@ -19,7 +19,9 @@ def test_sanity():
with Image.open(TEST_FILE) as im: with Image.open(TEST_FILE) as im:
# Assert that there is no unclosed file warning # Assert that there is no unclosed file warning
pytest.warns(None, im.load) with pytest.warns(None) as record:
im.load()
assert len(record) == 0
assert im.mode == "RGBA" assert im.mode == "RGBA"
assert im.size == (1024, 1024) assert im.size == (1024, 1024)

View File

@ -35,20 +35,20 @@ def test_unclosed_file():
def test_closed_file(): def test_closed_file():
def open(): with pytest.warns(None) as record:
im = Image.open(TEST_IM) im = Image.open(TEST_IM)
im.load() im.load()
im.close() im.close()
pytest.warns(None, open) assert len(record) == 0
def test_context_manager(): def test_context_manager():
def open(): with pytest.warns(None) as record:
with Image.open(TEST_IM) as im: with Image.open(TEST_IM) as im:
im.load() im.load()
pytest.warns(None, open) assert len(record) == 0
def test_tell(): def test_tell():

View File

@ -41,20 +41,20 @@ def test_unclosed_file():
def test_closed_file(): def test_closed_file():
def open(): with pytest.warns(None) as record:
im = Image.open(test_files[0]) im = Image.open(test_files[0])
im.load() im.load()
im.close() im.close()
pytest.warns(None, open) assert len(record) == 0
def test_context_manager(): def test_context_manager():
def open(): with pytest.warns(None) as record:
with Image.open(test_files[0]) as im: with Image.open(test_files[0]) as im:
im.load() im.load()
pytest.warns(None, open) assert len(record) == 0
def test_app(): def test_app():

View File

@ -324,7 +324,9 @@ class TestFilePng:
with Image.open(TEST_PNG_FILE) as im: with Image.open(TEST_PNG_FILE) as im:
# Assert that there is no unclosed file warning # Assert that there is no unclosed file warning
pytest.warns(None, im.verify) with pytest.warns(None) as record:
im.verify()
assert len(record) == 0
with Image.open(TEST_PNG_FILE) as im: with Image.open(TEST_PNG_FILE) as im:
im.load() im.load()

View File

@ -29,20 +29,20 @@ def test_unclosed_file():
def test_closed_file(): def test_closed_file():
def open(): with pytest.warns(None) as record:
im = Image.open(test_file) im = Image.open(test_file)
im.load() im.load()
im.close() im.close()
pytest.warns(None, open) assert len(record) == 0
def test_context_manager(): def test_context_manager():
def open(): with pytest.warns(None) as record:
with Image.open(test_file) as im: with Image.open(test_file) as im:
im.load() im.load()
pytest.warns(None, open) assert len(record) == 0
def test_invalid_file(): def test_invalid_file():

View File

@ -28,20 +28,20 @@ def test_unclosed_file():
def test_closed_file(): def test_closed_file():
def open(): with pytest.warns(None) as record:
im = Image.open(TEST_FILE) im = Image.open(TEST_FILE)
im.load() im.load()
im.close() im.close()
pytest.warns(None, open) assert len(record) == 0
def test_context_manager(): def test_context_manager():
def open(): with pytest.warns(None) as record:
with Image.open(TEST_FILE) as im: with Image.open(TEST_FILE) as im:
im.load() im.load()
pytest.warns(None, open) assert len(record) == 0
def test_save(tmp_path): def test_save(tmp_path):

View File

@ -31,16 +31,16 @@ def test_unclosed_file():
def test_close(): def test_close():
def open(): with pytest.warns(None) as record:
tar = TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg") tar = TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg")
tar.close() tar.close()
pytest.warns(None, open) assert len(record) == 0
def test_contextmanager(): def test_contextmanager():
def open(): with pytest.warns(None) as record:
with TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg"): with TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg"):
pass pass
pytest.warns(None, open) assert len(record) == 0

View File

@ -59,19 +59,19 @@ class TestFileTiff:
pytest.warns(ResourceWarning, open) pytest.warns(ResourceWarning, open)
def test_closed_file(self): def test_closed_file(self):
def open(): with pytest.warns(None) as record:
im = Image.open("Tests/images/multipage.tiff") im = Image.open("Tests/images/multipage.tiff")
im.load() im.load()
im.close() im.close()
pytest.warns(None, open) assert len(record) == 0
def test_context_manager(self): def test_context_manager(self):
def open(): with pytest.warns(None) as record:
with Image.open("Tests/images/multipage.tiff") as im: with Image.open("Tests/images/multipage.tiff") as im:
im.load() im.load()
pytest.warns(None, open) assert len(record) == 0
def test_mac_tiff(self): def test_mac_tiff(self):
# Read RGBa images from macOS [@PIL136] # Read RGBa images from macOS [@PIL136]

View File

@ -145,7 +145,9 @@ class TestFileWebp:
file_path = "Tests/images/hopper.webp" file_path = "Tests/images/hopper.webp"
with Image.open(file_path) as image: with Image.open(file_path) as image:
temp_file = str(tmp_path / "temp.webp") temp_file = str(tmp_path / "temp.webp")
pytest.warns(None, image.save, temp_file) with pytest.warns(None) as record:
image.save(temp_file)
assert len(record) == 0
def test_file_pointer_could_be_reused(self): def test_file_pointer_could_be_reused(self):
file_path = "Tests/images/hopper.webp" file_path = "Tests/images/hopper.webp"

View File

@ -636,7 +636,9 @@ class TestImage:
# Act/Assert # Act/Assert
with Image.open(test_file) as im: with Image.open(test_file) as im:
pytest.warns(None, im.save, temp_file) with pytest.warns(None) as record:
im.save(temp_file)
assert len(record) == 0
def test_load_on_nonexclusive_multiframe(self): def test_load_on_nonexclusive_multiframe(self):
with open("Tests/images/frozenpond.mpo", "rb") as fp: with open("Tests/images/frozenpond.mpo", "rb") as fp:

View File

@ -234,4 +234,6 @@ def test_no_resource_warning_for_numpy_array():
with Image.open(test_file) as im: with Image.open(test_file) as im:
# Act/Assert # Act/Assert
pytest.warns(None, lambda: array(im)) with pytest.warns(None) as record:
array(im)
assert len(record) == 0