6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-05 15:58:47 +00:00

Add ahead/behind detection, clean up tests

This commit is contained in:
Grzegorz Dlugoszewski
2020-05-28 16:21:07 +02:00
parent 0b371341e7
commit ca9be3d98f
5 changed files with 375 additions and 214 deletions

View File

@@ -1,44 +1,149 @@
package new
import "testing"
import (
"reflect"
"testing"
)
func TestBranchStatusLocal(t *testing.T) {
tr := NewRepoWithCommit(t)
tr.NewBranch("branch")
repo, err := OpenRepo(tr.Path)
checkFatal(t, err)
err = repo.LoadStatus()
checkFatal(t, err)
if repo.Status.Branches["master"].Upstream != nil {
t.Errorf("'master' branch should not have an upstream")
func TestStatus(t *testing.T) {
var tests = []struct {
makeTestRepo func(*testing.T) *TestRepo
want *RepoStatus
}{
{NewRepoEmpty, &RepoStatus{
HasUntrackedFiles: false,
HasUncommittedChanges: false,
Branches: nil,
}},
{NewRepoWithUntracked, &RepoStatus{
HasUntrackedFiles: true,
HasUncommittedChanges: false,
Branches: nil,
}},
{NewRepoWithStaged, &RepoStatus{
HasUntrackedFiles: false,
HasUncommittedChanges: true,
Branches: nil,
}},
{NewRepoWithCommit, &RepoStatus{
HasUntrackedFiles: false,
HasUncommittedChanges: false,
Branches: []*BranchStatus{
{
Name: "master",
Upstream: "",
NeedsPull: false,
NeedsPush: false,
},
},
}},
{NewRepoWithModified, &RepoStatus{
HasUntrackedFiles: false,
HasUncommittedChanges: true,
Branches: []*BranchStatus{
{
Name: "master",
Upstream: "",
NeedsPull: false,
NeedsPush: false,
},
},
}},
{NewRepoWithIgnored, &RepoStatus{
HasUntrackedFiles: false,
HasUncommittedChanges: false,
Branches: []*BranchStatus{
{
Name: "master",
Upstream: "",
NeedsPull: false,
NeedsPush: false,
},
},
}},
{NewRepoWithLocalBranch, &RepoStatus{
HasUntrackedFiles: false,
HasUncommittedChanges: false,
Branches: []*BranchStatus{
{
Name: "local",
Upstream: "",
NeedsPull: false,
NeedsPush: false,
}, {
Name: "master",
Upstream: "",
NeedsPull: false,
NeedsPush: false,
},
},
}},
{NewRepoWithClonedBranch, &RepoStatus{
HasUntrackedFiles: false,
HasUncommittedChanges: false,
Branches: []*BranchStatus{
{
Name: "local",
Upstream: "",
NeedsPull: false,
NeedsPush: false,
}, {
Name: "master",
Upstream: "origin/master",
NeedsPull: false,
NeedsPush: false,
},
},
}},
{NewRepoWithBranchAhead, &RepoStatus{
HasUntrackedFiles: false,
HasUncommittedChanges: false,
Branches: []*BranchStatus{
{
Name: "master",
Upstream: "origin/master",
NeedsPull: false,
NeedsPush: true,
},
},
}},
{NewRepoWithBranchBehind, &RepoStatus{
HasUntrackedFiles: false,
HasUncommittedChanges: false,
Branches: []*BranchStatus{
{
Name: "master",
Upstream: "origin/master",
NeedsPull: true,
NeedsPush: false,
},
},
}},
{NewRepoWithBranchAheadAndBehind, &RepoStatus{
HasUntrackedFiles: false,
HasUncommittedChanges: false,
Branches: []*BranchStatus{
{
Name: "master",
Upstream: "origin/master",
NeedsPull: true,
NeedsPush: true,
},
},
}},
}
if repo.Status.Branches["branch"].Upstream != nil {
t.Errorf("'branch' branch should not have an upstream")
for _, test := range tests {
tr := test.makeTestRepo(t)
repo, err := OpenRepo(tr.Path)
checkFatal(t, err)
err = repo.LoadStatus()
checkFatal(t, err)
if !reflect.DeepEqual(repo.Status, test.want) {
t.Errorf("Wrong repo status, got: %+v; want: %+v", repo.Status, test.want)
}
}
}
func TestBranchStatusCloned(t *testing.T) {
origin := NewRepoWithCommit(t)
clone := origin.Clone()
clone.NewBranch("local")
repo, err := OpenRepo(clone.Path)
checkFatal(t, err)
err = repo.LoadStatus()
checkFatal(t, err)
if repo.Status.Branches["master"].Upstream == nil {
t.Errorf("'master' branch should have an upstream")
}
if repo.Status.Branches["local"].Upstream != nil {
t.Errorf("'local' branch should not have an upstream")
}
}