6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-04 20:54:41 +00:00
Files
git-get/pkg/git/repo_test.go
Grzegorz Dlugoszewski 8c132cdafa Remove gogit and major refactoring (#2)
* Fix typo in readme

* Reimplement all git methods without go-git

* Rename repo pkg to git, add gitconfig methods

* Improve tests for configuration reading

* Rename package file to io and move RepoFinder there

* Refactor printers

- Remove smart printer
- Decouple printers from git repos with interfaces
- Update printer functions
- Remove unnecessary flags
- Add better remote URL detection

* Update readme and go.mod

* Add author to git commit in tests

Otherwise tests will fail in CI.

* Install git before running tests and don't use cgo

* Add better error message, revert installing git

* Ensure commit message is in quotes

* Set up git config before running tests
2020-06-24 23:54:44 +02:00

310 lines
6.1 KiB
Go

package git
import (
"git-get/pkg/io"
"reflect"
"testing"
)
func TestOpen(t *testing.T) {
_, err := Open("/paththatdoesnotexist/repo")
if err != io.ErrDirectoryAccess {
t.Errorf("Opening a repo in non existing path should throw an error")
}
}
func TestUncommitted(t *testing.T) {
tests := []struct {
name string
repoMaker func(*testing.T) *testRepo
want int
}{
{
name: "empty",
repoMaker: testRepoEmpty,
want: 0,
},
{
name: "single untracked",
repoMaker: testRepoWithUntracked,
want: 0,
},
{
name: "single tracked ",
repoMaker: testRepoWithStaged,
want: 1,
},
{
name: "committed",
repoMaker: testRepoWithCommit,
want: 0,
},
{
name: "untracked and uncommitted",
repoMaker: testRepoWithUncommittedAndUntracked,
want: 1,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r := test.repoMaker(t)
got, err := r.Uncommitted()
if err != nil {
t.Errorf("got error %q", err)
}
if got != test.want {
t.Errorf("expected %d; got %d", test.want, got)
}
})
}
}
func TestUntracked(t *testing.T) {
tests := []struct {
name string
repoMaker func(*testing.T) *testRepo
want int
}{
{
name: "empty",
repoMaker: testRepoEmpty,
want: 0,
},
{
name: "single untracked",
repoMaker: testRepoWithUntracked,
want: 0,
},
{
name: "single tracked ",
repoMaker: testRepoWithStaged,
want: 1,
},
{
name: "committed",
repoMaker: testRepoWithCommit,
want: 0,
},
{
name: "untracked and uncommitted",
repoMaker: testRepoWithUncommittedAndUntracked,
want: 1,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r := test.repoMaker(t)
got, err := r.Uncommitted()
if err != nil {
t.Errorf("got error %q", err)
}
if got != test.want {
t.Errorf("expected %d; got %d", test.want, got)
}
})
}
}
func TestCurrentBranch(t *testing.T) {
tests := []struct {
name string
repoMaker func(*testing.T) *testRepo
want string
}{
// TODO: maybe add wantErr to check if error is returned correctly?
// {
// name: "empty",
// repoMaker: newTestRepo,
// want: "",
// },
{
name: "only master branch",
repoMaker: testRepoWithCommit,
want: master,
},
{
name: "checked out new branch",
repoMaker: testRepoWithBranch,
want: "feature/branch",
},
{
name: "checked out new tag",
repoMaker: testRepoWithTag,
want: head,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r := test.repoMaker(t)
got, err := r.CurrentBranch()
if err != nil {
t.Errorf("got error %q", err)
}
if got != test.want {
t.Errorf("expected %q; got %q", test.want, got)
}
})
}
}
func TestBranches(t *testing.T) {
tests := []struct {
name string
repoMaker func(*testing.T) *testRepo
want []string
}{
{
name: "empty",
repoMaker: testRepoEmpty,
want: []string{""},
},
{
name: "only master branch",
repoMaker: testRepoWithCommit,
want: []string{"master"},
},
{
name: "new branch",
repoMaker: testRepoWithBranch,
want: []string{"feature/branch", "master"},
},
{
name: "checked out new tag",
repoMaker: testRepoWithTag,
want: []string{"master"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r := test.repoMaker(t)
got, err := r.Branches()
if err != nil {
t.Errorf("got error %q", err)
}
if !reflect.DeepEqual(got, test.want) {
t.Errorf("expected %+v; got %+v", test.want, got)
}
})
}
}
func TestUpstream(t *testing.T) {
tests := []struct {
name string
repoMaker func(*testing.T) *testRepo
branch string
want string
}{
{
name: "empty",
repoMaker: testRepoEmpty,
branch: "master",
want: "",
},
// TODO: add wantErr
{
name: "wrong branch name",
repoMaker: testRepoWithCommit,
branch: "wrong_branch_name",
want: "",
},
{
name: "master with upstream",
repoMaker: testRepoWithBranchWithUpstream,
branch: "master",
want: "origin/master",
},
{
name: "branch with upstream",
repoMaker: testRepoWithBranchWithUpstream,
branch: "feature/branch",
want: "origin/feature/branch",
},
{
name: "branch without upstream",
repoMaker: testRepoWithBranchWithoutUpstream,
branch: "feature/branch",
want: "",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r := test.repoMaker(t)
got, _ := r.Upstream(test.branch)
// TODO:
// if err != nil {
// t.Errorf("got error %q", err)
// }
if !reflect.DeepEqual(got, test.want) {
t.Errorf("expected %+v; got %+v", test.want, got)
}
})
}
}
func TestAheadBehind(t *testing.T) {
tests := []struct {
name string
repoMaker func(*testing.T) *testRepo
branch string
want []int
}{
{
name: "fresh clone",
repoMaker: testRepoWithBranchWithUpstream,
branch: "master",
want: []int{0, 0},
},
{
name: "branch ahead",
repoMaker: testRepoWithBranchAhead,
branch: "feature/branch",
want: []int{1, 0},
},
{
name: "branch behind",
repoMaker: testRepoWithBranchBehind,
branch: "feature/branch",
want: []int{0, 1},
},
{
name: "branch ahead and behind",
repoMaker: testRepoWithBranchAheadAndBehind,
branch: "feature/branch",
want: []int{2, 1},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r := test.repoMaker(t)
upstream, err := r.Upstream(test.branch)
if err != nil {
t.Errorf("got error %q", err)
}
ahead, behind, err := r.AheadBehind(test.branch, upstream)
if err != nil {
t.Errorf("got error %q", err)
}
if ahead != test.want[0] || behind != test.want[1] {
t.Errorf("expected %+v; got [%d, %d]", test.want, ahead, behind)
}
})
}
}