6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-12 19:20:14 +00:00

Refactor tests by removing redundant TestRepo struct

This commit is contained in:
Grzegorz Dlugoszewski
2020-05-29 10:11:08 +02:00
parent 0e551ea342
commit 3335c81aa4
5 changed files with 135 additions and 160 deletions

View File

@@ -1,6 +1,6 @@
# git-get # git-get
`git get` - a better way to clone and manage git repositories. `git get` - a better way to clone, organize and manage git repositories.
## Features ## Features
@@ -15,10 +15,3 @@ Show repo status:
- ahead/behind? - ahead/behind?
- submodules? - submodules?
find upstream branch
https://github.com/src-d/go-git/issues/600
ahead/behind:
https://stackoverflow.com/questions/57566661/use-go-git-to-check-if-branch-has-been-pushed-to-remote

View File

@@ -2,7 +2,6 @@ package pkg
import ( import (
"io/ioutil" "io/ioutil"
pkgurl "net/url"
"os" "os"
"testing" "testing"
"time" "time"
@@ -15,129 +14,115 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
type TestRepo struct { func newRepoEmpty(t *testing.T) *Repo {
Repo *git.Repository dir := newTempDir(t)
Path string
URL *pkgurl.URL
t *testing.T
}
func NewRepoEmpty(t *testing.T) *TestRepo {
dir := NewTempDir(t)
repo, err := git.PlainInit(dir, false) repo, err := git.PlainInit(dir, false)
checkFatal(t, err) checkFatal(t, err)
url, err := ParseURL("file://" + dir) return newRepo(repo, dir)
checkFatal(t, err)
return &TestRepo{
Repo: repo,
Path: dir,
URL: url,
t: t,
}
} }
func NewRepoWithUntracked(t *testing.T) *TestRepo { func newRepoWithUntracked(t *testing.T) *Repo {
tr := NewRepoEmpty(t) r := newRepoEmpty(t)
tr.WriteFile("README", "I'm a README file") r.writeFile(t, "README", "I'm a README file")
return tr return r
} }
func NewRepoWithStaged(t *testing.T) *TestRepo { func newRepoWithStaged(t *testing.T) *Repo {
tr := NewRepoEmpty(t) r := newRepoEmpty(t)
tr.WriteFile("README", "I'm a README file") r.writeFile(t, "README", "I'm a README file")
tr.AddFile("README") r.addFile(t, "README")
return tr return r
}
func NewRepoWithCommit(t *testing.T) *TestRepo {
tr := NewRepoEmpty(t)
tr.WriteFile("README", "I'm a README file")
tr.AddFile("README")
tr.NewCommit("Initial commit")
return tr
} }
func NewRepoWithModified(t *testing.T) *TestRepo { func newRepoWithCommit(t *testing.T) *Repo {
tr := NewRepoEmpty(t) r := newRepoEmpty(t)
tr.WriteFile("README", "I'm a README file") r.writeFile(t, "README", "I'm a README file")
tr.AddFile("README") r.addFile(t, "README")
tr.NewCommit("Initial commit") r.newCommit(t, "Initial commit")
tr.WriteFile("README", "I'm modified")
return tr return r
} }
func NewRepoWithIgnored(t *testing.T) *TestRepo { func newRepoWithModified(t *testing.T) *Repo {
tr := NewRepoEmpty(t) r := newRepoEmpty(t)
tr.WriteFile(".gitignore", "ignoreme") r.writeFile(t, "README", "I'm a README file")
tr.AddFile(".gitignore") r.addFile(t, "README")
tr.NewCommit("Initial commit") r.newCommit(t, "Initial commit")
tr.WriteFile("ignoreme", "I'm being ignored") r.writeFile(t, "README", "I'm modified")
return tr return r
} }
func NewRepoWithLocalBranch(t *testing.T) *TestRepo { func newRepoWithIgnored(t *testing.T) *Repo {
tr := NewRepoWithCommit(t) r := newRepoEmpty(t)
tr.NewBranch("local") r.writeFile(t, ".gitignore", "ignoreme")
return tr r.addFile(t, ".gitignore")
r.newCommit(t, "Initial commit")
r.writeFile(t, "ignoreme", "I'm being ignored")
return r
} }
func NewRepoWithClonedBranch(t *testing.T) *TestRepo { func newRepoWithLocalBranch(t *testing.T) *Repo {
origin := NewRepoWithCommit(t) r := newRepoWithCommit(t)
r.newBranch(t, "local")
tr := origin.Clone() return r
tr.NewBranch("local")
return tr
} }
func NewRepoWithBranchAhead(t *testing.T) *TestRepo { func newRepoWithClonedBranch(t *testing.T) *Repo {
origin := NewRepoWithCommit(t) origin := newRepoWithCommit(t)
tr := origin.Clone() r := origin.clone(t)
tr.WriteFile("new", "I'm a new file") r.newBranch(t, "local")
tr.AddFile("new")
tr.NewCommit("New commit")
return tr return r
} }
func NewRepoWithBranchBehind(t *testing.T) *TestRepo { func newRepoWithBranchAhead(t *testing.T) *Repo {
origin := NewRepoWithCommit(t) origin := newRepoWithCommit(t)
tr := origin.Clone() r := origin.clone(t)
r.writeFile(t, "new", "I'm a new file")
r.addFile(t, "new")
r.newCommit(t, "new commit")
origin.WriteFile("origin.new", "I'm a new file on origin") return r
origin.AddFile("origin.new")
origin.NewCommit("New origin commit")
tr.Fetch()
return tr
} }
func NewRepoWithBranchAheadAndBehind(t *testing.T) *TestRepo { func newRepoWithBranchBehind(t *testing.T) *Repo {
origin := NewRepoWithCommit(t) origin := newRepoWithCommit(t)
tr := origin.Clone() r := origin.clone(t)
tr.WriteFile("local.new", "I'm a new file on local")
tr.AddFile("local.new")
tr.NewCommit("New local commit")
origin.WriteFile("origin.new", "I'm a new file on origin") origin.writeFile(t, "origin.new", "I'm a new file on origin")
origin.AddFile("origin.new") origin.addFile(t, "origin.new")
origin.NewCommit("New origin commit") origin.newCommit(t, "new origin commit")
tr.Fetch() r.fetch(t)
return tr return r
} }
func NewTempDir(t *testing.T) string { func newRepoWithBranchAheadAndBehind(t *testing.T) *Repo {
origin := newRepoWithCommit(t)
r := origin.clone(t)
r.writeFile(t, "local.new", "I'm a new file on local")
r.addFile(t, "local.new")
r.newCommit(t, "new local commit")
origin.writeFile(t, "origin.new", "I'm a new file on origin")
origin.addFile(t, "origin.new")
origin.newCommit(t, "new origin commit")
r.fetch(t)
return r
}
func newTempDir(t *testing.T) string {
dir, err := ioutil.TempDir("", "git-get-repo-") dir, err := ioutil.TempDir("", "git-get-repo-")
checkFatal(t, errors.Wrap(err, "Failed creating test repo directory")) checkFatal(t, errors.Wrap(err, "Failed creating test repo directory"))
@@ -152,28 +137,28 @@ func NewTempDir(t *testing.T) string {
return dir return dir
} }
func (r *TestRepo) WriteFile(name string, content string) { func (r *Repo) writeFile(t *testing.T, name string, content string) {
wt, err := r.Repo.Worktree() wt, err := r.repo.Worktree()
checkFatal(r.t, errors.Wrap(err, "Failed getting worktree")) checkFatal(t, errors.Wrap(err, "Failed getting workree"))
file, err := wt.Filesystem.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) file, err := wt.Filesystem.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
checkFatal(r.t, errors.Wrap(err, "Failed opening a file")) checkFatal(t, errors.Wrap(err, "Failed opening a file"))
_, err = file.Write([]byte(content)) _, err = file.Write([]byte(content))
checkFatal(r.t, errors.Wrap(err, "Failed writing a file")) checkFatal(t, errors.Wrap(err, "Failed writing a file"))
} }
func (r *TestRepo) AddFile(name string) { func (r *Repo) addFile(t *testing.T, name string) {
wt, err := r.Repo.Worktree() wt, err := r.repo.Worktree()
checkFatal(r.t, errors.Wrap(err, "Failed getting worktree")) checkFatal(t, errors.Wrap(err, "Failed getting workree"))
_, err = wt.Add(name) _, err = wt.Add(name)
checkFatal(r.t, errors.Wrap(err, "Failed adding file to index")) checkFatal(t, errors.Wrap(err, "Failed adding file to index"))
} }
func (r *TestRepo) NewCommit(msg string) { func (r *Repo) newCommit(t *testing.T, msg string) {
wt, err := r.Repo.Worktree() wt, err := r.repo.Worktree()
checkFatal(r.t, errors.Wrap(err, "Failed getting worktree")) checkFatal(t, errors.Wrap(err, "Failed getting workree"))
opts := &git.CommitOptions{ opts := &git.CommitOptions{
Author: &object.Signature{ Author: &object.Signature{
@@ -184,43 +169,33 @@ func (r *TestRepo) NewCommit(msg string) {
} }
_, err = wt.Commit(msg, opts) _, err = wt.Commit(msg, opts)
checkFatal(r.t, errors.Wrap(err, "Failed creating commit")) checkFatal(t, errors.Wrap(err, "Failed creating commit"))
} }
func (r *TestRepo) NewBranch(name string) { func (r *Repo) newBranch(t *testing.T, name string) {
head, err := r.Repo.Head() head, err := r.repo.Head()
checkFatal(r.t, err) checkFatal(t, err)
ref := plumbing.NewHashReference(plumbing.NewBranchReferenceName(name), head.Hash()) ref := plumbing.NewHashReference(plumbing.NewBranchReferenceName(name), head.Hash())
err = r.Repo.Storer.SetReference(ref) err = r.repo.Storer.SetReference(ref)
checkFatal(r.t, err) checkFatal(t, err)
} }
func (r *TestRepo) Clone() *TestRepo { func (r *Repo) clone(t *testing.T) *Repo {
dir := NewTempDir(r.t) dir := newTempDir(t)
url, err := ParseURL("file://" + r.path)
checkFatal(t, err)
repo, err := CloneRepo(r.URL, dir, true) repo, err := CloneRepo(url, dir, true)
checkFatal(r.t, err) checkFatal(t, err)
url, err := ParseURL("file://" + dir) return repo
checkFatal(r.t, err)
return &TestRepo{
Repo: repo.repo,
Path: dir,
URL: url,
t: r.t,
}
} }
func (r *TestRepo) Fetch() { func (r *Repo) fetch(t *testing.T) {
repo := &Repo{ err := r.Fetch()
repo: r.Repo, checkFatal(t, err)
}
err := repo.Fetch()
checkFatal(r.t, err)
} }
func checkFatal(t *testing.T, err error) { func checkFatal(t *testing.T, err error) {

View File

@@ -1,9 +1,11 @@
package pkg package pkg
import ( import (
"fmt"
"io" "io"
"net/url" "net/url"
"os" "os"
"path"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -12,13 +14,18 @@ import (
type Repo struct { type Repo struct {
repo *git.Repository repo *git.Repository
path string
Status *RepoStatus Status *RepoStatus
} }
func CloneRepo(url *url.URL, path string, quiet bool) (r *Repo, err error) { func CloneRepo(url *url.URL, reposRoot string, quiet bool) (*Repo, error) {
repoSubPath := URLToPath(url)
repoPath := path.Join(reposRoot, repoSubPath)
var output io.Writer var output io.Writer
if !quiet { if !quiet {
output = os.Stdout output = os.Stdout
fmt.Printf("Cloning into '%s'...\n", repoPath)
} }
opts := &git.CloneOptions{ opts := &git.CloneOptions{
@@ -34,26 +41,27 @@ func CloneRepo(url *url.URL, path string, quiet bool) (r *Repo, err error) {
Tags: git.AllTags, Tags: git.AllTags,
} }
repo, err := git.PlainClone(path, false, opts) repo, err := git.PlainClone(repoPath, false, opts)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "Failed cloning repo") return nil, errors.Wrap(err, "Failed cloning repo")
} }
return newRepo(repo), nil return newRepo(repo, repoPath), nil
} }
func OpenRepo(path string) (r *Repo, err error) { func OpenRepo(repoPath string) (*Repo, error) {
repo, err := git.PlainOpen(path) repo, err := git.PlainOpen(repoPath)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "Failed opening repo") return nil, errors.Wrap(err, "Failed opening repo")
} }
return newRepo(repo), nil return newRepo(repo, repoPath), nil
} }
func newRepo(repo *git.Repository) *Repo { func newRepo(repo *git.Repository, repoPath string) *Repo {
return &Repo{ return &Repo{
repo: repo, repo: repo,
path: repoPath,
Status: &RepoStatus{}, Status: &RepoStatus{},
} }
} }

View File

@@ -129,7 +129,7 @@ func (r *Repo) newBranchStatus(branch string) (*BranchStatus, error) {
// //
// Information about upstream is taken from .git/config file. // Information about upstream is taken from .git/config file.
// If a branch has an upstream, there's a [branch] section in the file with two fields: // If a branch has an upstream, there's a [branch] section in the file with two fields:
// "remote" - name of the remote containing upstreamn branch (or "." if upstream is a local branch) // "remote" - name of the remote containing upstream branch (or "." if upstream is a local branch)
// "merge" - full ref name of the upstream branch (eg, ref/heads/master) // "merge" - full ref name of the upstream branch (eg, ref/heads/master)
func (r *Repo) upstream(branch string) (string, error) { func (r *Repo) upstream(branch string) (string, error) {
cfg, err := r.repo.Config() cfg, err := r.repo.Config()

View File

@@ -7,25 +7,25 @@ import (
func TestStatus(t *testing.T) { func TestStatus(t *testing.T) {
var tests = []struct { var tests = []struct {
makeTestRepo func(*testing.T) *TestRepo makeTestRepo func(*testing.T) *Repo
want *RepoStatus want *RepoStatus
}{ }{
{NewRepoEmpty, &RepoStatus{ {newRepoEmpty, &RepoStatus{
HasUntrackedFiles: false, HasUntrackedFiles: false,
HasUncommittedChanges: false, HasUncommittedChanges: false,
Branches: nil, Branches: nil,
}}, }},
{NewRepoWithUntracked, &RepoStatus{ {newRepoWithUntracked, &RepoStatus{
HasUntrackedFiles: true, HasUntrackedFiles: true,
HasUncommittedChanges: false, HasUncommittedChanges: false,
Branches: nil, Branches: nil,
}}, }},
{NewRepoWithStaged, &RepoStatus{ {newRepoWithStaged, &RepoStatus{
HasUntrackedFiles: false, HasUntrackedFiles: false,
HasUncommittedChanges: true, HasUncommittedChanges: true,
Branches: nil, Branches: nil,
}}, }},
{NewRepoWithCommit, &RepoStatus{ {newRepoWithCommit, &RepoStatus{
HasUntrackedFiles: false, HasUntrackedFiles: false,
HasUncommittedChanges: false, HasUncommittedChanges: false,
Branches: []*BranchStatus{ Branches: []*BranchStatus{
@@ -37,7 +37,7 @@ func TestStatus(t *testing.T) {
}, },
}, },
}}, }},
{NewRepoWithModified, &RepoStatus{ {newRepoWithModified, &RepoStatus{
HasUntrackedFiles: false, HasUntrackedFiles: false,
HasUncommittedChanges: true, HasUncommittedChanges: true,
Branches: []*BranchStatus{ Branches: []*BranchStatus{
@@ -49,7 +49,7 @@ func TestStatus(t *testing.T) {
}, },
}, },
}}, }},
{NewRepoWithIgnored, &RepoStatus{ {newRepoWithIgnored, &RepoStatus{
HasUntrackedFiles: false, HasUntrackedFiles: false,
HasUncommittedChanges: false, HasUncommittedChanges: false,
Branches: []*BranchStatus{ Branches: []*BranchStatus{
@@ -61,7 +61,7 @@ func TestStatus(t *testing.T) {
}, },
}, },
}}, }},
{NewRepoWithLocalBranch, &RepoStatus{ {newRepoWithLocalBranch, &RepoStatus{
HasUntrackedFiles: false, HasUntrackedFiles: false,
HasUncommittedChanges: false, HasUncommittedChanges: false,
Branches: []*BranchStatus{ Branches: []*BranchStatus{
@@ -78,7 +78,7 @@ func TestStatus(t *testing.T) {
}, },
}, },
}}, }},
{NewRepoWithClonedBranch, &RepoStatus{ {newRepoWithClonedBranch, &RepoStatus{
HasUntrackedFiles: false, HasUntrackedFiles: false,
HasUncommittedChanges: false, HasUncommittedChanges: false,
Branches: []*BranchStatus{ Branches: []*BranchStatus{
@@ -95,7 +95,7 @@ func TestStatus(t *testing.T) {
}, },
}, },
}}, }},
{NewRepoWithBranchAhead, &RepoStatus{ {newRepoWithBranchAhead, &RepoStatus{
HasUntrackedFiles: false, HasUntrackedFiles: false,
HasUncommittedChanges: false, HasUncommittedChanges: false,
Branches: []*BranchStatus{ Branches: []*BranchStatus{
@@ -107,7 +107,7 @@ func TestStatus(t *testing.T) {
}, },
}, },
}}, }},
{NewRepoWithBranchBehind, &RepoStatus{ {newRepoWithBranchBehind, &RepoStatus{
HasUntrackedFiles: false, HasUntrackedFiles: false,
HasUncommittedChanges: false, HasUncommittedChanges: false,
Branches: []*BranchStatus{ Branches: []*BranchStatus{
@@ -119,7 +119,7 @@ func TestStatus(t *testing.T) {
}, },
}, },
}}, }},
{NewRepoWithBranchAheadAndBehind, &RepoStatus{ {newRepoWithBranchAheadAndBehind, &RepoStatus{
HasUntrackedFiles: false, HasUntrackedFiles: false,
HasUncommittedChanges: false, HasUncommittedChanges: false,
Branches: []*BranchStatus{ Branches: []*BranchStatus{
@@ -134,12 +134,9 @@ func TestStatus(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
tr := test.makeTestRepo(t) repo := test.makeTestRepo(t)
repo, err := OpenRepo(tr.Path) err := repo.LoadStatus()
checkFatal(t, err)
err = repo.LoadStatus()
checkFatal(t, err) checkFatal(t, err)
if !reflect.DeepEqual(repo.Status, test.want) { if !reflect.DeepEqual(repo.Status, test.want) {
@@ -147,3 +144,5 @@ func TestStatus(t *testing.T) {
} }
} }
} }
// TODO: test branch status when tracking a local branch