mirror of
https://github.com/bluekeyes/go-gitdiff
synced 2026-06-08 13:18:30 +00:00
8584cd59af
This enables clients to move back and forth between parsed objects and text patches. The generated patches are semantically equal to the parsed object and should re-parse to the same object, but may not be byte-for-byte identical to the original input. In my testing, formatted text patches are usually identical to the input, but there may be cases where this is not true. Binary patches always differ. This is because Go's 'compress/flate' package ends streams with an empty block instead of adding the end-of-stream flag to the last non-empty block, like Git's C implementation. Since the streams will always be different for this reason, I chose to also enable default compression (the test patches I generated with Git used no compression.) The main tests for this feature involve parsing, formatting, and then re-parsing a patch to make sure we get equal objects. Formatting is handled by a new internal formatter type, which allows writing all data to the same stream. This isn't exposed publicly right now, but will be useful if there's a need for more flexible formatting functions in the future, like formatting to a user-provided io.Writer.
157 lines
5.0 KiB
Go
157 lines
5.0 KiB
Go
package gitdiff
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"testing"
|
|
)
|
|
|
|
func TestFormatRoundtrip(t *testing.T) {
|
|
patches := []struct {
|
|
File string
|
|
SkipTextCompare bool
|
|
}{
|
|
{File: "copy.patch"},
|
|
{File: "copy_modify.patch"},
|
|
{File: "delete.patch"},
|
|
{File: "mode.patch"},
|
|
{File: "mode_modify.patch"},
|
|
{File: "modify.patch"},
|
|
{File: "new.patch"},
|
|
{File: "new_empty.patch"},
|
|
{File: "new_mode.patch"},
|
|
{File: "rename.patch"},
|
|
{File: "rename_modify.patch"},
|
|
|
|
// Due to differences between Go's 'encoding/zlib' package and the zlib
|
|
// C library, binary patches cannot be compared directly as the patch
|
|
// data is slightly different when re-encoded by Go.
|
|
{File: "binary_modify.patch", SkipTextCompare: true},
|
|
{File: "binary_new.patch", SkipTextCompare: true},
|
|
}
|
|
|
|
for _, patch := range patches {
|
|
t.Run(patch.File, func(t *testing.T) {
|
|
b, err := os.ReadFile(filepath.Join("testdata", "string", patch.File))
|
|
if err != nil {
|
|
t.Fatalf("failed to read patch: %v", err)
|
|
}
|
|
|
|
original := assertParseSingleFile(t, b, "patch")
|
|
str := original.String()
|
|
|
|
if !patch.SkipTextCompare {
|
|
if string(b) != str {
|
|
t.Errorf("incorrect patch text\nexpected: %q\n actual: %q\n", string(b), str)
|
|
}
|
|
}
|
|
|
|
reparsed := assertParseSingleFile(t, []byte(str), "formatted patch")
|
|
assertFilesEqual(t, original, reparsed)
|
|
})
|
|
}
|
|
}
|
|
|
|
func assertParseSingleFile(t *testing.T, b []byte, kind string) *File {
|
|
files, _, err := Parse(bytes.NewReader(b))
|
|
if err != nil {
|
|
t.Fatalf("failed to parse %s: %v", kind, err)
|
|
}
|
|
if len(files) != 1 {
|
|
t.Fatalf("expected %s to contain a single files, but found %d", kind, len(files))
|
|
}
|
|
return files[0]
|
|
}
|
|
|
|
func assertFilesEqual(t *testing.T, expected, actual *File) {
|
|
assertEqual(t, expected.OldName, actual.OldName, "OldName")
|
|
assertEqual(t, expected.NewName, actual.NewName, "NewName")
|
|
|
|
assertEqual(t, expected.IsNew, actual.IsNew, "IsNew")
|
|
assertEqual(t, expected.IsDelete, actual.IsDelete, "IsDelete")
|
|
assertEqual(t, expected.IsCopy, actual.IsCopy, "IsCopy")
|
|
assertEqual(t, expected.IsRename, actual.IsRename, "IsRename")
|
|
|
|
assertEqual(t, expected.OldMode, actual.OldMode, "OldMode")
|
|
assertEqual(t, expected.NewMode, actual.NewMode, "NewMode")
|
|
|
|
assertEqual(t, expected.OldOIDPrefix, actual.OldOIDPrefix, "OldOIDPrefix")
|
|
assertEqual(t, expected.NewOIDPrefix, actual.NewOIDPrefix, "NewOIDPrefix")
|
|
assertEqual(t, expected.Score, actual.Score, "Score")
|
|
|
|
if len(expected.TextFragments) == len(actual.TextFragments) {
|
|
for i := range expected.TextFragments {
|
|
prefix := fmt.Sprintf("TextFragments[%d].", i)
|
|
ef := expected.TextFragments[i]
|
|
af := actual.TextFragments[i]
|
|
|
|
assertEqual(t, ef.Comment, af.Comment, prefix+"Comment")
|
|
|
|
assertEqual(t, ef.OldPosition, af.OldPosition, prefix+"OldPosition")
|
|
assertEqual(t, ef.OldLines, af.OldLines, prefix+"OldLines")
|
|
|
|
assertEqual(t, ef.NewPosition, af.NewPosition, prefix+"NewPosition")
|
|
assertEqual(t, ef.NewLines, af.NewLines, prefix+"NewLines")
|
|
|
|
assertEqual(t, ef.LinesAdded, af.LinesAdded, prefix+"LinesAdded")
|
|
assertEqual(t, ef.LinesDeleted, af.LinesDeleted, prefix+"LinesDeleted")
|
|
|
|
assertEqual(t, ef.LeadingContext, af.LeadingContext, prefix+"LeadingContext")
|
|
assertEqual(t, ef.TrailingContext, af.TrailingContext, prefix+"TrailingContext")
|
|
|
|
if !slices.Equal(ef.Lines, af.Lines) {
|
|
t.Errorf("%sLines: expected %#v, actual %#v", prefix, ef.Lines, af.Lines)
|
|
}
|
|
}
|
|
} else {
|
|
t.Errorf("TextFragments: expected length %d, actual length %d", len(expected.TextFragments), len(actual.TextFragments))
|
|
}
|
|
|
|
assertEqual(t, expected.IsBinary, actual.IsBinary, "IsBinary")
|
|
|
|
if expected.BinaryFragment != nil {
|
|
if actual.BinaryFragment == nil {
|
|
t.Errorf("BinaryFragment: expected non-nil, actual is nil")
|
|
} else {
|
|
ef := expected.BinaryFragment
|
|
af := expected.BinaryFragment
|
|
|
|
assertEqual(t, ef.Method, af.Method, "BinaryFragment.Method")
|
|
assertEqual(t, ef.Size, af.Size, "BinaryFragment.Size")
|
|
|
|
if !slices.Equal(ef.Data, af.Data) {
|
|
t.Errorf("BinaryFragment.Data: expected %#v, actual %#v", ef.Data, af.Data)
|
|
}
|
|
}
|
|
} else if actual.BinaryFragment != nil {
|
|
t.Errorf("BinaryFragment: expected nil, actual is non-nil")
|
|
}
|
|
|
|
if expected.ReverseBinaryFragment != nil {
|
|
if actual.ReverseBinaryFragment == nil {
|
|
t.Errorf("ReverseBinaryFragment: expected non-nil, actual is nil")
|
|
} else {
|
|
ef := expected.ReverseBinaryFragment
|
|
af := expected.ReverseBinaryFragment
|
|
|
|
assertEqual(t, ef.Method, af.Method, "ReverseBinaryFragment.Method")
|
|
assertEqual(t, ef.Size, af.Size, "ReverseBinaryFragment.Size")
|
|
|
|
if !slices.Equal(ef.Data, af.Data) {
|
|
t.Errorf("ReverseBinaryFragment.Data: expected %#v, actual %#v", ef.Data, af.Data)
|
|
}
|
|
}
|
|
} else if actual.ReverseBinaryFragment != nil {
|
|
t.Errorf("ReverseBinaryFragment: expected nil, actual is non-nil")
|
|
}
|
|
}
|
|
|
|
func assertEqual[T comparable](t *testing.T, expected, actual T, name string) {
|
|
if expected != actual {
|
|
t.Errorf("%s: expected %#v, actual %#v", name, expected, actual)
|
|
}
|
|
}
|