|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "encoding/json" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +// mendixlabs/mxcli#1751 (ako/mxcli#1982), as reported: |
| 15 | +// |
| 16 | +// 1. `git merge <ref>` stops on a non-model conflict ("resolve by hand…"). |
| 17 | +// 2. The human resolves it, `git add`s it and re-runs: the engine needs a |
| 18 | +// decision (exit 2) and the merge is aborted — and the text resolution |
| 19 | +// goes with the abort. |
| 20 | +// 3. `--plan` then wrote the model, repaired the hashes and failed at the |
| 21 | +// commit on "unmerged files": a half-done merge, the resolution lost. |
| 22 | +// |
| 23 | +// The staged resolution must survive the abort and come back on the --plan |
| 24 | +// run; and a --plan run with a text conflict still open must stop before it |
| 25 | +// touches the model. |
| 26 | + |
| 27 | +// modelAndTextConflictRepo is a modelConflictRepo where each side also changed |
| 28 | +// NOTES.txt, so the merge conflicts on both the domain model and a text file. |
| 29 | +func modelAndTextConflictRepo(t *testing.T) (string, string) { |
| 30 | + t.Helper() |
| 31 | + dir, mprPath := doctorNoteRepo(t) |
| 32 | + write := func(name, content string) { |
| 33 | + if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil { |
| 34 | + t.Fatal(err) |
| 35 | + } |
| 36 | + } |
| 37 | + units := baseUnits(t) |
| 38 | + units[tDM] = tUnit{tMod, unitDoc(t, "DomainModels$DomainModel", "", mainSideKey)} |
| 39 | + writeProject(t, dir, "11.13.0", units) |
| 40 | + write("NOTES.txt", "main\n") |
| 41 | + gitIn(t, dir, "add", "-A") |
| 42 | + gitIn(t, dir, "commit", "-q", "-m", "main side") |
| 43 | + gitIn(t, dir, "switch", "-q", "-c", "side", "HEAD~1") |
| 44 | + side := baseUnits(t) |
| 45 | + side[tDM] = tUnit{tMod, unitDoc(t, "DomainModels$DomainModel", "", sideKey)} |
| 46 | + writeProject(t, dir, "11.13.0", side) |
| 47 | + write("NOTES.txt", "side\n") |
| 48 | + gitIn(t, dir, "add", "-A") |
| 49 | + gitIn(t, dir, "commit", "-q", "-m", "side change") |
| 50 | + gitIn(t, dir, "switch", "-q", "feature") |
| 51 | + return dir, mprPath |
| 52 | +} |
| 53 | + |
| 54 | +// engineResult is what the fake engine writes for the undecided unit: ours' |
| 55 | +// content, marked. |
| 56 | +func engineResult(t *testing.T) map[string]tUnit { |
| 57 | + result := baseUnits(t) |
| 58 | + result[tDM] = tUnit{tMod, unitDoc(t, "DomainModels$DomainModel", "", mainSideKey)} |
| 59 | + return result |
| 60 | +} |
| 61 | + |
| 62 | +func TestGitMerge_TextResolutionSurvivesTheAbortAndThePlan(t *testing.T) { |
| 63 | + dir, mprPath := modelAndTextConflictRepo(t) |
| 64 | + doctorEnv(t, dir) |
| 65 | + mx := mxRunners{merge: engineDecides(t, engineResult(t), []string{tDM}, nil)} |
| 66 | + |
| 67 | + // 1. The text conflict stops the run before mx. |
| 68 | + var out bytes.Buffer |
| 69 | + if err := mergeRun(mprPath, "side", "", "", "", false, mx, &out); err == nil || |
| 70 | + !strings.Contains(out.String(), "NOTES.txt") { |
| 71 | + t.Fatalf("step 1: want the non-model conflict reported, got %v\n%s", err, out.String()) |
| 72 | + } |
| 73 | + |
| 74 | + // 2. Resolved by hand and staged; the re-run needs a model decision and |
| 75 | + // aborts. |
| 76 | + if err := os.WriteFile(filepath.Join(dir, "NOTES.txt"), []byte("resolved by hand\n"), 0o644); err != nil { |
| 77 | + t.Fatal(err) |
| 78 | + } |
| 79 | + gitIn(t, dir, "add", "NOTES.txt") |
| 80 | + out.Reset() |
| 81 | + if err := mergeRun(mprPath, "side", "", "", "", false, mx, &out); err == nil { |
| 82 | + t.Fatalf("step 2: want the exit-2 abort, got success\n%s", out.String()) |
| 83 | + } |
| 84 | + planPath := mprPath + ".merge-plan.json" |
| 85 | + if _, err := os.Stat(planPath); err != nil { |
| 86 | + t.Fatalf("step 2: no plan template written: %v\n%s", err, out.String()) |
| 87 | + } |
| 88 | + |
| 89 | + // 3. --plan resolves the model and commits — with the text resolution. |
| 90 | + out.Reset() |
| 91 | + if err := mergeRun(mprPath, "side", "", "", planPath, false, mx, &out); err != nil { |
| 92 | + t.Fatalf("step 3: mergeRun --plan: %v\n%s", err, out.String()) |
| 93 | + } |
| 94 | + if got := gitOutT(t, dir, "show", "HEAD:NOTES.txt"); got != "resolved by hand\n" { |
| 95 | + t.Errorf("the merge commit's NOTES.txt = %q, want the hand resolution", got) |
| 96 | + } |
| 97 | + os.Remove(planPath) // the template is not part of the merge |
| 98 | + assertPlanMergeCommitted(t, dir, mprPath) |
| 99 | +} |
| 100 | + |
| 101 | +// With a text conflict open and no resolution to restore, --plan stops before |
| 102 | +// the model: the engine does not run, nothing is written, and the merge stays |
| 103 | +// in progress for the human to resolve and re-run. |
| 104 | +func TestGitMerge_PlanStopsOnAnOpenTextConflict(t *testing.T) { |
| 105 | + dir, mprPath := modelAndTextConflictRepo(t) |
| 106 | + doctorEnv(t, dir) |
| 107 | + plan := mergePlanTemplate{ |
| 108 | + Base: strings.TrimSpace(gitOutT(t, dir, "merge-base", "HEAD", "side")), |
| 109 | + Ours: strings.TrimSpace(gitOutT(t, dir, "rev-parse", "HEAD")), |
| 110 | + Theirs: strings.TrimSpace(gitOutT(t, dir, "rev-parse", "side")), |
| 111 | + Units: []planUnit{{ID: tDM, Take: "ours"}}, |
| 112 | + } |
| 113 | + raw, _ := json.Marshal(plan) |
| 114 | + planPath := filepath.Join(t.TempDir(), "plan.json") |
| 115 | + if err := os.WriteFile(planPath, raw, 0o644); err != nil { |
| 116 | + t.Fatal(err) |
| 117 | + } |
| 118 | + calls := 0 |
| 119 | + mx := mxRunners{merge: func(base, mine, theirs string, shas [3]string) (int, string, error) { |
| 120 | + calls++ |
| 121 | + return 0, "", nil |
| 122 | + }} |
| 123 | + var out bytes.Buffer |
| 124 | + err := mergeRun(mprPath, "side", "", "", planPath, false, mx, &out) |
| 125 | + if err == nil || !strings.Contains(out.String(), "NOTES.txt") { |
| 126 | + t.Fatalf("want the open text conflict reported, got %v\n%s", err, out.String()) |
| 127 | + } |
| 128 | + if calls != 0 { |
| 129 | + t.Errorf("the engine ran %d time(s) with a text conflict open", calls) |
| 130 | + } |
| 131 | + if _, mhErr := gitInE(t, dir, "rev-parse", "--verify", "--quiet", "MERGE_HEAD"); mhErr != nil { |
| 132 | + t.Error("the merge was aborted; it must stay in progress for the human to resolve") |
| 133 | + } |
| 134 | + uu := gitOutT(t, dir, "diff", "--name-only", "--diff-filter=U") |
| 135 | + if !strings.Contains(uu, "NOTES.txt") || !strings.Contains(uu, ".mxunit") { |
| 136 | + t.Errorf("the conflicts must be left as they were, got:\n%s", uu) |
| 137 | + } |
| 138 | + gitInE(t, dir, "merge", "--abort") |
| 139 | +} |
0 commit comments