Answer review: keep Rust test modules for bugs only, order imports, reword
CI / tests (pull_request) Has been cancelled
CI / tests (pull_request) Has been cancelled
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
"""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. A file is bytes, since a Rust file is UTF-8 and a module is found by ASCII text alone."""
|
||||
"""The `#[cfg(test)]` modules of a Rust file, which stand in the file of the code they test. Here are where each is, and a file with
|
||||
the modules of another file put in place of its own. A file is bytes: 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. A string, a char and a comment may hold a brace.
|
||||
@@ -15,19 +15,19 @@ SCAN = re.compile(
|
||||
)
|
||||
STRING_BODY = re.compile(rb'(?:[^"\\]|\\.)*"', re.DOTALL)
|
||||
COMMENT_EDGE = re.compile(rb"/\*|\*/")
|
||||
# What must follow the attribute for it to start an inline module: other attributes, a visibility, then `mod name {`.
|
||||
# What follows the attribute that starts an inline module: any other attributes, an optional visibility, then `mod name {`.
|
||||
MODULE = re.compile(rb"\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+(?P<name>\w+)\s*\{")
|
||||
|
||||
|
||||
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."""
|
||||
"""Whether source, the file at path, is no Rust file, or holds test modules that `modules` can tell 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 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."""
|
||||
"""Each inline `#[cfg(test)] mod name { ... }` of source by its name, with the offsets of its first byte, the attribute's, and of the
|
||||
byte after its closing brace. A module inside a test module and a `#[cfg(test)]` on anything but an inline module are not listed.
|
||||
None where the modules cannot be told apart: two share a name, or a brace, string or comment is never closed."""
|
||||
found: dict[bytes, tuple[int, int]] = {}
|
||||
depth = 0
|
||||
# The module being read: its name, the offset of its attribute and the depth outside it.
|
||||
@@ -71,14 +71,15 @@ def after_string(source: bytes, position: int) -> int | None:
|
||||
|
||||
|
||||
def after_raw_string(source: bytes, position: int, hashes: bytes) -> int | None:
|
||||
"""The offset after the raw string, opened by `r`, hashes and a quote, that ends at position, None where it is never closed. It has no escapes."""
|
||||
"""The offset after the raw string, `r` or `br` then hashes and a quote, whose opening ends at position, None where it is never closed.
|
||||
It has no escapes."""
|
||||
closed = re.compile(b'"' + hashes).search(source, position)
|
||||
return closed.end() if closed else None
|
||||
|
||||
|
||||
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.
|
||||
"""copy with each of its test modules replaced by the one of that name in planted, and each that planted lacks left out. So a test a
|
||||
fix changed, removed or added doesn't count.
|
||||
|
||||
Where planted's modules cannot be told apart, copy is returned as it is. Where copy's cannot, or copy lacks one of planted's,
|
||||
planted is returned: Haiku's file has no module to put them in."""
|
||||
|
||||
Reference in New Issue
Block a user