@@ -4,54 +4,64 @@ import (
4
4
"bytes"
5
5
"fmt"
6
6
"os"
7
+ "path/filepath"
7
8
"slices"
8
9
"testing"
9
10
)
10
11
11
12
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 },
26
34
}
27
35
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
+ }
33
42
34
- original := assertParseSingleFile (t , src , b )
35
- str := original .String ()
43
+ original := assertParseSingleFile (t , b , "patch" )
44
+ str := original .String ()
36
45
37
- if string ( b ) != str {
38
- t . Errorf ( "%s: incorrect patch \n expected: %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 \n expected: %q \n actual: %q \n " , string ( b ), str )
49
+ }
50
+ }
42
51
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
+ })
45
55
}
46
56
}
47
57
48
- func assertParseSingleFile (t * testing.T , src string , b []byte ) * File {
58
+ func assertParseSingleFile (t * testing.T , b []byte , kind string ) * File {
49
59
files , _ , err := Parse (bytes .NewReader (b ))
50
60
if err != nil {
51
- t .Fatalf ("failed to parse patch %s: %v" , src , err )
61
+ t .Fatalf ("failed to parse %s: %v" , kind , err )
52
62
}
53
63
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 ))
55
65
}
56
66
return files [0 ]
57
67
}
0 commit comments