From c7abac388aadd274d4f23f996a15f8bba90f2a92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Mon, 27 May 2019 21:14:55 +0200 Subject: [PATCH] repo: refactor how test repo are created/cleaned --- 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 ++++ .../test/repo.go => repository/git_testing.go | 55 ++++++++++--------- tests/read_bugs_test.go | 22 +++----- 10 files changed, 86 insertions(+), 82 deletions(-) rename util/test/repo.go => repository/git_testing.go (50%) diff --git a/bridge/github/import_test.go b/bridge/github/import_test.go index 7f83130c54f61f7e14eb699944be8e978de072a4..1e31501b7ce8c81847db1678f3468ab7872ebc52 100644 --- a/bridge/github/import_test.go +++ b/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) diff --git a/bug/bug_actions_test.go b/bug/bug_actions_test.go index e35a7ece65808a6e3be51624b10de266917d0522..4bc58aea3007b845335c697a8cd5521a8674780d 100644 --- a/bug/bug_actions_test.go +++ b/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") diff --git a/bug/operation_test.go b/bug/operation_test.go index f9a7d1910a5299ef5f8901dccbd4bc292d2e9484..0ddb61c2d1524b082781379d36775ade7cecaa0c 100644 --- a/bug/operation_test.go +++ b/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 { diff --git a/cache/repo_cache_test.go b/cache/repo_cache_test.go index 8f4d47d0ce91501fa85d3047bd0603bbfd32b7a2..7e38b6bc7ccc86e1b702f3512591756737854a96 100644 --- a/cache/repo_cache_test.go +++ b/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) diff --git a/commands/select/select_test.go b/commands/select/select_test.go index 29fdb3b83d61f0bfb1ec100d54de4428770ee47b..b9594fee7eec51a5fb949658fce2bd67640f252f 100644 --- a/commands/select/select_test.go +++ b/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)) } diff --git a/graphql/graphql_test.go b/graphql/graphql_test.go index f8a3c0599946bb0fab6d1fbd71649187b45b1156..6784df96e6205ad8081d8737a5442aee3f12ea02 100644 --- a/graphql/graphql_test.go +++ b/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 { diff --git a/identity/identity_actions_test.go b/identity/identity_actions_test.go index 42563374a4aa02fc94adb02417f5ab0753b87a9d..142ffaa65f41061bd8c103ae2805fc25c90a429d 100644 --- a/identity/identity_actions_test.go +++ b/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) diff --git a/misc/random_bugs/create_random_bugs.go b/misc/random_bugs/create_random_bugs.go index 0657c808794f9f8271a424e858a8b6276890db81..ea8833ee89c222b6e217f4d751089599f557187e 100644 --- a/misc/random_bugs/create_random_bugs.go +++ b/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()) } diff --git a/util/test/repo.go b/repository/git_testing.go similarity index 50% rename from util/test/repo.go rename to repository/git_testing.go index c5d3c00099d8638702fb909f048ead75800f7ee0..dd597a8e4886ab17b4c5d437cccbfa7ddd4ce5c6 100644 --- a/util/test/repo.go +++ b/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) - } -} diff --git a/tests/read_bugs_test.go b/tests/read_bugs_test.go index 8b4379e7af601821b79bb932cfef68ba84637a7b..c510fdbea828cdcee3a653d27493acfa8526b1dd 100644 --- a/tests/read_bugs_test.go +++ b/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++ {