diff options
Diffstat (limited to 'cache')
-rw-r--r-- | cache/repo_cache_bug.go | 16 | ||||
-rw-r--r-- | cache/repo_cache_test.go | 53 |
2 files changed, 69 insertions, 0 deletions
diff --git a/cache/repo_cache_bug.go b/cache/repo_cache_bug.go index 30692363..bcbfcea3 100644 --- a/cache/repo_cache_bug.go +++ b/cache/repo_cache_bug.go @@ -359,3 +359,19 @@ func (c *RepoCache) NewBugRaw(author *IdentityCache, unixTime int64, title strin return cached, op, nil } + +// RemoveBug removes a bug from the cache and repo given a bug id prefix +func (c *RepoCache) RemoveBug(prefix string) error { + b, err := c.ResolveBugPrefix(prefix) + + if err != nil { + return err + } + + err = bug.RemoveBug(c.repo, b.Id()) + + delete(c.bugs, b.Id()) + delete(c.bugExcerpts, b.Id()) + + return c.writeBugCache() +} diff --git a/cache/repo_cache_test.go b/cache/repo_cache_test.go index 0a333c8f..0deb155e 100644 --- a/cache/repo_cache_test.go +++ b/cache/repo_cache_test.go @@ -1,10 +1,14 @@ package cache import ( + "fmt" "testing" + "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/query" "github.com/MichaelMure/git-bug/repository" ) @@ -157,3 +161,52 @@ 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)) + assert.Equal(t, 100, len(repoCache.bugExcerpts)) + + _, err = repoCache.ResolveBug(b1.Id()) + assert.Error(t, bug.ErrBugNotExist, err) +} |