-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconflict_test.go
More file actions
452 lines (345 loc) · 15.1 KB
/
conflict_test.go
File metadata and controls
452 lines (345 loc) · 15.1 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// TestConflictDetectionBasic tests basic conflict detection scenarios
func TestConflictDetectionBasic(t *testing.T) {
scenario := createTestScenario(t, "conflict_basic")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Create initial commit
initialContent := "line 1\nline 2\nline 3\n"
oldCommitHash := scenario.commitFile(scenario.filename, initialContent, "Initial commit")
// Modify the same line in a new commit
modifiedContent := "line 1\nmodified line 2\nline 3\n"
scenario.commitFile(scenario.filename, modifiedContent, "Modify line 2")
// Now modify line 2 again (unstaged)
finalContent := "line 1\nfurther modified line 2\nline 3\n"
scenario.modifyFile(scenario.filename, finalContent)
// Test conflict detection for line that was modified since original commit
hasConflict, reason := analyzeConflictRisk(scenario.filename, oldCommitHash, []int{2})
if !hasConflict {
t.Error("Expected conflict risk for line modified since original commit")
}
if !strings.Contains(reason, "modified since original commit") {
t.Errorf("Expected reason about line modification, got: %s", reason)
}
}
// TestConflictDetectionDistance tests conflict detection for distant commits
func TestConflictDetectionDistance(t *testing.T) {
scenario := createTestScenario(t, "conflict_distance")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Create initial commit
initialContent := "function main() {\n return 0;\n}\n"
oldCommitHash := scenario.commitFile(scenario.filename, initialContent, "Add main function")
// Create many commits to make the original distant
content := initialContent
for i := 0; i < 25; i++ {
content = fmt.Sprintf("function main() {\n return 0;\n}\n\n// Comment %d\n", i)
scenario.commitFile(scenario.filename, content, fmt.Sprintf("Add comment %d", i))
}
// Modify the original line
finalContent := "function main() {\n return 1; // changed return value\n}\n\n// Comment 24\n"
scenario.modifyFile(scenario.filename, finalContent)
// Test conflict detection for distant commit
hasConflict, reason := analyzeConflictRisk(scenario.filename, oldCommitHash, []int{2})
if !hasConflict {
t.Error("Expected conflict risk for distant commit")
}
if !strings.Contains(reason, "distant") && !strings.Contains(reason, "context may have changed") {
t.Errorf("Expected distance-related reason, got: %s", reason)
}
}
// TestConflictDetectionMergeCommits tests conflict detection with merge commits
func TestConflictDetectionMergeCommits(t *testing.T) {
scenario := createTestScenario(t, "conflict_merge")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Create initial commit
initialContent := "class MyClass {\n constructor() {}\n}\n"
oldCommitHash := scenario.commitFile(scenario.filename, initialContent, "Add MyClass")
// Create a feature branch
scenario.runGitCommand("checkout", "-b", "feature")
featureContent := "class MyClass {\n constructor() {}\n \n method() {\n return true;\n }\n}\n"
scenario.commitFile(scenario.filename, featureContent, "Add method to MyClass")
// Go back to main and make a different change (non-conflicting)
scenario.runGitCommand("checkout", "main")
mainContent := "// Added comment on main branch\nclass MyClass {\n constructor() {}\n}\n"
scenario.commitFile(scenario.filename, mainContent, "Add comment to MyClass")
// Merge feature branch (this creates a merge commit)
scenario.runGitCommand("merge", "feature", "--no-ff", "-m", "Merge feature branch")
// Now modify original constructor line
finalContent := strings.Replace(scenario.getFileContent(), "constructor() {}", "constructor(name) { this.name = name; }", 1)
scenario.modifyFile(scenario.filename, finalContent)
// Test conflict detection with merge commits present
hasConflict, reason := analyzeConflictRisk(scenario.filename, oldCommitHash, []int{2})
if !hasConflict {
t.Error("Expected conflict risk due to merge commits")
}
if !strings.Contains(reason, "merge") {
t.Errorf("Expected merge-related reason, got: %s", reason)
}
}
// TestConflictDetectionNoConflict tests scenarios that should not trigger conflicts
func TestConflictDetectionNoConflict(t *testing.T) {
scenario := createTestScenario(t, "conflict_none")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Create initial commit
initialContent := "line 1\nline 2\nline 3\nline 4\n"
oldCommitHash := scenario.commitFile(scenario.filename, initialContent, "Initial commit")
// Make a few more commits but don't touch the same lines
scenario.commitFile(scenario.filename, initialContent+"line 5\n", "Add line 5")
scenario.commitFile(scenario.filename, initialContent+"line 5\nline 6\n", "Add line 6")
// Modify a line from the original commit
finalContent := "modified line 1\nline 2\nline 3\nline 4\nline 5\nline 6\n"
scenario.modifyFile(scenario.filename, finalContent)
// Test conflict detection - should be low risk
hasConflict, reason := analyzeConflictRisk(scenario.filename, oldCommitHash, []int{1})
if hasConflict {
t.Errorf("Expected no conflict risk for recent commit without modifications, got: %s", reason)
}
}
// TestConflictDetectionMultipleLines tests conflict detection for multiple lines
func TestConflictDetectionMultipleLines(t *testing.T) {
scenario := createTestScenario(t, "conflict_multiline")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Create initial commit
initialContent := "line 1\nline 2\nline 3\nline 4\nline 5\n"
oldCommitHash := scenario.commitFile(scenario.filename, initialContent, "Initial commit")
// Modify some lines in a new commit
intermediateContent := "line 1\nmodified line 2\nline 3\nmodified line 4\nline 5\n"
scenario.commitFile(scenario.filename, intermediateContent, "Modify lines 2 and 4")
// Now modify all original lines
finalContent := "new line 1\nnew line 2\nnew line 3\nnew line 4\nnew line 5\n"
scenario.modifyFile(scenario.filename, finalContent)
// Test conflict detection for multiple lines
hasConflict, reason := analyzeConflictRisk(scenario.filename, oldCommitHash, []int{1, 2, 3, 4, 5})
if !hasConflict {
t.Error("Expected conflict risk for lines modified since original commit")
}
if !strings.Contains(reason, "modified since original commit") {
t.Errorf("Expected modification-related reason, got: %s", reason)
}
}
// TestConflictDetectionEdgeCases tests edge cases in conflict detection
func TestConflictDetectionEdgeCases(t *testing.T) {
t.Run("invalid commit hash", func(t *testing.T) {
scenario := createTestScenario(t, "conflict_invalid_hash")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
scenario.commitFile(scenario.filename, "content\n", "Initial commit")
// Test with invalid commit hash - should not crash
hasConflict, _ := analyzeConflictRisk(scenario.filename, "invalidhash123", []int{1})
// Should handle error gracefully and return false
if hasConflict {
t.Error("Expected no conflict risk for invalid commit hash")
}
})
t.Run("nonexistent file", func(t *testing.T) {
scenario := createTestScenario(t, "conflict_nonexistent")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
commitHash := scenario.commitFile(scenario.filename, "content\n", "Initial commit")
// Test with nonexistent file - should handle gracefully
hasConflict, _ := analyzeConflictRisk("nonexistent.txt", commitHash, []int{1})
// Should handle error gracefully
if hasConflict {
t.Error("Expected no conflict risk for nonexistent file")
}
})
t.Run("empty lines list", func(t *testing.T) {
scenario := createTestScenario(t, "conflict_empty_lines")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
commitHash := scenario.commitFile(scenario.filename, "content\n", "Initial commit")
// Test with empty lines list
hasConflict, _ := analyzeConflictRisk(scenario.filename, commitHash, []int{})
// Should not detect conflict for no lines
if hasConflict {
t.Error("Expected no conflict risk for empty lines list")
}
})
t.Run("line beyond file bounds", func(t *testing.T) {
scenario := createTestScenario(t, "conflict_bounds")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Create a short file
commitHash := scenario.commitFile(scenario.filename, "line 1\nline 2\n", "Initial commit")
// Test with line number beyond file bounds
hasConflict, _ := analyzeConflictRisk(scenario.filename, commitHash, []int{100})
// Should handle gracefully
if hasConflict {
t.Error("Expected no conflict risk for line beyond file bounds")
}
})
}
// TestConflictDetectionRealWorldScenarios tests realistic conflict scenarios
func TestConflictDetectionRealWorldScenarios(t *testing.T) {
t.Run("refactoring scenario", func(t *testing.T) {
scenario := createTestScenario(t, "conflict_refactor")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Create initial code
initialCode := `package main
import "fmt"
func main() {
fmt.Println("Hello World")
processData()
}
func processData() {
data := "test"
fmt.Println(data)
}`
oldCommitHash := scenario.commitFile(scenario.filename, initialCode, "Initial implementation")
// Refactor: Extract variable, rename function
refactoredCode := `package main
import "fmt"
func main() {
message := "Hello World"
fmt.Println(message)
handleData()
}
func handleData() {
data := "test"
fmt.Println(data)
}`
scenario.commitFile(scenario.filename, refactoredCode, "Refactor code")
// Now fix a bug in original function
fixedCode := strings.Replace(refactoredCode, `fmt.Println("Hello World")`, `fmt.Println("Hello, World!")`, 1)
scenario.modifyFile(scenario.filename, fixedCode)
// Should detect potential conflict due to refactoring
hasConflict, reason := analyzeConflictRisk(scenario.filename, oldCommitHash, []int{6})
if !hasConflict {
t.Error("Expected conflict risk for refactored code")
}
if !strings.Contains(reason, "modified since original commit") {
t.Errorf("Expected modification-related reason, got: %s", reason)
}
})
t.Run("hotfix on old release", func(t *testing.T) {
scenario := createTestScenario(t, "conflict_hotfix")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Simulate old release commit
releaseCode := `const VERSION = "1.0.0"
const DEBUG = false
function main() {
if (DEBUG) console.log("Debug mode")
console.log("App version: " + VERSION)
}`
oldReleaseHash := scenario.commitFile(scenario.filename, releaseCode, "Release 1.0.0")
// Many commits simulating ongoing development
currentCode := releaseCode
for i := 1; i <= 30; i++ {
currentCode = fmt.Sprintf(`const VERSION = "1.0.%d"
const DEBUG = false
function main() {
if (DEBUG) console.log("Debug mode")
console.log("App version: " + VERSION)
// Feature %d
}`, i, i)
scenario.commitFile(scenario.filename, currentCode, fmt.Sprintf("Development iteration %d", i))
}
// Now need to hotfix the old release (change DEBUG to true)
hotfixCode := strings.Replace(currentCode, "const DEBUG = false", "const DEBUG = true", 1)
scenario.modifyFile(scenario.filename, hotfixCode)
// Should detect high conflict risk due to distance
hasConflict, reason := analyzeConflictRisk(scenario.filename, oldReleaseHash, []int{2})
if !hasConflict {
t.Error("Expected conflict risk for hotfix on old release")
}
if !strings.Contains(reason, "distant") {
t.Errorf("Expected distance-related reason, got: %s", reason)
}
})
}
// TestConflictDetectionPerformance tests performance of conflict detection
func TestConflictDetectionPerformance(t *testing.T) {
if testing.Short() {
t.Skip("Skipping performance test in short mode")
}
scenario := createTestScenario(t, "conflict_perf")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Create a large file
var largeContent strings.Builder
for i := 1; i <= 1000; i++ {
largeContent.WriteString(fmt.Sprintf("line %d content here\n", i))
}
oldCommitHash := scenario.commitFile(scenario.filename, largeContent.String(), "Large initial file")
// Add some commits
for i := 0; i < 10; i++ {
largeContent.WriteString(fmt.Sprintf("additional line %d\n", i))
scenario.commitFile(scenario.filename, largeContent.String(), fmt.Sprintf("Add line %d", i))
}
// Modify many lines
modifiedContent := strings.ReplaceAll(largeContent.String(), "line ", "modified line ")
scenario.modifyFile(scenario.filename, modifiedContent)
// Create a large list of line numbers
var lines []int
for i := 1; i <= 500; i++ {
lines = append(lines, i)
}
// Measure performance
start := time.Now()
hasConflict, reason := analyzeConflictRisk(scenario.filename, oldCommitHash, lines)
duration := time.Since(start)
t.Logf("Conflict analysis for %d lines took %v", len(lines), duration)
t.Logf("Conflict detected: %v, Reason: %s", hasConflict, reason)
// Should complete within reasonable time
if duration > 10*time.Second {
t.Errorf("Conflict detection took too long: %v", duration)
}
}
// Helper method for TestScenario to get file content
func (s *TestScenario) getFileContent() string {
s.t.Helper()
content, err := os.ReadFile(filepath.Join(s.dir, s.filename))
if err != nil {
s.t.Fatal("Failed to read file content:", err)
}
return string(content)
}
// BenchmarkConflictDetection benchmarks the conflict detection function
func BenchmarkConflictDetection(b *testing.B) {
scenario := createTestScenario(&testing.T{}, "bench_conflict")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Setup test scenario
initialContent := strings.Repeat("line content\n", 100)
oldCommitHash := scenario.commitFile(scenario.filename, initialContent, "Initial commit")
// Add a few commits
for i := 0; i < 5; i++ {
content := initialContent + fmt.Sprintf("extra line %d\n", i)
scenario.commitFile(scenario.filename, content, fmt.Sprintf("Commit %d", i))
}
// Modify file
modifiedContent := strings.ReplaceAll(initialContent+"extra line 4\n", "line content", "modified content")
scenario.modifyFile(scenario.filename, modifiedContent)
lines := []int{1, 10, 20, 30, 40, 50}
b.ResetTimer()
for i := 0; i < b.N; i++ {
analyzeConflictRisk(scenario.filename, oldCommitHash, lines)
}
}