Bench features in task-bench with --tasks features, from the folder the config names as bench-tasks
CI / tests (pull_request) Has been cancelled
CI / tests (pull_request) Has been cancelled
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
"""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."""
|
||||
import pathlib
|
||||
import shutil
|
||||
import typing
|
||||
|
||||
from failure import Failure
|
||||
from project_config.main import located
|
||||
from project_config.settings import BENCH_TASKS, CONFIG
|
||||
from task_bench.figures import Side, SIDES
|
||||
from task_bench.runs import TimedOut
|
||||
from task_bench.tasks import Bench, committed, on_disk, Planned, Task, tests_in_copy, with_acceptance
|
||||
|
||||
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."""
|
||||
|
||||
name: str
|
||||
spec: str
|
||||
patch: str
|
||||
acceptance: str
|
||||
|
||||
|
||||
class NotApplied(Failure):
|
||||
"""The reference patch doesn't apply to the tree."""
|
||||
|
||||
|
||||
class Kept(typing.NamedTuple):
|
||||
"""A project's features and the folder that keeps them."""
|
||||
|
||||
folder: pathlib.Path
|
||||
features: list[Feature]
|
||||
|
||||
|
||||
def kept_features(home: pathlib.Path) -> Kept | Failure:
|
||||
"""The features of the current directory's project, read from the folder its config names."""
|
||||
folder = features_folder(home)
|
||||
if isinstance(folder, Failure):
|
||||
return folder
|
||||
found = features(folder)
|
||||
return found if isinstance(found, Failure) else Kept(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 features to bench, as `project-config --help` describes it")
|
||||
|
||||
|
||||
def features(folder: pathlib.Path) -> list[Feature] | Failure:
|
||||
"""The features folder holds, one in each folder there, in the order of their names. A Failure where it holds none, or one
|
||||
lacks a part."""
|
||||
if not folder.is_dir():
|
||||
return Failure(f"could not read the features in {folder}: it is no folder")
|
||||
try:
|
||||
found = [feature_in(inside) for inside in sorted(folder.iterdir()) if inside.is_dir()]
|
||||
except (OSError, UnicodeDecodeError) as failure:
|
||||
return Failure(f"could not read the features in {folder}: {failure}")
|
||||
if not found:
|
||||
return Failure(f"{folder} holds no feature: a folder for each, with {SPEC}, {PATCH} 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) -> Feature | Failure:
|
||||
"""The feature folder holds. Raises OSError or UnicodeDecodeError where a part cannot be read."""
|
||||
spec, patch, acceptance = folder / SPEC, folder / PATCH, folder / ACCEPTANCE
|
||||
for part in (spec, patch):
|
||||
if not part.is_file():
|
||||
return Failure(f"the feature in {folder} lacks {part.name}")
|
||||
told = spec.read_text(encoding="utf-8").strip()
|
||||
if not told:
|
||||
return Failure(f"the feature 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 feature in {folder} holds no file under {ACCEPTANCE}")
|
||||
return Feature(folder.name, told, str(patch), str(acceptance))
|
||||
|
||||
|
||||
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."""
|
||||
tasks: list[Task] = []
|
||||
passed_over: list[tuple[str, str]] = []
|
||||
for feature in found:
|
||||
task = feature_task(bench, len(tasks) + 1, feature)
|
||||
if isinstance(task, Failure):
|
||||
return task
|
||||
if isinstance(task, Task):
|
||||
tasks.append(task)
|
||||
else:
|
||||
passed_over.append((feature.name, task))
|
||||
bench.say(f"task-bench: passed over the feature {feature.name}: {task}.")
|
||||
if not tasks:
|
||||
return Failure(f"None of the {len(found)} features in {folder} counts. {' '.join(f'{name}: {why}.' for name, why in passed_over)}")
|
||||
return Planned(tasks, [f"Passed over the feature {name}: {why}." for name, why in passed_over])
|
||||
|
||||
|
||||
def feature_task(bench: Bench, number: int, feature: Feature) -> Task | str | Failure:
|
||||
"""The task of building feature, each side's tree committed once; or why the feature doesn't count."""
|
||||
trees: dict[Side, str] = {}
|
||||
for side in SIDES:
|
||||
tree = bench.folder / "features" / str(number) / side
|
||||
# A feature passed over before this one left its trees under the same number.
|
||||
shutil.rmtree(tree, ignore_errors=True)
|
||||
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
|
||||
passed_over = why_passed_over(bench, str(tree), side, feature)
|
||||
if passed_over is not None:
|
||||
return passed_over
|
||||
trees[side] = str(tree)
|
||||
return Task(number, feature.name, trees, dict.fromkeys(SIDES, feature.spec), acceptance=feature.acceptance)
|
||||
|
||||
|
||||
def why_passed_over(bench: Bench, tree: str, side: Side, feature: Feature) -> str | Failure | None:
|
||||
"""Why feature doesn't count at side, whose committed tree is tree; None where it counts. A Failure where the tests could
|
||||
not run."""
|
||||
def put(copy: str) -> Failure | None:
|
||||
return on_disk(f"put {feature.name}'s acceptance tests into {copy}", lambda: with_acceptance(copy, feature.acceptance))
|
||||
|
||||
def built(copy: str) -> Failure | None:
|
||||
# git has run in this bench already, so a failure here is the patch's.
|
||||
applied = bench.git("apply", "--whitespace=nowarn", feature.patch, cwd=copy)
|
||||
return NotApplied(applied.reason) if isinstance(applied, Failure) else put(copy)
|
||||
|
||||
for prepare, reference in ((put, ""), (built, " with the reference patch")):
|
||||
ran = tests_in_copy(bench.containers, tree, bench.folder, prepare)
|
||||
if isinstance(ran, NotApplied):
|
||||
return f"its reference patch doesn't apply at the {side}"
|
||||
if isinstance(ran, TimedOut):
|
||||
return f"its acceptance tests don't end{reference} at the {side}"
|
||||
if isinstance(ran, Failure):
|
||||
return ran
|
||||
if not reference and ran.code == 0:
|
||||
return f"its acceptance tests pass at the {side} before it is built"
|
||||
if reference and ran.code:
|
||||
return f"its acceptance tests fail with the reference patch at the {side}"
|
||||
return None
|
||||
Reference in New Issue
Block a user