65 lines
2.6 KiB
Python
65 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Split an oversized Rust test file into topic modules (STRA-338): its `#[test]` fns move, helpers and imports stay.
|
|
|
|
`foo/tests.rs` (or `tests/foo.rs`, `foo_tests.rs`) keeps its `use` lines, fixtures and helper fns; the `#[test]` fns move,
|
|
in file order and in groups of about --target lines, into child modules named after the words their test names share
|
|
(`tests/projection_family.rs` ...), each starting with `use super::*;`. The test ids gain the module segment
|
|
(`tests::name` -> `tests::topic::name`); nothing else changes. The children are declared with explicit `#[path]`
|
|
attributes (a file that is not a mod.rs), which are right for a plain module, for a module that was itself loaded
|
|
through `#[path]`, and for an integration test crate root, where cargo would take a sibling for a test target.
|
|
|
|
Usage: scripts/dev/split_tests.py [--dry-run] [--target 300] FILE.rs...
|
|
"""
|
|
import argparse
|
|
import pathlib
|
|
import sys
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
|
|
|
|
from rustsplit.testsplit import plan_test_split # noqa: E402
|
|
|
|
|
|
def read(path):
|
|
with open(path, encoding="utf-8", newline="") as handle:
|
|
return handle.read().replace("\r\n", "\n")
|
|
|
|
|
|
def write(path, text):
|
|
with open(path, "w", encoding="utf-8", newline="\n") as handle:
|
|
handle.write(text)
|
|
|
|
|
|
def layout(path):
|
|
"""(child directory, explicit #[path] directory or None) for the children of `path`."""
|
|
if path.name in ("mod.rs", "lib.rs", "main.rs"):
|
|
return path.parent, None
|
|
directory = path.with_suffix("")
|
|
return directory, directory.name
|
|
|
|
|
|
def main(argv):
|
|
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
parser.add_argument("files", nargs="+", type=pathlib.Path)
|
|
parser.add_argument("--dry-run", action="store_true")
|
|
parser.add_argument("--target", type=int, default=300)
|
|
args = parser.parse_args(argv)
|
|
for path in args.files:
|
|
directory, explicit = layout(path)
|
|
existing = {p.stem for p in directory.glob("*.rs")} if directory.is_dir() else set()
|
|
plan = plan_test_split(read(path), args.target, path.stem, explicit, existing)
|
|
if plan is None:
|
|
print(f"skipped {path} nothing to move")
|
|
continue
|
|
parent, children = plan
|
|
print(f"split {path} -> {len(children)} modules: {', '.join(children)}")
|
|
if args.dry_run:
|
|
continue
|
|
directory.mkdir(parents=True, exist_ok=True)
|
|
for name, text in children.items():
|
|
write(directory / f"{name}.rs", text)
|
|
write(path, parent)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main(sys.argv[1:])
|