Repository navigation
Expand file tree
/
Copy pathcommit_test.go
More file actions
233 lines (204 loc) · 6.65 KB
/
Copy pathcommit_test.go
File metadata and controls
233 lines (204 loc) · 6.65 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
package main
import (
"errors"
"strings"
"testing"
)
func initGitTestRepo(t *testing.T) {
t.Helper()
t.Setenv("GIT_CONFIG_GLOBAL", "/dev/null")
t.Setenv("GIT_CONFIG_SYSTEM", "/dev/null")
t.Chdir(t.TempDir())
runGitCommand(t, "init")
runGitCommand(t, "config", "user.name", "Test User")
runGitCommand(t, "config", "user.email", "test@example.com")
runGitCommand(t, "config", "commit.gpgsign", "false")
}
func runGitCommand(t *testing.T, args ...string) string {
t.Helper()
out, errOut, err := getCommandOutput(gitCommand, args...)
if err != nil {
t.Fatalf("git %s failed: %v: %s", strings.Join(args, " "), err, errOut)
}
return out
}
func commitTestMessage(t *testing.T, message string) {
t.Helper()
runGitCommand(t, "commit", "--allow-empty", "-m", message)
}
func TestCommitStatsDetermineReleaseType(t *testing.T) {
t.Parallel()
tests := []struct {
name string
stats CommitStats
want ReleaseType
}{
{"no commits is none", CommitStats{}, ReleaseTypeNone},
{
"breaking change is major",
CommitStats{HasBreakingChange: true, ByKind: map[string][]Commit{CommitKindFeat: {{}}}},
ReleaseTypeMajor,
},
{
"feat is minor",
CommitStats{ByKind: map[string][]Commit{CommitKindFeat: {{}}}},
ReleaseTypeMinor,
},
{
"fix is patch",
CommitStats{ByKind: map[string][]Commit{CommitKindFix: {{}}}},
ReleaseTypePatch,
},
{
"perf is patch",
CommitStats{ByKind: map[string][]Commit{CommitKindPerf: {{}}}},
ReleaseTypePatch,
},
{
"chore is none",
CommitStats{ByKind: map[string][]Commit{CommitKindChore: {{}}}},
ReleaseTypeNone,
},
{
"breaking wins over feat",
CommitStats{HasBreakingChange: true, ByKind: map[string][]Commit{CommitKindFix: {{}}}},
ReleaseTypeMajor,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := tt.stats.DetermineReleaseType(); got != tt.want {
t.Errorf("DetermineReleaseType() = %q, want %q", got, tt.want)
}
})
}
}
//nolint:paralleltest // t.Chdir changes the process working directory
func TestProcessCommitMessagesDeterminesReleaseTypes(t *testing.T) {
allowedKinds := getDefaultConfig().AllowedCommitKinds
tests := []struct {
name string
commits []string
want ReleaseType
}{
{"feat is minor", []string{"feat: add gadget"}, ReleaseTypeMinor},
{"fix is patch", []string{"fix: repair widget"}, ReleaseTypePatch},
{"perf is patch", []string{"perf: speed up search"}, ReleaseTypePatch},
{"chore is none", []string{"chore: tidy up"}, ReleaseTypeNone},
{
"body colons are valid",
[]string{
"fix: repair the focus ring\n\nthe browser renders: a white background,\nand *:focus { outline: none } contradicted `publish:dry`",
},
ReleaseTypePatch,
},
{
"breaking footer is major",
[]string{"feat: add api\n\nelaborated details\n\nBREAKING CHANGE: api changed"},
ReleaseTypeMajor,
},
{"bang is major", []string{"feat!: drop old api"}, ReleaseTypeMajor},
}
for _, tt := range tests {
initGitTestRepo(t)
for _, message := range tt.commits {
commitTestMessage(t, message)
}
stats, err := processCommitMessages("", false, allowedKinds)
if err != nil {
t.Fatalf("%s: processCommitMessages() returned error: %v", tt.name, err)
}
if got := stats.DetermineReleaseType(); got != tt.want {
t.Errorf("%s: DetermineReleaseType() = %q, want %q", tt.name, got, tt.want)
}
if stats.InWindowCommits != len(tt.commits) {
t.Errorf("%s: InWindowCommits = %d, want %d", tt.name, stats.InWindowCommits, len(tt.commits))
}
}
}
//nolint:paralleltest // t.Chdir changes the process working directory
func TestProcessCommitMessagesSinceTagExcludesPreTagCommits(t *testing.T) {
initGitTestRepo(t)
commitTestMessage(t, "chore: initial setup")
runGitCommand(t, "tag", "v1.0.0")
commitTestMessage(t, "fix: repair widget")
commitTestMessage(t, "feat: add gadget")
stats, err := processCommitMessages(
"v1.0.0",
false,
getDefaultConfig().AllowedCommitKinds,
)
if err != nil {
t.Fatalf("processCommitMessages() returned error: %v", err)
}
if stats.InWindowCommits != 2 {
t.Errorf("InWindowCommits = %d, want 2", stats.InWindowCommits)
}
if len(stats.ByKind) != 2 {
t.Errorf("ByKind has %d kinds, want 2: %v", len(stats.ByKind), stats.ByKind)
}
if len(stats.ByKind[CommitKindChore]) != 0 {
t.Error("pre-tag chore commit leaked into ByKind")
}
if got := stats.DetermineReleaseType(); got != ReleaseTypeMinor {
t.Errorf("DetermineReleaseType() = %q, want %q", got, ReleaseTypeMinor)
}
if stats.HasBreakingChange {
t.Error("HasBreakingChange = true, want false")
}
}
//nolint:paralleltest // t.Chdir changes the process working directory
func TestProcessCommitMessagesInvalidCommits(t *testing.T) {
initGitTestRepo(t)
commitTestMessage(t, "feat: valid work")
commitTestMessage(t, "this message is invalid")
allowedKinds := getDefaultConfig().AllowedCommitKinds
if _, err := processCommitMessages("", false, allowedKinds); !errors.Is(err, ErrFoundInvalidCommits) {
t.Errorf("strict mode error = %v, want ErrFoundInvalidCommits", err)
}
stats, err := processCommitMessages("", true, allowedKinds)
if err != nil {
t.Fatalf("lenient mode returned error: %v", err)
}
if stats.InWindowCommits != 1 {
t.Errorf("InWindowCommits = %d, want 1", stats.InWindowCommits)
}
if got := stats.DetermineReleaseType(); got != ReleaseTypeMinor {
t.Errorf("DetermineReleaseType() = %q, want %q", got, ReleaseTypeMinor)
}
}
//nolint:paralleltest // t.Chdir changes the process working directory
func TestProcessCommitMessagesRevertedPreTagCommit(t *testing.T) {
initGitTestRepo(t)
commitTestMessage(t, "feat!: remove old api")
fullHash := strings.TrimSpace(runGitCommand(t, "rev-parse", "HEAD"))
runGitCommand(t, "tag", "v1.0.0")
commitTestMessage(t, "revert: remove old api\n\nRefs: "+fullHash)
stats, err := processCommitMessages(
"v1.0.0",
false,
getDefaultConfig().AllowedCommitKinds,
)
if err != nil {
t.Fatalf("processCommitMessages() returned error: %v", err)
}
if stats.InWindowCommits != 1 {
t.Errorf("InWindowCommits = %d, want 1", stats.InWindowCommits)
}
if len(stats.ByKind) != 0 {
t.Errorf("ByKind = %v, want no kind sections (revert commits are excluded)", stats.ByKind)
}
if len(stats.RevertedPreTag) != 1 {
t.Fatalf("RevertedPreTag has %d commits, want 1", len(stats.RevertedPreTag))
}
if got := stats.RevertedPreTag[0].Hash; got != fullHash {
t.Errorf("RevertedPreTag hash = %q, want %q", got, fullHash)
}
if !stats.HasBreakingChange {
t.Error("HasBreakingChange = false, want true (reverted pre-tag breaking commit)")
}
if got := stats.DetermineReleaseType(); got != ReleaseTypeMajor {
t.Errorf("DetermineReleaseType() = %q, want %q", got, ReleaseTypeMajor)
}
}