2018-09-28 21:39:39 +03:00
|
|
|
package bug
|
2018-09-15 14:15:00 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
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
|
|
|
"github.com/MichaelMure/git-bug/util/git"
|
2018-10-01 12:37:37 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-09-15 14:15:00 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidate(t *testing.T) {
|
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
|
2018-11-21 20:56:12 +03:00
|
|
|
NewSetStatusOp(identity.NewBare("", "rene@descartes.fr"), unix, ClosedStatus),
|
|
|
|
NewSetStatusOp(identity.NewBare("René Descartes\u001b", "rene@descartes.fr"), unix, ClosedStatus),
|
|
|
|
NewSetStatusOp(identity.NewBare("René Descartes", "rene@descartes.fr\u001b"), unix, ClosedStatus),
|
|
|
|
NewSetStatusOp(identity.NewBare("René \nDescartes", "rene@descartes.fr"), unix, ClosedStatus),
|
|
|
|
NewSetStatusOp(identity.NewBare("René Descartes", "rene@\ndescartes.fr"), unix, ClosedStatus),
|
2018-10-01 12:37:17 +03:00
|
|
|
&CreateOperation{OpBase: OpBase{
|
2018-09-15 14:15:00 +03:00
|
|
|
Author: rene,
|
|
|
|
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),
|
|
|
|
NewCreateOp(rene, unix, "title", "message", []git.Hash{git.Hash("invalid")}),
|
|
|
|
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),
|
|
|
|
NewAddCommentOp(rene, unix, "message", []git.Hash{git.Hash("invalid")}),
|
|
|
|
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) {
|
|
|
|
op := NewCreateOp(rene, unix, "title", "message", nil)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
func TestHash(t *testing.T) {
|
|
|
|
repos := []repository.ClockedRepo{
|
|
|
|
repository.NewMockRepoForTest(),
|
|
|
|
createRepo(false),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, repo := range repos {
|
|
|
|
b, op, err := Create(rene, unix, "title", "message")
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
h1, err := op.Hash()
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
err = b.Commit(repo)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
op2 := b.FirstOp()
|
|
|
|
|
|
|
|
h2, err := op2.Hash()
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, h1, h2)
|
|
|
|
|
|
|
|
b2, err := ReadLocalBug(repo, b.id)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
op3 := b2.FirstOp()
|
|
|
|
|
|
|
|
h3, err := op3.Hash()
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, h1, h3)
|
|
|
|
}
|
|
|
|
}
|