Viewing files at commit 3cd2661fdbe9Commit detailsView latest

Browse files
tests/test_merge_suite.py 436 lines · 23.9 KB · python
1
"""Instrument tests for suites/merge (classification, laws, oracle parsing, vendoring).
2
3
Hermetic: no Oak binary and no network. Git-backed tests skip when git is absent.
4
"""
5
import hashlib
6
import json
7
from pathlib import Path
8
import shutil
9
import sys
10
import tempfile
11
import unittest
12
13
ROOT = Path(__file__).resolve().parents[1]
14
sys.path.insert(0, str(ROOT / "suites" / "merge"))
15
sys.path.insert(0, str(ROOT / "scripts"))
16
17
from mergesuite import classify as cl  # noqa: E402
18
from mergesuite.gitops import (Git, git_conflicts, normalize_git_conflict_type,  # noqa: E402
19
                               parse_merge_tree_z, prepare_git_home, worktree_manifest)
20
from mergesuite.oak_adapter import parse_conflict_show, summarize_prediction  # noqa: E402
21
22
VENDOR = ROOT / "suites" / "merge" / "vendor" / "gitoxide"
23
24
25
def v(tag):
26
    return ("file", hashlib.sha256(tag.encode()).hexdigest())
27
28
29
def oak(tree, conflicts=(), clean=None):
30
    return {"tree": tree, "conflicts": {p: [] for p in conflicts},
31
            "clean": (not conflicts) if clean is None else clean, "error": None, "error_kind": None}
32
33
34
def gitres(tree, conflicts=()):
35
    return {"tree": tree, "conflicts": {p: ["content"] for p in conflicts}, "clean": not conflicts}
36
37
38
class ClassifyTests(unittest.TestCase):
39
    base = {"a": v("a0"), "b": v("b0")}
40
    ours = {"a": v("a1"), "b": v("b0")}
41
    theirs = {"a": v("a0"), "b": v("b1")}
42
    merged = {"a": v("a1"), "b": v("b1")}
43
44
    def test_equal(self):
45
        r = cl.classify(gitres(self.merged), oak(self.merged), self.base, self.ours, self.theirs)
46
        self.assertEqual(r["class"], "equal-to-git")
47
48
    def test_lost_change_is_oak_wrong_even_if_git_agrees(self):
49
        # Oak drops theirs' change to b.
50
        r = cl.classify(gitres(self.merged), oak(self.ours), self.base, self.ours, self.theirs)
51
        self.assertEqual(r["class"], "oak-wrong")
52
        self.assertIn("lost_change: b", r["reasons"][0])
53
54
    def test_lost_deletion(self):
55
        theirs = {"a": v("a0")}  # theirs deletes b
56
        r = cl.classify(gitres({"a": v("a1")}), oak({"a": v("a1"), "b": v("b0")}),
57
                        self.base, self.ours, theirs)
58
        self.assertEqual(r["class"], "oak-wrong")
59
60
    def test_fabricated_content(self):
61
        r = cl.classify(gitres(self.merged), oak({"a": v("a1"), "b": v("zzz")}),
62
                        self.base, self.ours, self.theirs)
63
        self.assertEqual(r["class"], "oak-wrong")
64
        self.assertIn("fabricated", r["reasons"][0])
65
66
    def test_false_conflict_is_differs_but_valid(self):
67
        r = cl.classify(gitres(self.merged), oak(self.merged, conflicts=["a"]),
68
                        self.base, self.ours, self.theirs)
69
        self.assertEqual(r["class"], "differs-but-valid")
70
        self.assertTrue(any("oak_conflicts_where_git_clean" in x for x in r["reasons"]))
71
72
    def test_rename_not_detected_keeps_both_sides(self):
73
        # ours renames a->c; theirs edits b. git moves; Oak path-level result matches git here,
74
        # but when git conflicts and Oak keeps everything, it is differs-but-valid.
75
        base = {"a": v("a0")}
76
        ours = {"b": v("a0")}
77
        theirs = {"c": v("a0")}
78
        r = cl.classify(gitres({"b": v("a0"), "c": v("a0")}, conflicts=["a", "b", "c"]),
79
                        oak({"b": v("a0"), "c": v("a0")}), base, ours, theirs)
