6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-09 06:49:26 +00:00

Simplify RepoStatus unit tests

This commit is contained in:
Grzegorz Dlugoszewski
2020-05-19 11:22:41 +02:00
parent 17ec470340
commit a0f09ef74d
2 changed files with 37 additions and 26 deletions

View File

@@ -11,7 +11,7 @@ type RepoStatus struct {
BranchStatuses []BranchStatus BranchStatuses []BranchStatus
} }
func Status(path string) (RepoStatus, error) { func NewRepoStatus(path string) (RepoStatus, error) {
var status RepoStatus var status RepoStatus
repo, err := git.OpenRepository(path) repo, err := git.OpenRepository(path)

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"reflect"
"testing" "testing"
git "github.com/libgit2/git2go/v30" git "github.com/libgit2/git2go/v30"
@@ -16,15 +17,17 @@ func TestStatusWithEmptyRepo(t *testing.T) {
t.Errorf("Empty repo should have no status entries") t.Errorf("Empty repo should have no status entries")
} }
status, err := Status(repo.Workdir()) status, err := NewRepoStatus(repo.Workdir())
checkFatal(t, err) checkFatal(t, err)
if status.HasUntrackedFiles != false { want := RepoStatus{
t.Errorf("Repo should not have untracked files") HasUntrackedFiles: false,
HasUncommittedChanges: false,
BranchStatuses: nil,
} }
if status.HasUncommittedChanges != false { if !reflect.DeepEqual(status, want) {
t.Errorf("Repo should not have uncommitted changes") t.Errorf("Wrong repo status, got %+v; want %+v", status, want)
} }
} }
@@ -43,15 +46,17 @@ func TestStatusWithUntrackedFile(t *testing.T) {
t.Errorf("Invalid status, got %d; want %d", entries[0].Status, git.StatusWtNew) t.Errorf("Invalid status, got %d; want %d", entries[0].Status, git.StatusWtNew)
} }
status, err := Status(repo.Workdir()) status, err := NewRepoStatus(repo.Workdir())
checkFatal(t, err) checkFatal(t, err)
if status.HasUntrackedFiles != true { want := RepoStatus{
t.Errorf("Repo should have untracked files") HasUntrackedFiles: true,
HasUncommittedChanges: false,
BranchStatuses: nil,
} }
if status.HasUncommittedChanges != false { if !reflect.DeepEqual(status, want) {
t.Errorf("Repo should not have uncommitted changes") t.Errorf("Wrong repo status, got %+v; want %+v", status, want)
} }
} }
@@ -79,15 +84,17 @@ func TestStatusWithStagedFile(t *testing.T) {
t.Errorf("Invalid status, got %d; want %d", entries[0].Status, git.StatusIndexNew) t.Errorf("Invalid status, got %d; want %d", entries[0].Status, git.StatusIndexNew)
} }
status, err := Status(repo.Workdir()) status, err := NewRepoStatus(repo.Workdir())
checkFatal(t, err) checkFatal(t, err)
if status.HasUntrackedFiles != false { want := RepoStatus{
t.Errorf("Repo should not have untracked files") HasUntrackedFiles: false,
HasUncommittedChanges: true,
BranchStatuses: nil,
} }
if status.HasUncommittedChanges != true { if !reflect.DeepEqual(status, want) {
t.Errorf("Repo should have uncommitted changes") t.Errorf("Wrong repo status, got %+v; want %+v", status, want)
} }
} }
@@ -104,15 +111,17 @@ func TestStatusWithSingleCommit(t *testing.T) {
t.Errorf("Repo with no uncommitted files should have no status entries") t.Errorf("Repo with no uncommitted files should have no status entries")
} }
status, err := Status(repo.Workdir()) status, err := NewRepoStatus(repo.Workdir())
checkFatal(t, err) checkFatal(t, err)
if status.HasUntrackedFiles != false { want := RepoStatus{
t.Errorf("Repo should not have untracked files") HasUntrackedFiles: false,
HasUncommittedChanges: false,
BranchStatuses: nil,
} }
if status.HasUncommittedChanges != false { if !reflect.DeepEqual(status, want) {
t.Errorf("Repo should not have uncommitted changes") t.Errorf("Wrong repo status, got %+v; want %+v", status, want)
} }
} }
@@ -131,14 +140,16 @@ func TestStatusWithMultipleCommits(t *testing.T) {
if len(entries) != 0 { if len(entries) != 0 {
t.Errorf("Repo with no uncommitted files should have no status entries") t.Errorf("Repo with no uncommitted files should have no status entries")
} }
status, err := Status(repo.Workdir()) status, err := NewRepoStatus(repo.Workdir())
checkFatal(t, err) checkFatal(t, err)
if status.HasUntrackedFiles != false { want := RepoStatus{
t.Errorf("Repo should not have untracked files") HasUntrackedFiles: false,
HasUncommittedChanges: false,
BranchStatuses: nil,
} }
if status.HasUncommittedChanges != false { if !reflect.DeepEqual(status, want) {
t.Errorf("Repo should not have uncommitted changes") t.Errorf("Wrong repo status, got %+v; want %+v", status, want)
} }
} }