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

Move temp dir cleanup into tempDir function

Cleaning up belongs to a directory, not an abstract git repo.
This commit is contained in:
Grzegorz Dlugoszewski
2020-07-08 13:54:52 +02:00
parent 8ab4681c26
commit cdca8b89d9
2 changed files with 16 additions and 24 deletions

View File

@@ -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) {

View File

@@ -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
}