fork of go-gitdiff with jj support
at v0.8.0 5.1 kB view raw
1package gitdiff 2 3import ( 4 "bytes" 5 "fmt" 6 "os" 7 "path/filepath" 8 "slices" 9 "testing" 10) 11 12func TestFormatRoundtrip(t *testing.T) { 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}, 34 } 35 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 } 42 43 original := assertParseSingleFile(t, b, "patch") 44 str := original.String() 45 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 } 51 52 reparsed := assertParseSingleFile(t, []byte(str), "formatted patch") 53 assertFilesEqual(t, original, reparsed) 54 }) 55 } 56} 57 58func assertParseSingleFile(t *testing.T, b []byte, kind string) *File { 59 files, _, err := Parse(bytes.NewReader(b)) 60 if err != nil { 61 t.Fatalf("failed to parse %s: %v", kind, err) 62 } 63 if len(files) != 1 { 64 t.Fatalf("expected %s to contain a single files, but found %d", kind, len(files)) 65 } 66 return files[0] 67} 68 69func assertFilesEqual(t *testing.T, expected, actual *File) { 70 assertEqual(t, expected.OldName, actual.OldName, "OldName") 71 assertEqual(t, expected.NewName, actual.NewName, "NewName") 72 73 assertEqual(t, expected.IsNew, actual.IsNew, "IsNew") 74 assertEqual(t, expected.IsDelete, actual.IsDelete, "IsDelete") 75 assertEqual(t, expected.IsCopy, actual.IsCopy, "IsCopy") 76 assertEqual(t, expected.IsRename, actual.IsRename, "IsRename") 77 78 assertEqual(t, expected.OldMode, actual.OldMode, "OldMode") 79 assertEqual(t, expected.NewMode, actual.NewMode, "NewMode") 80 81 assertEqual(t, expected.OldOIDPrefix, actual.OldOIDPrefix, "OldOIDPrefix") 82 assertEqual(t, expected.NewOIDPrefix, actual.NewOIDPrefix, "NewOIDPrefix") 83 assertEqual(t, expected.Score, actual.Score, "Score") 84 85 if len(expected.TextFragments) == len(actual.TextFragments) { 86 for i := range expected.TextFragments { 87 prefix := fmt.Sprintf("TextFragments[%d].", i) 88 ef := expected.TextFragments[i] 89 af := actual.TextFragments[i] 90 91 assertEqual(t, ef.Comment, af.Comment, prefix+"Comment") 92 93 assertEqual(t, ef.OldPosition, af.OldPosition, prefix+"OldPosition") 94 assertEqual(t, ef.OldLines, af.OldLines, prefix+"OldLines") 95 96 assertEqual(t, ef.NewPosition, af.NewPosition, prefix+"NewPosition") 97 assertEqual(t, ef.NewLines, af.NewLines, prefix+"NewLines") 98 99 assertEqual(t, ef.LinesAdded, af.LinesAdded, prefix+"LinesAdded") 100 assertEqual(t, ef.LinesDeleted, af.LinesDeleted, prefix+"LinesDeleted") 101 102 assertEqual(t, ef.LeadingContext, af.LeadingContext, prefix+"LeadingContext") 103 assertEqual(t, ef.TrailingContext, af.TrailingContext, prefix+"TrailingContext") 104 105 if !slices.Equal(ef.Lines, af.Lines) { 106 t.Errorf("%sLines: expected %#v, actual %#v", prefix, ef.Lines, af.Lines) 107 } 108 } 109 } else { 110 t.Errorf("TextFragments: expected length %d, actual length %d", len(expected.TextFragments), len(actual.TextFragments)) 111 } 112 113 assertEqual(t, expected.IsBinary, actual.IsBinary, "IsBinary") 114 115 if expected.BinaryFragment != nil { 116 if actual.BinaryFragment == nil { 117 t.Errorf("BinaryFragment: expected non-nil, actual is nil") 118 } else { 119 ef := expected.BinaryFragment 120 af := expected.BinaryFragment 121 122 assertEqual(t, ef.Method, af.Method, "BinaryFragment.Method") 123 assertEqual(t, ef.Size, af.Size, "BinaryFragment.Size") 124 125 if !slices.Equal(ef.Data, af.Data) { 126 t.Errorf("BinaryFragment.Data: expected %#v, actual %#v", ef.Data, af.Data) 127 } 128 } 129 } else if actual.BinaryFragment != nil { 130 t.Errorf("BinaryFragment: expected nil, actual is non-nil") 131 } 132 133 if expected.ReverseBinaryFragment != nil { 134 if actual.ReverseBinaryFragment == nil { 135 t.Errorf("ReverseBinaryFragment: expected non-nil, actual is nil") 136 } else { 137 ef := expected.ReverseBinaryFragment 138 af := expected.ReverseBinaryFragment 139 140 assertEqual(t, ef.Method, af.Method, "ReverseBinaryFragment.Method") 141 assertEqual(t, ef.Size, af.Size, "ReverseBinaryFragment.Size") 142 143 if !slices.Equal(ef.Data, af.Data) { 144 t.Errorf("ReverseBinaryFragment.Data: expected %#v, actual %#v", ef.Data, af.Data) 145 } 146 } 147 } else if actual.ReverseBinaryFragment != nil { 148 t.Errorf("ReverseBinaryFragment: expected nil, actual is non-nil") 149 } 150} 151 152func assertEqual[T comparable](t *testing.T, expected, actual T, name string) { 153 if expected != actual { 154 t.Errorf("%s: expected %#v, actual %#v", name, expected, actual) 155 } 156}