package gitcmd import ( "testing" ) func findFile(t *testing.T, d *DiffResult, path string) *FileChange { t.Helper() for i := range d.Files { if d.Files[i].Path == path { return &d.Files[i] } } return nil } func TestCommitDiff_Modify(t *testing.T) { f := newFixture(t) d, err := f.repo.CommitDiff(f.c3) if err != nil { t.Fatal(err) } if len(d.Files) != 1 { t.Fatalf("files = %d, want 1: %+v", len(d.Files), d.Files) } fc := &d.Files[0] if fc.Path != "README.md" || fc.Status != "M" { t.Errorf("file = %+v, want README.md/M", fc) } if fc.Additions != 1 || fc.Deletions != 1 { t.Errorf("adds/dels = %d/%d, want 1/1", fc.Additions, fc.Deletions) } if len(fc.Hunks) != 1 { t.Fatalf("hunks = %d, want 1", len(fc.Hunks)) } h := fc.Hunks[0] if h.OldStart != 1 || h.OldCount != 2 || h.NewStart != 1 || h.NewCount != 2 { t.Errorf("hunk header = -%d,%d +%d,%d, want -1,2 +1,2", h.OldStart, h.OldCount, h.NewStart, h.NewCount) } wantLines := []DiffLine{ {DiffLineContext, "line1"}, {DiffLineDelete, "line2"}, {DiffLineAdd, "line3"}, } if len(h.Lines) != len(wantLines) { t.Fatalf("lines = %d, want %d: %+v", len(h.Lines), len(wantLines), h.Lines) } for i, wl := range wantLines { if h.Lines[i] != wl { t.Errorf("line[%d] = %+v, want %+v", i, h.Lines[i], wl) } } } func TestCommitDiff_Add(t *testing.T) { f := newFixture(t) d, err := f.repo.CommitDiff(f.c2) if err != nil { t.Fatal(err) } if len(d.Files) != 2 { t.Fatalf("files = %d, want 2", len(d.Files)) } fc := findFile(t, d, "src/main.txt") if fc == nil { t.Fatal("src/main.txt not in diff") } if fc.Status != "A" || fc.Additions != 1 || fc.Deletions != 0 { t.Errorf("src/main.txt = %+v, want A/1/0", fc) } if len(fc.Hunks) != 1 { t.Fatalf("hunks = %d, want 1", len(fc.Hunks)) } h := fc.Hunks[0] if h.OldStart != 0 || h.OldCount != 0 || h.NewStart != 1 || h.NewCount != 1 { t.Errorf("hunk header = -%d,%d +%d,%d, want -0,0 +1,1", h.OldStart, h.OldCount, h.NewStart, h.NewCount) } if len(h.Lines) != 1 || h.Lines[0].Type != DiffLineAdd || h.Lines[0].Text != "main line" { t.Errorf("hunk lines = %+v, want [{add 'main line'}]", h.Lines) } } func TestCommitDiff_Delete(t *testing.T) { f := newFixture(t) d, err := f.repo.CommitDiff(f.c5) if err != nil { t.Fatal(err) } if len(d.Files) != 1 { t.Fatalf("files = %d, want 1", len(d.Files)) } fc := &d.Files[0] if fc.Path != "deleted.txt" || fc.Status != "D" { t.Errorf("file = %+v, want deleted.txt/D", fc) } if fc.Additions != 0 || fc.Deletions != 1 { t.Errorf("adds/dels = %d/%d, want 0/1", fc.Additions, fc.Deletions) } h := fc.Hunks[0] if h.OldStart != 1 || h.OldCount != 1 || h.NewStart != 0 || h.NewCount != 0 { t.Errorf("hunk header = -%d,%d +%d,%d, want -1,1 +0,0", h.OldStart, h.OldCount, h.NewStart, h.NewCount) } if len(h.Lines) != 1 || h.Lines[0].Type != DiffLineDelete || h.Lines[0].Text != "to be deleted" { t.Errorf("hunk lines = %+v, want [{delete 'to be deleted'}]", h.Lines) } } func TestCommitDiff_Binary(t *testing.T) { f := newFixture(t) d, err := f.repo.CommitDiff(f.c4) if err != nil { t.Fatal(err) } if len(d.Files) != 1 { t.Fatalf("files = %d, want 1", len(d.Files)) } fc := &d.Files[0] if fc.Path != "binary.bin" || fc.Status != "A" || !fc.IsBinary { t.Errorf("file = %+v, want binary.bin/A/binary", fc) } if len(fc.Hunks) != 0 { t.Errorf("binary file should have no hunks, got %d", len(fc.Hunks)) } } func TestDiff_TwoTree(t *testing.T) { f := newFixture(t) d, err := f.repo.Diff(f.c1, f.c5, nil) if err != nil { t.Fatal(err) } if len(d.Files) != 3 { t.Fatalf("files = %d, want 3: %+v", len(d.Files), d.Files) } if fc := findFile(t, d, "README.md"); fc == nil || fc.Status != "M" || fc.Additions != 1 || fc.Deletions != 1 { t.Errorf("README.md = %+v, want M/1/1", fc) } if fc := findFile(t, d, "src/main.txt"); fc == nil || fc.Status != "A" || fc.Additions != 1 { t.Errorf("src/main.txt = %+v, want A/1", fc) } if fc := findFile(t, d, "binary.bin"); fc == nil || fc.Status != "A" || !fc.IsBinary { t.Errorf("binary.bin = %+v, want A/binary", fc) } } func TestDiff_PathLimit(t *testing.T) { f := newFixture(t) d, err := f.repo.Diff(f.c1, f.c5, []string{"README.md"}) if err != nil { t.Fatal(err) } if len(d.Files) != 1 || d.Files[0].Path != "README.md" { t.Fatalf("files = %+v, want only README.md", d.Files) } }