Skip to content

Commit

Permalink
Fix some type errors in LanguageProcessors (#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
Swatinem committed Sep 13, 2024
1 parent 7ebdf9f commit e867451
Show file tree
Hide file tree
Showing 18 changed files with 275 additions and 281 deletions.
39 changes: 16 additions & 23 deletions services/report/languages/cobertura.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@

class CoberturaProcessor(BaseLanguageProcessor):
def matches_content(self, content: Element, first_line: str, name: str) -> bool:
return bool(
next(content.iter("coverage"), None)
or next(content.iter("scoverage"), None)
)
return content.tag in ("coverage", "scoverage")

@sentry_sdk.trace
def process(
Expand All @@ -45,14 +42,7 @@ def from_xml(xml: Element, report_builder_session: ReportBuilderSession) -> None
("codecov", "max_report_age"), "12h ago"
):
try:
timestamp = next(xml.iter("coverage")).get("timestamp")
except StopIteration:
try:
timestamp = next(xml.iter("scoverage")).get("timestamp")
except StopIteration:
timestamp = None

try:
timestamp = xml.get("timestamp")
parsed_datetime = Date(timestamp)
is_valid_timestamp = True
except TimestringInvalid:
Expand All @@ -75,15 +65,18 @@ def from_xml(xml: Element, report_builder_session: ReportBuilderSession) -> None
for _class in xml.iter("class"):
filename = _class.attrib["filename"]
_file = report_builder_session.create_coverage_file(filename, do_fix_path=False)
assert (
_file is not None
), "`create_coverage_file` with pre-fixed path is infallible"

for line in _class.iter("line"):
_line = line.attrib
ln = _line["number"]
ln: str | int = _line["number"]
if ln == "undefined":
continue
ln = int(ln)
if ln > 0:
coverage = None
coverage: str | int
_type = CoverageType.line
missing_branches = None

Expand All @@ -100,9 +93,9 @@ def from_xml(xml: Element, report_builder_session: ReportBuilderSession) -> None
coverage = Int(_line.get("hits"))

# [python] [scoverage] [groovy] Conditions
conditions = _line.get("missing-branches", None)
if conditions:
conditions = conditions.split(",")
conditions_text = _line.get("missing-branches", None)
if conditions_text:
conditions = conditions_text.split(",")
if len(conditions) > 1 and set(conditions) == set(("exit",)):
# python: "return [...] missed"
conditions = ["loop", "exit"]
Expand Down Expand Up @@ -179,15 +172,15 @@ def from_xml(xml: Element, report_builder_session: ReportBuilderSession) -> None
# [scala] [scoverage]
for stmt in _class.iter("statement"):
# scoverage will have repeated data
stmt = stmt.attrib
if stmt.get("ignored") == "true":
attr = stmt.attrib
if attr.get("ignored") == "true":
continue
coverage = Int(stmt["invocation-count"])
line_no = int(stmt["line"])
coverage = Int(attr["invocation-count"])
line_no = int(attr["line"])
coverage_type = CoverageType.line
if stmt["branch"] == "true":
if attr["branch"] == "true":
coverage_type = CoverageType.branch
elif stmt["method"]:
elif attr["method"]:
coverage_type = CoverageType.method

_file.append(
Expand Down
19 changes: 17 additions & 2 deletions services/report/languages/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
def remove_non_ascii(string, replace_with=""):
from xml.etree.ElementTree import Element


def remove_non_ascii(string: str) -> str:
# ASCII control characters <=31, 127
# Extended ASCII characters: >=128
return "".join([i if 31 < ord(i) < 127 else replace_with for i in string])
return "".join([c if 31 < ord(c) < 127 else "" for c in string])


def child_text(parent: Element, element: str) -> str:
"""
Returns the text content of the first element of type `element` of `parent`.
This defaults to the empty string if no child is found, or the child does not have any text.
"""
child = parent.find(element)
if child is None:
return ""
return child.text or ""
22 changes: 14 additions & 8 deletions services/report/languages/jacoco.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def try_to_fix_path(path: str) -> str | None:
for package in xml.iter("package"):
base_name = package.attrib["name"]

file_method_complixity = defaultdict(dict)
file_method_complixity: dict[str, dict[int, tuple[int, int]]] = defaultdict(
dict
)
# Classes complexity
for _class in package.iter("class"):
class_name = _class.attrib["name"]
Expand Down Expand Up @@ -99,19 +101,23 @@ def try_to_fix_path(path: str) -> str | None:
_file = report_builder_session.create_coverage_file(
filename, do_fix_path=False
)
assert (
_file is not None
), "`create_coverage_file` with pre-fixed path is infallible"

for line in source.iter("line"):
line = line.attrib
if line["mb"] != "0":
cov = "%s/%s" % (line["cb"], int(line["mb"]) + int(line["cb"]))
attr = line.attrib
cov: int | str
if attr["mb"] != "0":
cov = "%s/%s" % (attr["cb"], int(attr["mb"]) + int(attr["cb"]))
coverage_type = CoverageType.branch

elif line["cb"] != "0":
cov = "%s/%s" % (line["cb"], line["cb"])
elif attr["cb"] != "0":
cov = "%s/%s" % (attr["cb"], attr["cb"])
coverage_type = CoverageType.branch

else:
cov = int(line["ci"])
cov = int(attr["ci"])
coverage_type = CoverageType.line

if (
Expand All @@ -121,7 +127,7 @@ def try_to_fix_path(path: str) -> str | None:
):
cov = 1

ln = int(line["nr"])
ln = int(attr["nr"])
if ln > 0:
complexity = method_complixity.get(ln)
if complexity:
Expand Down
34 changes: 18 additions & 16 deletions services/report/languages/lcov.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ def from_txt(reports: bytes, report_builder_session: ReportBuilderSession) -> No

def _process_file(
doc: bytes, report_builder_session: ReportBuilderSession
) -> ReportFile:
) -> ReportFile | None:
_already_informed_of_negative_execution_count = False
lines = {}
branches = defaultdict(dict)
branches: dict[str, dict[str, int]] = defaultdict(dict)
fln, fh = {}, {}
JS = False
CPP = False
skip_lines = []
skip_lines: list[str] = []
_file = None

for encoded_line in BytesIO(doc):
Expand All @@ -60,7 +59,7 @@ def _process_file(
# BRH: branches hit
continue

elif method == "SF":
if method == "SF":
"""
For each source file referenced in the .da file, there is a section
containing filename and coverage data:
Expand All @@ -69,13 +68,14 @@ def _process_file(
"""
# file name
_file = report_builder_session.create_coverage_file(content)
if _file is None:
return None

JS = content[-3:] == ".js"
CPP = content[-4:] == ".cpp"
continue

if _file is None:
return None

elif method == "DA":
if method == "DA":
"""
Then there is a list of execution counts for each instrumented line
(i.e. a line which resulted in executable code):
Expand All @@ -86,9 +86,9 @@ def _process_file(
if line.startswith("undefined,"):
continue

splited_content = content.split(",")
line = splited_content[0]
hit = splited_content[1]
splitted_content = content.split(",")
line = splitted_content[0]
hit = splitted_content[1]
if line[0] in ("0", "n") or hit[0] in ("=", "s"):
continue

Expand Down Expand Up @@ -163,24 +163,26 @@ def _process_file(
0 if taken in ("-", "0") else 1
)

if _file is None:
return None

# remove skipped
for sl in skip_lines:
branches.pop(sl, None)
lines.pop(sl, None)

methods = fln.values()

# work branches
for ln, br in branches.items():
s, li = sum(br.values()), len(br.values())
mb = [bid for bid, cov in br.items() if cov == 0]
cov = "%s/%s" % (s, li)

coverage = f"{s}/{li}"
coverage_type = CoverageType.method if ln in methods else CoverageType.branch

_file.append(
int(ln),
report_builder_session.create_coverage_line(
cov,
coverage,
coverage_type,
missing_branches=(mb if mb != [] else None),
),
Expand Down
14 changes: 7 additions & 7 deletions services/report/languages/lua.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ def process(
docs = re.compile(r"^=+\n", re.M).split


def from_txt(string: bytes, report_builder_session: ReportBuilderSession) -> None:
def from_txt(input: bytes, report_builder_session: ReportBuilderSession) -> None:
_file = None
for string in docs(string.decode(errors="replace").replace("\t", " ")):
string = string.rstrip()
if string == "Summary":
for line in docs(input.decode(errors="replace").replace("\t", " ")):
line = line.rstrip()
if line == "Summary":
_file = None

elif string.endswith((".lua", ".lisp")):
_file = report_builder_session.create_coverage_file(string)
elif line.endswith((".lua", ".lisp")):
_file = report_builder_session.create_coverage_file(line)

elif _file is not None:
for ln, source in enumerate(string.splitlines(), start=1):
for ln, source in enumerate(line.splitlines(), start=1):
try:
cov = source.strip().split(" ")[0]
cov = 0 if cov[-2:] in ("*0", "0") else int(cov)
Expand Down
7 changes: 4 additions & 3 deletions services/report/languages/mono.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ def from_xml(xml: Element, report_builder_session: ReportBuilderSession) -> None
if filename not in files:
_file = report_builder_session.create_coverage_file(filename)
files[filename] = _file

_file = files[filename]
if _file is None:
continue

# loop through statements
for line in method.iter("statement"):
line = line.attrib
coverage = int(line["counter"])
attr = line.attrib
coverage = int(attr["counter"])

_file.append(
int(line["line"]),
int(attr["line"]),
report_builder_session.create_coverage_line(
coverage,
),
Expand Down
Loading

0 comments on commit e867451

Please sign in to comment.