80
        self.assertEqual(r["class"], "differs-but-valid")
81
        self.assertTrue(any("oak_clean_where_git_conflicts" in x for x in r["reasons"]))
82
83
    def test_expectation_violation_wins(self):
84
        r = cl.classify(gitres(self.merged), oak(self.merged), self.base, self.ours, self.theirs,
85
                        expect={"paths": {"a": v("a0")[1]}})
86
        self.assertEqual(r["class"], "oak-wrong")
87
        self.assertIn("expectation_violated", r["reasons"][0])
88
89
    def test_expect_absent_and_clean(self):
90
        r = cl.classify(gitres(self.merged), oak(self.merged), self.base, self.ours, self.theirs,
91
                        expect={"clean": False})
92
        self.assertEqual(r["class"], "oak-wrong")
93
        r = cl.classify(gitres(self.merged), oak(self.merged), self.base, self.ours, self.theirs,
94
                        expect={"paths": {"gone": None}, "clean": True})
95
        self.assertEqual(r["class"], "equal-to-git")
96
97
    def test_unsupported_and_harness_and_oak_error(self):
98
        self.assertEqual(cl.classify(None, None, {}, {}, {}, unsupported="gitlink")["class"], "oak-unsupported")
99
        self.assertEqual(cl.classify(None, None, {}, {}, {}, harness_error="x")["class"], "harness-error")
100
        self.assertEqual(cl.classify(gitres({}), {"error_kind": "harness", "error": "shim"}, {}, {}, {})["class"],
101
                         "harness-error")
102
        r = cl.classify(gitres({}), {"error_kind": "oak", "error": "boom", "clean": None}, {}, {}, {})
103
        self.assertEqual(r["class"], "oak-wrong")
104
        self.assertEqual(cl.classify(gitres({}), None, {}, {}, {})["class"], "harness-error")
105
106
    def test_no_unique_base_leaves_law_unmeasured(self):
107
        r = cl.classify(gitres(self.merged), oak(self.merged), None, self.ours, self.theirs)
108
        self.assertEqual(r["class"], "equal-to-git")
109
        self.assertIsNone(r["evidence"]["law_no_lost_change"])
110
111
    def test_text_merge_law(self):
112
        base = b"1\n2\n3\n4\n5\n6\n"
113
        o = b"1 ours\n2\n3\n4\n5\n6\n"
114
        t = b"1\n2\n3\n4\n5\n6 theirs\n"
115
        good = b"1 ours\n2\n3\n4\n5\n6 theirs\n"
116
        self.assertTrue(cl.text_preserves_sides(base, o, t, good))
117
        self.assertFalse(cl.text_preserves_sides(base, o, t, o))
118
        self.assertFalse(cl.text_preserves_sides(base, o, t, b"<<<<<<< a\n" + good))
119
        trees = ({"f": v("b")}, {"f": v("o")}, {"f": v("t")})
120
        texts = {"f": {"base": base, "ours": o, "theirs": t, "merged": good}}
121
        law = cl.no_lost_change(*trees, {"f": v("m")}, set(), texts)
122
        self.assertEqual(law["text_merged"], ["f"])
123
        texts["f"]["merged"] = o
124
        self.assertEqual(cl.no_lost_change(*trees, {"f": v("m")}, set(), texts)["lost"], ["f"])
125
126
    def test_one_side_taken_wholesale_is_lost(self):
127
        # base exec+empty; ours: mode-only change; theirs: content change. Oak keeping theirs'
128
        # entry (content AND exec bit) drops ours' mode change.
129
        base = {"w": ("exec", "empty")}
130
        ours = {"w": ("file", "empty")}
131
        theirs = {"w": ("exec", "123")}
132
        law = cl.no_lost_change(base, ours, theirs, {"w": ("exec", "123")}, set())
133
        self.assertEqual(law["mode_lost"], ["w"])
134
        # a content change dropped wholesale stays a plain lost change
135
        law = cl.no_lost_change({"f": v("0")}, {"f": v("1")}, {"f": v("2")}, {"f": v("1")}, set())
136
        self.assertEqual(law["lost"], ["f"])
137
        law = cl.no_lost_change(base, ours, theirs, {"w": ("file", "123")}, set(),
138
                                {"w": {"base": b"", "ours": b"", "theirs": b"1\n2\n3\n", "merged": b"1\n2\n3\n"}})
