aboutsummaryrefslogtreecommitdiffstats
path: root/repository
diff options
context:
space:
mode:
authorSteve Moyer <smoyer1@selesy.com>2022-06-16 09:02:52 -0400
committerSteve Moyer <smoyer1@selesy.com>2022-06-16 09:02:52 -0400
commit1d4667c825bb5d291070bd4463c39a16f950f674 (patch)
tree84f1101f4cddc3804955c2c2ff90e3687c1dafee /repository
parentd853a6fbc996762ab264452150558bb92bdbd6fc (diff)
downloadgit-bug-1d4667c825bb5d291070bd4463c39a16f950f674.tar.gz
refactor(809): eliminate need to defer CleanupTestRepos()
Diffstat (limited to 'repository')
-rw-r--r--repository/gogit_test.go2
-rw-r--r--repository/gogit_testing.go31
-rw-r--r--repository/mock_repo_test.go5
-rw-r--r--repository/repo_testing.go28
4 files changed, 27 insertions, 39 deletions
diff --git a/repository/gogit_test.go b/repository/gogit_test.go
index c376de22..8179874c 100644
--- a/repository/gogit_test.go
+++ b/repository/gogit_test.go
@@ -71,7 +71,7 @@ func TestNewGoGitRepo(t *testing.T) {
}
func TestGoGitRepo(t *testing.T) {
- RepoTest(t, CreateGoGitTestRepo, CleanupTestRepos)
+ RepoTest(t, CreateGoGitTestRepo)
}
func TestGoGitRepo_Indexes(t *testing.T) {
diff --git a/repository/gogit_testing.go b/repository/gogit_testing.go
index 7647c711..1b39fe5c 100644
--- a/repository/gogit_testing.go
+++ b/repository/gogit_testing.go
@@ -1,21 +1,26 @@
package repository
import (
- "io/ioutil"
"log"
+ "testing"
"github.com/99designs/keyring"
)
+type TestingT interface {
+ Cleanup(func())
+ Helper()
+ TempDir() string
+}
+
const namespace = "git-bug"
// This is intended for testing only
-func CreateGoGitTestRepo(bare bool) TestedRepo {
- dir, err := ioutil.TempDir("", "")
- if err != nil {
- log.Fatal(err)
- }
+func CreateGoGitTestRepo(t TestingT, bare bool) TestedRepo {
+ t.Helper()
+
+ dir := t.TempDir()
var creator func(string, string) (*GoGitRepo, error)
@@ -30,6 +35,10 @@ func CreateGoGitTestRepo(bare bool) TestedRepo {
log.Fatal(err)
}
+ t.Cleanup(func() {
+ repo.Close()
+ })
+
config := repo.LocalConfig()
if err := config.StoreString("user.name", "testuser"); err != nil {
log.Fatal("failed to set user.name for test repository: ", err)
@@ -45,10 +54,12 @@ func CreateGoGitTestRepo(bare bool) TestedRepo {
}
}
-func SetupGoGitReposAndRemote() (repoA, repoB, remote TestedRepo) {
- repoA = CreateGoGitTestRepo(false)
- repoB = CreateGoGitTestRepo(false)
- remote = CreateGoGitTestRepo(true)
+func SetupGoGitReposAndRemote(t *testing.T) (repoA, repoB, remote TestedRepo) {
+ t.Helper()
+
+ repoA = CreateGoGitTestRepo(t, false)
+ repoB = CreateGoGitTestRepo(t, false)
+ remote = CreateGoGitTestRepo(t, true)
err := repoA.AddRemote("origin", remote.GetLocalRemote())
if err != nil {
diff --git a/repository/mock_repo_test.go b/repository/mock_repo_test.go
index 12851a80..f43e7ea6 100644
--- a/repository/mock_repo_test.go
+++ b/repository/mock_repo_test.go
@@ -5,8 +5,7 @@ import (
)
func TestMockRepo(t *testing.T) {
- creator := func(bare bool) TestedRepo { return NewMockRepo() }
- cleaner := func(repos ...Repo) {}
+ creator := func(t TestingT, bare bool) TestedRepo { return NewMockRepo() }
- RepoTest(t, creator, cleaner)
+ RepoTest(t, creator)
}
diff --git a/repository/repo_testing.go b/repository/repo_testing.go
index 0db585cd..c5bbe0c4 100644
--- a/repository/repo_testing.go
+++ b/repository/repo_testing.go
@@ -1,7 +1,6 @@
package repository
import (
- "log"
"math/rand"
"testing"
@@ -14,37 +13,16 @@ import (
// TODO: add tests for RepoBleve
// TODO: add tests for RepoStorage
-func CleanupTestRepos(repos ...Repo) {
- var firstErr error
- for _, repo := range repos {
- if repo, ok := repo.(TestedRepo); ok {
- err := repo.EraseFromDisk()
- if err != nil {
- log.Println(err)
- if firstErr == nil {
- firstErr = err
- }
- }
- }
- }
-
- if firstErr != nil {
- log.Fatal(firstErr)
- }
-}
-
-type RepoCreator func(bare bool) TestedRepo
-type RepoCleaner func(repos ...Repo)
+type RepoCreator func(t TestingT, bare bool) TestedRepo
// Test suite for a Repo implementation
-func RepoTest(t *testing.T, creator RepoCreator, cleaner RepoCleaner) {
+func RepoTest(t *testing.T, creator RepoCreator) {
for bare, name := range map[bool]string{
false: "Plain",
true: "Bare",
} {
t.Run(name, func(t *testing.T) {
- repo := creator(bare)
- defer cleaner(repo)
+ repo := creator(t, bare)
t.Run("Data", func(t *testing.T) {
RepoDataTest(t, repo)