|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import importlib.util |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from pr_split.constants import AssignmentType, Priority |
| 8 | +from pr_split.plan_store import PLAN_FILE |
| 9 | +from pr_split.schemas import GitState, Group, GroupAssignment, PlanFile, SplitPlan |
| 10 | + |
| 11 | +SCRIPT = Path(__file__).resolve().parents[1] / "scripts" / "score_pr.py" |
| 12 | + |
| 13 | + |
| 14 | +def _load_script(): |
| 15 | + spec = importlib.util.spec_from_file_location("score_pr", SCRIPT) |
| 16 | + module = importlib.util.module_from_spec(spec) |
| 17 | + assert spec.loader is not None |
| 18 | + sys.modules["score_pr"] = module |
| 19 | + spec.loader.exec_module(module) |
| 20 | + return module |
| 21 | + |
| 22 | + |
| 23 | +def _plan_file() -> PlanFile: |
| 24 | + group = Group( |
| 25 | + id="pr-1", |
| 26 | + title="t", |
| 27 | + description="d", |
| 28 | + assignments=[ |
| 29 | + GroupAssignment( |
| 30 | + file_path="a.py", |
| 31 | + assignment_type=AssignmentType.WHOLE_FILE, |
| 32 | + hunk_indices=[0], |
| 33 | + ) |
| 34 | + ], |
| 35 | + estimated_loc=3, |
| 36 | + ) |
| 37 | + plan = SplitPlan( |
| 38 | + dev_branch="feature", |
| 39 | + base_branch="main", |
| 40 | + max_loc=400, |
| 41 | + priority=Priority.ORTHOGONAL, |
| 42 | + groups=[group, group.model_copy(update={"id": "pr-2", "depends_on": ["pr-1"]})], |
| 43 | + ) |
| 44 | + return PlanFile(plan=plan, git_state=GitState()) |
| 45 | + |
| 46 | + |
| 47 | +class TestLoadPlanGroups: |
| 48 | + def test_reads_groups_from_saved_plan_file(self, tmp_path: Path) -> None: |
| 49 | + path = tmp_path / Path(PLAN_FILE).name |
| 50 | + path.write_text(_plan_file().model_dump_json()) |
| 51 | + groups = _load_script().load_plan_groups(str(path)) |
| 52 | + assert [g["id"] for g in groups] == ["pr-1", "pr-2"] |
| 53 | + assert groups[0]["assignments"][0]["file_path"] == "a.py" |
| 54 | + assert groups[1]["depends_on"] == ["pr-1"] |
| 55 | + |
| 56 | + def test_accepts_bare_plan_document(self, tmp_path: Path) -> None: |
| 57 | + path = tmp_path / "plan.json" |
| 58 | + path.write_text(_plan_file().plan.model_dump_json()) |
| 59 | + assert len(_load_script().load_plan_groups(str(path))) == 2 |
0 commit comments