139
        self.assertEqual(law["text_merged"], ["w"])
140
141
    def test_conflicted_paths_are_exempt_from_law(self):
142
        law = cl.no_lost_change(self.base, self.ours, self.theirs, {}, {"a", "b"})
143
        self.assertEqual(law, {"lost": [], "mode_lost": [], "markers": [], "markers_no_final_newline": [], "markers_symlink": [], "fabricated": [], "text_merged": []})
144
145
    def test_text_merged_but_mode_dropped_is_oak_wrong(self):
146
        # QA shape generated/s20260928-022: ours edits lines, theirs edits other lines AND chmods.
147
        base, ours, theirs = {"f": ("file", "b")}, {"f": ("file", "o")}, {"f": ("exec", "t")}
148
        merged = b"1 ours\n2\n3\n4\n5\n6 theirs\n"
149
        texts = {"f": {"base": b"1\n2\n3\n4\n5\n6\n", "ours": b"1 ours\n2\n3\n4\n5\n6\n",
150
                       "theirs": b"1\n2\n3\n4\n5\n6 theirs\n", "merged": merged}}
151
        bad = cl.classify(gitres({"f": ("exec", "m")}), oak({"f": ("file", "m")}), base, ours, theirs, texts=texts)
152
        self.assertEqual(bad["class"], "oak-wrong")
153
        self.assertTrue(bad["reasons"][0].startswith("lost_mode_change"))
154
        good = cl.classify(gitres({"f": ("exec", "m")}), oak({"f": ("exec", "m")}), base, ours, theirs, texts=texts)
155
        self.assertEqual(good["class"], "equal-to-git")
156
157
    def test_same_content_different_modes_taken_wholesale_is_mode_loss(self):
158
        # both sides rename to the same path with different modes; Oak keeps one side silently
159
        law = cl.no_lost_change({}, {"t": ("file", "x")}, {"t": ("exec", "x")}, {"t": ("exec", "x")}, set())
160
        self.assertEqual(law["mode_lost"], ["t"])
161
162
    def test_symlink_markers_are_distinguished(self):
163
        ev = {"base": b"", "ours": b"a", "theirs": b"b", "merged": b"<<<<<<< x\na=======\nb>>>>>>> main\n"}
164
        law = cl.no_lost_change({}, {"l": ("symlink", "a")}, {"l": ("symlink", "b")}, {"l": ("symlink", "m")},
165
                                set(), {"l": ev})
166
        self.assertEqual(law["markers_symlink"], ["l"])
167
168
    def test_committed_markers_have_their_own_reason(self):
169
        base, ours, theirs = {"f": v("b")}, {"f": v("o")}, {"f": v("t")}
170
        texts = {"f": {"base": b"a\nx", "ours": b"a\ny", "theirs": b"a\nz",
171
                       "merged": b"a\n<<<<<<< br\ny||||||| original\nx=======\nz>>>>>>> main\n"}}
172
        r = cl.classify(gitres({"f": v("g")}, conflicts=["f"]), oak({"f": v("m")}), base, ours, theirs, texts=texts)
173
        self.assertEqual(r["class"], "oak-wrong")
174
        self.assertTrue(r["reasons"][0].startswith("committed_conflict_markers_no_final_newline: f"))
175
176
    def test_failed_pull_dirty_worktree_reason(self):
177
        res = {"error_kind": "oak", "error": "IO error", "clean": None,
178
               "post_failure": {"hazard_worktree_dirty_head_unmoved": True, "worktree_changes_vs_head": 2,
179
                                "worktree_changes_sample": ["a", "b"]}}
180
        r = cl.classify(gitres({}), res, {}, {}, {})
181
        self.assertTrue(r["reasons"][1].startswith("failed_pull_left_worktree_dirty: "))
182
183
    def test_failed_pull_hazard_reason(self):
184
        res = {"error_kind": "oak", "error": "IO error", "clean": None,
185
               "post_failure": {"hazard_head_moved_worktree_stale": True, "head_before": "a" * 64,
186
                                "head_after": "b" * 64, "worktree_changes_vs_head": 2,
187
                                "repull_parent_sync": "up_to_date"}}
