185 lines
8.9 KiB
Python
185 lines
8.9 KiB
Python
"""The features task-bench has Haiku build: each a folder in the project's `bench-tasks` folder, with a spec, a reference patch
|
|
that builds it, and acceptance tests Haiku never sees. A feature counts at the two commits of a bench only where those tests fail
|
|
at both before it is built, and pass at both with the reference patch.
|
|
|
|
A data source Haiku adds is such a folder too, with a `generate-register` beside the three parts: the script that writes the
|
|
register Haiku imports, as task_bench.features.registers runs it. It counts as a feature does, in trees that hold its register."""
|
|
import itertools
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import typing
|
|
|
|
from task_bench.tasks import Bench, check_made, committed, DATA_SOURCE, FEATURE, Kind, on_disk, Planned, Side, SIDES, Task
|
|
from project_config.settings import BENCH_TASKS, CONFIG
|
|
from failure import Failure
|
|
from task_bench.features.registers import GENERATE, NotGenerated, registered_trees
|
|
from project_config.main import located
|
|
from mutate_changes.tool_process import Ran
|
|
from task_bench.runs import ran_tests, TimedOut
|
|
|
|
SPEC = "spec.md"
|
|
PATCH = "reference.patch"
|
|
ACCEPTANCE = "acceptance"
|
|
|
|
|
|
class Feature(typing.NamedTuple):
|
|
"""A feature as its folder holds it: the folder's name, the spec Haiku is told, the reference patch's file, and the folder
|
|
of the acceptance tests, each at the path it takes from the tree's top. register is the script that generates a data
|
|
source's register, and None in a feature."""
|
|
|
|
name: str
|
|
spec: str
|
|
patch: str
|
|
acceptance: str
|
|
register: str | None = None
|
|
|
|
|
|
class NotApplied(Failure):
|
|
"""The reference patch doesn't apply to the tree."""
|
|
|
|
|
|
class Features(typing.NamedTuple):
|
|
"""A project's features, found in the folder that keeps them."""
|
|
|
|
folder: pathlib.Path
|
|
found: list[Feature]
|
|
|
|
|
|
def read_features(home: pathlib.Path, kind: Kind) -> Features | Failure:
|
|
"""The features of the current directory's project, or its data sources where kind is DATA_SOURCE, read from the folder its
|
|
config names."""
|
|
folder = features_folder(home)
|
|
if isinstance(folder, Failure):
|
|
return folder
|
|
found = features(folder, kind)
|
|
return found if isinstance(found, Failure) else Features(folder, found)
|
|
|
|
|
|
def features_folder(home: pathlib.Path) -> pathlib.Path | Failure:
|
|
"""The folder the config of the current directory's project names as `bench-tasks`."""
|
|
config = located(None, home)
|
|
if isinstance(config, Failure):
|
|
return config
|
|
return config.paths.get(BENCH_TASKS) or Failure(f"{config.folder / CONFIG} sets no `{BENCH_TASKS}`, the folder of the tasks to bench, as `project-config --help` describes it")
|
|
|
|
|
|
def features(folder: pathlib.Path, kind: Kind = FEATURE) -> list[Feature] | Failure:
|
|
"""The tasks of kind in folder, in the order of their names. A folder there with a `generate-register` holds a data source,
|
|
and any other holds a feature. A Failure where folder holds no such task, or one has a part at fault."""
|
|
if not folder.is_dir():
|
|
return Failure(f"could not read the {kind.noun}s in {folder}: it is no folder")
|
|
try:
|
|
found = [feature_in(inside, kind.noun) for inside in sorted(folder.iterdir()) if inside.is_dir() and (inside / GENERATE).exists() == (kind is DATA_SOURCE)]
|
|
except (OSError, UnicodeDecodeError) as failure:
|
|
return Failure(f"could not read the {kind.noun}s in {folder}: {failure}")
|
|
if not found:
|
|
parts = f"{SPEC}, {PATCH}, {GENERATE}" if kind is DATA_SOURCE else f"{SPEC}, {PATCH}"
|
|
return Failure(f"{folder} holds no {kind.noun}: a folder for each, with {parts} and the acceptance tests under {ACCEPTANCE}")
|
|
return next((feature for feature in found if isinstance(feature, Failure)), None) or [feature for feature in found if not isinstance(feature, Failure)]
|
|
|
|
|
|
def feature_in(folder: pathlib.Path, noun: str) -> Feature | Failure:
|
|
"""The task that folder holds, which a failure calls by noun. Raises OSError or UnicodeDecodeError where a part cannot be read."""
|
|
spec, patch, acceptance, register = folder / SPEC, folder / PATCH, folder / ACCEPTANCE, folder / GENERATE
|
|
for part in (spec, patch):
|
|
if not part.is_file():
|
|
return Failure(f"the {noun} in {folder} lacks {part.name}")
|
|
told = spec.read_text(encoding="utf-8").strip()
|
|
if not told:
|
|
return Failure(f"the {noun} in {folder} has an empty {SPEC}")
|
|
if not acceptance.is_dir() or not any(inside.is_file() for inside in acceptance.rglob("*")):
|
|
return Failure(f"the {noun} in {folder} holds no file under {ACCEPTANCE}")
|
|
if not register.exists():
|
|
return Feature(folder.name, told, str(patch), str(acceptance))
|
|
if not register.is_file() or not os.access(register, os.X_OK):
|
|
return Failure(f"the {noun} in {folder} has a {GENERATE} that is no executable file")
|
|
return Feature(folder.name, told, str(patch), str(acceptance), str(register))
|
|
|
|
|
|
def feature_tasks(bench: Bench, found: list[Feature], folder: pathlib.Path) -> Planned | Failure:
|
|
"""The task of building each feature of found that counts at the bench's commits, numbered from 1, and a line on each that
|
|
doesn't. A Failure where none counts. folder is where the features are kept."""
|
|
shared = committed_trees(bench)
|
|
if isinstance(shared, Failure):
|
|
return shared
|
|
noun = bench.asked.kind.noun
|
|
tasks: list[Task] = []
|
|
passed_over: list[tuple[str, str]] = []
|
|
for feature in found:
|
|
trees = counted_trees(bench, shared, feature)
|
|
if isinstance(trees, Failure):
|
|
return trees
|
|
if not isinstance(trees, str):
|
|
tasks.append(Task(len(tasks) + 1, feature.name, trees, dict.fromkeys(SIDES, feature.spec), acceptance=feature.acceptance))
|
|
else:
|
|
passed_over.append((feature.name, trees))
|
|
bench.say(f"task-bench: passed over the {noun} {feature.name}: {trees}.")
|
|
if not tasks:
|
|
return Failure(f"None of the {len(found)} {noun}s in {folder} counts. {' '.join(f'{name}: {why}.' for name, why in passed_over)}")
|
|
return Planned(tasks, [f"Passed over the {noun} {name}: {why}." for name, why in passed_over], {})
|
|
|
|
|
|
def counted_trees(bench: Bench, shared: dict[Side, str], feature: Feature) -> dict[Side, str] | str | Failure:
|
|
"""Each side's tree that feature's task starts in, or why the feature doesn't count. A feature starts in shared, and a data
|
|
source in trees of its own, which hold its register. A Failure where a tree could not be made or the tests could not run."""
|
|
trees = shared if feature.register is None else registered_trees(bench, feature.name, feature.register)
|
|
if isinstance(trees, NotGenerated):
|
|
return trees.reason
|
|
if isinstance(trees, Failure):
|
|
return trees
|
|
return why_passed_over(bench, trees, feature) or trees
|
|
|
|
|
|
def committed_trees(bench: Bench) -> dict[Side, str] | Failure:
|
|
"""A copy of each side's tree, committed once. Every feature starts from it, since a feature doesn't add anything to the tree Haiku
|
|
works in."""
|
|
trees: dict[Side, str] = {}
|
|
for side in SIDES:
|
|
tree = bench.folder / "features" / side
|
|
copied = on_disk(f"copy the {side}'s tree to {tree}", lambda: shutil.copytree(bench.trees[side], tree, symlinks=True))
|
|
if isinstance(copied, Failure):
|
|
return copied
|
|
if failure := committed(bench.git, str(tree)):
|
|
return failure
|
|
trees[side] = str(tree)
|
|
return trees
|
|
|
|
|
|
def why_passed_over(bench: Bench, trees: dict[Side, str], feature: Feature) -> str | Failure | None:
|
|
"""Why feature doesn't count at the first side where it doesn't, in the committed trees; None where it counts at both. A
|
|
Failure where the tests could not run."""
|
|
for side, is_built in itertools.product(SIDES, (False, True)):
|
|
ran = checked_run(bench, trees[side], feature, is_built)
|
|
if isinstance(ran, NotApplied):
|
|
return f"its reference patch doesn't apply at the {side}"
|
|
if isinstance(ran, TimedOut):
|
|
return f"the tests run out of time at the {side}"
|
|
if isinstance(ran, Failure):
|
|
return ran
|
|
if not is_built and ran.code == 0:
|
|
return f"its acceptance tests pass at the {side} before it is built"
|
|
if is_built and ran.code:
|
|
return f"the tests fail with its reference patch at the {side}"
|
|
return None
|
|
|
|
|
|
def checked_run(bench: Bench, tree: str, feature: Feature, is_built: bool) -> Ran | Failure:
|
|
"""The test command's run over the tree that checks a copy of tree, with the reference patch applied to the copy where
|
|
is_built. It is checked as a run of Haiku's is, so a patch that leans on a test or `testdata` file of its own fails, as a
|
|
build would. Both trees are removed afterwards."""
|
|
copy, check = bench.folder / "copy", bench.folder / "check"
|
|
try:
|
|
copied = on_disk(f"copy {tree} to check {feature.name} in it", lambda: shutil.copytree(tree, copy, symlinks=True))
|
|
if isinstance(copied, Failure):
|
|
return copied
|
|
# git has run in this bench already, so a failure here is the patch's.
|
|
applied = bench.git("apply", feature.patch, cwd=str(copy)) if is_built else None
|
|
if isinstance(applied, Failure):
|
|
return NotApplied(applied.reason)
|
|
made = on_disk(f"make the tree that checks {feature.name}", lambda: check_made(tree, str(copy), str(check), feature.acceptance))
|
|
return made or ran_tests(bench.containers, str(check))
|
|
finally:
|
|
shutil.rmtree(copy, ignore_errors=True)
|
|
shutil.rmtree(check, ignore_errors=True)
|