From 01be7000814ad4032aff74eb659ca8fa8a5b4164 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 10 Feb 2021 23:37:55 +1100 Subject: [PATCH] Fixed asserting that no warnings were raised --- Tests/test_bmp_reference.py | 4 ++-- Tests/test_file_dcx.py | 8 ++++---- Tests/test_file_fli.py | 8 ++++---- Tests/test_file_gif.py | 8 ++++---- Tests/test_file_icns.py | 4 +++- Tests/test_file_im.py | 8 ++++---- Tests/test_file_mpo.py | 8 ++++---- Tests/test_file_png.py | 4 +++- Tests/test_file_psd.py | 8 ++++---- Tests/test_file_spider.py | 8 ++++---- Tests/test_file_tar.py | 8 ++++---- Tests/test_file_tiff.py | 8 ++++---- Tests/test_file_webp.py | 4 +++- Tests/test_image.py | 4 +++- Tests/test_numpy.py | 4 +++- 15 files changed, 53 insertions(+), 43 deletions(-) diff --git a/Tests/test_bmp_reference.py b/Tests/test_bmp_reference.py index 19602c1e7..46fe1750f 100644 --- a/Tests/test_bmp_reference.py +++ b/Tests/test_bmp_reference.py @@ -20,7 +20,7 @@ def test_bad(): either""" for f in get_files("b"): - def open(f): + with pytest.warns(None) as record: try: with Image.open(f) as im: im.load() @@ -28,7 +28,7 @@ def test_bad(): pass # Assert that there is no unclosed file warning - pytest.warns(None, open, f) + assert len(record) == 0 def test_questionable(): diff --git a/Tests/test_file_dcx.py b/Tests/test_file_dcx.py index 818d6ed5e..ec399d3ae 100644 --- a/Tests/test_file_dcx.py +++ b/Tests/test_file_dcx.py @@ -31,20 +31,20 @@ def test_unclosed_file(): def test_closed_file(): - def open(): + with pytest.warns(None) as record: im = Image.open(TEST_FILE) im.load() im.close() - pytest.warns(None, open) + assert len(record) == 0 def test_context_manager(): - def open(): + with pytest.warns(None) as record: with Image.open(TEST_FILE) as im: im.load() - pytest.warns(None, open) + assert len(record) == 0 def test_invalid_file(): diff --git a/Tests/test_file_fli.py b/Tests/test_file_fli.py index 16b3dc59a..43ae9b0e0 100644 --- a/Tests/test_file_fli.py +++ b/Tests/test_file_fli.py @@ -38,20 +38,20 @@ def test_unclosed_file(): def test_closed_file(): - def open(): + with pytest.warns(None) as record: im = Image.open(static_test_file) im.load() im.close() - pytest.warns(None, open) + assert len(record) == 0 def test_context_manager(): - def open(): + with pytest.warns(None) as record: with Image.open(static_test_file) as im: im.load() - pytest.warns(None, open) + assert len(record) == 0 def test_tell(): diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index cf3a65e18..adbc8c6aa 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -38,20 +38,20 @@ def test_unclosed_file(): def test_closed_file(): - def open(): + with pytest.warns(None) as record: im = Image.open(TEST_GIF) im.load() im.close() - pytest.warns(None, open) + assert len(record) == 0 def test_context_manager(): - def open(): + with pytest.warns(None) as record: with Image.open(TEST_GIF) as im: im.load() - pytest.warns(None, open) + assert len(record) == 0 def test_invalid_file(): diff --git a/Tests/test_file_icns.py b/Tests/test_file_icns.py index a3d502d42..04d17ccf5 100644 --- a/Tests/test_file_icns.py +++ b/Tests/test_file_icns.py @@ -19,7 +19,9 @@ def test_sanity(): with Image.open(TEST_FILE) as im: # 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.size == (1024, 1024) diff --git a/Tests/test_file_im.py b/Tests/test_file_im.py index afea82359..b736c3aa6 100644 --- a/Tests/test_file_im.py +++ b/Tests/test_file_im.py @@ -35,20 +35,20 @@ def test_unclosed_file(): def test_closed_file(): - def open(): + with pytest.warns(None) as record: im = Image.open(TEST_IM) im.load() im.close() - pytest.warns(None, open) + assert len(record) == 0 def test_context_manager(): - def open(): + with pytest.warns(None) as record: with Image.open(TEST_IM) as im: im.load() - pytest.warns(None, open) + assert len(record) == 0 def test_tell(): diff --git a/Tests/test_file_mpo.py b/Tests/test_file_mpo.py index 791efcc3f..ad11c3d5d 100644 --- a/Tests/test_file_mpo.py +++ b/Tests/test_file_mpo.py @@ -41,20 +41,20 @@ def test_unclosed_file(): def test_closed_file(): - def open(): + with pytest.warns(None) as record: im = Image.open(test_files[0]) im.load() im.close() - pytest.warns(None, open) + assert len(record) == 0 def test_context_manager(): - def open(): + with pytest.warns(None) as record: with Image.open(test_files[0]) as im: im.load() - pytest.warns(None, open) + assert len(record) == 0 def test_app(): diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 289c09767..16b461f30 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -324,7 +324,9 @@ class TestFilePng: with Image.open(TEST_PNG_FILE) as im: # 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: im.load() diff --git a/Tests/test_file_psd.py b/Tests/test_file_psd.py index 8bb45630e..eb2d94cd2 100644 --- a/Tests/test_file_psd.py +++ b/Tests/test_file_psd.py @@ -29,20 +29,20 @@ def test_unclosed_file(): def test_closed_file(): - def open(): + with pytest.warns(None) as record: im = Image.open(test_file) im.load() im.close() - pytest.warns(None, open) + assert len(record) == 0 def test_context_manager(): - def open(): + with pytest.warns(None) as record: with Image.open(test_file) as im: im.load() - pytest.warns(None, open) + assert len(record) == 0 def test_invalid_file(): diff --git a/Tests/test_file_spider.py b/Tests/test_file_spider.py index 9cdb451c9..c7ed91f0d 100644 --- a/Tests/test_file_spider.py +++ b/Tests/test_file_spider.py @@ -28,20 +28,20 @@ def test_unclosed_file(): def test_closed_file(): - def open(): + with pytest.warns(None) as record: im = Image.open(TEST_FILE) im.load() im.close() - pytest.warns(None, open) + assert len(record) == 0 def test_context_manager(): - def open(): + with pytest.warns(None) as record: with Image.open(TEST_FILE) as im: im.load() - pytest.warns(None, open) + assert len(record) == 0 def test_save(tmp_path): diff --git a/Tests/test_file_tar.py b/Tests/test_file_tar.py index 02001e5b1..39356c9ee 100644 --- a/Tests/test_file_tar.py +++ b/Tests/test_file_tar.py @@ -31,16 +31,16 @@ def test_unclosed_file(): def test_close(): - def open(): + with pytest.warns(None) as record: tar = TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg") tar.close() - pytest.warns(None, open) + assert len(record) == 0 def test_contextmanager(): - def open(): + with pytest.warns(None) as record: with TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg"): pass - pytest.warns(None, open) + assert len(record) == 0 diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index f644ef887..f378666ad 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -59,19 +59,19 @@ class TestFileTiff: pytest.warns(ResourceWarning, open) def test_closed_file(self): - def open(): + with pytest.warns(None) as record: im = Image.open("Tests/images/multipage.tiff") im.load() im.close() - pytest.warns(None, open) + assert len(record) == 0 def test_context_manager(self): - def open(): + with pytest.warns(None) as record: with Image.open("Tests/images/multipage.tiff") as im: im.load() - pytest.warns(None, open) + assert len(record) == 0 def test_mac_tiff(self): # Read RGBa images from macOS [@PIL136] diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index 11fbd9fd5..56c163aab 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -145,7 +145,9 @@ class TestFileWebp: file_path = "Tests/images/hopper.webp" with Image.open(file_path) as image: 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): file_path = "Tests/images/hopper.webp" diff --git a/Tests/test_image.py b/Tests/test_image.py index 3c2d128ee..13ab8b7bf 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -636,7 +636,9 @@ class TestImage: # Act/Assert 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): with open("Tests/images/frozenpond.mpo", "rb") as fp: diff --git a/Tests/test_numpy.py b/Tests/test_numpy.py index da367fa46..42c7a9281 100644 --- a/Tests/test_numpy.py +++ b/Tests/test_numpy.py @@ -234,4 +234,6 @@ def test_no_resource_warning_for_numpy_array(): with Image.open(test_file) as im: # Act/Assert - pytest.warns(None, lambda: array(im)) + with pytest.warns(None) as record: + array(im) + assert len(record) == 0