Skip to content

Commit 6b290dd

Browse files
committed
test(commit): tests for commit message builder
1 parent 76edcc8 commit 6b290dd

File tree

2 files changed

+121
-11
lines changed

2 files changed

+121
-11
lines changed

cmd/commit.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ import (
1313
var ErrInvalidFlag = errors.New("invalid flag")
1414

1515
func commit() *cli.Command {
16-
commit := conventional.Commit{}
16+
commitData := conventional.Commit{}
1717

1818
return &cli.Command{
1919
Name: "commit",
2020
Usage: "Create Conventional Commit",
21-
Flags: flags(&commit),
21+
Flags: flags(&commitData),
2222
Action: func(_ context.Context, _ *cli.Command) error {
23-
commit, err := conventional.BuildCommitMessage(&commit)
23+
message, err := conventional.BuildCommitMessage(&commitData)
2424
if err != nil {
2525
return fmt.Errorf("error building commit: %w", err)
2626
}
2727

28-
_, err = fmt.Println(commit)
28+
_, err = fmt.Println(message)
2929
if err != nil {
3030
return fmt.Errorf("error printing built commit message: %w", err)
3131
}
@@ -35,47 +35,47 @@ func commit() *cli.Command {
3535
}
3636
}
3737

38-
func flags(commit *conventional.Commit) []cli.Flag {
38+
func flags(commitData *conventional.Commit) []cli.Flag {
3939
return []cli.Flag{
4040
&cli.StringFlag{
4141
Name: "type",
4242
OnlyOnce: true,
43-
Destination: &commit.Type,
43+
Destination: &commitData.Type,
4444
Required: true,
4545
Usage: "Type of change (e.g., feat, fix, docs)",
4646
},
4747
&cli.StringFlag{
4848
Name: "scope",
4949
OnlyOnce: true,
50-
Destination: &commit.Scope,
50+
Destination: &commitData.Scope,
5151
Required: false,
5252
Usage: "Optional context for the change (e.g., api, cli)",
5353
},
5454
&cli.StringFlag{
5555
Name: "title",
5656
OnlyOnce: true,
57-
Destination: &commit.Title,
57+
Destination: &commitData.Title,
5858
Required: true,
5959
Usage: "Short description of changes",
6060
},
6161
&cli.StringFlag{
6262
Name: "body",
6363
OnlyOnce: true,
64-
Destination: &commit.Body,
64+
Destination: &commitData.Body,
6565
Required: false,
6666
Usage: "Optional longer description of the change",
6767
},
6868
&cli.StringFlag{
6969
Name: "breaking",
7070
OnlyOnce: true,
71-
Destination: &commit.BreakingChange,
71+
Destination: &commitData.BreakingChange,
7272
Required: false,
7373
Usage: "Optional description of breaking changes introduced with commit",
7474
},
7575
&cli.StringFlag{
7676
Name: "issue",
7777
OnlyOnce: true,
78-
Destination: &commit.Issue,
78+
Destination: &commitData.Issue,
7979
Required: false,
8080
Usage: "Optional issue number",
8181
},
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package conventional
2+
3+
import (
4+
"strconv"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type (
11+
testCases struct {
12+
commitData *Commit
13+
err error
14+
result string
15+
}
16+
)
17+
18+
var buildCommitMessageTestCases = []testCases{
19+
{
20+
result: "feat(api): some changes\n\nChanges description\n",
21+
commitData: &Commit{
22+
Title: "some changes",
23+
Type: "feat",
24+
Scope: "api",
25+
Body: "Changes description",
26+
},
27+
err: nil,
28+
},
29+
{
30+
result: "feat(api): some changes\n",
31+
commitData: &Commit{
32+
Title: "some changes",
33+
Type: "feat",
34+
Scope: "api",
35+
},
36+
err: nil,
37+
},
38+
{
39+
result: "feat: some changes\n\nChanges description\n",
40+
commitData: &Commit{
41+
Title: "some changes",
42+
Type: "feat",
43+
Body: "Changes description",
44+
},
45+
err: nil,
46+
},
47+
{
48+
result: "feat(api): some changes\n\nChanges description\n\nIssue: TEST-1\n",
49+
commitData: &Commit{
50+
Title: "some changes",
51+
Type: "feat",
52+
Scope: "api",
53+
Body: "Changes description",
54+
Issue: "TEST-1",
55+
},
56+
err: nil,
57+
},
58+
{
59+
result: "feat(api)!: some changes\n\nChanges description\n\nBREAKING CHANGE: Something breaking\nIssue: TEST-1\n",
60+
commitData: &Commit{
61+
Title: "some changes",
62+
Type: "feat",
63+
Scope: "api",
64+
Body: "Changes description",
65+
Issue: "TEST-1",
66+
BreakingChange: "Something breaking",
67+
},
68+
err: nil,
69+
},
70+
{
71+
result: "feat!: some changes\n\nChanges description\n\nBREAKING CHANGE: Something breaking\n",
72+
commitData: &Commit{
73+
Title: "some changes",
74+
Type: "feat",
75+
Body: "Changes description",
76+
BreakingChange: "Something breaking",
77+
},
78+
err: nil,
79+
},
80+
{
81+
result: "",
82+
commitData: &Commit{
83+
Type: "feat",
84+
Body: "Changes description",
85+
},
86+
err: ErrRequiredPartNotPreset,
87+
},
88+
{
89+
result: "",
90+
commitData: &Commit{
91+
Title: "some changes",
92+
Body: "Changes description",
93+
},
94+
err: ErrRequiredPartNotPreset,
95+
},
96+
}
97+
98+
func TestBuildCommitMessage(t *testing.T) {
99+
t.Parallel()
100+
for i, tt := range buildCommitMessageTestCases {
101+
t.Run(strconv.Itoa(i), func(t *testing.T) {
102+
t.Parallel()
103+
104+
message, err := BuildCommitMessage(tt.commitData)
105+
106+
assert.Equal(t, tt.result, message)
107+
assert.ErrorIs(t, err, tt.err)
108+
})
109+
}
110+
}

0 commit comments

Comments
 (0)