diff options
author | Michael Muré <batolettre@gmail.com> | 2019-04-02 22:01:42 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2019-04-02 22:01:42 +0200 |
commit | 96987bf6ae528024b1c34006ce7820e3144f3914 (patch) | |
tree | 14a6d1ff3971ec58b0fb45e3471bf16300be9318 /cache | |
parent | 9765465b607a8f060a9386cb3cb84c94d0232009 (diff) | |
download | git-bug-96987bf6ae528024b1c34006ce7820e3144f3914.tar.gz |
cache: add a push/pull test
Diffstat (limited to 'cache')
-rw-r--r-- | cache/repo_cache.go | 21 | ||||
-rw-r--r-- | cache/repo_cache_test.go | 55 |
2 files changed, 76 insertions, 0 deletions
diff --git a/cache/repo_cache.go b/cache/repo_cache.go index fce473f4..afeb6140 100644 --- a/cache/repo_cache.go +++ b/cache/repo_cache.go @@ -19,6 +19,7 @@ import ( "github.com/MichaelMure/git-bug/repository" "github.com/MichaelMure/git-bug/util/git" "github.com/MichaelMure/git-bug/util/process" + "github.com/pkg/errors" ) const bugCacheFile = "bug-cache" @@ -675,6 +676,26 @@ func (c *RepoCache) Push(remote string) (string, error) { return stdout1 + stdout2, nil } +// Pull will do a Fetch + MergeAll +// This function will return an error if a merge fail +func (c *RepoCache) Pull(remote string) error { + _, err := c.Fetch(remote) + if err != nil { + return err + } + + for merge := range c.MergeAll(remote) { + if merge.Err != nil { + return merge.Err + } + if merge.Status == entity.MergeStatusInvalid { + return errors.Errorf("merge failure: %s", merge.Reason) + } + } + + return nil +} + func repoLockFilePath(repo repository.Repo) string { return path.Join(repo.GetPath(), ".git", "git-bug", lockfile) } diff --git a/cache/repo_cache_test.go b/cache/repo_cache_test.go index c90ad28e..8f4d47d0 100644 --- a/cache/repo_cache_test.go +++ b/cache/repo_cache_test.go @@ -99,3 +99,58 @@ func TestCache(t *testing.T) { _, err = cache.ResolveBugPrefix(bug1.Id()[:10]) require.NoError(t, err) } + +func TestPushPull(t *testing.T) { + repoA, repoB, remote := test.SetupReposAndRemote(t) + defer test.CleanupRepos(repoA, repoB, remote) + + cacheA, err := NewRepoCache(repoA) + require.NoError(t, err) + + cacheB, err := NewRepoCache(repoB) + require.NoError(t, err) + + // Create, set and get user identity + reneA, err := cacheA.NewIdentity("René Descartes", "rene@descartes.fr") + require.NoError(t, err) + err = cacheA.SetUserIdentity(reneA) + require.NoError(t, err) + + // distribute the identity + _, err = cacheA.Push("origin") + require.NoError(t, err) + err = cacheB.Pull("origin") + require.NoError(t, err) + + // Create a bug in A + _, err = cacheA.NewBug("bug1", "message") + require.NoError(t, err) + + // A --> remote --> B + _, err = cacheA.Push("origin") + require.NoError(t, err) + + err = cacheB.Pull("origin") + require.NoError(t, err) + + require.Len(t, cacheB.AllBugsIds(), 1) + + // retrieve and set identity + reneB, err := cacheB.ResolveIdentity(reneA.Id()) + require.NoError(t, err) + + err = cacheB.SetUserIdentity(reneB) + require.NoError(t, err) + + // B --> remote --> A + _, err = cacheB.NewBug("bug2", "message") + require.NoError(t, err) + + _, err = cacheB.Push("origin") + require.NoError(t, err) + + err = cacheA.Pull("origin") + require.NoError(t, err) + + require.Len(t, cacheA.AllBugsIds(), 2) +} |