package dehub import ( "reflect" "strings" "testing" "github.com/davecgh/go-spew/spew" ) func TestChangeCommitVerify(t *testing.T) { type step struct { msg string msgHead string // defaults to msg tree map[string]string } testCases := []struct { descr string steps []step }{ { descr: "single commit", steps: []step{ { msg: "first commit", tree: map[string]string{"a": "0", "b": "1"}, }, }, }, { descr: "multiple commits", steps: []step{ { msg: "first commit", tree: map[string]string{"a": "0", "b": "1"}, }, { msg: "second commit, changing a", tree: map[string]string{"a": "1"}, }, { msg: "third commit, empty", }, { msg: "fourth commit, adding c, removing b", tree: map[string]string{"b": "", "c": "2"}, }, }, }, { descr: "big body commits", steps: []step{ { msg: "first commit, single line but with newline\n", }, { msg: "second commit, single line but with two newlines\n\n", msgHead: "second commit, single line but with two newlines\n\n", }, { msg: "third commit, multi-line with one newline\nanother line!", msgHead: "third commit, multi-line with one newline\n\n", }, { msg: "fourth commit, multi-line with two newlines\n\nanother line!", msgHead: "fourth commit, multi-line with two newlines\n\n", }, }, }, } for _, test := range testCases { t.Run(test.descr, func(t *testing.T) { h := newHarness(t) for _, step := range test.steps { h.stage(step.tree) account := h.cfg.Accounts[0] gitCommit := h.changeCommit(step.msg, account.ID, h.sig) if step.msgHead == "" { step.msgHead = strings.TrimSpace(step.msg) + "\n\n" } if !strings.HasPrefix(gitCommit.GitCommit.Message, step.msgHead) { t.Fatalf("commit message %q does not start with expected head %q", gitCommit.GitCommit.Message, step.msgHead) } var actualCommit Commit if err := actualCommit.UnmarshalText([]byte(gitCommit.GitCommit.Message)); err != nil { t.Fatalf("error unmarshaling commit body: %v", err) } else if !reflect.DeepEqual(actualCommit, gitCommit.Commit) { t.Fatalf("returned change commit:\n%s\ndoes not match actual one:\n%s", spew.Sdump(gitCommit.Commit), spew.Sdump(actualCommit)) } } }) } }