Tell a Rust file's test modules apart as bytes and kill the surviving mutants
CI / tests (pull_request) Failing after 5m15s
CI / tests (pull_request) Failing after 5m15s
This commit is contained in:
@@ -1,75 +1,79 @@
|
||||
"""The `#[cfg(test)]` modules of a Rust file, which stand in the file of the code they test: where each is, and a file with the modules
|
||||
of another file put in place of its own."""
|
||||
of another file put in place of its own. A file is bytes, since a Rust file is UTF-8 and a module is found by ASCII text alone."""
|
||||
import re
|
||||
|
||||
# What the scan steps over or stops at, in the order Rust's own lexer tries them. A string, a char and a comment may hold a brace.
|
||||
# What the scan steps over or stops at. A string, a char and a comment may hold a brace.
|
||||
SCAN = re.compile(
|
||||
r"""//[^\n]*"""
|
||||
r"""|/\*"""
|
||||
r"""|(?<![A-Za-z0-9_])b?r(?P<hashes>\#*)\""""
|
||||
r"""|\""""
|
||||
r"""|'(?:\\(?:u\{[0-9a-fA-F_]+\}|x[0-9a-fA-F]{2}|.)|[^\\'\n])'"""
|
||||
r"""|[{}]"""
|
||||
r"""|\#\[cfg\(test\)\]""",
|
||||
rb"""//[^\n]*"""
|
||||
rb"""|/\*"""
|
||||
rb"""|(?<![A-Za-z0-9_])b?r(?P<hashes>\#*)\""""
|
||||
rb"""|\""""
|
||||
rb"""|'(?:\\(?:u\{[0-9a-fA-F_]+\}|x[0-9a-fA-F]{2}|.)|[^\\'\n])'"""
|
||||
rb"""|[{}]"""
|
||||
rb"""|\#\[cfg\(test\)\]""",
|
||||
re.DOTALL,
|
||||
)
|
||||
COMMENT_EDGE = re.compile(r"/\*|\*/")
|
||||
COMMENT_EDGE = re.compile(rb"/\*|\*/")
|
||||
# What must follow the attribute for it to start an inline module: other attributes, a visibility, then `mod name {`.
|
||||
MODULE = re.compile(r"\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+(?P<name>\w+)\s*\{")
|
||||
MODULE = re.compile(rb"\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+(?P<name>\w+)\s*\{")
|
||||
|
||||
|
||||
def modules(source: str) -> dict[str, tuple[int, int]] | None:
|
||||
def tells_apart(path: str, source: bytes) -> bool:
|
||||
"""Whether source, the file at path, is no Rust file or has test modules that `modules` tells apart."""
|
||||
return not path.endswith(".rs") or modules(source) is not None
|
||||
|
||||
|
||||
def modules(source: bytes) -> dict[bytes, tuple[int, int]] | None:
|
||||
"""Each inline `#[cfg(test)] mod name { ... }` of source by its name, as the offsets from its attribute to its closing brace. A
|
||||
module inside another is not listed. None where the modules cannot be told apart: two hold one name, or a brace, string or
|
||||
module inside a test module is not listed. None where the modules cannot be told apart: two hold one name, or a brace, string or
|
||||
comment is never closed. A `#[cfg(test)]` on anything but an inline module is not listed."""
|
||||
found: dict[str, tuple[int, int]] = {}
|
||||
depth, open_at, start, name = 0, 0, -1, ""
|
||||
position = 0
|
||||
while match := SCAN.search(source, position):
|
||||
found: dict[bytes, tuple[int, int]] = {}
|
||||
depth = 0
|
||||
# The module being read: its name, the offset of its attribute and the depth outside it.
|
||||
inside: tuple[bytes, int, int] | None = None
|
||||
position: int | None = 0
|
||||
while position is not None and (match := SCAN.search(source, position)):
|
||||
token, position = match.group(), match.end()
|
||||
if token == "/*":
|
||||
if token == b"/*":
|
||||
position = after_comment(source, position)
|
||||
elif token == '"':
|
||||
position = after_string(source, position, '"')
|
||||
elif token == b'"':
|
||||
position = after_string(source, position, b'"')
|
||||
elif match.group("hashes") is not None:
|
||||
position = after_string(source, position, '"' + match.group("hashes"), raw=True)
|
||||
elif token == "{":
|
||||
position = after_string(source, position, b'"' + match.group("hashes"), raw=True)
|
||||
elif token == b"{":
|
||||
depth += 1
|
||||
elif token == "}":
|
||||
elif token == b"}":
|
||||
depth -= 1
|
||||
if start >= 0 and depth == open_at:
|
||||
if name in found:
|
||||
if inside and depth == inside[2]:
|
||||
if inside[0] in found:
|
||||
return None
|
||||
found[name], start = (start, position), -1
|
||||
elif start < 0 and (module := MODULE.match(source, position)):
|
||||
start, name, open_at = match.start(), module.group("name"), depth
|
||||
position = module.end()
|
||||
found[inside[0]], inside = (inside[1], position), None
|
||||
elif token.startswith(b"#[") and inside is None and (module := MODULE.match(source, position)):
|
||||
inside, position = (module.group("name"), match.start(), depth), module.end()
|
||||
depth += 1
|
||||
if position < 0:
|
||||
return None
|
||||
return found if depth == 0 and start < 0 else None
|
||||
return found if position is not None and depth == 0 and inside is None else None
|
||||
|
||||
|
||||
def after_comment(source: str, position: int) -> int:
|
||||
"""The offset after the block comment whose `/*` ends at position, -1 where it is never closed. Block comments nest in Rust."""
|
||||
def after_comment(source: bytes, position: int) -> int | None:
|
||||
"""The offset after the block comment whose `/*` ends at position, None where it is never closed. Block comments nest in Rust."""
|
||||
level = 1
|
||||
while level and (match := COMMENT_EDGE.search(source, position)):
|
||||
level += 1 if match.group() == "/*" else -1
|
||||
level += 1 if match.group() == b"/*" else -1
|
||||
position = match.end()
|
||||
return position if level == 0 else -1
|
||||
return position if level == 0 else None
|
||||
|
||||
|
||||
def after_string(source: str, position: int, closing: str, raw: bool = False) -> int:
|
||||
"""The offset after the string whose opening quote ends at position, -1 where it is never closed. A raw string has no escapes."""
|
||||
def after_string(source: bytes, position: int, closing: bytes, raw: bool = False) -> int | None:
|
||||
"""The offset after the string whose opening quote ends at position, None where it is never closed. A raw string has no escapes."""
|
||||
while (found := source.find(closing, position)) >= 0:
|
||||
backslashes = len(source[position:found]) - len(source[position:found].rstrip("\\"))
|
||||
if raw or backslashes % 2 == 0:
|
||||
between = source[position:found]
|
||||
if raw or (len(between) - len(between.rstrip(b"\\"))) % 2 == 0:
|
||||
return found + len(closing)
|
||||
position = found + 1
|
||||
return -1
|
||||
return None
|
||||
|
||||
|
||||
def with_tests_of(planted: str, copy: str) -> str:
|
||||
def with_tests_of(planted: bytes, copy: bytes) -> bytes:
|
||||
"""copy with each test module as planted holds it, and each test module planted lacks left out. So a test a fix changed, removed
|
||||
or added doesn't count.
|
||||
|
||||
@@ -80,8 +84,8 @@ def with_tests_of(planted: str, copy: str) -> str:
|
||||
return copy
|
||||
if after is None or before.keys() - after.keys():
|
||||
return planted
|
||||
text = copy
|
||||
for name, (start, end) in sorted(after.items(), key=lambda item: -item[1][0]):
|
||||
was = planted[before[name][0]:before[name][1]] if name in before else ""
|
||||
text = text[:start] + was + text[end:]
|
||||
return text
|
||||
text, last = b"", 0
|
||||
for (start, end), name in sorted((span, name) for name, span in after.items()):
|
||||
text += copy[last:start] + (planted[before[name][0]:before[name][1]] if name in before else b"")
|
||||
last = end
|
||||
return text + copy[last:]
|
||||
|
||||
Reference in New Issue
Block a user