Files
forge/modules/git/commit_message_test.go
zeekayandHanzo Dev aa87442fdd Record upstream authorship centrally in NOTICE instead of in every file header
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the
Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in
NOTICE, alongside LICENSE. The header lint accepts any copyright line above the
SPDX line, so it no longer demands an upstream one.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-10 17:50:35 -07:00

130 lines
4.2 KiB
Go

// Copyright 2026 Hanzo AI, Inc. All rights reserved.
// SPDX-License-Identifier: MIT
package git
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCommitMessageSanitizesInvalidUTF8(t *testing.T) {
commit := &Commit{
CommitMessage: CommitMessage{MessageRaw: "title \xff\n\n\n\nbody \xff\n\n\n"},
}
assert.Equal(t, "title ÿ", commit.MessageTitle())
assert.Equal(t, "body ÿ", commit.MessageBody())
assert.Equal(t, "title ÿ\n\n\n\nbody ÿ\n\n\n", commit.MessageUTF8())
}
func TestCommitMessageTrailer(t *testing.T) {
cases := []struct {
msg, body, sep, trailer string
}{
{"", "", "", ""},
{"a", "a", "", ""},
{"a\n\nk", "a\n\nk", "", ""},
{"a\n\nk:v", "a", "\n\n", "k:v"},
{"a\n\nk:v\n\n", "a", "\n\n", "k:v\n\n"},
{"a\n--\nk:v", "a\n--\nk:v", "", ""},
{"a\n---\nk:v", "a", "\n---\n", "k:v"},
{"a\n\n---\n\nk:v", "a\n", "\n---\n\n", "k:v"},
{"k: v", "", "", "k: v"},
{"\nk:v", "", "\n", "k:v"},
{"\n\nk:v", "", "\n\n", "k:v"},
{"---\nk:v", "", "---\n", "k:v"},
{"\n---\nk:v", "", "\n---\n", "k:v"},
{"a:b\n---\nk:v", "a:b", "\n---\n", "k:v"},
}
for _, c := range cases {
body, sep, trailer := CommitMessageSplitTrailer(c.msg)
assert.Equal(t, c.body, body, "input=%q", c.msg)
assert.Equal(t, c.sep, sep, "input=%q", c.msg)
assert.Equal(t, c.trailer, trailer, "input=%q", c.msg)
}
}
func TestCommitMessageParticipants(t *testing.T) {
sig := func(n, e string) *Signature { return &Signature{Name: n, Email: e} }
idt := func(n, e string, r int) *CommitIdentity { return &CommitIdentity{n, e, r} }
roleAuthor, roleCommitter, roleCoAuthor := commitIdentityRoleAuthor, commitIdentityRoleCommitter, commitIdentityRoleCoAuthor
type testCase struct {
name string
commit *Commit
identities []*CommitIdentity
}
t.Run("AllParticipants", func(t *testing.T) {
cases := []testCase{
{
"DifferentUsers",
&Commit{
Author: sig("a", "[email protected]"), Committer: sig("c", "[email protected]"),
CommitMessage: CommitMessage{MessageRaw: "CO-Authored-BY: [email protected]"},
},
[]*CommitIdentity{idt("a", "[email protected]", roleAuthor), idt("c", "[email protected]", roleCommitter), idt("", "[email protected]", roleCoAuthor)},
},
{
"SameUser",
&Commit{
Author: sig("a", "[email protected]"), Committer: sig("a", "[email protected]"),
CommitMessage: CommitMessage{MessageRaw: "CO-Authored-BY: [email protected]"},
},
[]*CommitIdentity{idt("a", "[email protected]", roleAuthor)},
},
{
"NoCommitter",
&Commit{
Author: sig("a", "[email protected]"), Committer: sig("", ""),
CommitMessage: CommitMessage{MessageRaw: "Co-authored-by: Full Name <[email protected]>"},
},
[]*CommitIdentity{idt("a", "[email protected]", roleAuthor), idt("Full Name", "[email protected]", roleCoAuthor)},
},
}
for _, c := range cases {
assert.Equal(t, c.identities, c.commit.AllParticipantIdentities(), "case: %s", c.name)
}
})
t.Run("CoAuthors", func(t *testing.T) {
cases := []testCase{
{
"GenuineCoAuthor",
&Commit{
Author: sig("a", "[email protected]"), Committer: sig("c", "[email protected]"),
CommitMessage: CommitMessage{MessageRaw: "Co-authored-by: x <[email protected]>"},
},
[]*CommitIdentity{idt("x", "[email protected]", roleCoAuthor)},
},
{
"CoAuthorIsCommitter",
&Commit{
Author: sig("a", "[email protected]"), Committer: sig("c", "[email protected]"),
CommitMessage: CommitMessage{MessageRaw: "Co-authored-by: c <[email protected]>"},
},
[]*CommitIdentity{idt("c", "[email protected]", roleCoAuthor)},
},
{
"CoAuthorIsAuthor",
&Commit{
Author: sig("a", "[email protected]"), Committer: sig("c", "[email protected]"),
CommitMessage: CommitMessage{MessageRaw: "Co-authored-by: a <[email protected]>"},
},
[]*CommitIdentity{},
},
{
"CoAuthorCommitterNameWithIndex", // restore the committer co-author to the co-author list by the index with correct name
&Commit{
Author: sig("a", "[email protected]"), Committer: sig("c", "[email protected]"),
CommitMessage: CommitMessage{MessageRaw: "Co-authored-by: x <[email protected]>\nCo-authored-by: c-other <[email protected]>\nCo-authored-by: y <[email protected]>"},
},
[]*CommitIdentity{idt("x", "[email protected]", roleCoAuthor), idt("c-other", "[email protected]", roleCoAuthor), idt("y", "[email protected]", roleCoAuthor)},
},
}
for _, c := range cases {
assert.Equal(t, c.identities, c.commit.CoAuthorIdentities(), "case: %s", c.name)
}
})
}