conftest coverage tool fix and pdfparser function coverage

This commit is contained in:
dutcu 2024-06-19 21:22:31 +02:00
parent d023c02904
commit 27c7ac4dad
3 changed files with 37 additions and 18 deletions

View File

@ -3,22 +3,30 @@ import pytest
import sys import sys
from PIL import Image from PIL import Image
from PIL import PdfParser
pytest_plugins = ["Tests.helper"] pytest_plugins = ["Tests.helper"]
def calculate_coverage(test_name): def calculate_coverage(test_name):
branch = Image.branches all_branches = {
branches = Image.branches "branches1": Image.branches,
"branches2": PdfParser.XrefTable.branches,
# Add more
}
for name, branches in all_branches.items():
num_branches = len(branches) num_branches = len(branches)
branch_covered = {key: value for key, value in branches.items() if value is True} branch_covered = {key: value for key, value in branches.items() if value is True}
sum_branches = len(branch_covered) sum_branches = len(branch_covered)
coverage = (sum_branches/num_branches) * 100 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
print(f"\n{name} - Branches covered: {sum_branches}")
print(f"{name} - Total branches: {num_branches}")
print(f"{name} - BRANCH COVERAGE: {coverage}%\n")
return all_branches["branches1"]
@pytest.hookimpl(tryfirst=True) @pytest.hookimpl(tryfirst=True)

View File

@ -67,6 +67,13 @@ except ImportError:
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
branches = {
"1": False,
"2": False,
"3": False,
"4": False,
"5": False,
}
class DecompressionBombWarning(RuntimeWarning): class DecompressionBombWarning(RuntimeWarning):
pass pass
@ -537,14 +544,6 @@ class Image:
self.pyaccess = None self.pyaccess = None
self._exif = None self._exif = None
branches = {
"1": False,
"2": False,
"3": False,
"4": False,
"5": False,
}
@property @property
def width(self) -> int: def width(self) -> int:
return self.size[0] return self.size[0]

View File

@ -112,6 +112,14 @@ class IndirectObjectDef(IndirectReference):
class XrefTable: class XrefTable:
branches = {
"1": False,
"2": False,
"3": False,
"4": False,
}
def __init__(self): def __init__(self):
self.existing_entries = {} # object ID => (offset, generation) self.existing_entries = {} # object ID => (offset, generation)
self.new_entries = {} # object ID => (offset, generation) self.new_entries = {} # object ID => (offset, generation)
@ -134,15 +142,19 @@ class XrefTable:
def __delitem__(self, key): def __delitem__(self, key):
if key in self.new_entries: if key in self.new_entries:
XrefTable.branches["1"] = True
generation = self.new_entries[key][1] + 1 generation = self.new_entries[key][1] + 1
del self.new_entries[key] del self.new_entries[key]
self.deleted_entries[key] = generation self.deleted_entries[key] = generation
elif key in self.existing_entries: elif key in self.existing_entries:
XrefTable.branches["2"] = True
generation = self.existing_entries[key][1] + 1 generation = self.existing_entries[key][1] + 1
self.deleted_entries[key] = generation self.deleted_entries[key] = generation
elif key in self.deleted_entries: elif key in self.deleted_entries:
XrefTable.branches["3"] = True
generation = self.deleted_entries[key] generation = self.deleted_entries[key]
else: else:
XrefTable.branches["4"] = True
msg = f"object ID {key} cannot be deleted because it doesn't exist" msg = f"object ID {key} cannot be deleted because it doesn't exist"
raise IndexError(msg) raise IndexError(msg)