-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaging_test.go
More file actions
505 lines (394 loc) · 14.3 KB
/
staging_test.go
File metadata and controls
505 lines (394 loc) · 14.3 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
494
495
496
497
498
499
500
501
502
503
504
505
package main
import (
"fmt"
"strings"
"testing"
"time"
)
// TestStagingBasicFunctionality tests basic line staging functionality
func TestStagingBasicFunctionality(t *testing.T) {
scenario := createTestScenario(t, "staging_basic")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Create initial commit
initialContent := "line 1\nline 2\nline 3\nline 4\nline 5\n"
scenario.commitFile(scenario.filename, initialContent, "Initial commit")
// Modify multiple lines
modifiedContent := "modified line 1\nmodified line 2\nline 3\nmodified line 4\nline 5\n"
scenario.modifyFile(scenario.filename, modifiedContent)
// Stage only lines 1 and 4
targetLines := []int{1, 4}
err := stageLines(scenario.filename, targetLines)
if err != nil {
t.Fatal("stageLines failed:", err)
}
// Check staged changes
stagedDiff := scenario.runGitCommand("diff", "--cached", scenario.filename)
if stagedDiff == "" {
t.Error("Expected staged changes but found none")
}
// Verify correct lines are staged
if !strings.Contains(stagedDiff, "modified line 1") {
t.Error("Line 1 should be staged")
}
if !strings.Contains(stagedDiff, "modified line 4") {
t.Error("Line 4 should be staged")
}
// Verify line 2 is not staged (should still be unstaged)
unstagedDiff := scenario.runGitCommand("diff", scenario.filename)
if !strings.Contains(unstagedDiff, "modified line 2") {
t.Error("Line 2 should remain unstaged")
}
}
// TestStagingHunkAnalysis tests hunk analysis and response generation
// TestStagingComplexHunks tests staging with complex diff patterns
func TestStagingComplexHunks(t *testing.T) {
scenario := createTestScenario(t, "staging_complex")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Create initial code-like content
initialContent := `function calculate(a, b) {
let result = a + b;
console.log("Calculating");
return result;
}
function display(value) {
console.log("Displaying: " + value);
}
function main() {
let x = 5;
let y = 10;
let sum = calculate(x, y);
display(sum);
}`
scenario.commitFile(scenario.filename, initialContent, "Initial functions")
// Make changes that create overlapping and complex hunks
modifiedContent := `function calculate(a, b, debug = false) {
let result = a + b;
if (debug) console.log("Calculating: " + a + " + " + b);
return result;
}
function display(value) {
console.log("Result: " + value);
}
function main() {
let x = 5;
let y = 10;
let sum = calculate(x, y, true);
display(sum);
console.log("Done");
}`
scenario.modifyFile(scenario.filename, modifiedContent)
// Test staging specific changes
targetLines := []int{1, 3, 14} // Function signature, debug log, and call with debug
// Test staging with new implementation
err := stageLines(scenario.filename, targetLines)
if err != nil {
t.Fatal("stageLines failed:", err)
}
// Test staging the target lines
err = stageLines(scenario.filename, targetLines)
if err != nil {
t.Fatal("stageLines failed:", err)
}
// Verify some changes are staged
stagedDiff := scenario.runGitCommand("diff", "--cached", scenario.filename)
if stagedDiff == "" {
t.Error("Expected staged changes")
}
}
// TestStagingEdgeCases tests edge cases in staging functionality
func TestStagingEdgeCases(t *testing.T) {
t.Run("empty file", func(t *testing.T) {
scenario := createTestScenario(t, "staging_empty")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Create empty file
scenario.commitFile(scenario.filename, "", "Empty file")
// Add content
scenario.modifyFile(scenario.filename, "first line\nsecond line\n")
// Stage specific line
err := stageLines(scenario.filename, []int{1})
if err != nil {
t.Fatal("stageLines failed for new file:", err)
}
// Verify staging worked
stagedDiff := scenario.runGitCommand("diff", "--cached", scenario.filename)
if !strings.Contains(stagedDiff, "first line") {
t.Error("First line should be staged")
}
})
t.Run("no target lines", func(t *testing.T) {
scenario := createTestScenario(t, "staging_no_targets")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
scenario.commitFile(scenario.filename, "content\n", "Initial")
scenario.modifyFile(scenario.filename, "modified content\n")
// Stage with empty target lines
err := stageLines(scenario.filename, []int{})
if err == nil {
t.Error("Expected error for empty target lines")
}
})
t.Run("line numbers out of range", func(t *testing.T) {
scenario := createTestScenario(t, "staging_out_of_range")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
scenario.commitFile(scenario.filename, "line 1\nline 2\n", "Initial")
scenario.modifyFile(scenario.filename, "modified line 1\nmodified line 2\n")
// Try to stage line that doesn't exist
err := stageLines(scenario.filename, []int{100})
// Should handle gracefully (git add -p will ignore non-existent hunks)
if err != nil {
t.Logf("stageLines with out-of-range line returned error (acceptable): %v", err)
}
})
t.Run("no actual changes", func(t *testing.T) {
scenario := createTestScenario(t, "staging_no_changes")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
scenario.commitFile(scenario.filename, "content\n", "Initial")
// Try to stage lines when there are no changes
err := stageLines(scenario.filename, []int{1})
if err == nil {
t.Error("Expected error when trying to stage unchanged file")
}
})
}
// TestStagingReset tests that staging resets work correctly
func TestStagingReset(t *testing.T) {
scenario := createTestScenario(t, "staging_reset")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
scenario.commitFile(scenario.filename, "line 1\nline 2\n", "Initial commit")
scenario.modifyFile(scenario.filename, "modified line 1\nmodified line 2\n")
// Manually stage all changes first
scenario.runGitCommand("add", scenario.filename)
// Verify something is staged
stagedDiff := scenario.runGitCommand("diff", "--cached", scenario.filename)
if stagedDiff == "" {
t.Fatal("Expected staged changes for test setup")
}
// Now use stageLines which should reset and then stage selectively
err := stageLines(scenario.filename, []int{1})
if err != nil {
t.Fatal("stageLines failed:", err)
}
// Verify only line 1 is staged
stagedDiff = scenario.runGitCommand("diff", "--cached", scenario.filename)
unstagedDiff := scenario.runGitCommand("diff", scenario.filename)
if !strings.Contains(stagedDiff, "modified line 1") {
t.Error("Line 1 should be staged")
}
if !strings.Contains(unstagedDiff, "modified line 2") {
t.Error("Line 2 should be unstaged")
}
}
// TestStagingLargeFiles tests staging with large files
func TestStagingLargeFiles(t *testing.T) {
if testing.Short() {
t.Skip("Skipping large file test in short mode")
}
scenario := createTestScenario(t, "staging_large")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Create a large file
var content strings.Builder
for i := 1; i <= 1000; i++ {
content.WriteString(fmt.Sprintf("line %d with some content to make it longer\n", i))
}
scenario.commitFile(scenario.filename, content.String(), "Large file")
// Modify several lines scattered throughout
modifiedContent := content.String()
modifiedContent = strings.Replace(modifiedContent, "line 50 ", "MODIFIED line 50 ", 1)
modifiedContent = strings.Replace(modifiedContent, "line 200 ", "MODIFIED line 200 ", 1)
modifiedContent = strings.Replace(modifiedContent, "line 500 ", "MODIFIED line 500 ", 1)
modifiedContent = strings.Replace(modifiedContent, "line 800 ", "MODIFIED line 800 ", 1)
scenario.modifyFile(scenario.filename, modifiedContent)
// Stage specific lines
targetLines := []int{50, 500}
start := time.Now()
err := stageLines(scenario.filename, targetLines)
duration := time.Since(start)
if err != nil {
t.Fatal("stageLines failed for large file:", err)
}
t.Logf("Staging large file took %v", duration)
// Verify correct staging
stagedDiff := scenario.runGitCommand("diff", "--cached", scenario.filename)
unstagedDiff := scenario.runGitCommand("diff", scenario.filename)
if !strings.Contains(stagedDiff, "MODIFIED line 50") {
t.Error("Line 50 should be staged")
}
if !strings.Contains(stagedDiff, "MODIFIED line 500") {
t.Error("Line 500 should be staged")
}
if !strings.Contains(unstagedDiff, "MODIFIED line 200") {
t.Error("Line 200 should remain unstaged")
}
if !strings.Contains(unstagedDiff, "MODIFIED line 800") {
t.Error("Line 800 should remain unstaged")
}
}
// TestStagingRealWorldScenarios tests realistic staging scenarios
func TestStagingRealWorldScenarios(t *testing.T) {
t.Run("code formatting fix", func(t *testing.T) {
scenario := createTestScenario(t, "staging_formatting")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Create poorly formatted code
poorCode := `function badFormatting(){
var x=1;
if(x==1){
console.log("bad spacing");
}
return x;
}`
scenario.commitFile(scenario.filename, poorCode, "Initial poorly formatted code")
// Apply formatting fixes
goodCode := `function badFormatting() {
var x = 1;
if (x == 1) {
console.log("good spacing");
}
return x;
}`
scenario.modifyFile(scenario.filename, goodCode)
// Stage only specific formatting fixes (lines 1, 2, 3)
err := stageLines(scenario.filename, []int{1, 2, 3})
if err != nil {
t.Fatal("stageLines failed for formatting:", err)
}
// Verify partial staging
stagedDiff := scenario.runGitCommand("diff", "--cached", scenario.filename)
unstagedDiff := scenario.runGitCommand("diff", scenario.filename)
if stagedDiff == "" {
t.Error("Expected some formatting changes to be staged")
}
if unstagedDiff == "" {
t.Error("Expected some formatting changes to remain unstaged")
}
})
t.Run("bug fix with cleanup", func(t *testing.T) {
scenario := createTestScenario(t, "staging_bugfix")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Create buggy code
buggyCode := `function processData(data) {
if (data = null) { // Bug: assignment instead of comparison
return [];
}
console.log("Processing:", data); // Debug line to remove
return data.map(item => item.value);
}`
scenario.commitFile(scenario.filename, buggyCode, "Initial buggy code")
// Fix bug and clean up
fixedCode := `function processData(data) {
if (data === null) { // Fixed: proper comparison
return [];
}
return data.map(item => item.value);
}`
scenario.modifyFile(scenario.filename, fixedCode)
// Stage only the bug fix (line 2), not the cleanup
err := stageLines(scenario.filename, []int{2})
if err != nil {
t.Fatal("stageLines failed for bug fix:", err)
}
// Verify the fix is staged but cleanup remains unstaged
stagedDiff := scenario.runGitCommand("diff", "--cached", scenario.filename)
unstagedDiff := scenario.runGitCommand("diff", scenario.filename)
// Check that the bug fix (comparison operator) is staged
if !strings.Contains(stagedDiff, "data === null") {
t.Error("Bug fix (comparison operator) should be staged")
}
// Check that the debug line removal is unstaged
// The unstaged diff should show console.log line being removed
if !strings.Contains(unstagedDiff, "-") || !strings.Contains(unstagedDiff, "console.log") {
t.Error("Debug line removal should remain unstaged")
}
})
}
// TestHunkBoundaryDetection tests detection of hunk boundaries
func TestHunkBoundaryDetection(t *testing.T) {
scenario := createTestScenario(t, "hunk_boundaries")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Create content with clear boundaries
initialContent := `// Section A
function a() {
return "a";
}
// Section B
function b() {
return "b";
}
// Section C
function c() {
return "c";
}`
scenario.commitFile(scenario.filename, initialContent, "Three sections")
// Modify each section
modifiedContent := `// Section A
function a() {
return "modified a";
}
// Section B
function b() {
return "modified b";
}
// Section C
function c() {
return "modified c";
}`
scenario.modifyFile(scenario.filename, modifiedContent)
// Test staging all changes
changedLines := []int{3, 8, 13}
err := stageLines(scenario.filename, changedLines)
if err != nil {
t.Fatal("stageLines failed:", err)
}
// Verify staging was successful by checking git status
statusOutput := scenario.runGitCommand("status", "--porcelain")
if !strings.Contains(statusOutput, "M ") {
t.Error("Expected staged changes after stageLines")
}
}
// BenchmarkStaging benchmarks the staging functionality
func BenchmarkStaging(b *testing.B) {
scenario := createTestScenario(&testing.T{}, "bench_staging")
defer scenario.cleanup()
restoreDir := scenario.changeToScenarioDir()
defer restoreDir()
// Create test content
var content strings.Builder
for i := 1; i <= 100; i++ {
content.WriteString(fmt.Sprintf("line %d content\n", i))
}
scenario.commitFile(scenario.filename, content.String(), "Initial content")
// Modify several lines
modifiedContent := strings.ReplaceAll(content.String(), "line ", "modified line ")
scenario.modifyFile(scenario.filename, modifiedContent)
targetLines := []int{10, 20, 30, 40, 50}
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Reset state
scenario.runGitCommand("reset", "HEAD", scenario.filename)
// Benchmark staging
err := stageLines(scenario.filename, targetLines)
if err != nil {
b.Fatal("stageLines failed:", err)
}
}
}