mirror of
https://github.com/grdl/git-get.git
synced 2026-02-05 01:29:42 +00:00
Refactor Repo code and tests
- Add Repo as a separate type - Merge together repo status and branches status - Load status on repo opening
This commit is contained in:
@@ -17,13 +17,13 @@ func TestStatusWithEmptyRepo(t *testing.T) {
|
||||
t.Errorf("Empty repo should have no status entries")
|
||||
}
|
||||
|
||||
status, err := NewRepoStatus(repo.Workdir())
|
||||
status, err := loadStatus(repo)
|
||||
checkFatal(t, err)
|
||||
|
||||
want := RepoStatus{
|
||||
want := &RepoStatus{
|
||||
HasUntrackedFiles: false,
|
||||
HasUncommittedChanges: false,
|
||||
BranchStatuses: status.BranchStatuses,
|
||||
Branches: status.Branches,
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(status, want) {
|
||||
@@ -46,13 +46,13 @@ func TestStatusWithUntrackedFile(t *testing.T) {
|
||||
t.Errorf("Invalid status, got %d; want %d", entries[0].Status, git.StatusWtNew)
|
||||
}
|
||||
|
||||
status, err := NewRepoStatus(repo.Workdir())
|
||||
status, err := loadStatus(repo)
|
||||
checkFatal(t, err)
|
||||
|
||||
want := RepoStatus{
|
||||
want := &RepoStatus{
|
||||
HasUntrackedFiles: true,
|
||||
HasUncommittedChanges: false,
|
||||
BranchStatuses: status.BranchStatuses,
|
||||
Branches: status.Branches,
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(status, want) {
|
||||
@@ -84,13 +84,13 @@ func TestStatusWithStagedFile(t *testing.T) {
|
||||
t.Errorf("Invalid status, got %d; want %d", entries[0].Status, git.StatusIndexNew)
|
||||
}
|
||||
|
||||
status, err := NewRepoStatus(repo.Workdir())
|
||||
status, err := loadStatus(repo)
|
||||
checkFatal(t, err)
|
||||
|
||||
want := RepoStatus{
|
||||
want := &RepoStatus{
|
||||
HasUntrackedFiles: false,
|
||||
HasUncommittedChanges: true,
|
||||
BranchStatuses: status.BranchStatuses,
|
||||
Branches: status.Branches,
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(status, want) {
|
||||
@@ -111,13 +111,13 @@ func TestStatusWithSingleCommit(t *testing.T) {
|
||||
t.Errorf("Repo with no uncommitted files should have no status entries")
|
||||
}
|
||||
|
||||
status, err := NewRepoStatus(repo.Workdir())
|
||||
status, err := loadStatus(repo)
|
||||
checkFatal(t, err)
|
||||
|
||||
want := RepoStatus{
|
||||
want := &RepoStatus{
|
||||
HasUntrackedFiles: false,
|
||||
HasUncommittedChanges: false,
|
||||
BranchStatuses: status.BranchStatuses,
|
||||
Branches: status.Branches,
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(status, want) {
|
||||
@@ -141,13 +141,13 @@ func TestStatusWithMultipleCommits(t *testing.T) {
|
||||
t.Errorf("Repo with no uncommitted files should have no status entries")
|
||||
}
|
||||
|
||||
status, err := NewRepoStatus(repo.Workdir())
|
||||
status, err := loadStatus(repo)
|
||||
checkFatal(t, err)
|
||||
|
||||
want := RepoStatus{
|
||||
want := &RepoStatus{
|
||||
HasUntrackedFiles: false,
|
||||
HasUncommittedChanges: false,
|
||||
BranchStatuses: status.BranchStatuses,
|
||||
Branches: status.Branches,
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(status, want) {
|
||||
@@ -159,19 +159,111 @@ func TestStatusCloned(t *testing.T) {
|
||||
origin := newTestRepo(t)
|
||||
dir := newTempDir(t)
|
||||
|
||||
repo, err := CloneRepo(origin.Path(), dir)
|
||||
err := CloneRepo(origin.Path(), dir)
|
||||
checkFatal(t, err)
|
||||
repo, err := OpenRepo(dir)
|
||||
checkFatal(t, err)
|
||||
|
||||
status, err := NewRepoStatus(repo.Workdir())
|
||||
status, err := loadStatus(repo.repo)
|
||||
checkFatal(t, err)
|
||||
|
||||
want := RepoStatus{
|
||||
want := &RepoStatus{
|
||||
HasUntrackedFiles: false,
|
||||
HasUncommittedChanges: false,
|
||||
BranchStatuses: status.BranchStatuses,
|
||||
Branches: status.Branches,
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(status, want) {
|
||||
t.Errorf("Wrong repo status, got %+v; want %+v", status, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBranchNewLocal(t *testing.T) {
|
||||
repo := newTestRepo(t)
|
||||
|
||||
createFile(t, repo, "file")
|
||||
stageFile(t, repo, "file")
|
||||
createCommit(t, repo, "Initial commit")
|
||||
branch := createBranch(t, repo, "branch")
|
||||
|
||||
status, err := branchStatus(branch)
|
||||
checkFatal(t, err)
|
||||
|
||||
want := BranchStatus{
|
||||
Name: "branch",
|
||||
IsRemote: false,
|
||||
HasUpstream: false,
|
||||
NeedsPull: false,
|
||||
NeedsPush: false,
|
||||
Ahead: 0,
|
||||
Behind: 0,
|
||||
}
|
||||
|
||||
if status != want {
|
||||
t.Errorf("Wrong branch status, got %+v; want %+v", status, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBranchCloned(t *testing.T) {
|
||||
origin := newTestRepo(t)
|
||||
createFile(t, origin, "file")
|
||||
stageFile(t, origin, "file")
|
||||
createCommit(t, origin, "Initial commit")
|
||||
|
||||
createBranch(t, origin, "branch")
|
||||
|
||||
dir := newTempDir(t)
|
||||
err := CloneRepo(origin.Path(), dir)
|
||||
checkFatal(t, err)
|
||||
repo, err := OpenRepo(dir)
|
||||
checkFatal(t, err)
|
||||
|
||||
createBranch(t, repo.repo, "local")
|
||||
|
||||
checkoutBranch(t, repo.repo, "branch")
|
||||
createFile(t, repo.repo, "anotherFile")
|
||||
stageFile(t, repo.repo, "anotherFile")
|
||||
createCommit(t, repo.repo, "Second commit")
|
||||
|
||||
err = repo.Reload()
|
||||
checkFatal(t, err)
|
||||
|
||||
var tests = []struct {
|
||||
got BranchStatus
|
||||
want BranchStatus
|
||||
}{
|
||||
{repo.Status.Branches["master"], BranchStatus{
|
||||
Name: "master",
|
||||
IsRemote: false,
|
||||
HasUpstream: true,
|
||||
}},
|
||||
{repo.Status.Branches["origin/master"], BranchStatus{
|
||||
Name: "origin/master",
|
||||
IsRemote: true,
|
||||
HasUpstream: false,
|
||||
}},
|
||||
{repo.Status.Branches["branch"], BranchStatus{
|
||||
Name: "branch",
|
||||
IsRemote: false,
|
||||
HasUpstream: true,
|
||||
Ahead: 1,
|
||||
NeedsPush: true,
|
||||
}},
|
||||
{repo.Status.Branches["origin/branch"], BranchStatus{
|
||||
Name: "origin/branch",
|
||||
IsRemote: true,
|
||||
HasUpstream: false,
|
||||
}},
|
||||
{repo.Status.Branches["local"], BranchStatus{
|
||||
Name: "local",
|
||||
IsRemote: false,
|
||||
HasUpstream: false,
|
||||
}},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if !reflect.DeepEqual(test.got, test.want) {
|
||||
t.Errorf("Wrong branch status, got %+v; want %+v", test.got, test.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user