diff --git a/pkg/test/helpers.go b/pkg/test/helpers.go index c662b32..9135a57 100644 --- a/pkg/test/helpers.go +++ b/pkg/test/helpers.go @@ -51,11 +51,10 @@ func (r *Repo) checkout(name string) { } func (r *Repo) clone() *Repo { - dir, err := tempDir("") - checkFatal(r.t, err) + dir := tempDir(r.t, "") url := fmt.Sprintf("file://%s/.git", r.path) - err = run.Git("clone", url, dir).AndShutUp() + err := run.Git("clone", url, dir).AndShutUp() checkFatal(r.t, err) clone := &Repo{ @@ -63,7 +62,6 @@ func (r *Repo) clone() *Repo { t: r.t, } - clone.t.Cleanup(r.cleanup) return clone } @@ -73,11 +71,20 @@ func (r *Repo) fetch() { } // 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(parent string) (string, error) { +// 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) - return dir, 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) { diff --git a/pkg/test/testrepos.go b/pkg/test/testrepos.go index 2143ad2..1f42310 100644 --- a/pkg/test/testrepos.go +++ b/pkg/test/testrepos.go @@ -1,7 +1,6 @@ package test import ( - "os" "path/filepath" "testing" ) @@ -13,32 +12,18 @@ type Repo struct { t *testing.T } -// Path returs path to a repository. +// Path returns path to a repository. func (r *Repo) Path() string { return r.path } -// TODO: this should be a method of a tempDir, not a repo -// Automatically remove test repo when the test is over. -func (r *Repo) cleanup() { - err := os.RemoveAll(r.path) - if err != nil { - r.t.Errorf("failed removing test repo directory %s", r.path) - } -} - // RepoEmpty creates an empty git repo. func RepoEmpty(t *testing.T) *Repo { - dir, err := tempDir("") - checkFatal(t, err) - r := &Repo{ - path: dir, + path: tempDir(t, ""), t: t, } - t.Cleanup(r.cleanup) - r.init() return r }