repo: refactor how test repo are created/cleaned

Michael MurΓ© created

Change summary

bridge/github/import_test.go           |  5 +-
bug/bug_actions_test.go                | 21 +++++-----
bug/operation_test.go                  |  9 +++-
cache/repo_cache_test.go               | 10 +++--
commands/select/select_test.go         | 10 ++--
graphql/graphql_test.go                | 18 ++-------
identity/identity_actions_test.go      |  7 ++-
misc/random_bugs/create_random_bugs.go | 11 +++++
repository/git_testing.go              | 55 ++++++++++++++-------------
tests/read_bugs_test.go                | 22 ++++-------
10 files changed, 86 insertions(+), 82 deletions(-)

Detailed changes

bridge/github/import_test.go πŸ”—

@@ -13,8 +13,8 @@ import (
 	"github.com/MichaelMure/git-bug/bug"
 	"github.com/MichaelMure/git-bug/cache"
 	"github.com/MichaelMure/git-bug/identity"
+	"github.com/MichaelMure/git-bug/repository"
 	"github.com/MichaelMure/git-bug/util/interrupt"
-	"github.com/MichaelMure/git-bug/util/test"
 )
 
 func Test_Importer(t *testing.T) {
@@ -124,7 +124,8 @@ func Test_Importer(t *testing.T) {
 		},
 	}
 
-	repo := test.CreateRepo(false)
+	repo := repository.CreateTestRepo(false)
+	defer repository.CleanupTestRepos(t, repo)
 
 	backend, err := cache.NewRepoCache(repo)
 	require.NoError(t, err)

bug/bug_actions_test.go πŸ”—

