Something went wrong. Try again.
A pure Go text diff library
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548package diff_test
import ( "bytes" "fmt" "os" "path/filepath" "strings" "testing"
"go.followtheprocess.codes/diff" "golang.org/x/tools/txtar")
// TestLines verifies the Lines method returns structured diff lines.func TestLines(t *testing.T) { tests := []struct { name string old []byte newText []byte oldName string newName string want []diff.Line // nil means we expect nil (inputs equal) }{ { name: "nil on equal inputs", oldName: "a", newName: "b", old: []byte("same\n"), newText: []byte("same\n"), want: nil, }, { name: "basic add and remove", oldName: "want", newName: "got", old: []byte("hello\nworld\n"), newText: []byte("hello\nearth\n"), want: []diff.Line{ {Kind: diff.KindHeader, Content: []byte("diff want got\n")}, {Kind: diff.KindHeader, Content: []byte("--- want\n")}, {Kind: diff.KindHeader, Content: []byte("+++ got\n")}, {Kind: diff.KindHeader, Content: []byte("@@ -1,2 +1,2 @@\n")}, {Kind: diff.KindContext, Content: []byte("hello\n")}, {Kind: diff.KindRemoved, Content: []byte("world\n")}, {Kind: diff.KindAdded, Content: []byte("earth\n")}, }, }, { name: "all added", oldName: "want", newName: "got", old: []byte(""), newText: []byte("new line\n"), want: []diff.Line{ {Kind: diff.KindHeader, Content: []byte("diff want got\n")}, {Kind: diff.KindHeader, Content: []byte("--- want\n")}, {Kind: diff.KindHeader, Content: []byte("+++ got\n")}, {Kind: diff.KindHeader, Content: []byte("@@ -0,0 +1,1 @@\n")}, {Kind: diff.KindAdded, Content: []byte("new line\n")}, }, }, { name: "all removed", oldName: "want", newName: "got", old: []byte("old line\n"), newText: []byte(""), want: []diff.Line{ {Kind: diff.KindHeader, Content: []byte("diff want got\n")}, {Kind: diff.KindHeader, Content: []byte("--- want\n")}, {Kind: diff.KindHeader, Content: []byte("+++ got\n")}, {Kind: diff.KindHeader, Content: []byte("@@ -1,1 +0,0 @@\n")}, {Kind: diff.KindRemoved, Content: []byte("old line\n")}, }, }, { name: "compact shifts duplicate removal to second occurrence", oldName: "want", newName: "got", old: []byte("a\na\nb\n"), newText: []byte("a\nb\n"), want: []diff.Line{ {Kind: diff.KindHeader, Content: []byte("diff want got\n")}, {Kind: diff.KindHeader, Content: []byte("--- want\n")}, {Kind: diff.KindHeader, Content: []byte("+++ got\n")}, {Kind: diff.KindHeader, Content: []byte("@@ -1,3 +1,2 @@\n")}, {Kind: diff.KindContext, Content: []byte("a\n")}, {Kind: diff.KindRemoved, Content: []byte("a\n")}, {Kind: diff.KindContext, Content: []byte("b\n")}, }, }, { name: "compact shifts duplicate addition to second occurrence", oldName: "want", newName: "got", old: []byte("a\nb\n"), newText: []byte("a\na\nb\n"), want: []diff.Line{ {Kind: diff.KindHeader, Content: []byte("diff want got\n")}, {Kind: diff.KindHeader, Content: []byte("--- want\n")}, {Kind: diff.KindHeader, Content: []byte("+++ got\n")}, {Kind: diff.KindHeader, Content: []byte("@@ -1,2 +1,3 @@\n")}, {Kind: diff.KindContext, Content: []byte("a\n")}, {Kind: diff.KindAdded, Content: []byte("a\n")}, {Kind: diff.KindContext, Content: []byte("b\n")}, }, }, { name: "compact does not shift block at end of hunk with no following context", oldName: "want", newName: "got", old: []byte("a\nb\n"), newText: []byte("a\n"), want: []diff.Line{ {Kind: diff.KindHeader, Content: []byte("diff want got\n")}, {Kind: diff.KindHeader, Content: []byte("--- want\n")}, {Kind: diff.KindHeader, Content: []byte("+++ got\n")}, {Kind: diff.KindHeader, Content: []byte("@@ -1,2 +1,1 @@\n")}, {Kind: diff.KindContext, Content: []byte("a\n")}, {Kind: diff.KindRemoved, Content: []byte("b\n")}, }, }, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := diff.New(tt.oldName, tt.old, tt.newName, tt.newText).Lines() if tt.want == nil { if got != nil { t.Fatalf("Lines() = %v, want nil", got) }
return }
if len(got) != len(tt.want) { t.Fatalf( "Lines() returned %d lines, want %d\ngot: %#v\nwant: %#v", len(got), len(tt.want), got, tt.want, ) }
for i, line := range got { if line.Kind != tt.want[i].Kind { t.Errorf("line[%d].Kind = %v, want %v", i, line.Kind, tt.want[i].Kind) }
if !bytes.Equal(line.Content, tt.want[i].Content) { t.Errorf("line[%d].Content = %q, want %q", i, line.Content, tt.want[i].Content) } } }) }}
// TestEqual verifies Equal returns true when old and new are identical.func TestEqual(t *testing.T) { tests := []struct { name string old []byte newText []byte }{ { name: "identical byte slices", old: []byte("same\n"), newText: []byte("same\n"), }, { name: "both empty", old: []byte(""), newText: []byte(""), }, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := diff.New("a", tt.old, "b", tt.newText); !got.Equal() { t.Fatal("Equal() = false, want true for identical inputs") } }) }}
// TestString verifies String returns an empty string for equal inputs and a// non-empty unified diff for differing inputs.func TestString(t *testing.T) { tests := []struct { name string wantContain string old []byte newText []byte wantEmpty bool }{ { name: "equal inputs produce empty string", old: []byte("same\n"), newText: []byte("same\n"), wantEmpty: true, }, { name: "both empty inputs produce empty string", old: []byte(""), newText: []byte(""), wantEmpty: true, }, { name: "differing inputs produce non-empty unified diff", old: []byte("hello\nworld\n"), newText: []byte("hello\nearth\n"), wantContain: "- world", }, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := diff.New("a", tt.old, "b", tt.newText).String() if tt.wantEmpty { if got != "" { t.Fatalf("String() = %q, want empty string for equal inputs", got) }
return }
if !strings.Contains(got, tt.wantContain) { t.Fatalf("String() = %q, want it to contain %q", got, tt.wantContain) } }) }}
// Test drives New against the *.txtar files in testdata/.func Test(t *testing.T) { files, err := filepath.Glob(filepath.Join("testdata", "*.txtar")) if err != nil { t.Fatalf("could not glob txtar files: %v", err) }
if len(files) == 0 { t.Fatal("no testdata") }
for _, file := range files { t.Run(filepath.Base(file), func(t *testing.T) { contents, err := os.ReadFile(file) if err != nil { t.Fatalf("could not read %s: %v", file, err) }
contents = bytes.ReplaceAll(contents, []byte("\r\n"), []byte("\n"))
archive := txtar.Parse(contents) if len(archive.Files) != 3 || archive.Files[2].Name != "diff" { t.Fatalf("%s: want three files, third named \"diff\", got: %v", file, archive.Files) }
got := diff.New( archive.Files[0].Name, clean(archive.Files[0].Data), archive.Files[1].Name, clean(archive.Files[1].Data), ).String() want := string(clean(archive.Files[2].Data))
if got != want { t.Fatalf("%s: have:\n%s\nwant:\n%s\n%s", file, got, want, diff.New("have", []byte(got), "want", []byte(want))) } }) }}
// FuzzLines verifies Lines and String never panic and return nil/empty iff inputs are equal.func FuzzLines(f *testing.F) { f.Add([]byte(""), []byte("")) f.Add([]byte("same\n"), []byte("same\n")) f.Add([]byte("hello\nworld\n"), []byte("hello\nearth\n")) f.Add([]byte("completely different\n"), []byte("nothing in common\n")) f.Add([]byte("a\nb\nc\n"), []byte("a\nd\nc\n")) f.Add([]byte("unicode: héllo\n"), []byte("unicode: wörld\n")) f.Add([]byte(" \n\t\n"), []byte(" \n\t\n"))
f.Fuzz(func(t *testing.T, old, newContent []byte) { d := diff.New("a", old, "b", newContent)
_ = d.String() // must not panic if bytes.Equal(old, newContent) { if !d.Equal() { t.Fatal("Equal() = false for equal inputs") }
if d.Lines() != nil { t.Fatal("Lines() = non-nil for equal inputs") } } else { if d.Equal() { t.Fatal("Equal() = true for non-equal inputs") }
if d.Lines() == nil { t.Fatal("Lines() = nil for non-equal inputs") } } })}
// BenchmarkLines benchmarks the Lines method using long.txtar as realistic input.func BenchmarkLines(b *testing.B) { contents, err := os.ReadFile(filepath.Join("testdata", "long.txtar")) if err != nil { b.Fatalf("could not read long.txtar: %v", err) }
archive := txtar.Parse(contents) old := clean(archive.Files[0].Data) newContent := clean(archive.Files[1].Data)
b.ResetTimer()
for b.Loop() { diff.New(archive.Files[0].Name, old, archive.Files[1].Name, newContent).Lines() }}
// BenchmarkString benchmarks String using long.txtar as realistic input.func BenchmarkString(b *testing.B) { contents, err := os.ReadFile(filepath.Join("testdata", "long.txtar")) if err != nil { b.Fatalf("could not read long.txtar: %v", err) }
archive := txtar.Parse(contents) old := clean(archive.Files[0].Data) newContent := clean(archive.Files[1].Data)
b.ResetTimer()
for b.Loop() { _ = diff.New(archive.Files[0].Name, old, archive.Files[1].Name, newContent).String() }}
// TestWithContextOption verifies WithContext is accepted and the default (3) is unchanged.func TestWithContextOption(t *testing.T) { tests := []struct { name string old []byte newText []byte opts []diff.Option wantLen int // expected number of Lines returned }{ { // Default context=3: 3 headers + 1 @@ + 2 context (a, b) + 1 removed + 1 added = 8 name: "default context unchanged", old: []byte("a\nb\nc\n"), newText: []byte("a\nb\nd\n"), opts: nil, wantLen: 8, }, { // WithContext(0): 3 headers + 1 @@ + 1 removed + 1 added = 6 name: "WithContext(0) suppresses context lines", old: []byte("a\nb\nc\n"), newText: []byte("a\nb\nd\n"), opts: []diff.Option{diff.WithContext(0)}, wantLen: 6, }, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := diff.New("old", tt.old, "new", tt.newText, tt.opts...).Lines() if len(got) != tt.wantLen { t.Fatalf("Lines() returned %d lines, want %d\n%#v", len(got), tt.wantLen, got) } }) }}
// TestGroupIntoHunksViaWithContext verifies the extracted groupIntoHunks function// respects the contextLines setting via the WithContext option.func TestGroupIntoHunksViaWithContext(t *testing.T) { tests := []struct { opt diff.Option name string old []byte new []byte wantContext int }{ { name: "default 3 context lines", old: []byte("a\nb\nc\nd\ne\nf\ng\n"), new: []byte("a\nb\nc\nX\ne\nf\ng\n"), opt: diff.WithContext(3), wantContext: 6, // 3 before + 3 after the change }, { name: "zero context lines", old: []byte("a\nb\nc\nd\ne\nf\ng\n"), new: []byte("a\nb\nc\nX\ne\nf\ng\n"), opt: diff.WithContext(0), wantContext: 0, }, { name: "one context line", old: []byte("a\nb\nc\nd\ne\nf\ng\n"), new: []byte("a\nb\nc\nX\ne\nf\ng\n"), opt: diff.WithContext(1), wantContext: 2, // 1 before + 1 after }, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { lines := diff.New("old", tt.old, "new", tt.new, tt.opt).Lines()
contextCount := 0
for _, l := range lines { if l.Kind == diff.KindContext { contextCount++ } }
if contextCount != tt.wantContext { t.Fatalf("got %d KindContext lines, want %d\n%#v", contextCount, tt.wantContext, lines) } }) }}
// TestLargeCommonRegionsProduceCorrectLineNumbers verifies that inputs with large// common regions produce correct line numbers and only show contextLines of context// (not the whole prefix).func TestLargeCommonRegionsProduceCorrectLineNumbers(t *testing.T) { tests := []struct { name string old string new string wantHunk string wantContext int }{ { // 100-line common prefix, then 1 changed line. // Hunk shows lines 98–101 (3 context + 1 change = 4 lines each side). name: "large common prefix — correct line numbers", old: strings.Repeat("line\n", 100) + "old content\n", new: strings.Repeat("line\n", 100) + "new content\n", wantContext: 3, wantHunk: "@@ -98,4 +98,4 @@", }, { // 1 changed line at the start, then 100-line common suffix. name: "large common suffix — correct line numbers", old: "old content\n" + strings.Repeat("line\n", 100), new: "new content\n" + strings.Repeat("line\n", 100), wantContext: 3, wantHunk: "@@ -1,4 +1,4 @@", }, { // 50 common prefix + 1 change + 50 common suffix. name: "common prefix and suffix", old: strings.Repeat("line\n", 50) + "old\n" + strings.Repeat("line\n", 50), new: strings.Repeat("line\n", 50) + "new\n" + strings.Repeat("line\n", 50), wantContext: 6, // 3 before + 3 after wantHunk: "@@ -48,7 +48,7 @@", }, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { lines := diff.New("old", []byte(tt.old), "new", []byte(tt.new)).Lines()
contextCount := 0
var hunkHeader string
for _, l := range lines { if l.Kind == diff.KindContext { contextCount++ }
if l.Kind == diff.KindHeader && len(l.Content) > 2 && l.Content[0] == '@' { hunkHeader = strings.TrimSuffix(string(l.Content), "\n") } }
if contextCount != tt.wantContext { t.Errorf("context line count = %d, want %d", contextCount, tt.wantContext) }
if hunkHeader != tt.wantHunk { t.Errorf("hunk header = %q, want %q", hunkHeader, tt.wantHunk) } }) }}
// TestDisjointFastPath verifies that large fully-disjoint inputs produce the// correct all-removed / all-added diff output (same as the normal TGS path).func TestDisjointFastPath(t *testing.T) { // Build two 512-line files with no lines in common. var oldLines, newLines strings.Builder for i := range 512 { fmt.Fprintf(&oldLines, "old line %d\n", i) fmt.Fprintf(&newLines, "new line %d\n", i) }
old := []byte(oldLines.String()) newText := []byte(newLines.String())
d := diff.New("old", old, "new", newText) if d.Equal() { t.Fatal("Equal() = true for non-equal inputs") }
lines := d.Lines() if lines == nil { t.Fatal("Lines() returned nil for non-equal inputs") }
removed := 0 added := 0
for _, l := range lines { switch l.Kind { case diff.KindRemoved: removed++ case diff.KindAdded: added++ default: // KindHeader and KindContext lines are not counted } }
if removed != 512 { t.Errorf("removed line count = %d, want 512", removed) }
if added != 512 { t.Errorf("added line count = %d, want 512", added) }}
func clean(text []byte) []byte { text = bytes.ReplaceAll(text, []byte("$\n"), []byte("\n")) text = bytes.TrimSuffix(text, []byte("^D\n"))
return text}