0
0
mirror of https://github.com/thegeeklab/wp-plugin-go.git synced 2024-11-24 23:20:41 +00:00

fix: fix commit message split and add test (#22)

This commit is contained in:
Robert Kaussow 2023-09-03 22:00:06 +02:00 committed by GitHub
parent 1d057f04d8
commit 6f5c8d54de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 99 additions and 4 deletions

View File

@ -130,6 +130,8 @@ func currFlags(category string) []cli.Flag {
} }
func currFromContext(c *cli.Context) Commit { func currFromContext(c *cli.Context) Commit {
commitTitle, commitDesc := splitMessage(c.String("commit.message"))
return Commit{ return Commit{
URL: c.String("commit.url"), URL: c.String("commit.url"),
SHA: c.String("commit.sha"), SHA: c.String("commit.sha"),
@ -141,8 +143,8 @@ func currFromContext(c *cli.Context) Commit {
Branch: c.String("commit.branch"), Branch: c.String("commit.branch"),
Tag: c.String("commit.tag"), Tag: c.String("commit.tag"),
Message: c.String("commit.message"), Message: c.String("commit.message"),
Title: strings.Split(c.String("commit.message"), "\n")[0], Title: commitTitle,
Description: strings.Split(c.String("commit.message"), "\n")[1], Description: commitDesc,
Author: Author{ Author: Author{
Name: c.String("commit.author.name"), Name: c.String("commit.author.name"),
Email: c.String("commit.author.email"), Email: c.String("commit.author.email"),
@ -211,6 +213,8 @@ func prevFlags(category string) []cli.Flag {
} }
func prevFromContext(c *cli.Context) Commit { func prevFromContext(c *cli.Context) Commit {
commitTitle, commitDesc := splitMessage(c.String("commit.message"))
return Commit{ return Commit{
URL: c.String("prev.commit.url"), URL: c.String("prev.commit.url"),
SHA: c.String("prev.commit.sha"), SHA: c.String("prev.commit.sha"),
@ -218,8 +222,8 @@ func prevFromContext(c *cli.Context) Commit {
Refspec: c.String("prev.commit.refspec"), Refspec: c.String("prev.commit.refspec"),
Branch: c.String("prev.commit.branch"), Branch: c.String("prev.commit.branch"),
Message: c.String("prev.commit.message"), Message: c.String("prev.commit.message"),
Title: strings.Split(c.String("commit.message"), "\n")[0], Title: commitTitle,
Description: strings.Split(c.String("commit.message"), "\n")[1], Description: commitDesc,
Author: Author{ Author: Author{
Name: c.String("prev.commit.author.name"), Name: c.String("prev.commit.author.name"),
Email: c.String("prev.commit.author.email"), Email: c.String("prev.commit.author.email"),
@ -227,3 +231,16 @@ func prevFromContext(c *cli.Context) Commit {
}, },
} }
} }
func splitMessage(message string) (string, string) {
//nolint:gomnd
switch parts := strings.SplitN(message, "\n", 2); len(parts) {
case 1:
return parts[0], ""
//nolint:gomnd
case 2:
return parts[0], parts[1]
}
return "", ""
}

78
plugin/commit_test.go Normal file
View File

@ -0,0 +1,78 @@
package plugin
import (
"context"
"testing"
"github.com/urfave/cli/v2"
)
func Test_currFromContext(t *testing.T) {
tests := []struct {
envs map[string]string
want map[string]string
}{
{
envs: map[string]string{
"CI_COMMIT_MESSAGE": "",
},
want: map[string]string{
"title": "",
"desc": "",
"message": "",
},
},
{
envs: map[string]string{
"CI_COMMIT_MESSAGE": "test_title\ntest_desc",
},
want: map[string]string{
"title": "test_title",
"desc": "test_desc",
"message": "test_title\ntest_desc",
},
},
{
envs: map[string]string{
"CI_COMMIT_MESSAGE": "test_title\ntest_desc\nadditional",
},
want: map[string]string{
"title": "test_title",
"desc": "test_desc\nadditional",
"message": "test_title\ntest_desc\nadditional",
},
},
}
for _, tt := range tests {
for key, value := range tt.envs {
t.Setenv(key, value)
}
options := Options{
Name: "dummy",
Execute: func(ctx context.Context) error { return nil },
}
got := New(options)
got.App.Action = func(ctx *cli.Context) error {
got.Metadata = MetadataFromContext(ctx)
return nil
}
_ = got.App.Run([]string{"dummy"})
if got.Metadata.Curr.Message != tt.want["message"] {
t.Errorf("got = %q, want = %q", got.Metadata.Curr.Message, tt.want["message"])
}
if got.Metadata.Curr.Title != tt.want["title"] {
t.Errorf("got = %q, want = %q", got.Metadata.Curr.Title, tt.want["title"])
}
if got.Metadata.Curr.Description != tt.want["desc"] {
t.Errorf("got = %q, want = %q", got.Metadata.Curr.Description, tt.want["desc"])
}
}
}