188
        r = cl.classify(gitres({}), res, {}, {}, {})
189
        self.assertEqual(r["class"], "oak-wrong")
190
        self.assertTrue(r["reasons"][1].startswith("failed_pull_moved_head_worktree_stale"))
191
192
    def test_unknown_class_is_rejected(self):
193
        self.assertEqual(cl.validate_class("equal-to-git"), "equal-to-git")
194
        with self.assertRaises(ValueError):
195
            cl.validate_class("pass")
196
197
    def test_symmetry(self):
198
        f = oak(self.merged, conflicts=["a"])
199
        r = oak({"a": v("other-side-markers"), "b": v("b1")}, conflicts=["a"])
200
        self.assertTrue(cl.symmetry(f, r)["holds"])
201
        self.assertFalse(cl.symmetry(f, oak(self.merged))["holds"])
202
        self.assertIsNone(cl.symmetry(f, {"clean": None}))
203
204
    def test_idempotence(self):
205
        self.assertIsNone(cl.idempotence_holds({}))
206
        good = {"idempotence": {"returncode": 0, "head_unchanged": True, "tree_unchanged": True}}
207
        self.assertTrue(cl.idempotence_holds(good))
208
        good["idempotence"]["tree_unchanged"] = False
209
        self.assertFalse(cl.idempotence_holds(good))
210
211
    def test_oak_conflict_type(self):
212
        base = {"a": ("file", "1"), "m": ("file", "1")}
213
        self.assertEqual(cl.oak_conflict_type("a", base, {"a": ("file", "2")}, {}), "modify/delete")
214
        self.assertEqual(cl.oak_conflict_type("n", base, {"n": ("file", "2")}, {"n": ("file", "3")}), "add/add")
215
        self.assertEqual(cl.oak_conflict_type("m", base, {"m": ("exec", "1")}, {"m": ("symlink", "1")}), "mode")
216
217
218
class RunnerAccountingTests(unittest.TestCase):
219
    def test_failed_pull_counts_as_prediction_disagreement(self):
220
        from mergesuite.runner import Suite
221
        oak_res = {"error_kind": "oak", "error": "x", "clean": None, "tree": {},
222
                   "prediction": {"available": True, "clean": True, "conflict_files": []},
223
                   "post_failure": {"hazard_head_moved_worktree_stale": True}}
224
        verdict = cl.classify(gitres({}), oak_res, {}, {}, {})
225
        out = Suite._result_fields(None, gitres({}), oak_res, verdict)
226
        self.assertIs(out["laws"]["prediction_agrees"], False)
227
        self.assertTrue(out["post_failure_hazard"])
228
229
    LAWS = {k: None for k in ("no_lost_change", "idempotent", "symmetric", "false_conflict",
230
                              "missed_conflict", "prediction_agrees")}
231
232
    def row(self, scenario, reasons, cls="oak-wrong", direction="A-B"):
233
        return {"class": cls, "scenario": scenario, "direction": direction, "origin": "o",
234
                "reasons": reasons, "laws": dict(self.LAWS)}
235
236
    def test_known_bug_mapping_positive(self):
237
        from mergesuite.runner import load_known_bugs, match_known_bug
238
        bugs = load_known_bugs()
239
        self.assertEqual({b["id"] for b in bugs}, {"fb-537", "fb-538", "fb-539", "fb-540", "fb-541"})
240
        cases = [
241
            (["committed_conflict_markers_symlink: link"], "fb-538"),
242
            (["committed_conflict_markers_no_final_newline: file"], "fb-537"),
243
            (["lost_mode_change: w"], "fb-539"),
244
            (["oak_error: IO error: File exists (os error 17)"], "fb-540"),
245
            (["oak_error: IO error: Not a directory (os error 20)",
246
              "failed_pull_left_worktree_dirty: HEAD unchanged, 2 worktree path(s) differ from HEAD (e.g. a)"], "fb-540"),
247
            (['oak_error: cannot build tree with both file "e" and child path "e/e"'], "fb-540"),
248
            (["oak_error: refusing to replace non-empty directory '/x/a' with a tracked file",
249
              "failed_pull_moved_head_worktree_stale: head a -> b"], "fb-540"),
250
            (["expectation_violated[criss_cross_divergent_resolution]: clean=True expected False"], "fb-541"),
251
        ]
