Answer stability round 1: 1 🔴, 1 🟡 (0 misread); a fix that drops a test file's declaration gets the planted file, and any file may stand beside or under its declarer
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""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."""
|
||||
their own. Here are where each inline one is, which modules a file declares and where their files may stand, 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
|
||||
|
||||
@@ -20,9 +20,7 @@ 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")
|
||||
DECLARATION = re.compile(rb"\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+(?P<name>\w+)\s*;")
|
||||
|
||||
|
||||
def tells_apart(path: str, source: bytes) -> bool:
|
||||
@@ -59,8 +57,8 @@ def modules(source: bytes) -> dict[bytes, tuple[int, int]] | 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."""
|
||||
"""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 string or comment is never closed."""
|
||||
names = []
|
||||
position = 0
|
||||
while match := SCAN.search(source, position):
|
||||
@@ -68,18 +66,16 @@ def declared(source: bytes) -> list[str] | 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."""
|
||||
"""Each place the file of the module `name`, declared in the Rust file at path, may stand, by its path from the same top. Rust keeps
|
||||
it in the folder of the declaring file, or in a folder of that file's name where it is a module file, `a.rs` for `a/`; which of the
|
||||
two the file is depends on what the crate's root is, so both count."""
|
||||
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")]
|
||||
return [str(folder / part) for folder in (declaring.parent, declaring.parent / declaring.stem) for part in (f"{name}.rs", f"{name}/mod.rs")]
|
||||
|
||||
|
||||
def after_token(source: bytes, match: re.Match[bytes]) -> int | None:
|
||||
@@ -121,7 +117,10 @@ def with_tests_of(planted: bytes, copy: bytes) -> bytes:
|
||||
|
||||
Where planted's modules cannot be told apart, or planted has none and copy's cannot be told apart, copy is returned as it is. Where
|
||||
planted has some and copy's cannot be told apart, or copy lacks one of planted's, planted is returned: Haiku's file has no module
|
||||
to put them in."""
|
||||
to put them in. The same holds for the `mod name;` declarations of planted, whose files stand as planted: a fix that drops one
|
||||
would leave its tests uncompiled."""
|
||||
if not set(declared(planted) or []) <= set(declared(copy) or []):
|
||||
return planted
|
||||
before, after = modules(planted), modules(copy)
|
||||
# Haiku's file stands where planted's modules can't be told apart. The bench never plants a bug in such a file, and a guess at
|
||||
# which module is which could swap one test for another. Haiku's file is also what the check took before the bench put back
|
||||
|
||||
Reference in New Issue
Block a user