File size: 1,550 Bytes
c4f3819
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""One-click live regression demo."""

from __future__ import annotations

import shutil
from dataclasses import dataclass
from pathlib import Path

from mutator import MUTATIONS, apply_single_mutation
from runner import RunResult, run_pytest


INJECT_MUTATION_ID = "bulk_threshold_ge_to_gt"


@dataclass(frozen=True)
class InjectResult:
    mutation_label: str
    run: RunResult


def reset_sample(source_parent: str | Path, run_dir: str | Path) -> Path:
    """Copy samples into a runnable demo directory."""
    source_parent = Path(source_parent).resolve()
    run_dir = Path(run_dir).resolve()
    sample_target = run_dir / "samples"
    if sample_target.exists():
        shutil.rmtree(sample_target)
    shutil.copytree(source_parent, sample_target)
    return sample_target / "legacy_repo"


def apply(run_dir: str | Path, package_name: str = "legacy_repo") -> bool:
    package_root = Path(run_dir).resolve() / "samples" / package_name
    return apply_single_mutation(package_root, INJECT_MUTATION_ID)


def run_injected_suite(run_dir: str | Path, package_name: str = "legacy_repo") -> InjectResult:
    mutation = next(item for item in MUTATIONS if item.id == INJECT_MUTATION_ID)
    applied = apply(run_dir, package_name=package_name)
    if not applied:
        raise RuntimeError(f"Could not apply inject mutation: {mutation.label}")
    run_dir = Path(run_dir).resolve()
    result = run_pytest(run_dir, package_parent=run_dir / "samples", package_name=package_name)
    return InjectResult(mutation_label=mutation.label, run=result)