252
        for reasons, bug in cases:
253
            self.assertEqual(match_known_bug(self.row("gix-x", reasons), bugs), bug, reasons)
254
255
    def test_known_bug_escape_routes_stay_unmapped(self):
256
        from mergesuite.runner import load_known_bugs, match_known_bug
257
        bugs = load_known_bugs()
258
        escapes = [
259
            # any other Oak error (e.g. a panic or server 500) is not fb-540
260
            ("gix-tree/a", ["oak_error: Server error: HTTP 500"]),
261
            ("gix-tree/a", ["oak_error: thread 'main' panicked at src/x.rs"]),
262
            # t6416 rows with another reason are not fb-541
263
            ("git-t6416-criss-cross-modify-delete", ["lost_change: file"]),
264
            ("git-t6416-criss-cross-modify-delete", ["expectation_violated: clean=True expected False"]),
265
            # one explained reason plus one unexplained reason stays unmapped
266
            ("gix-text/x", ["committed_conflict_markers_no_final_newline: f", "lost_change: g"]),
267
            ("gix-tree/x", ["oak_error: IO error: File exists (os error 17)", "fabricated_content: q"]),
268
            # markers in files that DO end with a newline are a different root cause
269
            ("gix-text/x", ["committed_conflict_markers: f"]),
270
            ("gix-text/x", []),
271
        ]
272
        for scenario, reasons in escapes:
273
            self.assertIsNone(match_known_bug(self.row(scenario, reasons), bugs), (scenario, reasons))
274
        # only oak-wrong rows are ever mapped
275
        self.assertIsNone(match_known_bug(self.row("gix-x", ["lost_mode_change: w"], cls="differs-but-valid"), bugs))
276
277
    def test_setup_failure_is_not_an_oak_verdict(self):
278
        res = {"error_kind": "oak-setup", "error": "oak clone exited 101: panicked", "clean": None}
279
        r = cl.classify(gitres({}), res, {}, {}, {})
280
        self.assertEqual(r["class"], "harness-error")
281
        self.assertTrue(r["reasons"][0].startswith("setup_failed: "))
282
283
    def test_markers_split_by_final_newline(self):
284
        base, ours, theirs = {"f": v("b")}, {"f": v("o")}, {"f": v("t")}
285
        glued = {"base": b"a\nx", "ours": b"a\ny", "theirs": b"a\nz",
286
                 "merged": b"a\n<<<<<<< br\ny||||||| original\nx=======\nz>>>>>>> main\n"}
287
        law = cl.no_lost_change(base, ours, theirs, {"f": v("m")}, set(), {"f": glued})
288
        self.assertEqual(law["markers_no_final_newline"], ["f"])
289
        clean_eol = {"base": b"a\nx\n", "ours": b"a\ny\n", "theirs": b"a\nz\n",
290
                     "merged": b"a\n<<<<<<< br\ny\n=======\nz\n>>>>>>> main\n"}
291
        law = cl.no_lost_change(base, ours, theirs, {"f": v("m")}, set(), {"f": clean_eol})
292
        self.assertEqual((law["markers"], law["markers_no_final_newline"]), (["f"], []))
293
294
    def test_pinned_rows_report_new_and_vanished(self):
295
        from mergesuite.runner import summarize
296
        bugs = [{"id": "fb-x", "family": "F", "reason_patterns": ["^lost_mode_change: "],
297
                 "expected_rows": ["gix-a|A-B", "gix-b|A-B"]},
298
                {"id": "fb-y", "family": "G", "reason_patterns": ["^nothing"], "expected_rows": ["z|A-B"]}]
299
        rows = [self.row("gix-a", ["lost_mode_change: w"]), self.row("gix-c", ["lost_mode_change: w"]),
300
                self.row("gix-d", ["lost_change: q"]), self.row("ok", [], cls="equal-to-git")]
301
        summary = summarize(rows, bugs)
302
        fx = summary["known_bugs"]["fb-x"]
303
        self.assertEqual((fx["new_rows"], fx["vanished_rows"], fx["status"]),
304
                         (["gix-c|A-B"], ["gix-b|A-B"], "changed"))
