Skip to content

Commit 7cec3fe

Browse files
committed
Use subtests in parse roundtrip tests
1 parent 43a7035 commit 7cec3fe

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

gitdiff/gitdiff_string_test.go

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,64 @@ import (
44
"bytes"
55
"fmt"
66
"os"
7+
"path/filepath"
78
"slices"
89
"testing"
910
)
1011

1112
func TestParseRoundtrip(t *testing.T) {
12-
sources := []string{
13-
"testdata/string/binary_modify.patch",
14-
"testdata/string/binary_new.patch",
15-
"testdata/string/copy.patch",
16-
"testdata/string/copy_modify.patch",
17-
"testdata/string/delete.patch",
18-
"testdata/string/mode.patch",
19-
"testdata/string/mode_modify.patch",
20-
"testdata/string/modify.patch",
21-
"testdata/string/new.patch",
22-
"testdata/string/new_empty.patch",
23-
"testdata/string/new_mode.patch",
24-
"testdata/string/rename.patch",
25-
"testdata/string/rename_modify.patch",
13+
patches := []struct {
14+
File string
15+
SkipTextCompare bool
16+
}{
17+
{File: "copy.patch"},
18+
{File: "copy_modify.patch"},
19+
{File: "delete.patch"},
20+
{File: "mode.patch"},
21+
{File: "mode_modify.patch"},
22+
{File: "modify.patch"},
23+
{File: "new.patch"},
24+
{File: "new_empty.patch"},
25+
{File: "new_mode.patch"},
26+
{File: "rename.patch"},
27+
{File: "rename_modify.patch"},
28+
29+
// Due to differences between Go's 'encoding/zlib' package and the zlib
30+
// C library, binary patches cannot be compared directly as the patch
31+
// data is slightly different when re-encoded by Go.
32+
{File: "binary_modify.patch", SkipTextCompare: true},
33+
{File: "binary_new.patch", SkipTextCompare: true},
2634
}
2735

28-
for _, src := range sources {
29-
b, err := os.ReadFile(src)
30-
if err != nil {
31-
t.Fatalf("failed to read %s: %v", src, err)
32-
}
36+
for _, patch := range patches {
37+
t.Run(patch.File, func(t *testing.T) {
38+
b, err := os.ReadFile(filepath.Join("testdata", "string", patch.File))
39+
if err != nil {
40+
t.Fatalf("failed to read patch: %v", err)
41+
}
3342

34-
original := assertParseSingleFile(t, src, b)
35-
str := original.String()
43+
original := assertParseSingleFile(t, b, "patch")
44+
str := original.String()
3645

37-
if string(b) != str {
38-
t.Errorf("%s: incorrect patch\nexpected: %q\n actual: %q\n", src, string(b), str)
39-
}
40-
41-
reparsed := assertParseSingleFile(t, fmt.Sprintf("Parse(%q).String()", src), []byte(str))
46+
if !patch.SkipTextCompare {
47+
if string(b) != str {
48+
t.Errorf("incorrect patch text\nexpected: %q\n actual: %q\n", string(b), str)
49+
}
50+
}
4251

43-
// TODO(bkeyes): include source in these messages (via subtest?)
44-
assertFilesEqual(t, original, reparsed)
52+
reparsed := assertParseSingleFile(t, []byte(str), "formatted patch")
53+
assertFilesEqual(t, original, reparsed)
54+
})
4555
}
4656
}
4757

48-
func assertParseSingleFile(t *testing.T, src string, b []byte) *File {
58+
func assertParseSingleFile(t *testing.T, b []byte, kind string) *File {
4959
files, _, err := Parse(bytes.NewReader(b))
5060
if err != nil {
51-
t.Fatalf("failed to parse patch %s: %v", src, err)
61+
t.Fatalf("failed to parse %s: %v", kind, err)
5262
}
5363
if len(files) != 1 {
54-
t.Fatalf("expected %s to contain a single files, but found %d", src, len(files))
64+
t.Fatalf("expected %s to contain a single files, but found %d", kind, len(files))
5565
}
5666
return files[0]
5767
}

0 commit comments

Comments
 (0)