87 lines
4.1 KiB
Python
87 lines
4.1 KiB
Python
"""Which files of a tree are its tests, and the tree the tests check a fix in: the planted tests over the code as fixed."""
|
|
import contextlib
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import typing
|
|
|
|
from task_bench.rust_test_modules import declared, homes, with_tests_of
|
|
from mutate_changes.changed_lines import is_test
|
|
|
|
|
|
def checked_tree(planted: str, copy: str, into: str, rust_tests_planted: bool = True) -> None:
|
|
"""Makes at `into` the tree the tests check a fix in: `planted`, the code with the bug as Haiku got it, with each file that is no
|
|
test as `copy` holds it after the fix. So a test Haiku changed, removed or added doesn't count, and neither does a file under
|
|
a `testdata` folder, where Go's tests keep the output they expect. Where rust_tests_planted, a Rust file's inline `#[cfg(test)]`
|
|
modules stand as the planted file has them too, as `with_tests_of` says, and so does each file that `rust_test_files` finds in
|
|
the planted tree.
|
|
|
|
Raises OSError where a file cannot be copied."""
|
|
shutil.copytree(planted, into, symlinks=True)
|
|
# `rust_test_files` is None for a tree with a declaration whose file it can't find, and the bench plants no bug in such a tree.
|
|
rust_tests = (rust_test_files(planted) or set()) if rust_tests_planted else set()
|
|
before, after = set(entries(planted)), set(entries(copy))
|
|
for path in before - after:
|
|
if not of_tests(path, rust_tests):
|
|
with contextlib.suppress(FileNotFoundError):
|
|
os.unlink(os.path.join(into, path))
|
|
for path in sorted(after):
|
|
target = os.path.join(into, path)
|
|
# A link is copied as a link, never followed; and one that would stand in for a folder of the planted tree is left out.
|
|
if of_tests(path, rust_tests) or (os.path.isdir(target) and not os.path.islink(target)):
|
|
continue
|
|
os.makedirs(os.path.dirname(target), exist_ok=True)
|
|
with contextlib.suppress(FileNotFoundError):
|
|
os.unlink(target)
|
|
shutil.copy2(os.path.join(copy, path), target, follow_symlinks=False)
|
|
if rust_tests_planted and path.endswith(".rs"):
|
|
keep_rust_tests(os.path.join(planted, path), target)
|
|
|
|
|
|
def keep_rust_tests(planted_file: str, target: str) -> None:
|
|
"""Puts into the Rust file at target, a copy of Haiku's, the test modules of planted_file, as `with_tests_of` says. A planted tree
|
|
without the file has no test module, so a file Haiku added keeps none. A link, planted's or Haiku's, is left as it is.
|
|
|
|
Raises OSError where a file cannot be read or written."""
|
|
if os.path.islink(planted_file) or os.path.islink(target):
|
|
return
|
|
planted = pathlib.Path(planted_file).read_bytes() if os.path.isfile(planted_file) else b""
|
|
pathlib.Path(target).write_bytes(with_tests_of(planted, pathlib.Path(target).read_bytes()))
|
|
|
|
|
|
def of_tests(path: str, rust_tests: typing.AbstractSet[str] = frozenset()) -> bool:
|
|
"""Whether path is a test by its path alone, or one of rust_tests."""
|
|
return is_test(path) or "testdata" in pathlib.PurePath(path).parts or path in rust_tests
|
|
|
|
|
|
def rust_test_files(tree: str) -> set[str] | None:
|
|
"""The Rust files under tree that a `#[cfg(test)] mod name;` declaration names, by their paths from tree. None where a declaration
|
|
names no file there, or a string or comment of a Rust file is never closed: the tree may hold a test file this can't find.
|
|
|
|
Raises OSError where a file cannot be read."""
|
|
files = set(entries(tree))
|
|
found = set()
|
|
for path in sorted(files):
|
|
if not path.endswith(".rs") or os.path.islink(os.path.join(tree, path)):
|
|
continue
|
|
declarations = declared(pathlib.Path(tree, path).read_bytes())
|
|
if declarations is None:
|
|
return None
|
|
for name, _ in declarations:
|
|
standing = files.intersection(homes(path, name))
|
|
if not standing:
|
|
return None
|
|
found |= standing
|
|
return found
|
|
|
|
|
|
def entries(top: str) -> list[str]:
|
|
"""Each file and link under top, by its path from top, without git's own under `.git`."""
|
|
found = []
|
|
for folder, folders, names in os.walk(top):
|
|
if folder == top and ".git" in folders:
|
|
folders.remove(".git")
|
|
links = [name for name in folders if os.path.islink(os.path.join(folder, name))]
|
|
found += [os.path.relpath(os.path.join(folder, name), top) for name in (*names, *links)]
|
|
return found
|