2018-09-18 13:49:16 +03:00
|
|
|
package _select
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-02-16 21:34:52 +03:00
|
|
|
"time"
|
2018-09-18 13:49:16 +03:00
|
|
|
|
|
|
|
"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"
|
2018-09-18 13:49:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSelect(t *testing.T) {
|
2019-02-16 19:32:30 +03:00
|
|
|
repo := test.CreateRepo(false)
|
2018-09-18 13:49:16 +03:00
|
|
|
|
2019-02-16 19:32:30 +03:00
|
|
|
repoCache, err := cache.NewRepoCache(repo)
|
|
|
|
require.NoError(t, err)
|
2018-09-18 13:49:16 +03:00
|
|
|
|
2019-02-16 19:32:30 +03:00
|
|
|
_, _, err = ResolveBug(repoCache, []string{})
|
|
|
|
require.Equal(t, ErrNoValidId, err)
|
2018-09-18 13:49:16 +03:00
|
|
|
|
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)
|
2018-09-18 13:49:16 +03:00
|
|
|
|
|
|
|
// generate a bunch of bugs
|
2019-02-16 21:34:52 +03:00
|
|
|
|
|
|
|
rene := identity.NewBare("René Descartes", "rene@descartes.fr")
|
|
|
|
|
2018-09-18 13:49:16 +03:00
|
|
|
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)
|
2018-09-18 13:49:16 +03:00
|
|
|
}
|
|
|
|
|
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)
|
2018-09-18 13:49:16 +03:00
|
|
|
|
2019-02-16 19:32:30 +03:00
|
|
|
err = Select(repoCache, b1.Id())
|
|
|
|
require.NoError(t, err)
|
2018-09-18 13:49:16 +03:00
|
|
|
|
|
|
|
// 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())
|
2018-09-18 13:49:16 +03:00
|
|
|
|
|
|
|
// 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())
|
2018-09-18 13:49:16 +03:00
|
|
|
|
|
|
|
// 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())
|
2018-09-18 13:49:16 +03:00
|
|
|
|
|
|
|
// 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())
|
2018-09-18 13:49:16 +03:00
|
|
|
|
|
|
|
// 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())
|
2018-09-18 13:49:16 +03:00
|
|
|
|
2019-02-16 19:32:30 +03:00
|
|
|
err = Clear(repoCache)
|
|
|
|
require.NoError(t, err)
|
2018-09-18 13:49:16 +03:00
|
|
|
|
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))
|
2018-09-18 13:49:16 +03:00
|
|
|
}
|