This repository was archived by the owner on May 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommit_test.go
More file actions
277 lines (255 loc) · 8.8 KB
/
Copy pathcommit_test.go
File metadata and controls
277 lines (255 loc) · 8.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package commit_test
import (
"fmt"
"sort"
"strings"
"testing"
"github.com/arsham/gitrelease/commit"
"github.com/blokur/testament"
"github.com/google/go-cmp/cmp"
)
func TestGroupFromCommit(t *testing.T) {
t.Parallel()
additional := `\n\n` + testament.RandomString(50)
tcs := map[string]struct {
line string
want commit.Group
}{
"not special": {
line: "something",
want: commit.NewGroup("Misc", "", "something", false),
},
"not special titled": {
line: "Something",
want: commit.NewGroup("Misc", "", "Something", false),
},
"simply topic": {
line: "fix something",
want: commit.NewGroup("Fix", "", "something", false),
},
"simply topic multi": {
line: "fix something" + additional,
want: commit.NewGroup("Fix", "", "something"+additional, false),
},
"simply topic titled": {
line: "Fix Something",
want: commit.NewGroup("Fix", "", "Something", false),
},
"topic section": {
line: "Fix(repo) something",
want: commit.NewGroup("Fix", "repo", "something", false),
},
"topic section multi": {
line: "Fix(repo) something" + additional,
want: commit.NewGroup("Fix", "repo", "something"+additional, false),
},
"topic section colon": {
line: "Fix(repo): something",
want: commit.NewGroup("Fix", "repo", "something", false),
},
"topic section colon multi": {
line: "Fix(repo): something" + additional,
want: commit.NewGroup("Fix", "repo", "something"+additional, false),
},
"ref": {line: "ref something", want: commit.NewGroup("Refactor", "", "something", false)},
"refactor": {line: "refactor something", want: commit.NewGroup("Refactor", "", "something", false)},
"feat": {line: "feat something", want: commit.NewGroup("Feature", "", "something", false)},
"feature": {line: "feature something", want: commit.NewGroup("Feature", "", "something", false)},
"fix": {line: "fix something", want: commit.NewGroup("Fix", "", "something", false)},
"fixed": {line: "fixed something", want: commit.NewGroup("Fix", "", "something", false)},
"chore": {line: "chore something", want: commit.NewGroup("Chore", "", "something", false)},
"upgrade": {line: "upgrade something", want: commit.NewGroup("Upgrades", "", "something", false)},
"enhance": {line: "enhance something", want: commit.NewGroup("Enhancements", "", "something", false)},
"enhancement": {line: "enhancement something", want: commit.NewGroup("Enhancements", "", "something", false)},
"enhancements": {line: "enhancements something", want: commit.NewGroup("Enhancements", "", "something", false)},
"style": {line: "style something", want: commit.NewGroup("Style", "", "something", false)},
"ci": {line: "ci: change something", want: commit.NewGroup("CI", "", "change something", false)},
"comma sep": {line: "fix(git,commit): something", want: commit.NewGroup("Fix", "git,commit", "something", false)},
"hyphen subj": {line: "fix(git-commit): something", want: commit.NewGroup("Fix", "git-commit", "something", false)},
"underscore": {line: "fix(git_commit): something", want: commit.NewGroup("Fix", "git_commit", "something", false)},
"docs": {line: "docs: change something", want: commit.NewGroup("Docs", "", "change something", false)},
}
for name, tc := range tcs {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
got := commit.GroupFromCommit(tc.line)
if diff := cmp.Diff(tc.want, got, commit.GroupComparer...); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func TestGroup(t *testing.T) {
t.Parallel()
t.Run("DescriptionString", testGroupDescriptionString)
t.Run("ParseGroups", testGroupParseGroups)
}
func testGroupDescriptionString(t *testing.T) {
t.Parallel()
// the first letter will be uppercased.
letter := testament.RandomLowerString(1)
randomString := testament.RandomLowerString(30)
msg := letter + randomString
wantMsg := strings.ToUpper(letter) + randomString
additional := `\n\n` + testament.RandomString(50)
prefix := commit.ItemPrefix
issue := "Close #666"
tcs := map[string]struct {
group commit.Group
want string
}{
"simple": {
group: commit.NewGroup("Fix", "", msg, false),
want: prefix + wantMsg,
},
"with verb": {
group: commit.NewGroup("Fix", "repo", msg, false),
want: fmt.Sprintf("%s**Repo:** %s", prefix, wantMsg),
},
"with ci verb": {
group: commit.NewGroup("Fix", "ci", msg, false),
want: fmt.Sprintf("%s**CI:** %s", prefix, wantMsg),
},
"multi line": {
group: commit.NewGroup("Fix", "repo", msg+additional, false),
want: fmt.Sprintf("%s**Repo:** %s", prefix, wantMsg),
},
"with issue ref": {
group: commit.NewGroup("Fix", "repo", msg+additional+`\n`+issue, false),
want: fmt.Sprintf("%s**Repo:** %s (%s)", prefix, wantMsg, issue),
},
"with issue (ref)": {
group: commit.NewGroup("Fix", "repo", msg+additional+`\n(`+issue+`)`, false),
want: fmt.Sprintf("%s**Repo:** %s (%s)", prefix, wantMsg, issue),
},
"multi issue refs": {
group: commit.NewGroup("Fix", "repo", msg+additional+`\n`+issue+`\n`+issue, false),
want: fmt.Sprintf("%s**Repo:** %s (%s, %s)", prefix, wantMsg, issue, issue),
},
"comma separated": {
group: commit.NewGroup("Fix", "git,commit", msg, false),
want: fmt.Sprintf("%s**Git,Commit:** %s", prefix, wantMsg),
},
"hyphenated subjects": {
group: commit.NewGroup("Fix", "git-commit", msg, false),
want: fmt.Sprintf("%s**Git-commit:** %s", prefix, wantMsg),
},
"docs": {
group: commit.NewGroup("Docs", "README", msg, false),
want: fmt.Sprintf("%s**README:** %s", prefix, wantMsg),
},
}
for name, tc := range tcs {
tc := tc
t.Run(name, func(t *testing.T) {
got := tc.group.DescriptionString()
if diff := cmp.Diff(tc.want, got, commit.GroupComparer...); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func testGroupParseGroups(t *testing.T) {
t.Run("OneGroup", testGroupParseGroupsOneGroup)
t.Run("MultipleGroups", testGroupParseGroupsMultipleGroups)
t.Run("BreakingSign", testGroupParseGroupsBreakingSign)
t.Run("BreakingFooter", testGroupParseGroupsBreakingFooter)
}
func testGroupParseGroupsOneGroup(t *testing.T) {
t.Parallel()
logs := []string{"Feat(testing): this is a test"}
got := commit.ParseGroups(logs)
got = strings.TrimRight(got, "\n")
want := "### Feature\n\n- **Testing:** This is a test"
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func testGroupParseGroupsMultipleGroups(t *testing.T) {
t.Parallel()
logs := []string{
"Feat(testing): this is a test",
"Misc: this is another test",
"feat: yet another",
}
got := commit.ParseGroups(logs)
want := []string{
"### Feature\n\n- **Testing:** This is a test\n- Yet another",
"### Misc\n\n- This is another test",
}
gotS := strings.Split(got, "\n\n\n")
sort.Strings(gotS)
if diff := cmp.Diff(want, gotS); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func testGroupParseGroupsBreakingSign(t *testing.T) {
t.Run("NoScope", testGroupParseGroupsBreakingSignNoScope)
t.Run("BeforeScope", testGroupParseGroupsBreakingSignBeforeScope)
t.Run("AfterScope", testGroupParseGroupsBreakingSignAfterScope)
}
func testGroupParseGroupsBreakingSignNoScope(t *testing.T) {
t.Parallel()
logs := []string{
"ref: nothing important",
"ref!: this is a test",
}
got := commit.ParseGroups(logs)
want := strings.Join([]string{
"### Refactor\n",
"- Nothing important",
"- This is a test [**BREAKING CHANGE**]",
}, "\n")
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func testGroupParseGroupsBreakingSignBeforeScope(t *testing.T) {
t.Parallel()
logs := []string{
"ref: nothing important",
"ref!(repo): this is a test",
}
got := commit.ParseGroups(logs)
want := strings.Join([]string{
"### Refactor\n",
"- Nothing important",
"- **Repo:** This is a test [**BREAKING CHANGE**]",
}, "\n")
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func testGroupParseGroupsBreakingSignAfterScope(t *testing.T) {
t.Parallel()
logs := []string{
"ref: nothing important",
"ref(repo)!: this is a test",
}
got := commit.ParseGroups(logs)
want := strings.Join([]string{
"### Refactor\n",
"- Nothing important",
"- **Repo:** This is a test [**BREAKING CHANGE**]",
}, "\n")
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func testGroupParseGroupsBreakingFooter(t *testing.T) {
t.Parallel()
logs := []string{
"ref(server): nothing special",
"ref(repo): this is a new api\n\nBREAKING CHANGE: this is a changed api",
}
got := commit.ParseGroups(logs)
want := strings.Join([]string{
"### Refactor\n",
"- **Server:** Nothing special",
"- **Repo:** This is a new api [**BREAKING CHANGE**]",
}, "\n")
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}