2018-09-28 21:39:39 +03:00
|
|
|
package bug
|
2018-09-15 14:15:00 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-02-16 19:32:30 +03:00
|
|
|
"time"
|
2018-09-15 14:15:00 +03:00
|
|
|
|
2019-05-27 22:14:55 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
"github.com/MichaelMure/git-bug/identity"
|
2018-10-01 22:44:42 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2018-09-15 14:15:00 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidate(t *testing.T) {
|
2020-11-08 21:15:06 +03:00
|
|
|
repo := repository.NewMockRepoClock()
|
|
|
|
|
|
|
|
makeIdentity := func(t *testing.T, name, email string) *identity.Identity {
|
|
|
|
i, err := identity.NewIdentity(repo, name, email)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
rene := makeIdentity(t, "René Descartes", "rene@descartes.fr")
|
|
|
|
|
2019-02-16 19:32:30 +03:00
|
|
|
unix := time.Now().Unix()
|
|
|
|
|
2018-09-28 21:39:39 +03:00
|
|
|
good := []Operation{
|
2018-09-25 18:56:58 +03:00
|
|
|
NewCreateOp(rene, unix, "title", "message", nil),
|
|
|
|
NewSetTitleOp(rene, unix, "title2", "title1"),
|
|
|
|
NewAddCommentOp(rene, unix, "message2", nil),
|
2018-09-28 21:39:39 +03:00
|
|
|
NewSetStatusOp(rene, unix, ClosedStatus),
|
|
|
|
NewLabelChangeOperation(rene, unix, []Label{"added"}, []Label{"removed"}),
|
2018-09-15 14:15:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, op := range good {
|
|
|
|
if err := op.Validate(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:39:39 +03:00
|
|
|
bad := []Operation{
|
2018-09-15 14:15:00 +03:00
|
|
|
// opbase
|
2020-11-08 21:15:06 +03:00
|
|
|
NewSetStatusOp(makeIdentity(t, "", "rene@descartes.fr"), unix, ClosedStatus),
|
|
|
|
NewSetStatusOp(makeIdentity(t, "René Descartes\u001b", "rene@descartes.fr"), unix, ClosedStatus),
|
|
|
|
NewSetStatusOp(makeIdentity(t, "René Descartes", "rene@descartes.fr\u001b"), unix, ClosedStatus),
|
|
|
|
NewSetStatusOp(makeIdentity(t, "René \nDescartes", "rene@descartes.fr"), unix, ClosedStatus),
|
|
|
|
NewSetStatusOp(makeIdentity(t, "René Descartes", "rene@\ndescartes.fr"), unix, ClosedStatus),
|
2018-10-01 12:37:17 +03:00
|
|
|
&CreateOperation{OpBase: OpBase{
|
2021-02-14 13:36:32 +03:00
|
|
|
Author_: rene,
|
2018-09-15 14:15:00 +03:00
|
|
|
UnixTime: 0,
|
2018-09-28 21:39:39 +03:00
|
|
|
OperationType: CreateOp,
|
2018-09-15 14:15:00 +03:00
|
|
|
},
|
|
|
|
Title: "title",
|
|
|
|
Message: "message",
|
|
|
|
},
|
|
|
|
|
2018-09-25 18:56:58 +03:00
|
|
|
NewCreateOp(rene, unix, "multi\nline", "message", nil),
|
2020-07-01 20:39:02 +03:00
|
|
|
NewCreateOp(rene, unix, "title", "message", []repository.Hash{repository.Hash("invalid")}),
|
2018-09-25 18:56:58 +03:00
|
|
|
NewCreateOp(rene, unix, "title\u001b", "message", nil),
|
|
|
|
NewCreateOp(rene, unix, "title", "message\u001b", nil),
|
|
|
|
NewSetTitleOp(rene, unix, "multi\nline", "title1"),
|
|
|
|
NewSetTitleOp(rene, unix, "title", "multi\nline"),
|
|
|
|
NewSetTitleOp(rene, unix, "title\u001b", "title2"),
|
|
|
|
NewSetTitleOp(rene, unix, "title", "title2\u001b"),
|
|
|
|
NewAddCommentOp(rene, unix, "message\u001b", nil),
|
2020-07-01 20:39:02 +03:00
|
|
|
NewAddCommentOp(rene, unix, "message", []repository.Hash{repository.Hash("invalid")}),
|
2018-09-25 18:56:58 +03:00
|
|
|
NewSetStatusOp(rene, unix, 1000),
|
|
|
|
NewSetStatusOp(rene, unix, 0),
|
2018-09-28 21:39:39 +03:00
|
|
|
NewLabelChangeOperation(rene, unix, []Label{}, []Label{}),
|
|
|
|
NewLabelChangeOperation(rene, unix, []Label{"multi\nline"}, []Label{}),
|
2018-09-15 14:15:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, op := range bad {
|
|
|
|
if err := op.Validate(); err == nil {
|
|
|
|
t.Fatal("validation should have failed", i, op)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-01 12:37:37 +03:00
|
|
|
|
|
|
|
func TestMetadata(t *testing.T) {
|
2020-11-08 21:15:06 +03:00
|
|
|
repo := repository.NewMockRepoClock()
|
|
|
|
|
|
|
|
rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-02-16 19:32:30 +03:00
|
|
|
op := NewCreateOp(rene, time.Now().Unix(), "title", "message", nil)
|
2018-10-01 12:37:37 +03:00
|
|
|
|
|
|
|
op.SetMetadata("key", "value")
|
|
|
|
|
|
|
|
val, ok := op.GetMetadata("key")
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, val, "value")
|
|
|
|
}
|
2018-10-01 22:44:42 +03:00
|
|
|
|
2019-08-07 16:31:38 +03:00
|
|
|
func TestID(t *testing.T) {
|
2020-10-04 20:56:16 +03:00
|
|
|
repo := repository.CreateGoGitTestRepo(false)
|
2020-06-23 19:02:54 +03:00
|
|
|
defer repository.CleanupTestRepos(repo)
|
2019-05-27 22:14:55 +03:00
|
|
|
|
2018-10-01 22:44:42 +03:00
|
|
|
repos := []repository.ClockedRepo{
|
2020-11-08 19:54:28 +03:00
|
|
|
repository.NewMockRepo(),
|
2019-05-27 22:14:55 +03:00
|
|
|
repo,
|
2018-10-01 22:44:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, repo := range repos {
|
2020-11-08 21:15:06 +03:00
|
|
|
rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr")
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = rene.Commit(repo)
|
2020-07-01 21:00:53 +03:00
|
|
|
require.NoError(t, err)
|
2019-02-16 19:32:30 +03:00
|
|
|
|
|
|
|
b, op, err := Create(rene, time.Now().Unix(), "title", "message")
|
2020-07-01 21:00:53 +03:00
|
|
|
require.NoError(t, err)
|
2018-10-01 22:44:42 +03:00
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
id1 := op.Id()
|
|
|
|
require.NoError(t, id1.Validate())
|
2018-10-01 22:44:42 +03:00
|
|
|
|
|
|
|
err = b.Commit(repo)
|
2020-07-01 21:00:53 +03:00
|
|
|
require.NoError(t, err)
|
2018-10-01 22:44:42 +03:00
|
|
|
|
|
|
|
op2 := b.FirstOp()
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
id2 := op2.Id()
|
|
|
|
require.NoError(t, id2.Validate())
|
2019-08-07 16:31:38 +03:00
|
|
|
require.Equal(t, id1, id2)
|
2018-10-01 22:44:42 +03:00
|
|
|
|
2021-02-14 13:36:32 +03:00
|
|
|
b2, err := Read(repo, b.Id())
|
2020-07-01 21:00:53 +03:00
|
|
|
require.NoError(t, err)
|
2018-10-01 22:44:42 +03:00
|
|
|
|
|
|
|
op3 := b2.FirstOp()
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
id3 := op3.Id()
|
|
|
|
require.NoError(t, id3.Validate())
|
2019-08-07 16:31:38 +03:00
|
|
|
require.Equal(t, id1, id3)
|
2018-10-01 22:44:42 +03:00
|
|
|
}
|
|
|
|
}
|