From cd7ed7ff9e3250c10e97fe16c934b5a6151527bb Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sat, 16 Feb 2019 13:48:46 +0100 Subject: identity: add more test for serialisation and push/pull/merge + fixes --- bug/bug_actions.go | 7 ++--- bug/bug_actions_test.go | 84 ++++++------------------------------------------- bug/operation_test.go | 3 +- 3 files changed, 13 insertions(+), 81 deletions(-) (limited to 'bug') diff --git a/bug/bug_actions.go b/bug/bug_actions.go index 6b9135b0..f214716d 100644 --- a/bug/bug_actions.go +++ b/bug/bug_actions.go @@ -23,8 +23,7 @@ func Push(repo repository.Repo, remote string) (string, error) { } // Pull will do a Fetch + MergeAll -// This function won't give details on the underlying process. If you need more, -// use Fetch and MergeAll separately. +// This function will return an error if a merge fail func Pull(repo repository.ClockedRepo, remote string) error { _, err := Fetch(repo, remote) if err != nil { @@ -36,9 +35,7 @@ func Pull(repo repository.ClockedRepo, remote string) error { return merge.Err } if merge.Status == MergeStatusInvalid { - // Not awesome: simply output the merge failure here as this function - // is only used in tests for now. - fmt.Println(merge) + return errors.Errorf("merge failure: %s", merge.Reason) } } diff --git a/bug/bug_actions_test.go b/bug/bug_actions_test.go index af561bf6..39438ec7 100644 --- a/bug/bug_actions_test.go +++ b/bug/bug_actions_test.go @@ -1,81 +1,15 @@ package bug import ( - "github.com/MichaelMure/git-bug/repository" + "github.com/MichaelMure/git-bug/util/test" "github.com/stretchr/testify/assert" - "io/ioutil" - "log" - "os" "testing" ) -func createRepo(bare bool) *repository.GitRepo { - dir, err := ioutil.TempDir("", "") - if err != nil { - log.Fatal(err) - } - - // fmt.Println("Creating repo:", dir) - - var creator func(string) (*repository.GitRepo, error) - - if bare { - creator = repository.InitBareGitRepo - } else { - creator = repository.InitGitRepo - } - - repo, err := creator(dir) - if err != nil { - log.Fatal(err) - } - - if err := repo.StoreConfig("user.name", "testuser"); err != nil { - log.Fatal("failed to set user.name for test repository: ", err) - } - if err := repo.StoreConfig("user.email", "testuser@example.com"); err != nil { - log.Fatal("failed to set user.email for test repository: ", err) - } - - return repo -} - -func cleanupRepo(repo repository.Repo) error { - path := repo.GetPath() - // fmt.Println("Cleaning repo:", path) - return os.RemoveAll(path) -} - -func setupRepos(t testing.TB) (repoA, repoB, remote *repository.GitRepo) { - repoA = createRepo(false) - repoB = createRepo(false) - remote = createRepo(true) - - remoteAddr := "file://" + remote.GetPath() - - err := repoA.AddRemote("origin", remoteAddr) - if err != nil { - t.Fatal(err) - } - - err = repoB.AddRemote("origin", remoteAddr) - if err != nil { - t.Fatal(err) - } - - return repoA, repoB, remote -} - -func cleanupRepos(repoA, repoB, remote *repository.GitRepo) { - cleanupRepo(repoA) - cleanupRepo(repoB) - cleanupRepo(remote) -} - func TestPushPull(t *testing.T) { - repoA, repoB, remote := setupRepos(t) - defer cleanupRepos(repoA, repoB, remote) + repoA, repoB, remote := test.SetupReposAndRemote(t) + defer test.CleanupRepos(repoA, repoB, remote) err := rene.Commit(repoA) assert.NoError(t, err) @@ -139,8 +73,8 @@ func BenchmarkRebaseTheirs(b *testing.B) { } func _RebaseTheirs(t testing.TB) { - repoA, repoB, remote := setupRepos(t) - defer cleanupRepos(repoA, repoB, remote) + repoA, repoB, remote := test.SetupReposAndRemote(t) + defer test.CleanupRepos(repoA, repoB, remote) bug1, _, err := Create(rene, unix, "bug1", "message") assert.NoError(t, err) @@ -200,8 +134,8 @@ func BenchmarkRebaseOurs(b *testing.B) { } func _RebaseOurs(t testing.TB) { - repoA, repoB, remote := setupRepos(t) - defer cleanupRepos(repoA, repoB, remote) + repoA, repoB, remote := test.SetupReposAndRemote(t) + defer test.CleanupRepos(repoA, repoB, remote) bug1, _, err := Create(rene, unix, "bug1", "message") assert.NoError(t, err) @@ -281,8 +215,8 @@ func BenchmarkRebaseConflict(b *testing.B) { } func _RebaseConflict(t testing.TB) { - repoA, repoB, remote := setupRepos(t) - defer cleanupRepos(repoA, repoB, remote) + repoA, repoB, remote := test.SetupReposAndRemote(t) + defer test.CleanupRepos(repoA, repoB, remote) bug1, _, err := Create(rene, unix, "bug1", "message") assert.NoError(t, err) diff --git a/bug/operation_test.go b/bug/operation_test.go index 083ccb1e..d5cb5090 100644 --- a/bug/operation_test.go +++ b/bug/operation_test.go @@ -6,6 +6,7 @@ import ( "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" ) @@ -76,7 +77,7 @@ func TestMetadata(t *testing.T) { func TestHash(t *testing.T) { repos := []repository.ClockedRepo{ repository.NewMockRepoForTest(), - createRepo(false), + test.CreateRepo(false), } for _, repo := range repos { -- cgit