-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
493 lines (437 loc) · 11.1 KB
/
main_test.go
File metadata and controls
493 lines (437 loc) · 11.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package main
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
func writeTestFile(t *testing.T, dir, name, content string) string {
t.Helper()
path := filepath.Join(dir, name)
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatalf("failed to write test file: %v", err)
}
return path
}
func TestBasicConflict(t *testing.T) {
dir := t.TempDir()
content := `line 1
line 2
<<<<<<< HEAD
our change
=======
their change
>>>>>>> feature-branch
line 8
line 9
`
path := writeTestFile(t, dir, "test.txt", content)
config := Config{Files: []string{path}, ContextLines: 1}
result, err := parseConflicts(config)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Total != 1 {
t.Fatalf("expected 1 conflict, got %d", result.Total)
}
c := result.Files[0].Conflicts[0]
if c.Line != 3 {
t.Errorf("expected line 3, got %d", c.Line)
}
if c.EndLine != 7 {
t.Errorf("expected end_line 7, got %d", c.EndLine)
}
if c.OursRef != "HEAD" {
t.Errorf("expected ours_ref HEAD, got %q", c.OursRef)
}
if c.TheirsRef != "feature-branch" {
t.Errorf("expected theirs_ref feature-branch, got %q", c.TheirsRef)
}
if c.Ours != "our change" {
t.Errorf("expected ours %q, got %q", "our change", c.Ours)
}
if c.Theirs != "their change" {
t.Errorf("expected theirs %q, got %q", "their change", c.Theirs)
}
if c.Base != "" {
t.Errorf("expected empty base, got %q", c.Base)
}
if !result.HasDiff3 {
// Should be false for standard conflict
}
if result.HasDiff3 {
t.Error("expected has_diff3 false for standard conflict")
}
}
func TestDiff3Conflict(t *testing.T) {
dir := t.TempDir()
content := `before
<<<<<<< HEAD
our version
||||||| merged common ancestors
original version
=======
their version
>>>>>>> feature
after
`
path := writeTestFile(t, dir, "diff3.txt", content)
config := Config{Files: []string{path}, ContextLines: 1}
result, err := parseConflicts(config)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Total != 1 {
t.Fatalf("expected 1 conflict, got %d", result.Total)
}
if !result.HasDiff3 {
t.Error("expected has_diff3 true")
}
c := result.Files[0].Conflicts[0]
if c.Ours != "our version" {
t.Errorf("expected ours %q, got %q", "our version", c.Ours)
}
if c.Base != "original version" {
t.Errorf("expected base %q, got %q", "original version", c.Base)
}
if c.Theirs != "their version" {
t.Errorf("expected theirs %q, got %q", "their version", c.Theirs)
}
}
func TestMultipleConflicts(t *testing.T) {
dir := t.TempDir()
content := `first section
<<<<<<< HEAD
ours 1
=======
theirs 1
>>>>>>> branch1
middle section
<<<<<<< HEAD
ours 2
=======
theirs 2
>>>>>>> branch2
end section
`
path := writeTestFile(t, dir, "multi.txt", content)
config := Config{Files: []string{path}, ContextLines: 0}
result, err := parseConflicts(config)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Total != 2 {
t.Fatalf("expected 2 conflicts, got %d", result.Total)
}
if result.Files[0].Conflicts[0].Ours != "ours 1" {
t.Errorf("conflict 1 ours: got %q", result.Files[0].Conflicts[0].Ours)
}
if result.Files[0].Conflicts[1].Ours != "ours 2" {
t.Errorf("conflict 2 ours: got %q", result.Files[0].Conflicts[1].Ours)
}
}
func TestMultiLineContent(t *testing.T) {
dir := t.TempDir()
content := `<<<<<<< HEAD
line a
line b
line c
=======
line x
line y
>>>>>>> other
`
path := writeTestFile(t, dir, "multiline.txt", content)
config := Config{Files: []string{path}, ContextLines: 0}
result, err := parseConflicts(config)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
c := result.Files[0].Conflicts[0]
if c.Ours != "line a\nline b\nline c" {
t.Errorf("expected multi-line ours, got %q", c.Ours)
}
if c.Theirs != "line x\nline y" {
t.Errorf("expected multi-line theirs, got %q", c.Theirs)
}
}
func TestContextLines(t *testing.T) {
dir := t.TempDir()
content := `line 1
line 2
line 3
<<<<<<< HEAD
ours
=======
theirs
>>>>>>> branch
line 9
line 10
line 11
`
path := writeTestFile(t, dir, "context.txt", content)
config := Config{Files: []string{path}, ContextLines: 2}
result, err := parseConflicts(config)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
c := result.Files[0].Conflicts[0]
if c.ContextAbove != "line 2\nline 3" {
t.Errorf("expected context_above %q, got %q", "line 2\nline 3", c.ContextAbove)
}
if c.ContextBelow != "line 9\nline 10" {
t.Errorf("expected context_below %q, got %q", "line 9\nline 10", c.ContextBelow)
}
}
func TestZeroContextLines(t *testing.T) {
dir := t.TempDir()
content := `before
<<<<<<< HEAD
ours
=======
theirs
>>>>>>> branch
after
`
path := writeTestFile(t, dir, "zerocontext.txt", content)
config := Config{Files: []string{path}, ContextLines: 0}
result, err := parseConflicts(config)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
c := result.Files[0].Conflicts[0]
if c.ContextAbove != "" {
t.Errorf("expected empty context_above with 0 context lines, got %q", c.ContextAbove)
}
if c.ContextBelow != "" {
t.Errorf("expected empty context_below with 0 context lines, got %q", c.ContextBelow)
}
}
func TestNoConflicts(t *testing.T) {
dir := t.TempDir()
content := `just a normal file
with no conflicts
at all
`
path := writeTestFile(t, dir, "clean.txt", content)
config := Config{Files: []string{path}, ContextLines: 1}
result, err := parseConflicts(config)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Total != 0 {
t.Errorf("expected 0 conflicts, got %d", result.Total)
}
if !strings.Contains(result.Summary, "0 conflicts") {
t.Errorf("summary should mention 0 conflicts: %q", result.Summary)
}
}
func TestEmptyOursSide(t *testing.T) {
dir := t.TempDir()
content := `<<<<<<< HEAD
=======
their addition
>>>>>>> branch
`
path := writeTestFile(t, dir, "emptyours.txt", content)
config := Config{Files: []string{path}, ContextLines: 0}
result, err := parseConflicts(config)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
c := result.Files[0].Conflicts[0]
if c.Ours != "" {
t.Errorf("expected empty ours, got %q", c.Ours)
}
if c.Theirs != "their addition" {
t.Errorf("expected theirs %q, got %q", "their addition", c.Theirs)
}
}
func TestEmptyTheirsSide(t *testing.T) {
dir := t.TempDir()
content := `<<<<<<< HEAD
our content
=======
>>>>>>> branch
`
path := writeTestFile(t, dir, "emptytheirs.txt", content)
config := Config{Files: []string{path}, ContextLines: 0}
result, err := parseConflicts(config)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
c := result.Files[0].Conflicts[0]
if c.Ours != "our content" {
t.Errorf("expected ours %q, got %q", "our content", c.Ours)
}
if c.Theirs != "" {
t.Errorf("expected empty theirs, got %q", c.Theirs)
}
}
func TestMultipleFiles(t *testing.T) {
dir := t.TempDir()
content1 := `<<<<<<< HEAD
a
=======
b
>>>>>>> branch
`
content2 := `<<<<<<< HEAD
x
=======
y
>>>>>>> branch
`
path1 := writeTestFile(t, dir, "file1.txt", content1)
path2 := writeTestFile(t, dir, "file2.txt", content2)
config := Config{Files: []string{path1, path2}, ContextLines: 0}
result, err := parseConflicts(config)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Total != 2 {
t.Fatalf("expected 2 total conflicts, got %d", result.Total)
}
if len(result.Files) != 2 {
t.Fatalf("expected 2 file results, got %d", len(result.Files))
}
}
func TestCRLFLineEndings(t *testing.T) {
dir := t.TempDir()
content := "before\r\n<<<<<<< HEAD\r\nours\r\n=======\r\ntheirs\r\n>>>>>>> branch\r\nafter\r\n"
path := writeTestFile(t, dir, "crlf.txt", content)
config := Config{Files: []string{path}, ContextLines: 1}
result, err := parseConflicts(config)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Total != 1 {
t.Fatalf("expected 1 conflict, got %d", result.Total)
}
c := result.Files[0].Conflicts[0]
if c.Ours != "ours" {
t.Errorf("expected ours %q, got %q", "ours", c.Ours)
}
if c.Theirs != "theirs" {
t.Errorf("expected theirs %q, got %q", "theirs", c.Theirs)
}
}
func TestEqualsInContent(t *testing.T) {
dir := t.TempDir()
// Content with ======== (more than 7 =) should not be treated as separator
content := `<<<<<<< HEAD
x = 1
========
some other thing
=======
y = 2
>>>>>>> branch
`
path := writeTestFile(t, dir, "equals.txt", content)
config := Config{Files: []string{path}, ContextLines: 0}
result, err := parseConflicts(config)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Total != 1 {
t.Fatalf("expected 1 conflict, got %d", result.Total)
}
c := result.Files[0].Conflicts[0]
if c.Ours != "x = 1\n========\nsome other thing" {
t.Errorf("ours content wrong: %q", c.Ours)
}
if c.Theirs != "y = 2" {
t.Errorf("theirs content wrong: %q", c.Theirs)
}
}
func TestJSONOutput(t *testing.T) {
dir := t.TempDir()
content := `<<<<<<< HEAD
ours
=======
theirs
>>>>>>> branch
`
path := writeTestFile(t, dir, "json.txt", content)
config := Config{Files: []string{path}, ContextLines: 0}
result, err := parseConflicts(config)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
output, err := json.Marshal(result)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
var parsed Result
if err := json.Unmarshal(output, &parsed); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if parsed.Total != 1 {
t.Errorf("round-trip total: expected 1, got %d", parsed.Total)
}
}
func TestNonexistentFile(t *testing.T) {
config := Config{Files: []string{"/nonexistent/file.txt"}, ContextLines: 0}
_, err := parseConflicts(config)
if err == nil {
t.Fatal("expected error for nonexistent file")
}
}
func TestIsConflictMarker(t *testing.T) {
tests := []struct {
line string
expect bool
}{
{"<<<<<<< HEAD", true},
{"<<<<<<< branch-name", true},
{"=======", true},
{">>>>>>> branch", true},
{"||||||| base", true},
{"========", false},
{"<<<<<<<", true},
{"just normal text", false},
{" <<<<<<< indented", false},
}
for _, tt := range tests {
got := isConflictMarker(tt.line)
if got != tt.expect {
t.Errorf("isConflictMarker(%q) = %v, want %v", tt.line, got, tt.expect)
}
}
}
func TestContextStopsAtConflictMarker(t *testing.T) {
dir := t.TempDir()
// Two conflicts back-to-back with only one line between them
content := `<<<<<<< HEAD
a
=======
b
>>>>>>> branch1
middle line
<<<<<<< HEAD
c
=======
d
>>>>>>> branch2
`
path := writeTestFile(t, dir, "adjacent.txt", content)
config := Config{Files: []string{path}, ContextLines: 3}
result, err := parseConflicts(config)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Total != 2 {
t.Fatalf("expected 2 conflicts, got %d", result.Total)
}
// First conflict's context_below should be "middle line", not the next conflict marker
c1 := result.Files[0].Conflicts[0]
if c1.ContextBelow != "middle line" {
t.Errorf("conflict 1 context_below: expected %q, got %q", "middle line", c1.ContextBelow)
}
// Second conflict's context_above should be "middle line", not the previous conflict marker
c2 := result.Files[0].Conflicts[1]
if c2.ContextAbove != "middle line" {
t.Errorf("conflict 2 context_above: expected %q, got %q", "middle line", c2.ContextAbove)
}
}