@@ -4,14 +4,15 @@ import (
 	"testing"
 	"time"
 
-	"github.com/MichaelMure/git-bug/identity"
-	"github.com/MichaelMure/git-bug/util/test"
 	"github.com/stretchr/testify/require"
+
+	"github.com/MichaelMure/git-bug/identity"
+	"github.com/MichaelMure/git-bug/repository"
 )
 
 func TestPushPull(t *testing.T) {
-	repoA, repoB, remote := test.SetupReposAndRemote(t)
-	defer test.CleanupRepos(repoA, repoB, remote)
+	repoA, repoB, remote := repository.SetupReposAndRemote(t)
+	defer repository.CleanupTestRepos(t, repoA, repoB, remote)
 
 	reneA := identity.NewIdentity("RenΓ© Descartes", "rene@descartes.fr")
 
@@ -83,8 +84,8 @@ func BenchmarkRebaseTheirs(b *testing.B) {
 }
 
 func _RebaseTheirs(t testing.TB) {
-	repoA, repoB, remote := test.SetupReposAndRemote(t)
-	defer test.CleanupRepos(repoA, repoB, remote)
+	repoA, repoB, remote := repository.SetupReposAndRemote(t)
+	defer repository.CleanupTestRepos(t, repoA, repoB, remote)
 
 	reneA := identity.NewIdentity("RenΓ© Descartes", "rene@descartes.fr")
 
@@ -156,8 +157,8 @@ func BenchmarkRebaseOurs(b *testing.B) {
 }
 
 func _RebaseOurs(t testing.TB) {
-	repoA, repoB, remote := test.SetupReposAndRemote(t)
-	defer test.CleanupRepos(repoA, repoB, remote)
+	repoA, repoB, remote := repository.SetupReposAndRemote(t)
+	defer repository.CleanupTestRepos(t, repoA, repoB, remote)
 
 	reneA := identity.NewIdentity("RenΓ© Descartes", "rene@descartes.fr")
 
@@ -245,8 +246,8 @@ func BenchmarkRebaseConflict(b *testing.B) {
 }
 
 func _RebaseConflict(t testing.TB) {
-	repoA, repoB, remote := test.SetupReposAndRemote(t)
-	defer test.CleanupRepos(repoA, repoB, remote)
+	repoA, repoB, remote := repository.SetupReposAndRemote(t)
+	defer repository.CleanupTestRepos(t, repoA, repoB, remote)
 
 	reneA := identity.NewIdentity("RenΓ© Descartes", "rene@descartes.fr")
 

bug/operation_test.go πŸ”—

@@ -4,11 +4,11 @@ import (
 	"testing"
 	"time"
 
+	"github.com/stretchr/testify/require"
+
 	"github.com/MichaelMure/git-bug/identity"
 	"github.com/MichaelMure/git-bug/repository"
 	"github.com/MichaelMure/git-bug/util/git"
-	"github.com/MichaelMure/git-bug/util/test"
-	"github.com/stretchr/testify/require"
 )
 
 func TestValidate(t *testing.T) {
@@ -80,9 +80,12 @@ func TestMetadata(t *testing.T) {
 }
 
 func TestHash(t *testing.T) {
+	repo := repository.CreateTestRepo(false)
+	defer repository.CleanupTestRepos(t, repo)
+
 	repos := []repository.ClockedRepo{
 		repository.NewMockRepoForTest(),
-		test.CreateRepo(false),
+		repo,
 	}
 
 	for _, repo := range repos {

cache/repo_cache_test.go πŸ”—

@@ -3,12 +3,14 @@ package cache
 import (
 	"testing"
 
-	"github.com/MichaelMure/git-bug/util/test"
 	"github.com/stretchr/testify/require"
+
+	"github.com/MichaelMure/git-bug/repository"
 )
 
 func TestCache(t *testing.T) {
-	repo := test.CreateRepo(false)
+	repo := repository.CreateTestRepo(false)
+	defer repository.CleanupTestRepos(t, repo)
 
 	cache, err := NewRepoCache(repo)
 	require.NoError(t, err)
@@ -101,8 +103,8 @@ func TestCache(t *testing.T) {
 }
 
 func TestPushPull(t *testing.T) {
-	repoA, repoB, remote := test.SetupReposAndRemote(t)
-	defer test.CleanupRepos(repoA, repoB, remote)
+	repoA, repoB, remote := repository.SetupReposAndRemote(t)
+	defer repository.CleanupTestRepos(t, repoA, repoB, remote)
 
 	cacheA, err := NewRepoCache(repoA)
 	require.NoError(t, err)

commands/select/select_test.go πŸ”—

@@ -4,13 +4,15 @@ import (
 	"testing"
 	"time"
 
-	"github.com/MichaelMure/git-bug/cache"
-	"github.com/MichaelMure/git-bug/util/test"
 	"github.com/stretchr/testify/require"
+
+	"github.com/MichaelMure/git-bug/cache"
+	"github.com/MichaelMure/git-bug/repository"
 )
 
 func TestSelect(t *testing.T) {
-	repo := test.CreateRepo(false)
+	repo := repository.CreateTestRepo(false)
+	defer repository.CleanupTestRepos(t, repo)
 
 	repoCache, err := cache.NewRepoCache(repo)
 	require.NoError(t, err)
@@ -75,6 +77,4 @@ func TestSelect(t *testing.T) {
 	// Resolve without a pattern should error again after clearing the selected bug
 	_, _, err = ResolveBug(repoCache, []string{})
 	require.Error(t, err)
-
-	require.NoError(t, test.CleanupRepo(repo))
 }

graphql/graphql_test.go πŸ”—

@@ -9,23 +9,13 @@ import (
 	"github.com/MichaelMure/git-bug/graphql/models"
 	"github.com/MichaelMure/git-bug/misc/random_bugs"
 	"github.com/MichaelMure/git-bug/repository"
-	"github.com/MichaelMure/git-bug/util/test"
 )
 
-func CreateFilledRepo(bugNumber int) repository.ClockedRepo {
-	repo := test.CreateRepo(false)
-
-	var seed int64 = 42
-	options := random_bugs.DefaultOptions()
-
-	options.BugNumber = bugNumber
-
-	random_bugs.CommitRandomBugsWithSeed(repo, options, seed)
-	return repo
-}
-
 func TestQueries(t *testing.T) {
-	repo := CreateFilledRepo(10)
+	repo := repository.CreateTestRepo(false)
+	defer repository.CleanupTestRepos(t, repo)
+
+	random_bugs.FillRepoWithSeed(repo, 10, 42)
 
 	handler, err := NewHandler(repo)
 	if err != nil {

identity/identity_actions_test.go πŸ”—

@@ -3,13 +3,14 @@ package identity
 import (
 	"testing"
 
-	"github.com/MichaelMure/git-bug/util/test"
 	"github.com/stretchr/testify/require"
+
+	"github.com/MichaelMure/git-bug/repository"
 )
 
 func TestPushPull(t *testing.T) {
-	repoA, repoB, remote := test.SetupReposAndRemote(t)
-	defer test.CleanupRepos(repoA, repoB, remote)
+	repoA, repoB, remote := repository.SetupReposAndRemote(t)
+	defer repository.CleanupTestRepos(t, repoA, repoB, remote)
 
 	identity1 := NewIdentity("name1", "email1")
 	err := identity1.Commit(repoA)

misc/random_bugs/create_random_bugs.go πŸ”—

@@ -29,6 +29,17 @@ func DefaultOptions() Options {
 	}
 }
 
+func FillRepo(repo repository.ClockedRepo, bugNumber int) {
+	FillRepoWithSeed(repo, bugNumber, time.Now().UnixNano())
+}
+
+func FillRepoWithSeed(repo repository.ClockedRepo, bugNumber int, seed int64) {
+	options := DefaultOptions()
+	options.BugNumber = bugNumber
+
+	CommitRandomBugsWithSeed(repo, options, seed)
+}
+
 func CommitRandomBugs(repo repository.ClockedRepo, opts Options) {
 	CommitRandomBugsWithSeed(repo, opts, time.Now().UnixNano())
 }

util/test/repo.go β†’ repository/git_testing.go πŸ”—

@@ -1,15 +1,15 @@
-package test
+package repository
 
 import (
 	"io/ioutil"
 	"log"
 	"os"
 	"testing"
-
-	"github.com/MichaelMure/git-bug/repository"
 )
 
-func CreateRepo(bare bool) *repository.GitRepo {
+// This is intended for testing only
+
+func CreateTestRepo(bare bool) *GitRepo {
 	dir, err := ioutil.TempDir("", "")
 	if err != nil {
 		log.Fatal(err)
@@ -17,12 +17,12 @@ func CreateRepo(bare bool) *repository.GitRepo {
 
 	// fmt.Println("Creating repo:", dir)
 
-	var creator func(string) (*repository.GitRepo, error)
+	var creator func(string) (*GitRepo, error)
 
 	if bare {
-		creator = repository.InitBareGitRepo
+		creator = InitBareGitRepo
 	} else {
-		creator = repository.InitGitRepo
+		creator = InitGitRepo
 	}
 
 	repo, err := creator(dir)
@@ -40,16 +40,29 @@ func CreateRepo(bare bool) *repository.GitRepo {
 	return repo
 }
 
-func CleanupRepo(repo repository.Repo) error {
-	path := repo.GetPath()
-	// fmt.Println("Cleaning repo:", path)
-	return os.RemoveAll(path)
+func CleanupTestRepos(t testing.TB, repos ...Repo) {
+	var firstErr error
+	for _, repo := range repos {
+		path := repo.GetPath()
+		// fmt.Println("Cleaning repo:", path)
+		err := os.RemoveAll(path)
+		if err != nil {
+			log.Println(err)
+			if firstErr == nil {
+				firstErr = err
+			}
+		}
+	}
+
+	if firstErr != nil {
+		t.Fatal(firstErr)
+	}
 }
 
-func SetupReposAndRemote(t testing.TB) (repoA, repoB, remote *repository.GitRepo) {
-	repoA = CreateRepo(false)
-	repoB = CreateRepo(false)
-	remote = CreateRepo(true)
+func SetupReposAndRemote(t testing.TB) (repoA, repoB, remote *GitRepo) {
+	repoA = CreateTestRepo(false)
+	repoB = CreateTestRepo(false)
+	remote = CreateTestRepo(true)
 
 	remoteAddr := "file://" + remote.GetPath()
 
@@ -65,15 +78,3 @@ func SetupReposAndRemote(t testing.TB) (repoA, repoB, remote *repository.GitRepo
 
 	return repoA, repoB, remote
 }
-
-func CleanupRepos(repoA, repoB, remote *repository.GitRepo) {
-	if err := CleanupRepo(repoA); err != nil {
-		log.Println(err)
-	}
-	if err := CleanupRepo(repoB); err != nil {
-		log.Println(err)
-	}
-	if err := CleanupRepo(remote); err != nil {
-		log.Println(err)
-	}
-}

tests/read_bugs_test.go πŸ”—

@@ -6,23 +6,14 @@ import (
 	"github.com/MichaelMure/git-bug/bug"
 	"github.com/MichaelMure/git-bug/misc/random_bugs"
 	"github.com/MichaelMure/git-bug/repository"
-	"github.com/MichaelMure/git-bug/util/test"
 )
 
-func CreateFilledRepo(bugNumber int) repository.ClockedRepo {
-	repo := test.CreateRepo(false)
-
-	var seed int64 = 42
-	options := random_bugs.DefaultOptions()
-
-	options.BugNumber = bugNumber
+func TestReadBugs(t *testing.T) {
+	repo := repository.CreateTestRepo(false)
+	defer repository.CleanupTestRepos(t, repo)
 
-	random_bugs.CommitRandomBugsWithSeed(repo, options, seed)
-	return repo
-}
+	random_bugs.FillRepoWithSeed(repo, 15, 42)
 
-func TestReadBugs(t *testing.T) {
-	repo := CreateFilledRepo(15)
 	bugs := bug.ReadAllLocalBugs(repo)
 	for b := range bugs {
 		if b.Err != nil {
@@ -32,7 +23,10 @@ func TestReadBugs(t *testing.T) {
 }
 
 func benchmarkReadBugs(bugNumber int, t *testing.B) {
-	repo := CreateFilledRepo(bugNumber)
+	repo := repository.CreateTestRepo(false)
+	defer repository.CleanupTestRepos(t, repo)
+
+	random_bugs.FillRepoWithSeed(repo, bugNumber, 42)
 	t.ResetTimer()
 
 	for n := 0; n < t.N; n++ {