Keep a Rust test in a file of its own as planted in a task-bench fix

This commit is contained in:
2026-11-27 16:05:56 +01:00
parent 4a4a8526af
commit 86edf81d28
5 changed files with 195 additions and 23 deletions
+51 -13
View File
@@ -1,5 +1,7 @@
"""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."""
"""The `#[cfg(test)]` modules of a Rust file, which stand in the file of the code they test or, as `mod name;` declares, in a file of
their own. Here are where each inline one is, which files hold the others, 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 pathlib
import re
# What the scan steps over or stops at. A string, a char and a comment may hold a brace.
@@ -17,6 +19,10 @@ STRING_BODY = re.compile(rb'(?:[^"\\]|\\.)*"', re.DOTALL)
COMMENT_EDGE = re.compile(rb"/\*|\*/")
# 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*\{")
# What follows the attribute that starts a module kept in a file of its own: any other attributes, an optional visibility, then `mod name;`.
DECLARATION = re.compile(rb"\s*(?P<attributes>(?:#\[[^\]]*\]\s*)*)(?:pub(?:\([^)]*\))?\s+)?mod\s+(?P<name>\w+)\s*;")
PATH_ATTRIBUTE = re.compile(rb"#\[\s*path\b")
KEEP_BESIDE = ("lib.rs", "main.rs", "mod.rs")
def tells_apart(path: str, source: bytes) -> bool:
@@ -32,16 +38,13 @@ def modules(source: bytes) -> dict[bytes, tuple[int, int]] | None:
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 == b"/*":
position = after_comment(source, position)
elif token == b'"':
position = after_string(source, position)
elif match.group("hashes") is not None:
position = after_raw_string(source, position, match.group("hashes"))
elif token == b"{":
position = 0
while match := SCAN.search(source, position):
token = match.group()
if (after := after_token(source, match)) is None:
return None
position = after
if token == b"{":
depth += 1
elif token == b"}":
depth -= 1
@@ -52,7 +55,42 @@ def modules(source: bytes) -> dict[bytes, tuple[int, int]] | 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
return found if position is not None and depth == 0 and inside is None else None
return found if depth == 0 and inside is None else None
def declared(source: bytes) -> list[str] | None:
"""The name of each `#[cfg(test)] mod name;` of source, a test module kept in a file of its own, in the order they stand.
None where a `#[path]` attribute moves such a file, or a string or comment is never closed."""
names = []
position = 0
while match := SCAN.search(source, position):
if (after := after_token(source, match)) is None:
return None
position = after
if match.group() == b"#[cfg(test)]" and (declaration := DECLARATION.match(source, position)):
if PATH_ATTRIBUTE.search(declaration.group("attributes")):
return None
names.append(declaration.group("name").decode())
return names
def homes(path: str, name: str) -> list[str]:
"""Where the file of the module `name`, declared in the Rust file at path, may stand, by its path from the same top. A crate root
(`lib.rs`, `main.rs`) and a `mod.rs` keep their modules beside them; any other file keeps them in a folder of its own name."""
declaring = pathlib.PurePosixPath(path)
folder = declaring.parent if declaring.name in KEEP_BESIDE else declaring.parent / declaring.stem
return [str(folder / f"{name}.rs"), str(folder / name / "mod.rs")]
def after_token(source: bytes, match: re.Match[bytes]) -> int | None:
"""The offset after match, or after the comment, string or raw string that match opens; None where that is never closed."""
if match.group() == b"/*":
return after_comment(source, match.end())
if match.group() == b'"':
return after_string(source, match.end())
if match.group("hashes") is not None:
return after_raw_string(source, match.end(), match.group("hashes"))
return match.end()
def after_comment(source: bytes, position: int) -> int | None: