|
| 1 | +// Copyright ©️ Ant Group. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package odb |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "code.alipay.com/zeta/zeta/modules/plumbing" |
| 10 | + "code.alipay.com/zeta/zeta/modules/plumbing/filemode" |
| 11 | + "code.alipay.com/zeta/zeta/modules/zeta/object" |
| 12 | +) |
| 13 | + |
| 14 | +// TestNameConflictsFirstMatch verifies that nameConflicts() records the first |
| 15 | +// matching path (sorted order) when a file name is a prefix of multiple paths. |
| 16 | +func TestNameConflictsFirstMatch(t *testing.T) { |
| 17 | + d := &differences{ |
| 18 | + entries: map[string]*ChangeEntry{ |
| 19 | + "a": {Path: "a", Our: &object.TreeEntry{Name: "a", Mode: filemode.Regular, Hash: plumbing.NewHash("1111111111111111111111111111111111111111111111111111111111111111")}}, |
| 20 | + "a/b": {Path: "a/b", Their: &object.TreeEntry{Name: "b", Mode: filemode.Regular, Hash: plumbing.NewHash("2222222222222222222222222222222222222222222222222222222222222222")}}, |
| 21 | + "a/c": {Path: "a/c", Their: &object.TreeEntry{Name: "c", Mode: filemode.Regular, Hash: plumbing.NewHash("3333333333333333333333333333333333333333333333333333333333333333")}}, |
| 22 | + }, |
| 23 | + renames: make(map[string]*RenameEntry), |
| 24 | + ours: map[string]bool{"a": true}, |
| 25 | + theirs: map[string]bool{"a/b": true, "a/c": true}, |
| 26 | + } |
| 27 | + |
| 28 | + conflicts := d.nameConflicts() |
| 29 | + |
| 30 | + if len(conflicts) != 1 { |
| 31 | + t.Fatalf("expected 1 conflict entry, got %d", len(conflicts)) |
| 32 | + } |
| 33 | + |
| 34 | + // After fix: records the first matching path in sorted order ("a/b" < "a/c"). |
| 35 | + val, ok := conflicts["a"] |
| 36 | + if !ok { |
| 37 | + t.Fatal("expected conflict for key 'a'") |
| 38 | + } |
| 39 | + |
| 40 | + if val != "a/b" { |
| 41 | + t.Errorf("expected conflict value 'a/b' (first sorted match), got %q", val) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// TestHasConflictCorrectness verifies that hasConflict() logic is correct. |
| 46 | +func TestHasConflictCorrectness(t *testing.T) { |
| 47 | + hash1 := plumbing.NewHash("1111111111111111111111111111111111111111111111111111111111111111") |
| 48 | + hash2 := plumbing.NewHash("2222222222222222222222222222222222222222222222222222222222222222") |
| 49 | + hash3 := plumbing.NewHash("3333333333333333333333333333333333333333333333333333333333333333") |
| 50 | + |
| 51 | + tests := []struct { |
| 52 | + name string |
| 53 | + entry ChangeEntry |
| 54 | + expected bool |
| 55 | + }{ |
| 56 | + { |
| 57 | + name: "ancestor==ours, theirs modified (no conflict - fast forward theirs)", |
| 58 | + entry: ChangeEntry{ |
| 59 | + Path: "file.txt", |
| 60 | + Ancestor: &object.TreeEntry{Name: "file.txt", Hash: hash1, Mode: filemode.Regular}, |
| 61 | + Our: &object.TreeEntry{Name: "file.txt", Hash: hash1, Mode: filemode.Regular}, |
| 62 | + Their: &object.TreeEntry{Name: "file.txt", Hash: hash2, Mode: filemode.Regular}, |
| 63 | + }, |
| 64 | + expected: false, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "ancestor==theirs, ours modified (no conflict - fast forward ours)", |
| 68 | + entry: ChangeEntry{ |
| 69 | + Path: "file.txt", |
| 70 | + Ancestor: &object.TreeEntry{Name: "file.txt", Hash: hash1, Mode: filemode.Regular}, |
| 71 | + Our: &object.TreeEntry{Name: "file.txt", Hash: hash2, Mode: filemode.Regular}, |
| 72 | + Their: &object.TreeEntry{Name: "file.txt", Hash: hash1, Mode: filemode.Regular}, |
| 73 | + }, |
| 74 | + expected: false, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "ours==theirs, both modified same way (no conflict)", |
| 78 | + entry: ChangeEntry{ |
| 79 | + Path: "file.txt", |
| 80 | + Ancestor: &object.TreeEntry{Name: "file.txt", Hash: hash1, Mode: filemode.Regular}, |
| 81 | + Our: &object.TreeEntry{Name: "file.txt", Hash: hash2, Mode: filemode.Regular}, |
| 82 | + Their: &object.TreeEntry{Name: "file.txt", Hash: hash2, Mode: filemode.Regular}, |
| 83 | + }, |
| 84 | + expected: false, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "all three different (conflict)", |
| 88 | + entry: ChangeEntry{ |
| 89 | + Path: "file.txt", |
| 90 | + Ancestor: &object.TreeEntry{Name: "file.txt", Hash: hash1, Mode: filemode.Regular}, |
| 91 | + Our: &object.TreeEntry{Name: "file.txt", Hash: hash2, Mode: filemode.Regular}, |
| 92 | + Their: &object.TreeEntry{Name: "file.txt", Hash: hash3, Mode: filemode.Regular}, |
| 93 | + }, |
| 94 | + expected: true, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "ours deleted (nil), theirs modified (conflict)", |
| 98 | + entry: ChangeEntry{ |
| 99 | + Path: "file.txt", |
| 100 | + Ancestor: &object.TreeEntry{Name: "file.txt", Hash: hash1, Mode: filemode.Regular}, |
| 101 | + Our: nil, |
| 102 | + Their: &object.TreeEntry{Name: "file.txt", Hash: hash2, Mode: filemode.Regular}, |
| 103 | + }, |
| 104 | + expected: true, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "both deleted (nil) (no conflict - both agree)", |
| 108 | + entry: ChangeEntry{ |
| 109 | + Path: "file.txt", |
| 110 | + Ancestor: &object.TreeEntry{Name: "file.txt", Hash: hash1, Mode: filemode.Regular}, |
| 111 | + Our: nil, |
| 112 | + Their: nil, |
| 113 | + }, |
| 114 | + expected: false, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + for _, tt := range tests { |
| 119 | + t.Run(tt.name, func(t *testing.T) { |
| 120 | + got := tt.entry.hasConflict() |
| 121 | + if got != tt.expected { |
| 122 | + t.Errorf("hasConflict() = %v, want %v", got, tt.expected) |
| 123 | + } |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// TestNameConflictsBasic verifies nameConflicts() works for simple cases. |
| 129 | +func TestNameConflictsBasic(t *testing.T) { |
| 130 | + tests := []struct { |
| 131 | + name string |
| 132 | + entries map[string]*ChangeEntry |
| 133 | + expected map[string]string |
| 134 | + }{ |
| 135 | + { |
| 136 | + name: "no conflicts", |
| 137 | + entries: map[string]*ChangeEntry{ |
| 138 | + "a.txt": {Path: "a.txt"}, |
| 139 | + "b.txt": {Path: "b.txt"}, |
| 140 | + }, |
| 141 | + expected: map[string]string{}, |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "simple file/directory conflict", |
| 145 | + entries: map[string]*ChangeEntry{ |
| 146 | + "a": {Path: "a"}, |
| 147 | + "a/b": {Path: "a/b"}, |
| 148 | + "c.txt": {Path: "c.txt"}, |
| 149 | + }, |
| 150 | + expected: map[string]string{"a": "a/b"}, |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "multiple conflicts under same prefix (first match recorded)", |
| 154 | + entries: map[string]*ChangeEntry{ |
| 155 | + "dir": {Path: "dir"}, |
| 156 | + "dir/x": {Path: "dir/x"}, |
| 157 | + "dir/y": {Path: "dir/y"}, |
| 158 | + "dir/z": {Path: "dir/z"}, |
| 159 | + "other": {Path: "other"}, |
| 160 | + "other/w": {Path: "other/w"}, |
| 161 | + }, |
| 162 | + // After fix: records the first sorted match for each prefix |
| 163 | + expected: map[string]string{ |
| 164 | + "dir": "dir/x", // first sorted match |
| 165 | + "other": "other/w", // only one match |
| 166 | + }, |
| 167 | + }, |
| 168 | + } |
| 169 | + |
| 170 | + for _, tt := range tests { |
| 171 | + t.Run(tt.name, func(t *testing.T) { |
| 172 | + d := &differences{ |
| 173 | + entries: tt.entries, |
| 174 | + renames: make(map[string]*RenameEntry), |
| 175 | + ours: make(map[string]bool), |
| 176 | + theirs: make(map[string]bool), |
| 177 | + } |
| 178 | + got := d.nameConflicts() |
| 179 | + if len(got) != len(tt.expected) { |
| 180 | + t.Errorf("nameConflicts() returned %d entries, want %d\n got: %v\n want: %v", |
| 181 | + len(got), len(tt.expected), got, tt.expected) |
| 182 | + return |
| 183 | + } |
| 184 | + for k, v := range tt.expected { |
| 185 | + if got[k] != v { |
| 186 | + t.Errorf("nameConflicts()[%q] = %q, want %q", k, got[k], v) |
| 187 | + } |
| 188 | + } |
| 189 | + }) |
| 190 | + } |
| 191 | +} |
0 commit comments