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

Add repo finder tests

This commit is contained in:
Grzegorz Dlugoszewski
2020-07-08 14:58:19 +02:00
parent cdca8b89d9
commit 13e936c376
4 changed files with 116 additions and 20 deletions

View File

@@ -9,6 +9,23 @@ import (
"testing"
)
// TempDir creates a temporary directory inside the parent dir.
// If parent is empty, it will use a system default temp dir (usually /tmp).
func TempDir(t *testing.T, parent string) string {
dir, err := ioutil.TempDir(parent, "git-get-repo-")
checkFatal(t, err)
// Automatically remove temp dir when the test is over.
t.Cleanup(func() {
err := os.RemoveAll(dir)
if err != nil {
t.Errorf("failed removing test repo %s", dir)
}
})
return dir
}
func (r *Repo) init() {
err := run.Git("init", "--quiet", r.path).AndShutUp()
checkFatal(r.t, err)
@@ -51,7 +68,7 @@ func (r *Repo) checkout(name string) {
}
func (r *Repo) clone() *Repo {
dir := tempDir(r.t, "")
dir := TempDir(r.t, "")
url := fmt.Sprintf("file://%s/.git", r.path)
err := run.Git("clone", url, dir).AndShutUp()
@@ -70,23 +87,6 @@ func (r *Repo) fetch() {
checkFatal(r.t, err)
}
// tempDir creates a temporary directory inside the parent dir.
// If parent is empty, it will use a system default temp dir (usually /tmp).
func tempDir(t *testing.T, parent string) string {
dir, err := ioutil.TempDir(parent, "git-get-repo-")
checkFatal(t, err)
// Automatically remove temp dir when the test is over.
t.Cleanup(func() {
err := os.RemoveAll(dir)
if err != nil {
t.Errorf("failed removing test repo %s", dir)
}
})
return dir
}
func checkFatal(t *testing.T, err error) {
if err != nil {
t.Fatalf("failed making test repo: %+v", err)

View File

@@ -19,8 +19,13 @@ func (r *Repo) Path() string {
// RepoEmpty creates an empty git repo.
func RepoEmpty(t *testing.T) *Repo {
return RepoEmptyInDir(t, "")
}
// RepoEmptyInDir creates an empty git repo inside a given parent dir.
func RepoEmptyInDir(t *testing.T, parent string) *Repo {
r := &Repo{
path: tempDir(t, ""),
path: TempDir(t, parent),
t: t,
}