diff options
-rw-r--r-- | bug/bug.go | 55 | ||||
-rw-r--r-- | bug/bug_test.go | 71 | ||||
-rw-r--r-- | cache/repo_cache_test.go | 53 |
3 files changed, 156 insertions, 23 deletions
@@ -109,8 +109,8 @@ func ReadLocalBug(repo repository.ClockedRepo, id entity.Id) (*Bug, error) { } // ReadRemoteBug will read a remote bug from its hash -func ReadRemoteBug(repo repository.ClockedRepo, remote string, id string) (*Bug, error) { - ref := fmt.Sprintf(bugsRemoteRefPattern, remote) + id +func ReadRemoteBug(repo repository.ClockedRepo, remote string, id entity.Id) (*Bug, error) { + ref := fmt.Sprintf(bugsRemoteRefPattern, remote) + id.String() return readBug(repo, ref) } @@ -242,18 +242,55 @@ func readBug(repo repository.ClockedRepo, ref string) (*Bug, error) { return &bug, nil } -// RemoveLocalBug will remove a local bug from its hash +// RemoveBug will remove a local bug from its entity.Id func RemoveBug(repo repository.ClockedRepo, id entity.Id) error { - refs, err := repo.ListRefs(id.String()) + matching := entity.Id("") + fullMatches := []string{} + + refs, err := repo.ListRefs(bugsRefPattern + id.String()) + if err != nil { + return err + } else if num := len(refs); num > 1 { + return NewErrMultipleMatchBug(refsToIds(refs)) + } else if num == 1 { + matching = refToId(refs[0]) + fullMatches = []string{refs[0]} + } + + remotes, err := repo.GetRemotes() if err != nil { return err } - for _, ref := range refs { + + for remote := range remotes { + remotePrefix := fmt.Sprintf(bugsRemoteRefPattern+id.String(), remote) + remoteRefs, err := repo.ListRefs(remotePrefix) + if err != nil { + return err + } else if num := len(remoteRefs); num > 1 { + return NewErrMultipleMatchBug(refsToIds(refs)) + } else if num == 1 { + id := refToId(remoteRefs[0]) + fullMatches = append(fullMatches, remoteRefs[0]) + if matching.String() == "" { + matching = id + } else if id != matching { + return NewErrMultipleMatchBug([]entity.Id{matching, id}) + } + } + } + + if matching == "" { + return ErrBugNotExist + } + + for _, ref := range fullMatches { err = repo.RemoveRef(ref) if err != nil { return err } } + return nil } @@ -315,13 +352,17 @@ func refsToIds(refs []string) []entity.Id { ids := make([]entity.Id, len(refs)) for i, ref := range refs { - split := strings.Split(ref, "/") - ids[i] = entity.Id(split[len(split)-1]) + ids[i] = refToId(ref) } return ids } +func refToId(ref string) entity.Id { + split := strings.Split(ref, "/") + return entity.Id(split[len(split)-1]) +} + // Validate check if the Bug data is valid func (bug *Bug) Validate() error { // non-empty diff --git a/bug/bug_test.go b/bug/bug_test.go index 43e760af..d6c80efc 100644 --- a/bug/bug_test.go +++ b/bug/bug_test.go @@ -1,10 +1,12 @@ package bug import ( + "fmt" "testing" "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/repository" @@ -64,7 +66,7 @@ func TestBugValidity(t *testing.T) { } } -func TestBugCommitLoadRemove(t *testing.T) { +func TestBugCommitLoad(t *testing.T) { bug1 := NewBug() rene := identity.NewIdentity("René Descartes", "rene@descartes.fr") @@ -100,16 +102,6 @@ func TestBugCommitLoadRemove(t *testing.T) { bug3, err := ReadLocalBug(repo, bug1.Id()) assert.NoError(t, err) equivalentBug(t, bug1, bug3) - - err = RemoveLocalBug(repo, bug1.Id()) - assert.NoError(t, err) - - streamedBugs := ReadAllLocalBugs(repo) - count := 0 - for range streamedBugs { - count++ - } - assert.Equal(t, 0, count) } func equivalentBug(t *testing.T, expected, actual *Bug) { @@ -123,3 +115,60 @@ func equivalentBug(t *testing.T, expected, actual *Bug) { assert.Equal(t, expected, actual) } + +func TestBugRemove(t *testing.T) { + repo := repository.CreateTestRepo(false) + remoteA := repository.CreateTestRepo(true) + remoteB := repository.CreateTestRepo(true) + defer repository.CleanupTestRepos(repo, remoteA, remoteB) + + err := repo.AddRemote("remoteA", "file://"+remoteA.GetPath()) + require.NoError(t, err) + + err = repo.AddRemote("remoteB", "file://"+remoteB.GetPath()) + require.NoError(t, err) + + // generate a bunch of bugs + rene := identity.NewIdentity("René Descartes", "rene@descartes.fr") + err = rene.Commit(repo) + require.NoError(t, err) + + for i := 0; i < 100; i++ { + b := NewBug() + createOp := NewCreateOp(rene, time.Now().Unix(), "title", fmt.Sprintf("message%v", i), nil) + b.Append(createOp) + err = b.Commit(repo) + require.NoError(t, err) + } + + // and one more for testing + b := NewBug() + createOp := NewCreateOp(rene, time.Now().Unix(), "title", "message", nil) + b.Append(createOp) + err = b.Commit(repo) + require.NoError(t, err) + + _, err = Push(repo, "remoteA") + require.NoError(t, err) + + _, err = Push(repo, "remoteB") + require.NoError(t, err) + + _, err = Fetch(repo, "remoteA") + require.NoError(t, err) + + _, err = Fetch(repo, "remoteB") + require.NoError(t, err) + + err = RemoveBug(repo, b.Id()) + require.NoError(t, err) + + _, err = ReadLocalBug(repo, b.Id()) + require.Error(t, ErrBugNotExist, err) + + _, err = ReadRemoteBug(repo, "remoteA", b.Id()) + require.Error(t, ErrBugNotExist, err) + + _, err = ReadRemoteBug(repo, "remoteB", b.Id()) + require.Error(t, ErrBugNotExist, err) +} diff --git a/cache/repo_cache_test.go b/cache/repo_cache_test.go index e5f8a037..e5603fca 100644 --- a/cache/repo_cache_test.go +++ b/cache/repo_cache_test.go @@ -1,8 +1,11 @@ package cache import ( + "fmt" "testing" + "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/MichaelMure/git-bug/query" @@ -101,11 +104,6 @@ func TestCache(t *testing.T) { require.NoError(t, err) _, err = cache.ResolveBugPrefix(bug1.Id().String()[:10]) require.NoError(t, err) - - // Possible to delete a bug - err = cache.RemoveBug(bug1.Id().Human(), "") - require.NoError(t, err) - require.Equal(t, len(cache.AllBugsIds()), 1) } func TestPushPull(t *testing.T) { @@ -162,3 +160,48 @@ func TestPushPull(t *testing.T) { require.Len(t, cacheA.AllBugsIds(), 2) } + +func TestRemove(t *testing.T) { + repo := repository.CreateTestRepo(false) + remoteA := repository.CreateTestRepo(true) + remoteB := repository.CreateTestRepo(true) + defer repository.CleanupTestRepos(repo, remoteA, remoteB) + + err := repo.AddRemote("remoteA", "file://"+remoteA.GetPath()) + require.NoError(t, err) + + err = repo.AddRemote("remoteB", "file://"+remoteB.GetPath()) + require.NoError(t, err) + + repoCache, err := NewRepoCache(repo) + require.NoError(t, err) + + // generate a bunch of bugs + rene, err := repoCache.NewIdentity("René Descartes", "rene@descartes.fr") + require.NoError(t, err) + + for i := 0; i < 100; i++ { + _, _, err := repoCache.NewBugRaw(rene, time.Now().Unix(), "title", fmt.Sprintf("message%v", i), nil, nil) + require.NoError(t, err) + } + + // and one more for testing + b1, _, err := repoCache.NewBugRaw(rene, time.Now().Unix(), "title", "message", nil, nil) + require.NoError(t, err) + + _, err = repoCache.Push("remoteA") + require.NoError(t, err) + + _, err = repoCache.Push("remoteB") + require.NoError(t, err) + + _, err = repoCache.Fetch("remoteA") + require.NoError(t, err) + + _, err = repoCache.Fetch("remoteB") + require.NoError(t, err) + + err = repoCache.RemoveBug(b1.Id().String()) + require.NoError(t, err) + assert.Equal(t, 100, len(repoCache.bugs)) +} |