diff options
author | Michael Muré <batolettre@gmail.com> | 2019-05-27 21:14:55 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2019-05-27 21:14:55 +0200 |
commit | c7abac388aadd274d4f23f996a15f8bba90f2a92 (patch) | |
tree | ced1116de500346eb340f72b7bfc672136e40c1e | |
parent | 9865dfcdf0810e288283f2ef4eba450041db32f9 (diff) | |
download | git-bug-c7abac388aadd274d4f23f996a15f8bba90f2a92.tar.gz |
repo: refactor how test repo are created/cleaned
-rw-r--r-- | bridge/github/import_test.go | 5 | ||||
-rw-r--r-- | bug/bug_actions_test.go | 21 | ||||
-rw-r--r-- | bug/operation_test.go | 9 | ||||
-rw-r--r-- | cache/repo_cache_test.go | 10 | ||||
-rw-r--r-- | commands/select/select_test.go | 10 | ||||
-rw-r--r-- | graphql/graphql_test.go | 18 | ||||
-rw-r--r-- | identity/identity_actions_test.go | 7 | ||||
-rw-r--r-- | misc/random_bugs/create_random_bugs.go | 11 | ||||
-rw-r--r-- | repository/git_testing.go (renamed from util/test/repo.go) | 55 | ||||
-rw-r--r-- | tests/read_bugs_test.go | 22 |
10 files changed, 86 insertions, 82 deletions
diff --git a/bridge/github/import_test.go b/bridge/github/import_test.go index 7f83130c..1e31501b 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 e35a7ece..4bc58aea 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 f9a7d191..0ddb61c2 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 8f4d47d0..7e38b6bc 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 29fdb3b8..b9594fee 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 f8a3c059..6784df96 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 42563374..142ffaa6 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 0657c808..ea8833ee 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 index c5d3c000..dd597a8e 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 8b4379e7..c510fdbe 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++ { |