diff --git a/Tests/test_image.py b/Tests/test_image.py index c1065d463..cd056d205 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -1087,6 +1087,3 @@ class TestRegistry: def test_encode_registry_fail(self) -> None: with pytest.raises(OSError): Image._getencoder("RGB", "DoesNotExist", ("args",), extra=("extra",)) - - - diff --git a/Tests/test_image_merge.py b/Tests/test_image_merge.py index 40e198623..21b8a1bfd 100644 --- a/Tests/test_image_merge.py +++ b/Tests/test_image_merge.py @@ -2,6 +2,11 @@ import pytest from PIL import Image +def calculate_branch_coverage(): + b = Image.Branches + print("Branches covered:", sum(b.values())) + + def test_merge_wrong_number_of_bands(): R = Image.new('L', (100, 100), color=255) G = Image.new('L', (100, 100), color=128) @@ -22,4 +27,4 @@ def test_merge_size_mismatch(): with pytest.raises(ValueError, match="size mismatch"): Image.merge('RGB', [R, G, B]) - + diff --git a/conftest.py b/conftest.py index 5fda4f477..9fb8731fb 100644 --- a/conftest.py +++ b/conftest.py @@ -1,3 +1,43 @@ from __future__ import annotations +import pytest +import sys + +from PIL import Image pytest_plugins = ["Tests.helper"] + + +def calculate_coverage(test_name): + branch = Image.branches + branches = Image.branches + num_branches = len(branches) + branch_covered = {key: value for key, value in branches.items() if value is True} + sum_branches = len(branch_covered) + coverage = (sum_branches/num_branches) * 100 + print(f"\nBranches covered: {sum_branches}") + print(f"\nTotal branches: {num_branches}") + print("\nBRANCH COVERAGE:", coverage, "%\n") + return coverage + + + +@pytest.hookimpl(tryfirst=True) +def pytest_runtest_protocol(item, nextitem): + global test_name + + last_arg = sys.argv[-1] + + test_name = last_arg.split('/')[-1].split('::')[-1] + + test_name = test_name.rstrip('.py') + + return None + +@pytest.hookimpl(tryfirst=True) +def pytest_sessionfinish(session, exitstatus): + global test_name + + coverage = calculate_coverage(test_name) + print("\nBRANCH COVERAGE for", test_name, ":", coverage, "%\n") + + \ No newline at end of file diff --git a/coveragetool.py b/coveragetool.py deleted file mode 100644 index 60b74e26e..000000000 --- a/coveragetool.py +++ /dev/null @@ -1,22 +0,0 @@ -from PIL import Image -from PIL.Image import branches - -# Define a function to calculate and print branch coverage - - -def calculate_branch_coverage(): - num_branches = len(branches) - branch_covered = {key: value for key, value in branches.items() if value is True} - sum_branches = len(branch_covered) - coverage = (sum_branches/num_branches) * 100 - print(f"Branches covered: {sum_branches}") - print(f"Total branches: {num_branches}") - print("\nBRANCH COVERAGE:", coverage, "%\n") - -R = Image.new('L', (100, 100), color=255) -G = Image.new('L', (100, 100), color=128) -B = Image.new('L', (100, 100), color=0) -merged_image = Image.merge('RGB', (R, G, B)) -merged_image.save('merged_image.png') - -calculate_branch_coverage() diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 947deeb78..315b65851 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -75,14 +75,6 @@ class DecompressionBombWarning(RuntimeWarning): class DecompressionBombError(Exception): pass -branches = { - "1": False, - "2": False, - "3": False, - "4": False, - "5": False, -} - # Limit to around a quarter gigabyte for a 24-bit (3 bpp) image MAX_IMAGE_PIXELS: int | None = int(1024 * 1024 * 1024 // 4 // 3) @@ -545,6 +537,14 @@ class Image: self.pyaccess = None self._exif = None + branches = { + "1": False, + "2": False, + "3": False, + "4": False, + "5": False, + } + @property def width(self) -> int: return self.size[0] @@ -594,6 +594,7 @@ class Image: self._close_fp() self.fp = None + def close(self) -> None: """ Closes the file pointer, if possible.