package gitcmd import ( "testing" ) func TestCompare_Ancestor(t *testing.T) { f := newFixture(t) c, err := f.repo.Compare(f.c1, f.c5) if err != nil { t.Fatal(err) } if c.Ahead != 4 || c.Behind != 0 { t.Errorf("ahead/behind = %d/%d, want 4/0", c.Ahead, c.Behind) } if len(c.Commits) != 4 { t.Fatalf("commits = %d, want 4", len(c.Commits)) } if c.Commits[0].ID != f.c5 || c.Commits[3].ID != f.c2 { t.Errorf("commit order = %s..%s, want c5..c2", c.Commits[0].ID, c.Commits[3].ID) } if len(c.Diff.Files) != 3 { t.Fatalf("diff files = %d, want 3: %+v", len(c.Diff.Files), c.Diff.Files) } if fc := findFile(t, &c.Diff, "README.md"); fc == nil || fc.Status != "M" { t.Errorf("README.md = %+v, want M", fc) } if fc := findFile(t, &c.Diff, "binary.bin"); fc == nil || !fc.IsBinary { t.Errorf("binary.bin = %+v, want binary", fc) } } func TestCompare_Identical(t *testing.T) { f := newFixture(t) c, err := f.repo.Compare("feature", f.c3) if err != nil { t.Fatal(err) } if c.Ahead != 0 || c.Behind != 0 { t.Errorf("ahead/behind = %d/%d, want 0/0", c.Ahead, c.Behind) } if len(c.Commits) != 0 || len(c.Diff.Files) != 0 { t.Errorf("expected empty commits/diff, got %d commits / %d files", len(c.Commits), len(c.Diff.Files)) } } func TestCompare_Diverged(t *testing.T) { f := newMergeFixture(t) c, err := f.repo.Compare(f.c3, f.c2) if err != nil { t.Fatal(err) } if c.Ahead != 1 || c.Behind != 1 { t.Errorf("ahead/behind = %d/%d, want 1/1", c.Ahead, c.Behind) } if len(c.Commits) != 1 || c.Commits[0].ID != f.c2 { t.Errorf("commits = %+v, want [c2]", c.Commits) } if len(c.Diff.Files) != 2 { t.Fatalf("diff files = %d, want 2: %+v", len(c.Diff.Files), c.Diff.Files) } if fc := findFile(t, &c.Diff, "c.txt"); fc == nil || fc.Status != "D" { t.Errorf("c.txt = %+v, want D", fc) } if fc := findFile(t, &c.Diff, "b.txt"); fc == nil || fc.Status != "A" { t.Errorf("b.txt = %+v, want A", fc) } } func TestCompare_Reversed(t *testing.T) { f := newFixture(t) c, err := f.repo.Compare(f.c5, f.c1) if err != nil { t.Fatal(err) } if c.Ahead != 0 || c.Behind != 4 { t.Errorf("ahead/behind = %d/%d, want 0/4", c.Ahead, c.Behind) } if len(c.Commits) != 0 { t.Errorf("commits = %d, want 0 (c1 is ancestor of c5)", len(c.Commits)) } if fc := findFile(t, &c.Diff, "src/main.txt"); fc == nil || fc.Status != "D" { t.Errorf("src/main.txt = %+v, want D (reversed)", fc) } }