forked from autotag-dev/autotag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautotag_test.go
497 lines (455 loc) · 11.7 KB
/
autotag_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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package autotag
import (
"fmt"
"testing"
"time"
"github.com/alecthomas/assert"
"github.com/gogs/git-module"
)
func init() {
// fixed point-in-time time.Now() for testing
timeNow = func() time.Time {
return time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
}
}
// testRepoSetup provides a method for setting up a new git repo in a temporary directory
type testRepoSetup struct {
// (optional) versioning scheme to use, eg: "" or "autotag", "conventional". If not set, defaults to "" (autotag)
scheme string
// (optional) branch to create. If not set, defaults to "master"
branch string
// (optional) initial tag. If not set, defaults to "v0.0.1"
initialTag string
// (optional) extra tags to add to the repo
extraTags []string
// (optional) the prerelease name to use, eg "pre". If not set, no prerelease name will be used
preReleaseName string
// (optional) the prerelease timestamp format to use, eg: "epoch". If not set, no prerelease timestamp will be used
preReleaseTimestampLayout string
// (optional) build metadata to append to the version
buildMetadata string
// (optional) prepend literal 'v' to version tags (default: true)
disablePrefix bool
// (optional) commit message to use for the next, untagged commit. Settings this allows for testing the
// commit message parsing logic. eg: "#major this is a major commit"
nextCommit string
}
// newTestRepo creates a new git repo in a temporary directory and returns an autotag.GitRepo struct for
// testing the autotag package.
// You must call cleanupTestRepo(t, r.repo) to remove the temporary directory after running tests.
func newTestRepo(t *testing.T, setup testRepoSetup) GitRepo {
tr := createTestRepo(t)
repo, err := git.OpenRepository(tr)
checkFatal(t, err)
branch := setup.branch
if branch == "" {
branch = "master"
}
tag := setup.initialTag
if setup.initialTag == "" {
tag = "v0.0.1"
if setup.disablePrefix {
tag = "0.0.1"
}
}
seedTestRepo(t, tag, repo)
if len(setup.extraTags) > 0 {
for _, t := range setup.extraTags {
makeTag(repo, t)
}
}
if setup.nextCommit != "" {
updateReadme(t, repo, setup.nextCommit)
}
r, err := NewRepo(GitRepoConfig{
RepoPath: repo.Path,
Branch: branch,
PreReleaseName: setup.preReleaseName,
PreReleaseTimestampLayout: setup.preReleaseTimestampLayout,
BuildMetadata: setup.buildMetadata,
Scheme: setup.scheme,
Prefix: !setup.disablePrefix,
})
if err != nil {
t.Fatal("Error creating repo: ", err)
}
return *r
}
func TestValidateConfig(t *testing.T) {
tests := []struct {
name string
cfg GitRepoConfig
shouldErr bool
}{
{
name: "missing branch",
cfg: GitRepoConfig{},
shouldErr: true,
},
{
name: "invalid build metadata",
cfg: GitRepoConfig{
Branch: "master",
BuildMetadata: "foo..bar",
},
shouldErr: true,
},
{
name: "invalid build metadata - purely empty identifier",
cfg: GitRepoConfig{
Branch: "master",
BuildMetadata: "...",
},
shouldErr: true,
},
{
name: "invalid pre-release-name - leading zero",
cfg: GitRepoConfig{
Branch: "master",
PreReleaseName: "024",
},
shouldErr: true,
},
{
name: "invalid pre-release-name - empty identifier",
cfg: GitRepoConfig{
Branch: "master",
PreReleaseName: "...",
},
shouldErr: true,
},
{
name: "invalid pre-release-timestamp",
cfg: GitRepoConfig{
Branch: "master",
PreReleaseTimestampLayout: "foo",
},
shouldErr: true,
},
{
name: "valid config with all options used",
cfg: GitRepoConfig{
Branch: "master",
PreReleaseName: "foo",
PreReleaseTimestampLayout: "epoch",
BuildMetadata: "g12345678",
Prefix: true,
},
shouldErr: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := validateConfig(tc.cfg)
if tc.shouldErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
func TestMajor(t *testing.T) {
r := newTestRepo(t, testRepoSetup{
initialTag: "v1.0.1",
})
defer cleanupTestRepo(t, r.repo)
v, err := r.MajorBump()
if err != nil {
t.Fatal("MajorBump failed: ", err)
}
if v.String() != "2.0.0" {
t.Fatalf("MajorBump failed expected '2.0.0' got '%s' ", v)
}
fmt.Printf("Major is now %s\n", v)
}
func TestMinor(t *testing.T) {
r := newTestRepo(t, testRepoSetup{
initialTag: "v1.0.1",
})
defer cleanupTestRepo(t, r.repo)
v, err := r.MinorBump()
if err != nil {
t.Fatal("MinorBump failed: ", err)
}
if v.String() != "1.1.0" {
t.Fatalf("MinorBump failed expected '1.1.0' got '%s' \n", v)
}
}
func TestPatch(t *testing.T) {
r := newTestRepo(t, testRepoSetup{
initialTag: "v1.0.1",
})
defer cleanupTestRepo(t, r.repo)
v, err := r.PatchBump()
if err != nil {
t.Fatal("PatchBump failed: ", err)
}
if v.String() != "1.0.2" {
t.Fatalf("PatchBump failed expected '1.0.2' got '%s' \n", v)
}
}
func TestMissingInitialTag(t *testing.T) {
tr := createTestRepo(t)
repo, err := git.OpenRepository(tr)
checkFatal(t, err)
defer cleanupTestRepo(t, repo)
updateReadme(t, repo, "a commit before any usable tag has been created")
_, err = NewRepo(GitRepoConfig{
RepoPath: repo.Path,
Branch: "master",
})
assert.Error(t, err)
}
func TestAutoTag(t *testing.T) {
tests := []struct {
name string
setup testRepoSetup
shouldErr bool
expectedTag string
}{
// tests for autotag (default) scheme
{
name: "autotag scheme, [major] bump",
setup: testRepoSetup{
scheme: "autotag",
nextCommit: "[major] this is a big release\n\nfoo bar baz\n",
initialTag: "v1.0.0",
},
expectedTag: "v2.0.0",
},
{
name: "autotag scheme, [minor] bump",
setup: testRepoSetup{
scheme: "autotag",
nextCommit: "[minor] this is a smaller release\n\nfoo bar baz\n",
initialTag: "v1.0.0",
},
expectedTag: "v1.1.0",
},
{
name: "autotag scheme, patch bump",
setup: testRepoSetup{
scheme: "autotag",
nextCommit: "this is just a basic change\n\nfoo bar baz\n",
initialTag: "v1.0.0",
},
expectedTag: "v1.0.1",
},
{
name: "autotag scheme, #major bump",
setup: testRepoSetup{
scheme: "autotag",
nextCommit: "#major this is a big release\n\nfoo bar baz\n",
initialTag: "v1.0.0",
},
expectedTag: "v2.0.0",
},
{
name: "autotag scheme, #minor bump",
setup: testRepoSetup{
scheme: "autotag",
nextCommit: "#minor this is a smaller release\n\nfoo bar baz\n",
initialTag: "v1.0.0",
},
expectedTag: "v1.1.0",
},
{
name: "pre-release-name with patch bump",
setup: testRepoSetup{
scheme: "autotag",
nextCommit: "#patch bump",
initialTag: "v1.0.0",
preReleaseName: "dev",
},
expectedTag: "v1.0.1-dev",
},
{
name: "epoch pre-release-timestamp",
setup: testRepoSetup{
scheme: "autotag",
nextCommit: "#patch bump",
initialTag: "v1.0.0",
preReleaseTimestampLayout: "epoch",
},
expectedTag: fmt.Sprintf("v1.0.1-%d", timeNow().UTC().Unix()),
},
{
name: "datetime pre-release-timestamp",
setup: testRepoSetup{
scheme: "autotag",
nextCommit: "#patch bump",
initialTag: "v1.0.0",
preReleaseTimestampLayout: "datetime",
},
expectedTag: fmt.Sprintf("v1.0.1-%s", timeNow().Format(datetimeTsLayout)),
},
{
name: "epoch pre-release-timestamp and pre-release-name",
setup: testRepoSetup{
scheme: "autotag",
nextCommit: "#patch bump",
initialTag: "v1.0.0",
preReleaseName: "dev",
preReleaseTimestampLayout: "epoch",
},
expectedTag: fmt.Sprintf("v1.0.1-dev.%d", timeNow().UTC().Unix()),
},
{
name: "ignore non-conforming tags",
setup: testRepoSetup{
scheme: "autotag",
nextCommit: "#patch bump",
initialTag: "v1.0.0",
extraTags: []string{"foo", ""},
},
expectedTag: "v1.0.1",
},
{
name: "test with pre-relase tag",
setup: testRepoSetup{
scheme: "autotag",
nextCommit: "#patch bump",
initialTag: "v1.0.0",
extraTags: []string{"v1.0.1-pre"},
},
expectedTag: "v1.0.1",
},
{
name: "build metadata",
setup: testRepoSetup{
scheme: "autotag",
nextCommit: "#patch bump",
initialTag: "v1.0.0",
buildMetadata: "g012345678",
},
expectedTag: "v1.0.1+g012345678",
},
{
name: "autotag scheme, [major] bump without prefix",
setup: testRepoSetup{
scheme: "autotag",
nextCommit: "[major] this is a big release\n\nfoo bar baz\n",
initialTag: "1.0.0",
disablePrefix: true,
},
expectedTag: "2.0.0",
},
// tests for conventional commits scheme. Based on:
// https://www.conventionalcommits.org/en/v1.0.0/#summary
// and
// https://www.conventionalcommits.org/en/v1.0.0/#examples
{
name: "conventional commits, minor bump without scope",
setup: testRepoSetup{
scheme: "conventional",
nextCommit: "feat: allow provided config object to extend other configs",
initialTag: "v1.0.0",
},
expectedTag: "v1.1.0",
},
{
name: "conventional commits, minor bump with scope",
setup: testRepoSetup{
scheme: "conventional",
nextCommit: "feat(lang): add polish language",
initialTag: "v1.0.0",
},
expectedTag: "v1.1.0",
},
{
name: "conventional commits, breaking change via ! appended to type",
setup: testRepoSetup{
scheme: "conventional",
nextCommit: "refactor!: drop support for Node 6",
initialTag: "v1.0.0",
},
expectedTag: "v2.0.0",
},
{
name: "conventional commits, breaking change via ! appended to type/scope",
setup: testRepoSetup{
scheme: "conventional",
nextCommit: "refactor(runtime)!: drop support for Node 6",
initialTag: "v1.0.0",
},
expectedTag: "v2.0.0",
},
{
name: "conventional commits, breaking change via footer",
setup: testRepoSetup{
scheme: "conventional",
nextCommit: "feat: allow provided config object to extend other configs\n\nbody before footer\n\nBREAKING CHANGE: non-backwards compatible",
initialTag: "v1.0.0",
},
expectedTag: "v2.0.0",
},
{
name: "conventional commits, patch/minor bump",
setup: testRepoSetup{
scheme: "conventional",
nextCommit: "fix: correct minor typos in code",
initialTag: "v1.0.0",
},
expectedTag: "v1.0.1",
},
{
name: "conventional commits, non-conforming fallback to patch bump",
setup: testRepoSetup{
scheme: "conventional",
nextCommit: "not a conventional commit message",
initialTag: "v1.0.0",
},
expectedTag: "v1.0.1",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
r := newTestRepo(t, tc.setup)
defer cleanupTestRepo(t, r.repo)
err := r.AutoTag()
if tc.shouldErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
tags, err := r.repo.GetTags()
checkFatal(t, err)
assert.Contains(t, tags, tc.expectedTag)
})
}
}
func TestValidateSemVerBuildMetadata(t *testing.T) {
tests := []struct {
name string
meta string
valid bool
}{
{
name: "valid single-part metadata",
meta: "g123456",
valid: true,
},
{
name: "valid multi-part metadata",
meta: "g123456.20200512",
valid: true,
},
{
name: "invalid characters",
meta: "g123456,foo_bar",
valid: false,
},
{
name: "empty string",
meta: "",
valid: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
valid := validateSemVerBuildMetadata(tc.meta)
assert.Equal(t, tc.valid, valid)
})
}
}