-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
448 lines (410 loc) · 10.1 KB
/
main_test.go
File metadata and controls
448 lines (410 loc) · 10.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
package main
import (
"os/exec"
"regexp"
"strconv"
"strings"
"testing"
)
func TestHasChanges(t *testing.T) {
// Skip if not in a git repo
if !isInGitRepo() {
t.Skip("Not in a git repository")
}
tests := []struct {
name string
filename string
description string
}{
{
name: "nonexistent file",
filename: "this_file_does_not_exist.txt",
description: "should return false for nonexistent files",
},
{
name: "current file",
filename: "main.go",
description: "test with actual project file",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Just test that the function doesn't panic and returns a boolean
result := hasChanges(tt.filename)
t.Logf("%s: hasChanges(%q) = %v", tt.description, tt.filename, result)
// We can't predict the exact result since it depends on git state
// but we can verify the function runs without error
})
}
}
func TestGetChangedLines_DiffParsing(t *testing.T) {
tests := []struct {
name string
diffOutput string
expectedLines []int
shouldError bool
}{
{
name: "simple additions",
diffOutput: `diff --git a/test.txt b/test.txt
index 1234567..abcdefg 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
line 1
+new line 2
line 3
+another new line`,
expectedLines: []int{2, 4},
shouldError: false,
},
{
name: "multiple hunks",
diffOutput: `diff --git a/test.txt b/test.txt
index 1234567..abcdefg 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,3 @@
line 1
+added line 2
line 3
@@ -10,2 +11,3 @@
line 10
+added line 11
line 12`,
expectedLines: []int{2, 12},
shouldError: false,
},
{
name: "only context lines",
diffOutput: `diff --git a/test.txt b/test.txt
index 1234567..abcdefg 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,3 @@
line 1
line 2
line 3`,
expectedLines: []int{},
shouldError: false,
},
{
name: "mixed additions and deletions",
diffOutput: `diff --git a/test.txt b/test.txt
index 1234567..abcdefg 100644
--- a/test.txt
+++ b/test.txt
@@ -1,5 +1,5 @@
line 1
-old line 2
+new line 2
line 3
-old line 4
+new line 4
line 5`,
expectedLines: []int{2, 4},
shouldError: false,
},
{
name: "no changes",
diffOutput: "",
expectedLines: []int{},
shouldError: false,
},
{
name: "complex hunk with line number gaps",
diffOutput: `diff --git a/test.txt b/test.txt
index 1234567..abcdefg 100644
--- a/test.txt
+++ b/test.txt
@@ -15,8 +15,10 @@
context line 15
context line 16
+new line 17
context line 17
+new line 18
context line 18
context line 19
+final new line
context line 20`,
expectedLines: []int{17, 19, 22},
shouldError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lines := parseChangedLinesFromDiff(tt.diffOutput)
if len(lines) != len(tt.expectedLines) {
t.Errorf("parseChangedLinesFromDiff() returned %d lines, want %d\nGot: %v\nWant: %v",
len(lines), len(tt.expectedLines), lines, tt.expectedLines)
return
}
for i, line := range lines {
if line != tt.expectedLines[i] {
t.Errorf("parseChangedLinesFromDiff() line %d = %d, want %d", i, line, tt.expectedLines[i])
}
}
})
}
}
func TestFormatLines(t *testing.T) {
tests := []struct {
name string
lines []int
expected string
}{
{
name: "single line",
lines: []int{42},
expected: "42",
},
{
name: "multiple lines",
lines: []int{10, 20, 30},
expected: "10, 20, 30",
},
{
name: "empty slice",
lines: []int{},
expected: "",
},
{
name: "unordered lines",
lines: []int{30, 10, 20},
expected: "30, 10, 20",
},
{
name: "single line zero",
lines: []int{0},
expected: "0",
},
{
name: "large numbers",
lines: []int{1000, 2000, 3000},
expected: "1000, 2000, 3000",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := formatLines(tt.lines)
if result != tt.expected {
t.Errorf("formatLines() = %q, want %q", result, tt.expected)
}
})
}
}
func TestCommitInfoGrouping(t *testing.T) {
tests := []struct {
name string
lines []int
blameOutputs map[int]string // line number -> blame output
expectedCommits []CommitInfo
shouldError bool
}{
{
name: "single commit multiple lines",
lines: []int{10, 20, 30},
blameOutputs: map[int]string{
10: "abc123 author 2023-01-01 line content",
20: "abc123 author 2023-01-01 line content",
30: "abc123 author 2023-01-01 line content",
},
expectedCommits: []CommitInfo{
{
Hash: "abc123",
Lines: []int{10, 20, 30},
},
},
},
{
name: "multiple commits preserving order",
lines: []int{10, 20, 30, 40},
blameOutputs: map[int]string{
10: "abc123 author 2023-01-01 line content",
20: "def456 author 2023-01-02 line content",
30: "abc123 author 2023-01-01 line content",
40: "def456 author 2023-01-02 line content",
},
expectedCommits: []CommitInfo{
{
Hash: "abc123",
Lines: []int{10, 30},
},
{
Hash: "def456",
Lines: []int{20, 40},
},
},
},
{
name: "commits with different hash lengths",
lines: []int{5, 15},
blameOutputs: map[int]string{
5: "a1b2c3d4e5f6 author 2023-01-01 line content",
15: "123abc author 2023-01-02 line content",
},
expectedCommits: []CommitInfo{
{
Hash: "a1b2c3d4e5f6",
Lines: []int{5},
},
{
Hash: "123abc",
Lines: []int{15},
},
},
},
{
name: "single line single commit",
lines: []int{1},
blameOutputs: map[int]string{
1: "abcdef123456 user 2023-05-15 some line content here",
},
expectedCommits: []CommitInfo{
{
Hash: "abcdef123456",
Lines: []int{1},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
commits := simulateCommitGrouping(tt.lines, tt.blameOutputs)
if len(commits) != len(tt.expectedCommits) {
t.Errorf("got %d commits, want %d", len(commits), len(tt.expectedCommits))
return
}
for i, commit := range commits {
expected := tt.expectedCommits[i]
if commit.Hash != expected.Hash {
t.Errorf("commit %d hash = %q, want %q", i, commit.Hash, expected.Hash)
}
if len(commit.Lines) != len(expected.Lines) {
t.Errorf("commit %d has %d lines, want %d", i, len(commit.Lines), len(expected.Lines))
continue
}
for j, line := range commit.Lines {
if line != expected.Lines[j] {
t.Errorf("commit %d line %d = %d, want %d", i, j, line, expected.Lines[j])
}
}
}
})
}
}
func TestAnalyzeConflictRisk_Logic(t *testing.T) {
// Test the conflict risk analysis logic without requiring actual Git repositories
tests := []struct {
name string
commitAge int // commits between target and HEAD
hasMerges bool
expectConflict bool
expectedReason string
}{
{
name: "recent commit no merges",
commitAge: 5,
hasMerges: false,
expectConflict: false,
expectedReason: "",
},
{
name: "distant commit",
commitAge: 25,
hasMerges: false,
expectConflict: true,
expectedReason: "distant",
},
{
name: "recent commit with merges",
commitAge: 5,
hasMerges: true,
expectConflict: true,
expectedReason: "merge",
},
{
name: "distant commit with merges",
commitAge: 30,
hasMerges: true,
expectConflict: true,
expectedReason: "distant", // Should catch distance first
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hasConflict, reason := simulateConflictAnalysis(tt.commitAge, tt.hasMerges)
if hasConflict != tt.expectConflict {
t.Errorf("Expected conflict risk %v, got %v", tt.expectConflict, hasConflict)
}
if tt.expectConflict && !strings.Contains(strings.ToLower(reason), tt.expectedReason) {
t.Errorf("Expected reason to contain %q, got %q", tt.expectedReason, reason)
}
})
}
}
// Helper functions for testing
func isInGitRepo() bool {
command := exec.Command("git", "rev-parse", "--git-dir")
return command.Run() == nil
}
func parseChangedLinesFromDiff(diffOutput string) []int {
// This extracts the core parsing logic from getChangedLines for testing
var changedLines []int
lines := strings.Split(diffOutput, "\n")
hunkRegex := regexp.MustCompile(`^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@`)
var currentLine int
for _, line := range lines {
if matches := hunkRegex.FindStringSubmatch(line); matches != nil {
currentLine, _ = strconv.Atoi(matches[1])
} else if strings.HasPrefix(line, "+") && !strings.HasPrefix(line, "+++") {
changedLines = append(changedLines, currentLine)
currentLine++
} else if strings.HasPrefix(line, " ") {
currentLine++
}
}
return changedLines
}
func simulateCommitGrouping(lines []int, blameOutputs map[int]string) []CommitInfo {
// This simulates the commit grouping logic for testing
lineToCommit := make(map[int]string)
for lineNum, blameOutput := range blameOutputs {
parts := strings.Fields(blameOutput)
if len(parts) > 0 {
commitHash := parts[0]
lineToCommit[lineNum] = commitHash
}
}
// Group lines by commit, preserving order of first appearance
var seenCommits []string
commitToLines := make(map[string][]int)
for _, lineNum := range lines {
if commitHash, exists := lineToCommit[lineNum]; exists {
if _, seen := commitToLines[commitHash]; !seen {
seenCommits = append(seenCommits, commitHash)
commitToLines[commitHash] = []int{}
}
commitToLines[commitHash] = append(commitToLines[commitHash], lineNum)
}
}
// Create result slice
var commits []CommitInfo
for _, commitHash := range seenCommits {
commits = append(commits, CommitInfo{
Hash: commitHash,
Lines: commitToLines[commitHash],
})
}
return commits
}
func simulateConflictAnalysis(commitAge int, hasMerges bool) (bool, string) {
// Simulate the conflict analysis logic for testing
if commitAge > 20 {
return true, "commit is distant, context may have changed"
}
if hasMerges {
return true, "merge commits present, potential context conflicts"
}
return false, ""
}