mirror of
https://github.com/grdl/git-get.git
synced 2026-02-11 23:49:04 +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:
@@ -51,11 +51,10 @@ func (r *Repo) checkout(name string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repo) clone() *Repo {
|
func (r *Repo) clone() *Repo {
|
||||||
dir, err := tempDir("")
|
dir := tempDir(r.t, "")
|
||||||
checkFatal(r.t, err)
|
|
||||||
|
|
||||||
url := fmt.Sprintf("file://%s/.git", r.path)
|
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)
|
checkFatal(r.t, err)
|
||||||
|
|
||||||
clone := &Repo{
|
clone := &Repo{
|
||||||
@@ -63,7 +62,6 @@ func (r *Repo) clone() *Repo {
|
|||||||
t: r.t,
|
t: r.t,
|
||||||
}
|
}
|
||||||
|
|
||||||
clone.t.Cleanup(r.cleanup)
|
|
||||||
return clone
|
return clone
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,11 +71,20 @@ func (r *Repo) fetch() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// tempDir creates a temporary directory inside the parent dir.
|
// tempDir creates a temporary directory inside the parent dir.
|
||||||
// If parent is empty it will use a system default temp dir (usually /tmp).
|
// If parent is empty, it will use a system default temp dir (usually /tmp).
|
||||||
func tempDir(parent string) (string, error) {
|
func tempDir(t *testing.T, parent string) string {
|
||||||
dir, err := ioutil.TempDir(parent, "git-get-repo-")
|
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) {
|
func checkFatal(t *testing.T, err error) {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package test
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -13,32 +12,18 @@ type Repo struct {
|
|||||||
t *testing.T
|
t *testing.T
|
||||||
}
|
}
|
||||||
|
|
||||||
// Path returs path to a repository.
|
// Path returns path to a repository.
|
||||||
func (r *Repo) Path() string {
|
func (r *Repo) Path() string {
|
||||||
return r.path
|
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.
|
// RepoEmpty creates an empty git repo.
|
||||||
func RepoEmpty(t *testing.T) *Repo {
|
func RepoEmpty(t *testing.T) *Repo {
|
||||||
dir, err := tempDir("")
|
|
||||||
checkFatal(t, err)
|
|
||||||
|
|
||||||
r := &Repo{
|
r := &Repo{
|
||||||
path: dir,
|
path: tempDir(t, ""),
|
||||||
t: t,
|
t: t,
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Cleanup(r.cleanup)
|
|
||||||
|
|
||||||
r.init()
|
r.init()
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user