-
Notifications
You must be signed in to change notification settings - Fork 44
/
autotag_test.go
693 lines (627 loc) · 16 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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
package autotag
import (
"fmt"
"os/exec"
"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
// (optional) Supply a list of commits to apply so you can test the logic between to possible tags wheere they may be more complex multiple bumps
commitList []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, setup.branch)
repo, err := git.Open(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)
}
if len(setup.commitList) != 0 {
for _, c := range setup.commitList {
updateReadme(t, repo, c)
}
}
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: "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 TestNewRepo(t *testing.T) {
var newRepoTests = []struct {
createBranch string
requestBranch string
expectBranch string
}{
{"main", "main", "main"},
{"main", "", "main"},
{"master", "master", "master"},
{"master", "", "master"},
}
for _, tt := range newRepoTests {
tr := createTestRepo(t, tt.createBranch)
repo, err := git.Open(tr)
checkFatal(t, err)
tag := "v0.0.1"
seedTestRepo(t, tag, repo)
r, err := NewRepo(GitRepoConfig{
Branch: tt.requestBranch,
RepoPath: repo.Path(),
})
if err != nil {
t.Fatal("Error creating repo: ", err)
}
if r.branch != tt.expectBranch {
t.Fatalf("Expected branch %s, got [%s]", tt.expectBranch, r.branch)
}
}
}
func TestNewRepoMainAndMaster(t *testing.T) {
// create repo w/"master" branch
tr := createTestRepo(t, "master")
repo, err := git.Open(tr)
checkFatal(t, err)
seedTestRepo(t, "v0.0.1", repo)
// also create "main" branch
f := repoRoot(repo) + "/main"
err = exec.Command("touch", f).Run()
if err != nil {
fmt.Println("FAILED to touch the file ", f, err)
checkFatal(t, err)
}
cmd := exec.Command("git", "checkout", "-b", "main")
cmd.Dir = repoRoot(repo)
err = cmd.Run()
if err != nil {
fmt.Println("FAILED to create/checkout main branch", err)
checkFatal(t, err)
}
makeCommit(repo, "this is a commit on main")
makeTag(repo, "v0.2.1")
// check results
var newRepoTests = []struct {
requestBranch string
expectBranch string
}{
{"main", "main"},
{"master", "master"},
{"", "main"},
}
for _, tt := range newRepoTests {
r, err := NewRepo(GitRepoConfig{
Branch: tt.requestBranch,
RepoPath: repo.Path(),
})
if err != nil {
t.Fatal("Error creating repo: ", err)
}
if r.branch != tt.expectBranch {
t.Fatalf("Expected branch %s, got [%s]", tt.expectBranch, r.branch)
}
}
}
func TestMajor(t *testing.T) {
r := newTestRepo(t, testRepoSetup{
branch: "master",
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 TestMajorWithMain(t *testing.T) {
r := newTestRepo(t, testRepoSetup{
branch: "main",
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.Open(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",
},
{
name: "autotag scheme, Bump with Major with interstitial minor changes",
setup: testRepoSetup{
scheme: "autotag",
initialTag: "1.0.0",
disablePrefix: true,
commitList: []string{
"#patch: thing 1",
"[minor]: break thing 1",
"feat: thing 2",
"[major]: drop support for Node 6",
},
},
expectedTag: "2.0.0",
},
{
name: "autotag scheme, Bump with Major between minor changes",
setup: testRepoSetup{
scheme: "autotag",
initialTag: "1.0.0",
disablePrefix: true,
commitList: []string{
"[minor]: thing 1",
"[major]: drop support for Node 6",
"[minor]: thing 2",
},
},
expectedTag: "2.0.0",
},
{
name: "autotag scheme, version comparison is not lexicographic",
setup: testRepoSetup{
scheme: "autotag",
initialTag: "v0.9.0",
commitList: []string{
"[minor]: thing 1",
"[minor]: thing 2",
},
},
expectedTag: "v0.10.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",
},
{
name: "conventional commits, breaking change with minor interstitial commits",
setup: testRepoSetup{
scheme: "conventional",
commitList: []string{
"feat: thing 1",
"feat!: break thing 1",
"feat: thing 2",
"refactor(runtime)!: drop support for Node 6",
},
initialTag: "v1.0.0",
},
expectedTag: "v2.0.0",
},
{
name: "conventional commits, breaking change between minor commits",
setup: testRepoSetup{
scheme: "conventional",
commitList: []string{
"feat: thing 1",
"feat!: break thing 1",
"feat: thing 2",
},
initialTag: "v1.0.0",
},
expectedTag: "v2.0.0",
},
{
name: "conventional commits, version comparison is not lexicographic",
setup: testRepoSetup{
scheme: "conventional",
commitList: []string{
"feat: thing 1",
"feat: thing 2",
},
initialTag: "v0.9.0",
},
expectedTag: "v0.10.0",
},
}
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.Tags()
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)
})
}
}