-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
changelog_test.go
371 lines (351 loc) · 9.97 KB
/
changelog_test.go
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2020 Jim Schubert
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package changelog
import (
"bytes"
"fmt"
"sort"
"testing"
"time"
"github.com/google/go-github/v29/github"
"github.com/jimschubert/changelog/model"
)
func commits(items ...model.ChangeItem) *[]model.ChangeItem {
arr := make([]model.ChangeItem, 0)
if len(items) > 0 {
arr = append(arr, items...)
}
return &arr
}
func TestCommitDescendingSorter(t *testing.T) {
present := time.Time{}
past := present.Add(time.Hour * -5)
future := present.Add(time.Hour * 12)
type expect struct {
first int64
second int64
}
tests := []struct {
name string
a *[]model.ChangeItem
args expect
}{
{"descending from out of order (past, present)",
commits(
model.ChangeItem{DateRaw: &past},
model.ChangeItem{DateRaw: &present}),
expect{first: present.Unix(), second: past.Unix()},
},
{"descending from in order (future, present)",
commits(
model.ChangeItem{DateRaw: &future},
model.ChangeItem{DateRaw: &present}),
expect{first: future.Unix(), second: present.Unix()},
},
{"descending from same (present, present)",
commits(
model.ChangeItem{DateRaw: &present},
model.ChangeItem{DateRaw: &present}),
expect{first: present.Unix(), second: present.Unix()},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sort.Sort(CommitDescendingSorter(*tt.a))
if (*tt.a)[0].Date().Unix() != tt.args.first && (*tt.a)[1].Date().Unix() != tt.args.second {
t.Errorf("CommitDescendingSorter failed to sort %d > %d", tt.args.first, tt.args.second)
}
})
}
}
func TestCommitAscendingSorter(t *testing.T) {
present := time.Time{}
past := present.Add(time.Hour * -5)
future := present.Add(time.Hour * 12)
type expect struct {
first int64
second int64
}
tests := []struct {
name string
a *[]model.ChangeItem
args expect
}{
{"ascending from out of order (past, present)",
commits(
model.ChangeItem{DateRaw: &past},
model.ChangeItem{DateRaw: &present}),
expect{first: past.Unix(), second: present.Unix()},
},
{"ascending from in order (future, present)",
commits(
model.ChangeItem{DateRaw: &future},
model.ChangeItem{DateRaw: &present}),
expect{first: present.Unix(), second: future.Unix()},
},
{"ascending from same (present, present)",
commits(
model.ChangeItem{DateRaw: &present},
model.ChangeItem{DateRaw: &present}),
expect{first: present.Unix(), second: present.Unix()},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sort.Sort(CommitAscendingSorter(*tt.a))
if (*tt.a)[0].Date().Unix() != tt.args.first && (*tt.a)[1].Date().Unix() != tt.args.second {
t.Errorf("CommitAscendingSorter failed to sort %d < %d", tt.args.first, tt.args.second)
}
})
}
}
// func TestChangelog_Generate(t *testing.T) {
// type fields struct {
// Config *model.Config
// From string
// To string
// }
// tests := []struct {
// name string
// fields fields
// wantWriter string
// wantErr bool
// }{
// // TODO: Add test cases.
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// c := &Changelog{
// Config: tt.fields.Config,
// From: tt.fields.From,
// To: tt.fields.To,
// }
// writer := &bytes.Buffer{}
// err := c.Generate(writer)
// if (err != nil) != tt.wantErr {
// t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr)
// return
// }
// if gotWriter := writer.String(); gotWriter != tt.wantWriter {
// t.Errorf("Generate() gotWriter = %v, want %v", gotWriter, tt.wantWriter)
// }
// })
// }
// }
// func TestChangelog_convertToChangeItem(t *testing.T) {
// type fields struct {
// Config *model.Config
// From string
// To string
// }
// type args struct {
// commit *github.RepositoryCommit
// ch chan *model.ChangeItem
// wg *sync.WaitGroup
// }
// tests := []struct {
// name string
// fields fields
// args args
// }{
// // TODO: Add test cases.
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// c := &Changelog{
// Config: tt.fields.Config,
// From: tt.fields.From,
// To: tt.fields.To,
// }
// })
// }
// }
func TestChangelog_writeChangelog(t *testing.T) {
p := func(s string) *string { return &s }
gp := func(arr []model.Grouping) *[]model.Grouping { return &arr }
fromTimestamp := func(ts int64) *time.Time {
t := time.Unix(ts, 0)
return &t
}
compareUrl := func(c *model.Config, from string, to string) string {
u := fmt.Sprintf("https://github.com/%s/%s/compare/%s...%s",
c.Owner,
c.Repo,
from,
to)
s := fmt.Sprintf("<em>For more details, see <a href=\"%s\">%s..%s</a></em>\n",
u,
from,
to)
return s
}
ci := func(ci *model.ChangeItem) string {
pullPart := ""
if ci.IsPull() {
pullPart = fmt.Sprintf("[contributed](%s) by ", ci.PullURL())
}
li := fmt.Sprintf("* [%s](%s) %s (%s[%s](%s))\n",
ci.CommitHashShort(),
ci.CommitURL(),
ci.Title(),
pullPart,
ci.Author(),
ci.AuthorURL(),
)
return li
}
withGroup := func(group string, ci *model.ChangeItem) model.ChangeItem {
date := ci.Date()
return model.ChangeItem{
AuthorRaw: ci.AuthorRaw,
AuthorURLRaw: ci.AuthorURLRaw,
CommitMessageRaw: ci.CommitMessageRaw,
DateRaw: &date,
IsPullRaw: ci.IsPullRaw,
PullURLRaw: ci.PullURLRaw,
CommitHashRaw: ci.CommitHashRaw,
CommitURLRaw: ci.CommitURLRaw,
GroupRaw: &group,
}
}
// git log --format="%ct %H %an %s"
flatConfig := &model.Config{
Owner: "jimschubert",
Repo: "changelog",
SortDirection: model.Descending.Ptr(),
Groupings: nil,
}
groupedConfig := &model.Config{
Owner: "jimschubert",
Repo: "changelog",
SortDirection: model.Descending.Ptr(),
Groupings: gp([]model.Grouping{
{Name: "Features", Patterns: []string{"(?i)\badd\b"}},
{Name: "Other", Patterns: []string{".?"}},
}),
}
expectedFlat := func(c *model.Config, from string, to string, items ...model.ChangeItem) string {
result := &bytes.Buffer{}
result.WriteString(fmt.Sprintf("## %s\n\n", to))
for _, item := range items {
result.WriteString(ci(&item))
}
result.WriteString("\n")
result.WriteString(compareUrl(c, from, to))
return result.String()
}
expectedGrouped := func(c *model.Config, from string, to string, items map[string][]model.ChangeItem) string {
result := &bytes.Buffer{}
result.WriteString(fmt.Sprintf("## %s\n", to))
for key, changeItems := range items {
result.WriteString("\n")
result.WriteString("### ")
result.WriteString(key)
result.WriteString("\n\n")
for _, item := range changeItems {
result.WriteString(ci(&item))
}
}
result.WriteString("\n")
result.WriteString(compareUrl(c, from, to))
return result.String()
}
// note; initialize ChangeItem without Group here. call setGroup(model.ChangeItem) for group expectations.
first := model.ChangeItem{
AuthorRaw: p("jimschubert"),
AuthorURLRaw: p("https://github.com/jimschubert"),
CommitMessageRaw: p("Initial commit"),
CommitHashRaw: p("ae494dca96571b5cf8cd6ad8c9fccf86d8455982"),
CommitURLRaw: p("https://github.com/jimschubert/changelog/commit/ae494dca96571b5cf8cd6ad8c9fccf86d8455982"),
DateRaw: fromTimestamp(1583008420),
}
second := model.ChangeItem{
AuthorRaw: p("jimschubert"),
AuthorURLRaw: p("https://github.com/jimschubert"),
CommitMessageRaw: p("Add some placeholder command line args"),
CommitHashRaw: p("d707829d23b58326182c3c17fb5f52d275feda6b"),
CommitURLRaw: p("https://github.com/jimschubert/changelog/commit/d707829d23b58326182c3c17fb5f52d275feda6b"),
DateRaw: fromTimestamp(1583008987),
}
type fields struct {
Config *model.Config
From string
To string
}
type args struct {
all []model.ChangeItem
comparison *github.CommitsComparison
}
tests := []struct {
name string
fields fields
args args
wantWriter string
wantErr bool
}{
{
"single item flat output",
fields{
Config: flatConfig,
From: "v0.0.0",
To: "v0.0.1",
},
args{
all: []model.ChangeItem{first},
comparison: &github.CommitsComparison{
HTMLURL: p("https://github.com/jimschubert/changelog/compare/v0.0.0...v0.0.1"),
},
},
expectedFlat(flatConfig, "v0.0.0", "v0.0.1", first),
false,
},
{
"single item grouped output",
fields{
Config: groupedConfig,
From: "v0.0.0",
To: "v0.0.1",
},
args{
all: []model.ChangeItem{withGroup("Other", &first), withGroup("Features", &second)},
comparison: &github.CommitsComparison{
HTMLURL: p("https://github.com/jimschubert/changelog/compare/v0.0.0...v0.0.1"),
},
},
expectedGrouped(groupedConfig, "v0.0.0", "v0.0.1", map[string][]model.ChangeItem{
"Features": {withGroup("Features", &second)},
"Other": {withGroup("Other", &first)},
}),
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Changelog{
Config: tt.fields.Config,
From: tt.fields.From,
To: tt.fields.To,
}
writer := &bytes.Buffer{}
err := c.writeChangelog(tt.args.all, writer)
if (err != nil) != tt.wantErr {
t.Errorf("writeChangelog() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotWriter := writer.String(); gotWriter != tt.wantWriter {
t.Errorf("writeChangelog() gotWriter = '''%v''', want '''%v'''", gotWriter, tt.wantWriter)
}
})
}
}