File size: 3,006 Bytes
065ea6f
 
 
0190001
065ea6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0190001
065ea6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
"""Check that every instance's source_patch + test_patch apply cleanly.

For each task we create an isolated detached worktree at ``v_minus_1_commit``, apply
``source_patch`` (recreating V-0.5), then apply ``test_patch`` (the gold V0 test
changes). Failures are reported per instance. Requires the local repos under
``defects4j-projects``; only used for verification, not for dataset consumption.
"""
import argparse, os, shutil, subprocess, tempfile
from pathlib import Path
import pandas as pd

HERE = Path(__file__).resolve().parent
REPOS = HERE.parent.parent / "TUDataset" / "defects4j-projects"


def apply_check(wt, patch):
    if not patch.strip():
        return True, ""
    p = subprocess.run(["git", "-C", wt, "-c", "core.autocrlf=false",
                        "apply", "--check", "-"],
                       input=patch, text=True, capture_output=True)
    return p.returncode == 0, p.stderr.strip()


def apply(wt, patch):
    if not patch.strip():
        return
    subprocess.run(["git", "-C", wt, "-c", "core.autocrlf=false", "apply", "-"],
                   input=patch, text=True, capture_output=True)


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--parquet", type=Path, default=HERE / "data" / "test.parquet")
    ap.add_argument("--repos", type=Path, default=REPOS)
    ap.add_argument("--limit", type=int, default=0, help="0 = all")
    args = ap.parse_args()

    df = pd.read_parquet(args.parquet)
    if args.limit:
        df = df.head(args.limit)

    ok_src = ok_test = 0
    fails = []
    for n, (_, r) in enumerate(df.iterrows(), 1):
        repo = args.repos / r["project"]
        wt = tempfile.mkdtemp(prefix="teb_wt_")
        try:
            subprocess.run(["git", "-C", str(repo), "worktree", "add", "-q",
                            "--detach", wt, r["v_minus_1_commit"]], capture_output=True)
            s_ok, s_err = apply_check(wt, r["source_patch"])
            if s_ok:
                ok_src += 1
                apply(wt, r["source_patch"])
                t_ok, t_err = apply_check(wt, r["test_patch"])
            else:
                t_ok, t_err = False, "(source failed first)"
            if t_ok:
                ok_test += 1
            if not (s_ok and t_ok):
                fails.append((r["instance_id"], s_ok, t_ok,
                              (s_err or t_err).splitlines()[-1][:120] if (s_err or t_err) else ""))
        finally:
            subprocess.run(["git", "-C", str(repo), "worktree", "remove",
                            "--force", wt], capture_output=True)
            shutil.rmtree(wt, ignore_errors=True)
        if n % 50 == 0:
            print(f"  ...{n}/{len(df)}")

    print(f"\nsource_patch apply ok: {ok_src}/{len(df)}")
    print(f"test_patch   apply ok: {ok_test}/{len(df)}")
    if fails:
        print(f"\n{len(fails)} failures:")
        for iid, s, t, msg in fails:
            print(f"  {iid:34s} src={s} test={t}  {msg}")


if __name__ == "__main__":
    main()