-
Notifications
You must be signed in to change notification settings - Fork 3
/
history_test.go
352 lines (327 loc) · 10.4 KB
/
history_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
package relic
import (
"runtime/debug"
"testing"
"text/template"
)
const relicName = "Relic"
const relicURL = "https://github.com/monax/relic"
func TestHistory_DeclareReleases(t *testing.T) {
_, err := NewHistory(relicName, relicURL).DeclareReleases(
Release{
Version: parseVersion(t, "2.1.1"),
Notes: `Everything fixed`,
},
"2.1.0",
`Everything broken`,
"0.0.2",
`Wonderful things were achieved`,
"0.0.1",
`Marvelous advances were made`,
)
if err == nil {
t.Errorf("error expected")
}
history, err := NewHistory(relicName, relicURL).DeclareReleases(
Release{
Version: parseVersion(t, "2.1.1"),
Notes: `Everything fixed`,
},
"2.1.0",
`Everything broken`,
"2.0.0",
`Wonderful things were achieved`,
"1.0.0",
`Wonderful things were achieved`,
"0.0.2",
`Wonderful things were achieved`,
"0.0.1",
`Marvelous advances were made`,
)
if err != nil {
t.Error(err)
}
changelog, err := history.Changelog()
if err != nil {
t.Error(err)
}
assertChangelog(t, "# [Relic](https://github.com/monax/relic) Changelog\n## [2.1.1]\nEverything fixed\n\n## [2.1.0]\nEverything broken\n\n## [2.0.0]\nWonderful things were achieved\n\n## [1.0.0]\nWonderful things were achieved\n\n## [0.0.2]\nWonderful things were achieved\n\n## [0.0.1]\nMarvelous advances were made\n\n[2.1.1]: https://github.com/monax/relic/compare/v2.1.0...v2.1.1\n[2.1.0]: https://github.com/monax/relic/compare/v2.0.0...v2.1.0\n[2.0.0]: https://github.com/monax/relic/compare/v1.0.0...v2.0.0\n[1.0.0]: https://github.com/monax/relic/compare/v0.0.2...v1.0.0\n[0.0.2]: https://github.com/monax/relic/compare/v0.0.1...v0.0.2\n[0.0.1]: https://github.com/monax/relic/commits/v0.0.1",
changelog)
// Fail gap
_, err = NewHistory(relicName, relicURL).DeclareReleases(
Release{
Version: parseVersion(t, "1.0.3"),
Notes: `Wonderful things were achieved`,
},
"0.0.2",
`Wonderful things were achieved`,
Release{
Version: parseVersion(t, "0.0.1"),
Notes: `Marvelous advances were made`,
},
)
if err == nil {
t.Errorf("error expected")
}
history, err = NewHistory(relicName, relicURL).DeclareReleases(
Release{
Version: parseVersion(t, "1.0.3"),
Notes: `Wonderful things were achieved`,
},
"1.0.2",
`Hotfix`,
"1.0.1",
`Hotfix`,
"1.0.0",
`Wonderful things were achieved`,
Release{
Version: parseVersion(t, "0.0.1"),
Notes: `Marvelous advances were made`,
},
)
if err != nil {
t.Error(err)
}
changelog, err = history.Changelog()
if err != nil {
t.Error(err)
}
assertChangelog(t, "# [Relic](https://github.com/monax/relic) Changelog\n## [1.0.3]\nWonderful things were achieved\n\n## [1.0.2]\nHotfix\n\n## [1.0.1]\nHotfix\n\n## [1.0.0]\nWonderful things were achieved\n\n## [0.0.1]\nMarvelous advances were made\n\n[1.0.3]: https://github.com/monax/relic/compare/v1.0.2...v1.0.3\n[1.0.2]: https://github.com/monax/relic/compare/v1.0.1...v1.0.2\n[1.0.1]: https://github.com/monax/relic/compare/v1.0.0...v1.0.1\n[1.0.0]: https://github.com/monax/relic/compare/v0.0.1...v1.0.0\n[0.0.1]: https://github.com/monax/relic/commits/v0.0.1",
changelog)
_, err = NewHistory(relicName, relicURL).DeclareReleases(
"0.1.3",
`Wonderful things were achieved`,
"0.0.2",
`Wonderful things were achieved`,
"0.0.1",
`Marvelous advances were made`,
)
if err == nil {
t.Errorf("error expected")
}
history, err = NewHistory(relicName, relicURL).DeclareReleases(
"0.0.3",
`Wonderful things were achieved`,
"0.0.2",
`Wonderful things were achieved`,
"0.0.1",
`Marvelous advances were made`,
)
if err != nil {
t.Error(err)
}
changelog, err = history.Changelog()
if err != nil {
t.Error(err)
}
assertChangelog(t, "# [Relic](https://github.com/monax/relic) Changelog\n## [0.0.3]\nWonderful things were achieved\n\n## [0.0.2]\nWonderful things were achieved\n\n## [0.0.1]\nMarvelous advances were made\n\n[0.0.3]: https://github.com/monax/relic/compare/v0.0.2...v0.0.3\n[0.0.2]: https://github.com/monax/relic/compare/v0.0.1...v0.0.2\n[0.0.1]: https://github.com/monax/relic/commits/v0.0.1",
changelog)
_, err = NewHistory(relicName, relicURL).DeclareReleases(
"0.0.3",
`Wonderful things were achieved`,
"0.0.2",
`Wonderful things were achieved`,
"0.0.1",
)
if err == nil {
t.Errorf("error expected")
}
_, err = NewHistory(relicName, relicURL).DeclareReleases(
"0.0.2",
`Wonderful things were achieved`,
"0.0.3",
`Wonderful things were achieved`,
"0.0.1",
`Marvelous advances were made`,
)
if err == nil {
t.Errorf("error expected")
}
_, err = history.DeclareReleases(
"1.0.0",
"finally",
"0.2.1",
"",
"0.2.0",
"Came after 0.1.0",
)
if err == nil {
t.Errorf("error expected")
}
}
func TestHistory_DeclareReleases_Multiple(t *testing.T) {
history, err := NewHistory(relicName, relicURL).DeclareReleases(
"0.1.0",
"Basic functionality",
"0.0.2",
"Build scripts",
"0.0.1",
"Proof of concept",
)
if err != nil {
t.Error(err)
}
changelog, err := history.Changelog()
if err != nil {
t.Error(err)
}
assertChangelog(t, "# [Relic](https://github.com/monax/relic) Changelog\n## [0.1.0]\nBasic functionality\n\n## [0.0.2]\nBuild scripts\n\n## [0.0.1]\nProof of concept\n\n[0.1.0]: https://github.com/monax/relic/compare/v0.0.2...v0.1.0\n[0.0.2]: https://github.com/monax/relic/compare/v0.0.1...v0.0.2\n[0.0.1]: https://github.com/monax/relic/commits/v0.0.1",
changelog)
history1, err := history.DeclareReleases(
"1.0.0",
"finally",
"0.2.1",
"Patch",
"0.2.0",
"Came after 0.1.0",
)
if err != nil {
t.Error(err)
}
if history1 != history {
t.Errorf("history1 and history should be a pointer to the same object")
}
changelog, err = history1.Changelog()
if err != nil {
t.Error(err)
}
assertChangelog(t, "# [Relic](https://github.com/monax/relic) Changelog\n## [1.0.0]\nfinally\n\n## [0.2.1]\nPatch\n\n## [0.2.0]\nCame after 0.1.0\n\n## [0.1.0]\nBasic functionality\n\n## [0.0.2]\nBuild scripts\n\n## [0.0.1]\nProof of concept\n\n[1.0.0]: https://github.com/monax/relic/compare/v0.2.1...v1.0.0\n[0.2.1]: https://github.com/monax/relic/compare/v0.2.0...v0.2.1\n[0.2.0]: https://github.com/monax/relic/compare/v0.1.0...v0.2.0\n[0.1.0]: https://github.com/monax/relic/compare/v0.0.2...v0.1.0\n[0.0.2]: https://github.com/monax/relic/compare/v0.0.1...v0.0.2\n[0.0.1]: https://github.com/monax/relic/commits/v0.0.1",
changelog)
_, err = history.DeclareReleases(
"0.1.3",
`New newness`,
"0.1.2",
`Added blockchain`,
"0.1.1",
"Dried apricot",
)
if err == nil {
t.Errorf("error expected")
}
}
func TestHistory_WithChangelogTemplate(t *testing.T) {
history, err := NewHistory("Test Project", relicURL).
WithChangelogTemplate(template.Must(template.New("tests").
Parse("{{range .Releases}}{{$.Project}} (v{{.Version}}): {{.Notes}}\n{{end}}"))).
DeclareReleases(
"0.1.0",
"Basic functionality",
"0.0.2",
"Build scripts",
"0.0.1",
"Proof of concept",
)
if err != nil {
t.Fatal(err)
}
changelog, err := history.Changelog()
if err != nil {
t.Error(err)
}
assertChangelog(t, "Test Project (v0.1.0): Basic functionality\nTest Project (v0.0.2): Build scripts\nTest Project (v0.0.1): Proof of concept\n",
changelog)
}
func TestHistory_Release(t *testing.T) {
history, err := NewHistory(relicName, relicURL).
DeclareReleases(
"0.1.0",
"Basic functionality",
"0.0.2",
"Build scripts",
"0.0.1",
"Proof of concept",
)
if err != nil {
t.Fatal(err)
}
release, err := history.Release("0.0.2")
if err != nil {
t.Fatal(err)
}
if release.Notes != "Build scripts" {
t.Errorf("release notes should be 'Build scripts' but is '%s'", release.Notes)
}
}
func TestHistory_Changelog_Dates(t *testing.T) {
history, err := NewHistory(relicName, relicURL).
DeclareReleases("",
"Some unreleased things",
"0.1.0 - 2018-08-01",
"Basic functionality",
"0.0.2",
"Build scripts",
"0.0.1 - 2018-02-28",
"Proof of concept",
)
if err != nil {
t.Fatal(err)
}
changelog, err := history.Changelog()
if err != nil {
t.Error(err)
}
assertChangelog(t, "# [Relic](https://github.com/monax/relic) Changelog\n## [Unreleased]\nSome unreleased things\n\n## [0.1.0] - 2018-08-01\nBasic functionality\n\n## [0.0.2]\nBuild scripts\n\n## [0.0.1] - 2018-02-28\nProof of concept\n\n[Unreleased]: https://github.com/monax/relic/compare/v0.1.0...HEAD\n[0.1.0]: https://github.com/monax/relic/compare/v0.0.2...v0.1.0\n[0.0.2]: https://github.com/monax/relic/compare/v0.0.1...v0.0.2\n[0.0.1]: https://github.com/monax/relic/commits/v0.0.1",
changelog)
}
func TestHistory_Changelog_Unreleased(t *testing.T) {
history, err := NewHistory(relicName, relicURL).
DeclareReleases("",
"Some unreleased things",
)
if err != nil {
t.Fatal(err)
}
changelog, err := history.Changelog()
if err != nil {
t.Error(err)
}
_, err = NewHistory(relicName, relicURL).
DeclareReleases("",
"Some unreleased things",
"",
"more unreleased things",
)
if err == nil {
t.Fatal("Should not allow multiple unreleased sections")
}
assertChangelog(t, "# [Relic](https://github.com/monax/relic) Changelog\n## [Unreleased]\nSome unreleased things\n\n[Unreleased]: https://github.com/monax/relic/commits/HEAD",
changelog)
_, err = NewHistory(relicName, relicURL).
DeclareReleases("",
"Some unreleased things",
"0.0.1",
"Initial version",
"",
"more unreleased things",
)
if err == nil {
t.Fatal("Should not allow multiple unreleased sections")
}
history, err = NewHistory(relicName, relicURL).
DeclareReleases("",
"Some unreleased things",
"1.0.0",
"More to add",
"0.0.1",
"Initial version",
)
changelog, err = history.Changelog()
if err != nil {
t.Error(err)
}
assertChangelog(t, "# [Relic](https://github.com/monax/relic) Changelog\n## [Unreleased]\nSome unreleased things\n\n## [1.0.0]\nMore to add\n\n## [0.0.1]\nInitial version\n\n[Unreleased]: https://github.com/monax/relic/compare/v1.0.0...HEAD\n[1.0.0]: https://github.com/monax/relic/compare/v0.0.1...v1.0.0\n[0.0.1]: https://github.com/monax/relic/commits/v0.0.1",
changelog)
}
func assertChangelog(t *testing.T, expected, actual string) {
if expected != actual {
t.Errorf("expected changelog:\n%s\n\nBut actual changelog was:\n\n%s\nActual (yankable):\nassertChangelog(t, %#v,\nchangelog)\n\n%s\n",
expected, actual, actual, debug.Stack())
}
}
func parseVersion(t *testing.T, versionString string) Version {
version, err := ParseVersion(versionString)
if err != nil {
t.Error(err)
}
return version
}