305
        self.assertEqual(summary["known_bugs"]["fb-y"]["status"], "NOT reproduced (fixed?)")
306
        self.assertEqual(summary["oak_wrong_unmapped_rows"], ["gix-d|A-B"])
307
308
    def test_committed_known_bugs_are_pinned(self):
309
        from mergesuite.runner import load_known_bugs
310
        for bug in load_known_bugs():
311
            self.assertTrue(bug["expected_rows"], bug["id"])
312
            self.assertEqual(len(bug["expected_rows"]), len(set(bug["expected_rows"])), bug["id"])
313
314
315
class OracleParsingTests(unittest.TestCase):
316
    def test_parse_merge_tree_z(self):
317
        out = (b"tree123\0"
318
               b"100644 aaa 1\ta\x00100644 bbb 2\ta\x00100644 ccc 3\ta\x00"
319
               b"\0"
320
               b"1\0a\0Auto-merging\0Auto-merging a\n\0"
321
               b"1\0a\0CONFLICT (contents)\0CONFLICT (content): Merge conflict in a\n\0")
322
        parsed = parse_merge_tree_z(out)
323
        self.assertEqual(parsed["tree_oid"], "tree123")
324
        self.assertEqual(parsed["conflict_stages"], {"a": [1, 2, 3]})
325
        self.assertEqual(git_conflicts(parsed), {"a": ["content"]})
326
327
    def test_parse_clean(self):
328
        parsed = parse_merge_tree_z(b"t\0\0")
329
        self.assertEqual(parsed["conflict_stages"], {})
330
        self.assertEqual(parsed["messages"], [])
331
332
    def test_type_vocabulary(self):
333
        self.assertIsNone(normalize_git_conflict_type("Auto-merging"))
334
        self.assertEqual(normalize_git_conflict_type("CONFLICT (modify/delete)"), "modify/delete")
335
        self.assertEqual(normalize_git_conflict_type("CONFLICT (rename/delete)"), "rename/delete")
336
        self.assertEqual(normalize_git_conflict_type("CONFLICT (directory rename split)"), "directory-rename")
337
338
    def test_conflict_show_and_prediction_parsing(self):
339
        doc = {"conflicts": [{"path": "f", "has_conflict_markers": False, "exists": True}]}
340
        self.assertEqual(parse_conflict_show(doc, {}), {"f": ["no-markers"]})
341
        self.assertEqual(parse_conflict_show(None, {"conflict_paths": ["x"]}), {"x": []})
342
        self.assertEqual(summarize_prediction(None, 6), {"available": False, "returncode": 6})
343
        pred = summarize_prediction({"prediction_available": True, "clean": False,
344
                                     "conflict_files": [{"path": "b"}, "a"]}, 0)
345
        self.assertEqual(pred["conflict_files"], ["a", "b"])
346
347
348
@unittest.skipUnless(shutil.which("git"), "git not installed")
349
class GitBackedTests(unittest.TestCase):
350
    def setUp(self):
351
        self.tmp = Path(tempfile.mkdtemp(prefix="mergesuite-test-"))
352
        self.home = self.tmp / "home"
353
        from oakbench.subjects import which_measured_git
354
        self.git_bin = which_measured_git() or shutil.which("git")
355
        prepare_git_home(self.home, self.git_bin)
356
357
    def tearDown(self):
358
        shutil.rmtree(self.tmp, ignore_errors=True)
359
360
    def test_builder_oracle_and_materialize_roundtrip(self):
361
        from mergesuite.builder import RepoBuilder, lines
362
        b = RepoBuilder(self.tmp / "r", self.git_bin, self.home)
363
        base = b.commit({"f": lines("1", "2", "3"), "x": ("exec", b"#!/bin/sh\n"), "l": ("link", "f")})
364
        a = b.commit({"f": lines("1 a", "2", "3"), "x": ("exec", b"#!/bin/sh\n"), "l": ("link", "f")}, [base])
365
        c = b.commit({"f": lines("1", "2", "3 c"), "l": ("link", "f")}, [base], message="unique")
366
        self.assertEqual(b.merge_bases(a, c), [base])
367
        merge = b.merge_tree(a, c)
368
        self.assertTrue(merge["clean"])
