|
| 1 | +package dangerJs |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +// parseDiffContent extracts the diff parsing logic for testing |
| 12 | +func parseDiffContent(diffContent string) FileDiff { |
| 13 | + var fileDiff FileDiff |
| 14 | + // Only match lines that start with + or - but not +++ or --- (file headers) |
| 15 | + addedRe := regexp.MustCompile(`^\+([^+].*|$)`) |
| 16 | + removedRe := regexp.MustCompile(`^-([^-].*|$)`) |
| 17 | + |
| 18 | + lines := strings.Split(diffContent, "\n") |
| 19 | + for _, line := range lines { |
| 20 | + if matches := addedRe.FindStringSubmatch(line); len(matches) > 1 { |
| 21 | + fileDiff.AddedLines = append(fileDiff.AddedLines, DiffLine{Content: matches[1]}) |
| 22 | + } else if matches := removedRe.FindStringSubmatch(line); len(matches) > 1 { |
| 23 | + fileDiff.RemovedLines = append(fileDiff.RemovedLines, DiffLine{Content: matches[1]}) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + return fileDiff |
| 28 | +} |
| 29 | + |
| 30 | +func TestParseDiffContent(t *testing.T) { |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + gitDiffOutput string |
| 34 | + wantFileDiff FileDiff |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "basic added and removed lines", |
| 38 | + gitDiffOutput: `diff --git a/test.go b/test.go |
| 39 | +index 123..456 100644 |
| 40 | +--- a/test.go |
| 41 | ++++ b/test.go |
| 42 | +@@ -1 +1 @@ |
| 43 | +-func oldFunction() { |
| 44 | ++func newFunction() { |
| 45 | +@@ -5 +5,2 @@ |
| 46 | ++ return "added line" |
| 47 | +- fmt.Println("removed line")`, |
| 48 | + wantFileDiff: FileDiff{ |
| 49 | + AddedLines: []DiffLine{ |
| 50 | + {Content: "func newFunction() {", Line: 0}, |
| 51 | + {Content: "\treturn \"added line\"", Line: 0}, |
| 52 | + }, |
| 53 | + RemovedLines: []DiffLine{ |
| 54 | + {Content: "func oldFunction() {", Line: 0}, |
| 55 | + {Content: "\tfmt.Println(\"removed line\")", Line: 0}, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "only added lines", |
| 61 | + gitDiffOutput: `diff --git a/new.go b/new.go |
| 62 | +index 123..456 100644 |
| 63 | +--- a/new.go |
| 64 | ++++ b/new.go |
| 65 | +@@ -1,0 +1,3 @@ |
| 66 | ++package main |
| 67 | ++ |
| 68 | ++func main() {}`, |
| 69 | + wantFileDiff: FileDiff{ |
| 70 | + AddedLines: []DiffLine{ |
| 71 | + {Content: "package main", Line: 0}, |
| 72 | + {Content: "", Line: 0}, |
| 73 | + {Content: "func main() {}", Line: 0}, |
| 74 | + }, |
| 75 | + RemovedLines: nil, |
| 76 | + }, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "only removed lines", |
| 80 | + gitDiffOutput: `diff --git a/old.go b/old.go |
| 81 | +index 123..456 100644 |
| 82 | +--- a/old.go |
| 83 | ++++ b/old.go |
| 84 | +@@ -1,3 +0,0 @@ |
| 85 | +-package main |
| 86 | +- |
| 87 | +-func old() {}`, |
| 88 | + wantFileDiff: FileDiff{ |
| 89 | + AddedLines: nil, |
| 90 | + RemovedLines: []DiffLine{ |
| 91 | + {Content: "package main", Line: 0}, |
| 92 | + {Content: "", Line: 0}, |
| 93 | + {Content: "func old() {}", Line: 0}, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "no changes", |
| 99 | + gitDiffOutput: ``, |
| 100 | + wantFileDiff: FileDiff{ |
| 101 | + AddedLines: nil, |
| 102 | + RemovedLines: nil, |
| 103 | + }, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "complex diff with context lines", |
| 107 | + gitDiffOutput: `diff --git a/complex.go b/complex.go |
| 108 | +index 123..456 100644 |
| 109 | +--- a/complex.go |
| 110 | ++++ b/complex.go |
| 111 | +@@ -10,5 +10,6 @@ |
| 112 | + unchanged line 1 |
| 113 | + unchanged line 2 |
| 114 | +- old implementation |
| 115 | ++ new implementation |
| 116 | ++ additional line |
| 117 | + unchanged line 3`, |
| 118 | + wantFileDiff: FileDiff{ |
| 119 | + AddedLines: []DiffLine{ |
| 120 | + {Content: "\tnew implementation", Line: 0}, |
| 121 | + {Content: "\tadditional line", Line: 0}, |
| 122 | + }, |
| 123 | + RemovedLines: []DiffLine{ |
| 124 | + {Content: "\told implementation", Line: 0}, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "lines with special characters and whitespace", |
| 130 | + gitDiffOutput: `diff --git a/special.go b/special.go |
| 131 | +index 123..456 100644 |
| 132 | +--- a/special.go |
| 133 | ++++ b/special.go |
| 134 | +@@ -1,2 +1,2 @@ |
| 135 | +- fmt.Printf("Hello %s\n", name) |
| 136 | ++ fmt.Printf("Hi %s!\n", name)`, |
| 137 | + wantFileDiff: FileDiff{ |
| 138 | + AddedLines: []DiffLine{ |
| 139 | + {Content: "\tfmt.Printf(\"Hi %s!\\n\", name)", Line: 0}, |
| 140 | + }, |
| 141 | + RemovedLines: []DiffLine{ |
| 142 | + {Content: "\tfmt.Printf(\"Hello %s\\n\", name)", Line: 0}, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "lines with only symbols", |
| 148 | + gitDiffOutput: `diff --git a/symbols.go b/symbols.go |
| 149 | +index 123..456 100644 |
| 150 | +--- a/symbols.go |
| 151 | ++++ b/symbols.go |
| 152 | +@@ -1,2 +1,2 @@ |
| 153 | +-} |
| 154 | ++},`, |
| 155 | + wantFileDiff: FileDiff{ |
| 156 | + AddedLines: []DiffLine{ |
| 157 | + {Content: "},", Line: 0}, |
| 158 | + }, |
| 159 | + RemovedLines: []DiffLine{ |
| 160 | + {Content: "}", Line: 0}, |
| 161 | + }, |
| 162 | + }, |
| 163 | + }, |
| 164 | + { |
| 165 | + name: "empty added and removed lines", |
| 166 | + gitDiffOutput: `diff --git a/empty.go b/empty.go |
| 167 | +index 123..456 100644 |
| 168 | +--- a/empty.go |
| 169 | ++++ b/empty.go |
| 170 | +@@ -1,2 +1,2 @@ |
| 171 | +- |
| 172 | ++ |
| 173 | +- |
| 174 | ++ `, |
| 175 | + wantFileDiff: FileDiff{ |
| 176 | + AddedLines: []DiffLine{ |
| 177 | + {Content: "", Line: 0}, |
| 178 | + {Content: " ", Line: 0}, |
| 179 | + }, |
| 180 | + RemovedLines: []DiffLine{ |
| 181 | + {Content: "", Line: 0}, |
| 182 | + {Content: " ", Line: 0}, |
| 183 | + }, |
| 184 | + }, |
| 185 | + }, |
| 186 | + { |
| 187 | + name: "diff with file headers only", |
| 188 | + gitDiffOutput: `diff --git a/test.go b/test.go |
| 189 | +index 123..456 100644 |
| 190 | +--- a/test.go |
| 191 | ++++ b/test.go`, |
| 192 | + wantFileDiff: FileDiff{ |
| 193 | + AddedLines: nil, |
| 194 | + RemovedLines: nil, |
| 195 | + }, |
| 196 | + }, |
| 197 | + { |
| 198 | + name: "multiple hunks with mixed changes", |
| 199 | + gitDiffOutput: `diff --git a/multi.go b/multi.go |
| 200 | +index 123..456 100644 |
| 201 | +--- a/multi.go |
| 202 | ++++ b/multi.go |
| 203 | +@@ -1,3 +1,4 @@ |
| 204 | + package main |
| 205 | + |
| 206 | ++import "fmt" |
| 207 | + func main() { |
| 208 | +@@ -10,6 +11,7 @@ |
| 209 | + if true { |
| 210 | +- fmt.Println("old") |
| 211 | ++ fmt.Println("new") |
| 212 | ++ fmt.Println("extra") |
| 213 | + } |
| 214 | + }`, |
| 215 | + wantFileDiff: FileDiff{ |
| 216 | + AddedLines: []DiffLine{ |
| 217 | + {Content: "import \"fmt\"", Line: 0}, |
| 218 | + {Content: "\t\tfmt.Println(\"new\")", Line: 0}, |
| 219 | + {Content: "\t\tfmt.Println(\"extra\")", Line: 0}, |
| 220 | + }, |
| 221 | + RemovedLines: []DiffLine{ |
| 222 | + {Content: "\t\tfmt.Println(\"old\")", Line: 0}, |
| 223 | + }, |
| 224 | + }, |
| 225 | + }, |
| 226 | + } |
| 227 | + |
| 228 | + for _, tt := range tests { |
| 229 | + t.Run(tt.name, func(t *testing.T) { |
| 230 | + gotFileDiff := parseDiffContent(tt.gitDiffOutput) |
| 231 | + require.Equal(t, tt.wantFileDiff, gotFileDiff) |
| 232 | + }) |
| 233 | + } |
| 234 | +} |
0 commit comments