this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add more tests for git file header parsing

Also fix a bug with EOF handling in the parsing function and make sure
the test does not ignore unexpected errors.

+106 -5
+3 -1
gitdiff/parser.go
··· 137 137 138 138 for { 139 139 line, err := p.PeekLine() 140 - if err != nil { 140 + if err == io.EOF { 141 + break 142 + } else if err != nil { 141 143 return err 142 144 } 143 145
+103 -4
gitdiff/parser_test.go
··· 136 136 Output *File 137 137 Err bool 138 138 }{ 139 - "simpleFileChange": { 139 + "fileContentChange": { 140 140 Input: `diff --git a/dir/file.txt b/dir/file.txt 141 141 index 1c23fcc..40a1b33 100644 142 142 --- a/dir/file.txt 143 143 +++ b/dir/file.txt 144 + @@ -2,3 +4,5 @@ 144 145 `, 145 146 Output: &File{ 146 147 OldName: "dir/file.txt", ··· 150 151 NewOIDPrefix: "40a1b33", 151 152 }, 152 153 }, 153 - "fileCreation": { 154 + "newFile": { 154 155 Input: `diff --git a/dir/file.txt b/dir/file.txt 155 156 new file mode 100644 156 157 index 0000000..f5711e4 ··· 165 166 IsNew: true, 166 167 }, 167 168 }, 169 + "newEmptyFile": { 170 + Input: `diff --git a/empty.txt b/empty.txt 171 + new file mode 100644 172 + index 0000000..e69de29 173 + `, 174 + Output: &File{ 175 + NewName: "empty.txt", 176 + NewMode: os.FileMode(0100644), 177 + OldOIDPrefix: "0000000", 178 + NewOIDPrefix: "e69de29", 179 + IsNew: true, 180 + }, 181 + }, 182 + "deleteFile": { 183 + Input: `diff --git a/dir/file.txt b/dir/file.txt 184 + deleted file mode 100644 185 + index 44cc321..0000000 186 + --- a/dir/file.txt 187 + +++ /dev/null 188 + `, 189 + Output: &File{ 190 + OldName: "dir/file.txt", 191 + OldMode: os.FileMode(0100644), 192 + OldOIDPrefix: "44cc321", 193 + NewOIDPrefix: "0000000", 194 + IsDelete: true, 195 + }, 196 + }, 197 + "changeMode": { 198 + Input: `diff --git a/file.sh b/file.sh 199 + old mode 100644 200 + new mode 100755 201 + `, 202 + Output: &File{ 203 + OldName: "file.sh", 204 + NewName: "file.sh", 205 + OldMode: os.FileMode(0100644), 206 + NewMode: os.FileMode(0100755), 207 + }, 208 + }, 209 + "rename": { 210 + Input: `diff --git a/foo.txt b/bar.txt 211 + similarity index 100% 212 + rename from foo.txt 213 + rename to bar.txt 214 + `, 215 + Output: &File{ 216 + OldName: "foo.txt", 217 + NewName: "bar.txt", 218 + Score: 100, 219 + IsRename: true, 220 + }, 221 + }, 222 + "copy": { 223 + Input: `diff --git a/file.txt b/copy.txt 224 + similarity index 100% 225 + copy from file.txt 226 + copy to copy.txt 227 + `, 228 + Output: &File{ 229 + OldName: "file.txt", 230 + NewName: "copy.txt", 231 + Score: 100, 232 + IsCopy: true, 233 + }, 234 + }, 235 + "missingDefaultFilename": { 236 + Input: `diff --git a/foo.sh b/bar.sh 237 + old mode 100644 238 + new mode 100755 239 + `, 240 + Err: true, 241 + }, 242 + "missingNewFilename": { 243 + Input: `diff --git a/file.txt b/file.txt 244 + index 1c23fcc..40a1b33 100644 245 + --- a/file.txt 246 + `, 247 + Err: true, 248 + }, 249 + "missingOldFilename": { 250 + Input: `diff --git a/file.txt b/file.txt 251 + index 1c23fcc..40a1b33 100644 252 + +++ b/file.txt 253 + `, 254 + Err: true, 255 + }, 256 + "invalidHeaderLine": { 257 + Input: `diff --git a/file.txt b/file.txt 258 + index deadbeef 259 + --- a/file.txt 260 + +++ b/file.txt 261 + `, 262 + Err: true, 263 + }, 168 264 } 169 265 170 266 for name, test := range tests { ··· 176 272 err := p.ParseGitFileHeader(&f, header) 177 273 if test.Err { 178 274 if err == nil { 179 - t.Fatalf("expected error parsing git header, got nil") 275 + t.Fatalf("expected error parsing git file header, got nil") 180 276 } 181 277 return 182 278 } 279 + if err != nil { 280 + t.Fatalf("unexpected error parsing git file header: %v", err) 281 + } 183 282 184 283 if test.Output != nil && !reflect.DeepEqual(f, *test.Output) { 185 - t.Errorf("incorrect file\nexpected: %+v\nactual: %+v", *test.Output, f) 284 + t.Errorf("incorrect file\nexpected: %+v\n actual: %+v", *test.Output, f) 186 285 } 187 286 }) 188 287 }