369
        self.assertEqual(sorted(merge["tree"]), ["f", "l"])
370
        self.assertEqual(merge["tree"]["l"][0], "symlink")
371
        out = self.tmp / "mat"
372
        out.mkdir()
373
        b.materialize(base, out)
374
        self.assertEqual(worktree_manifest(out), b.tree(base))
375
        # first parent order is preserved
376
        m = b.commit({"f": lines("z")}, [a, c])
377
        self.assertEqual(b.out("rev-parse", m + "^1"), a)
378
379
    def test_oracle_reports_conflict_types(self):
380
        from mergesuite.builder import RepoBuilder, lines
381
        b = RepoBuilder(self.tmp / "r2", self.git_bin, self.home)
382
        base = b.commit({"f": lines("1", "2"), "g": lines("g")})
383
        a = b.commit({"f": lines("1", "A")}, [base])
384
        c = b.commit({"f": lines("1", "C"), "g": lines("g2")}, [base])
385
        merge = b.merge_tree(a, c)
386
        self.assertFalse(merge["clean"])
387
        self.assertEqual(git_conflicts(merge), {"f": ["content"], "g": ["modify/delete"]})
388
389
    def test_sources_build_without_oak(self):
390
        from mergesuite import sources
391
        scen = sources.git_t64xx(self.tmp / "t64", self.git_bin, self.home)
392
        ids = [s["id"] for s in scen]
393
        self.assertIn("git-t6423-1a", ids)
394
        self.assertTrue(all(len(s["directions"]) == 2 and s["unsupported"] is None for s in scen))
395
        native = sources.native_incidents(self.tmp / "nat", self.git_bin, self.home)
396
        by_id = {s["id"]: s for s in native}
397
        cc = by_id["incident/criss-cross-tie-break-side-newer"]["directions"][0]
398
        self.assertEqual(len(cc["merge_bases"]), 2)  # two best common ancestors by construction
399
        self.assertIsNone(cc["trees"]["base"])
400
        skew = by_id["incident/clock-skew-merge-base-skewed"]["directions"][0]
401
        self.assertEqual(len(skew["merge_bases"]), 1)
402
        # self-merges produce a single direction (no duplicate rows sharing a case dir)
403
        rev = by_id["incident/revert-after-client-sync"]
404
        tip = rev["directions"][0]["oracle"]["theirs"]
405
        one = sources.symmetric("self", "o", {}, Git(rev["repo"], self.git_bin, self.home), tip, tip)
406
        self.assertEqual(len(one["directions"]), 1)
407
        gen = sources.generated(self.tmp / "gen", self.git_bin, self.home, 3, 7)
408
        gen2 = sources.generated(self.tmp / "gen2", self.git_bin, self.home, 3, 7)
409
        self.assertEqual([s["notes"] for s in gen], [s["notes"] for s in gen2])  # seeded
410
411
412
class VendoringTests(unittest.TestCase):
413
    def test_vendored_scripts_are_byte_identical_to_pinned_upstream(self):
414
        meta = json.loads((VENDOR / "UPSTREAM.json").read_text())
415
        self.assertEqual(meta["license"], "MIT OR Apache-2.0")
416
        for name, info in meta["files"].items():
417
            path = VENDOR / name
418
            self.assertTrue(path.is_file(), name)
419
            if "sha256" in info:
420
                self.assertEqual(hashlib.sha256(path.read_bytes()).hexdigest(), info["sha256"], name)
421
        notice = (VENDOR / "NOTICE").read_text()
422
        self.assertIn(meta["commit"], notice)
423
        self.assertIn("Permission is hereby granted", (VENDOR / "LICENSE-MIT").read_text())
424
        self.assertIn("Apache License", (VENDOR / "LICENSE-APACHE").read_text())
425
426
    def test_no_git_test_suite_text_is_vendored(self):
427
        merge_root = ROOT / "suites" / "merge"
428
        for path in merge_root.rglob("*"):
429
            if path.is_file() and path.suffix in (".py", ".sh", ".md", ".json"):
430
                text = path.read_text(errors="replace")
431
                self.assertNotIn("test_expect_success", text, path)
432
                self.assertNotIn(". ./test-lib.sh", text, path)
433
434
435
if __name__ == "__main__":
436
    unittest.main()