git-bug/commands/select/select_test.go

81 lines
2.1 KiB
Go
Raw Normal View History

package _select
import (
"testing"
2019-02-16 21:34:52 +03:00
"time"
"github.com/MichaelMure/git-bug/cache"
2019-02-16 21:34:52 +03:00
"github.com/MichaelMure/git-bug/identity"
2019-02-16 19:32:30 +03:00
"github.com/MichaelMure/git-bug/util/test"
"github.com/stretchr/testify/require"
)
func TestSelect(t *testing.T) {
2019-02-16 19:32:30 +03:00
repo := test.CreateRepo(false)
2019-02-16 19:32:30 +03:00
repoCache, err := cache.NewRepoCache(repo)
require.NoError(t, err)
2019-02-16 19:32:30 +03:00
_, _, err = ResolveBug(repoCache, []string{})
require.Equal(t, ErrNoValidId, err)
2019-02-16 19:32:30 +03:00
err = Select(repoCache, "invalid")
require.NoError(t, err)
// Resolve without a pattern should fail when no bug is selected
_, _, err = ResolveBug(repoCache, []string{})
require.Error(t, err)
// generate a bunch of bugs
2019-02-16 21:34:52 +03:00
rene := identity.NewBare("René Descartes", "rene@descartes.fr")
for i := 0; i < 10; i++ {
2019-02-16 21:34:52 +03:00
_, err := repoCache.NewBugRaw(rene, time.Now().Unix(), "title", "message", nil, nil)
2019-02-16 19:32:30 +03:00
require.NoError(t, err)
}
2019-02-16 19:32:30 +03:00
// and two more for testing
2019-02-16 21:34:52 +03:00
b1, err := repoCache.NewBugRaw(rene, time.Now().Unix(), "title", "message", nil, nil)
2019-02-16 19:32:30 +03:00
require.NoError(t, err)
2019-02-16 21:34:52 +03:00
b2, err := repoCache.NewBugRaw(rene, time.Now().Unix(), "title", "message", nil, nil)
2019-02-16 19:32:30 +03:00
require.NoError(t, err)
2019-02-16 19:32:30 +03:00
err = Select(repoCache, b1.Id())
require.NoError(t, err)
// normal select without args
2019-02-16 19:32:30 +03:00
b3, _, err := ResolveBug(repoCache, []string{})
require.NoError(t, err)
require.Equal(t, b1.Id(), b3.Id())
// override selection with same id
2019-02-16 19:32:30 +03:00
b4, _, err := ResolveBug(repoCache, []string{b1.Id()})
require.NoError(t, err)
require.Equal(t, b1.Id(), b4.Id())
// override selection with a prefix
2019-02-16 19:32:30 +03:00
b5, _, err := ResolveBug(repoCache, []string{b1.HumanId()})
require.NoError(t, err)
require.Equal(t, b1.Id(), b5.Id())
// args that shouldn't override
2019-02-16 19:32:30 +03:00
b6, _, err := ResolveBug(repoCache, []string{"arg"})
require.NoError(t, err)
require.Equal(t, b1.Id(), b6.Id())
// override with a different id
2019-02-16 19:32:30 +03:00
b7, _, err := ResolveBug(repoCache, []string{b2.Id()})
require.NoError(t, err)
require.Equal(t, b2.Id(), b7.Id())
2019-02-16 19:32:30 +03:00
err = Clear(repoCache)
require.NoError(t, err)
2019-02-16 19:32:30 +03:00
// Resolve without a pattern should error again after clearing the selected bug
_, _, err = ResolveBug(repoCache, []string{})
require.Error(t, err)
2019-02-16 21:34:52 +03:00
require.NoError(t, test.CleanupRepo(repo))
}