Skip to content

Commit f0da09b

Browse files
authored
Add ParsePatchHeader and related types (#6)
This function parses patch headers (the preamble returned by the existing Parse function) to extract information about the commit that generated the patch. This is useful when patches are an interchange format and this library is applying commits generated elsewhere. Because of the variety of header formats, parsing is fairly lenient and best-effort, although certain invalid input does cause errors. This also extracts some test utilities from the apply tests for reuse.
1 parent c2035ad commit f0da09b

File tree

4 files changed

+778
-21
lines changed

4 files changed

+778
-21
lines changed

gitdiff/apply_test.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"io/ioutil"
88
"path/filepath"
9-
"strings"
109
"testing"
1110
)
1211

@@ -221,7 +220,7 @@ func (at applyTest) run(t *testing.T, apply func(io.Writer, *Applier, *File) err
221220
var dst bytes.Buffer
222221
err = apply(&dst, applier, files[0])
223222
if at.Err != nil {
224-
at.assertError(t, err)
223+
assertError(t, at.Err, err, "applying fragment")
225224
return
226225
}
227226
if err != nil {
@@ -238,25 +237,6 @@ func (at applyTest) run(t *testing.T, apply func(io.Writer, *Applier, *File) err
238237
}
239238
}
240239

241-
func (at applyTest) assertError(t *testing.T, err error) {
242-
if err == nil {
243-
t.Fatalf("expected error applying fragment, but got nil")
244-
}
245-
246-
switch terr := at.Err.(type) {
247-
case string:
248-
if !strings.Contains(err.Error(), terr) {
249-
t.Fatalf("incorrect apply error: %q does not contain %q", err.Error(), terr)
250-
}
251-
case error:
252-
if !errors.Is(err, terr) {
253-
t.Fatalf("incorrect apply error: expected: %T (%v), actual: %T (%v)", terr, terr, err, err)
254-
}
255-
default:
256-
t.Fatalf("unsupported expected error type: %T", terr)
257-
}
258-
}
259-
260240
type applyFiles struct {
261241
Src string
262242
Patch string

gitdiff/assert_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package gitdiff
2+
3+
import (
4+
"errors"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func assertError(t *testing.T, expected interface{}, actual error, action string) {
10+
if actual == nil {
11+
t.Fatalf("expected error %s, but got nil", action)
12+
}
13+
14+
switch exp := expected.(type) {
15+
case bool:
16+
if !exp {
17+
t.Fatalf("unexpected error %s: %v", action, actual)
18+
}
19+
case string:
20+
if !strings.Contains(actual.Error(), exp) {
21+
t.Fatalf("incorrect error %s: %q does not contain %q", action, actual.Error(), exp)
22+
}
23+
case error:
24+
if !errors.Is(actual, exp) {
25+
t.Fatalf("incorrect error %s: expected %T (%v), actual: %T (%v)", action, exp, exp, actual, actual)
26+
}
27+
default:
28+
t.Fatalf("unsupported expected error type: %T", exp)
29+
}
30+
}

0 commit comments

Comments
 (0)