forked from edahlseng/standard-version
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
944 lines (791 loc) · 30.7 KB
/
test.js
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
/* global describe it beforeEach afterEach */
'use strict'
const shell = require('shelljs')
const fs = require('fs')
const path = require('path')
const stream = require('stream')
const mockGit = require('mock-git')
const mockery = require('mockery')
const semver = require('semver')
const formatCommitMessage = require('./lib/format-commit-message')
const cli = require('./command')
const standardVersion = require('./index')
require('chai').should()
var cliPath = path.resolve(__dirname, './bin/cli.js')
function branch (branch) {
shell.exec('git branch ' + branch)
}
function checkout (branch) {
shell.exec('git checkout ' + branch)
}
function commit (msg) {
shell.exec('git commit --allow-empty -m"' + msg + '"')
}
function merge (msg, branch) {
shell.exec('git merge --no-ff -m"' + msg + '" ' + branch)
}
function execCli (argString) {
return shell.exec('node ' + cliPath + (argString != null ? ' ' + argString : ''))
}
function execCliAsync (argString) {
return standardVersion(cli.parse('standard-version ' + argString + ' --silent'))
}
function writePackageJson (version, option) {
option = option || {}
var pkg = Object.assign(option, { version: version })
fs.writeFileSync('package.json', JSON.stringify(pkg), 'utf-8')
}
function writeBowerJson (version, option) {
option = option || {}
var bower = Object.assign(option, { version: version })
fs.writeFileSync('bower.json', JSON.stringify(bower), 'utf-8')
}
function writeManifestJson (version, option) {
option = option || {}
var manifest = Object.assign(option, { version: version })
fs.writeFileSync('manifest.json', JSON.stringify(manifest), 'utf-8')
}
function writeNpmShrinkwrapJson (version, option) {
option = option || {}
var shrinkwrap = Object.assign(option, { version: version })
fs.writeFileSync('npm-shrinkwrap.json', JSON.stringify(shrinkwrap), 'utf-8')
}
function writePackageLockJson (version, option) {
option = option || {}
var pkgLock = Object.assign(option, { version: version })
fs.writeFileSync('package-lock.json', JSON.stringify(pkgLock), 'utf-8')
}
function writeGitPreCommitHook () {
fs.writeFileSync('.git/hooks/pre-commit', '#!/bin/sh\necho "precommit ran"\nexit 1', 'utf-8')
fs.chmodSync('.git/hooks/pre-commit', '755')
}
function writePostBumpHook (causeError) {
writeHook('postbump', causeError)
}
function writeHook (hookName, causeError, script) {
shell.mkdir('-p', 'scripts')
var content = script || 'console.error("' + hookName + ' ran")'
content += causeError ? '\nthrow new Error("' + hookName + '-failure")' : ''
fs.writeFileSync('scripts/' + hookName + '.js', content, 'utf-8')
fs.chmodSync('scripts/' + hookName + '.js', '755')
}
function initInTempFolder () {
shell.rm('-rf', 'tmp')
shell.config.silent = true
shell.mkdir('tmp')
shell.cd('tmp')
shell.exec('git init')
commit('root-commit')
;['package.json',
'manifest.json',
'bower.json'
].forEach(metadata => {
try {
delete require.cache[require.resolve(path.join(process.cwd(), metadata))]
} catch (err) {
// we haven't loaded the metadata file yet.
}
})
writePackageJson('1.0.0')
}
function finishTemp () {
shell.cd('../')
shell.rm('-rf', 'tmp')
}
function getPackageVersion () {
return JSON.parse(fs.readFileSync('package.json', 'utf-8')).version
}
describe('format-commit-message', function () {
it('works for no %s', function () {
formatCommitMessage('chore(release): 1.0.0', '1.0.0').should.equal('chore(release): 1.0.0')
})
it('works for one %s', function () {
formatCommitMessage('chore(release): %s', '1.0.0').should.equal('chore(release): 1.0.0')
})
it('works for two %s', function () {
formatCommitMessage('chore(release): %s \n\n* CHANGELOG: https://github.com/conventional-changelog/standard-version/blob/v%s/CHANGELOG.md', '1.0.0').should.equal('chore(release): 1.0.0 \n\n* CHANGELOG: https://github.com/conventional-changelog/standard-version/blob/v1.0.0/CHANGELOG.md')
})
})
describe('cli', function () {
beforeEach(initInTempFolder)
afterEach(finishTemp)
describe('CHANGELOG.md does not exist', function () {
it('populates changelog with commits since last tag by default', function () {
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('fix: patch release')
execCli().code.should.equal(0)
var content = fs.readFileSync('CHANGELOG.md', 'utf-8')
content.should.match(/patch release/)
content.should.not.match(/first commit/)
})
it('includes all commits if --first-release is true', function () {
writePackageJson('1.0.1')
commit('feat: first commit')
commit('fix: patch release')
execCli('--first-release').code.should.equal(0)
var content = fs.readFileSync('CHANGELOG.md', 'utf-8')
content.should.match(/patch release/)
content.should.match(/first commit/)
shell.exec('git tag').stdout.should.match(/1\.0\.1/)
})
})
describe('CHANGELOG.md exists', function () {
it('appends the new release above the last release, removing the old header', function () {
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('fix: patch release')
execCli().code.should.equal(0)
var content = fs.readFileSync('CHANGELOG.md', 'utf-8')
content.should.match(/1\.0\.1/)
content.should.not.match(/legacy header format/)
})
it('commits all staged files', function () {
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('fix: patch release')
fs.writeFileSync('STUFF.md', 'stuff\n', 'utf-8')
shell.exec('git add STUFF.md')
execCli('--commit-all').code.should.equal(0)
var content = fs.readFileSync('CHANGELOG.md', 'utf-8')
var status = shell.exec('git status --porcelain') // see http://unix.stackexchange.com/questions/155046/determine-if-git-working-directory-is-clean-from-a-script
status.should.equal('')
status.should.not.match(/STUFF.md/)
content.should.match(/1\.0\.1/)
content.should.not.match(/legacy header format/)
})
})
describe('with mocked git', function () {
it('--sign signs the commit and tag', function () {
// mock git with file that writes args to gitcapture.log
return mockGit('require("fs").appendFileSync("gitcapture.log", JSON.stringify(process.argv.splice(2)) + "\\n")')
.then(function (unmock) {
execCli('--sign').code.should.equal(0)
var captured = shell.cat('gitcapture.log').stdout.split('\n').map(function (line) {
return line ? JSON.parse(line) : line
})
captured[captured.length - 3].should.deep.equal(['commit', '-S', 'CHANGELOG.md', 'package.json', '-m', 'chore(release): 1.0.1'])
captured[captured.length - 2].should.deep.equal(['tag', '-s', 'v1.0.1', '-m', 'chore(release): 1.0.1'])
unmock()
})
})
it('exits with error code if git commit fails', function () {
// mock git by throwing on attempt to commit
return mockGit('console.error("commit yourself"); process.exit(128);', 'commit')
.then(function (unmock) {
var result = execCli()
result.code.should.equal(1)
result.stderr.should.match(/commit yourself/)
unmock()
})
})
it('exits with error code if git add fails', function () {
// mock git by throwing on attempt to add
return mockGit('console.error("addition is hard"); process.exit(128);', 'add')
.then(function (unmock) {
var result = execCli()
result.code.should.equal(1)
result.stderr.should.match(/addition is hard/)
unmock()
})
})
it('exits with error code if git tag fails', function () {
// mock git by throwing on attempt to commit
return mockGit('console.error("tag, you\'re it"); process.exit(128);', 'tag')
.then(function (unmock) {
var result = execCli()
result.code.should.equal(1)
result.stderr.should.match(/tag, you're it/)
unmock()
})
})
it('doesn\'t fail fast on stderr output from git', function () {
// mock git by throwing on attempt to commit
return mockGit('console.error("haha, kidding, this is just a warning"); process.exit(0);', 'add')
.then(function (unmock) {
writePackageJson('1.0.0')
var result = execCli()
result.code.should.equal(0)
result.stderr.should.match(/haha, kidding, this is just a warning/)
unmock()
})
})
})
describe('lifecycle scripts', () => {
describe('prerelease hook', function () {
it('should run the prerelease hook when provided', function () {
writePackageJson('1.0.0', {
'standard-version': {
'scripts': {
'prerelease': 'node scripts/prerelease'
}
}
})
writeHook('prerelease')
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('feat: first commit')
var result = execCli('--patch')
result.code.should.equal(0)
result.stderr.should.match(/prerelease ran/)
})
it('should abort if the hook returns a non-zero exit code', function () {
writePackageJson('1.0.0', {
'standard-version': {
'scripts': {
'prerelease': 'node scripts/prerelease && exit 1'
}
}
})
writeHook('prerelease')
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('feat: first commit')
var result = execCli('--patch')
result.code.should.equal(1)
result.stderr.should.match(/prerelease ran/)
})
})
describe('prebump hook', function () {
it('should allow prebump hook to return an alternate version #', function () {
writePackageJson('1.0.0', {
'standard-version': {
'scripts': {
'prebump': 'node scripts/prebump'
}
}
})
writeHook('prebump', false, 'console.log("9.9.9")')
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('feat: first commit')
var result = execCli('--patch')
result.stdout.should.match(/9\.9\.9/)
result.code.should.equal(0)
})
})
describe('postbump hook', function () {
it('should run the postbump hook when provided', function () {
writePackageJson('1.0.0', {
'standard-version': {
'scripts': {
'postbump': 'node scripts/postbump'
}
}
})
writePostBumpHook()
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('feat: first commit')
var result = execCli('--patch')
result.code.should.equal(0)
result.stderr.should.match(/postbump ran/)
})
it('should run the postbump and exit with error when postbump fails', function () {
writePackageJson('1.0.0', {
'standard-version': {
'scripts': {
'postbump': 'node scripts/postbump'
}
}
})
writePostBumpHook(true)
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('feat: first commit')
var result = execCli('--patch')
result.code.should.equal(1)
result.stderr.should.match(/postbump-failure/)
})
})
describe('precommit hook', function () {
it('should run the precommit hook when provided', function () {
writePackageJson('1.0.0', {
'standard-version': {
'scripts': {
'precommit': 'node scripts/precommit'
}
}
})
writeHook('precommit')
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('feat: first commit')
var result = execCli('--patch')
result.code.should.equal(0)
result.stderr.should.match(/precommit ran/)
})
it('should run the precommit hook and exit with error when precommit fails', function () {
writePackageJson('1.0.0', {
'standard-version': {
'scripts': {
'precommit': 'node scripts/precommit'
}
}
})
writeHook('precommit', true)
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('feat: first commit')
var result = execCli('--patch')
result.code.should.equal(1)
result.stderr.should.match(/precommit-failure/)
})
it('should allow an alternate commit message to be provided by precommit script', function () {
writePackageJson('1.0.0', {
'standard-version': {
'scripts': {
'precommit': 'node scripts/precommit'
}
}
})
writeHook('precommit', false, 'console.log("releasing %s delivers #222")')
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('feat: first commit')
var result = execCli('--patch')
result.code.should.equal(0)
shell.exec('git log --oneline -n1').should.match(/delivers #222/)
})
})
})
describe('pre-release', function () {
it('works fine without specifying a tag id when prereleasing', function () {
writePackageJson('1.0.0')
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('feat: first commit')
return execCliAsync('--prerelease')
.then(function () {
// it's a feature commit, so it's minor type
getPackageVersion().should.equal('1.1.0-0')
})
})
it('advises use of --tag prerelease for publishing to npm', function () {
writePackageJson('1.0.0')
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('feat: first commit')
execCli('--prerelease').stdout.should.include('--tag prerelease')
})
it('advises use of --tag alpha for publishing to npm when tagging alpha', function () {
writePackageJson('1.0.0')
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('feat: first commit')
execCli('--prerelease alpha').stdout.should.include('--tag alpha')
})
})
describe('manual-release', function () {
it('throws error when not specifying a release type', function () {
writePackageJson('1.0.0')
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('fix: first commit')
execCli('--release-as').code.should.above(0)
})
describe('release-types', function () {
var regularTypes = ['major', 'minor', 'patch']
regularTypes.forEach(function (type) {
it('creates a ' + type + ' release', function () {
var originVer = '1.0.0'
writePackageJson(originVer)
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('fix: first commit')
return execCliAsync('--release-as ' + type)
.then(function () {
var version = {
major: semver.major(originVer),
minor: semver.minor(originVer),
patch: semver.patch(originVer)
}
version[type] += 1
getPackageVersion().should.equal(version.major + '.' + version.minor + '.' + version.patch)
})
})
})
// this is for pre-releases
regularTypes.forEach(function (type) {
it('creates a pre' + type + ' release', function () {
var originVer = '1.0.0'
writePackageJson(originVer)
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('fix: first commit')
return execCliAsync('--release-as ' + type + ' --prerelease ' + type)
.then(function () {
var version = {
major: semver.major(originVer),
minor: semver.minor(originVer),
patch: semver.patch(originVer)
}
version[type] += 1
getPackageVersion().should.equal(version.major + '.' + version.minor + '.' + version.patch + '-' + type + '.0')
})
})
})
})
describe('release-as-exact', function () {
it('releases as v100.0.0', function () {
var originVer = '1.0.0'
writePackageJson(originVer)
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('fix: first commit')
return execCliAsync('--release-as v100.0.0')
.then(function () {
getPackageVersion().should.equal('100.0.0')
})
})
it('releases as 200.0.0-amazing', function () {
var originVer = '1.0.0'
writePackageJson(originVer)
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('fix: first commit')
return execCliAsync('--release-as 200.0.0-amazing')
.then(function () {
getPackageVersion().should.equal('200.0.0-amazing')
})
})
})
it('creates a prerelease with a new minor version after two prerelease patches', function () {
writePackageJson('1.0.0')
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
commit('fix: first patch')
return execCliAsync('--release-as patch --prerelease dev')
.then(function () {
getPackageVersion().should.equal('1.0.1-dev.0')
})
// second
.then(function () {
commit('fix: second patch')
return execCliAsync('--prerelease dev')
})
.then(function () {
getPackageVersion().should.equal('1.0.1-dev.1')
})
// third
.then(function () {
commit('feat: first new feat')
return execCliAsync('--release-as minor --prerelease dev')
})
.then(function () {
getPackageVersion().should.equal('1.1.0-dev.0')
})
.then(function () {
commit('fix: third patch')
return execCliAsync('--release-as minor --prerelease dev')
})
.then(function () {
getPackageVersion().should.equal('1.1.0-dev.1')
})
.then(function () {
commit('fix: forth patch')
return execCliAsync('--prerelease dev')
})
.then(function () {
getPackageVersion().should.equal('1.1.0-dev.2')
})
})
})
it('handles commit messages longer than 80 characters', function () {
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('fix: this is my fairly long commit message which is testing whether or not we allow for long commit messages')
execCli().code.should.equal(0)
var content = fs.readFileSync('CHANGELOG.md', 'utf-8')
content.should.match(/this is my fairly long commit message which is testing whether or not we allow for long commit messages/)
})
it('formats the commit and tag messages appropriately', function () {
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')
execCli().code.should.equal(0)
// check last commit message
shell.exec('git log --oneline -n1').stdout.should.match(/chore\(release\): 1\.1\.0/)
// check annotated tag message
shell.exec('git tag -l -n1 v1.1.0').stdout.should.match(/chore\(release\): 1\.1\.0/)
})
it('appends line feed at end of package.json', function () {
execCli().code.should.equal(0)
var pkgJson = fs.readFileSync('package.json', 'utf-8')
pkgJson.should.equal(['{', ' "version": "1.0.1"', '}', ''].join('\n'))
})
it('does not run git hooks if the --no-verify flag is passed', function () {
writeGitPreCommitHook()
commit('feat: first commit')
execCli('--no-verify').code.should.equal(0)
commit('feat: second commit')
execCli('-n').code.should.equal(0)
})
it('does not print output when the --silent flag is passed', function () {
var result = execCli('--silent')
result.code.should.equal(0)
result.stdout.should.equal('')
result.stderr.should.equal('')
})
it('does not display `npm publish` if the package is private', function () {
writePackageJson('1.0.0', { private: true })
var result = execCli()
result.code.should.equal(0)
result.stdout.should.not.match(/npm publish/)
})
it('includes merge commits', function () {
var branchName = 'new-feature'
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
branch(branchName)
checkout(branchName)
commit('Implementing new feature')
checkout('master')
merge('feat: new feature from branch', branchName)
execCli().code.should.equal(0)
var content = fs.readFileSync('CHANGELOG.md', 'utf-8')
content.should.match(/new feature from branch/)
var pkgJson = fs.readFileSync('package.json', 'utf-8')
pkgJson.should.equal(['{', ' "version": "1.1.0"', '}', ''].join('\n'))
})
it('exits with error code if "scripts" is not an object', () => {
writePackageJson('1.0.0', {
'standard-version': {
scripts: 'echo hello'
}
})
commit('feat: first commit')
var result = execCli()
result.code.should.equal(1)
result.stderr.should.match(/scripts must be an object/)
})
it('exits with error code if "skip" is not an object', () => {
writePackageJson('1.0.0', {
'standard-version': {
skip: true
}
})
commit('feat: first commit')
var result = execCli()
result.code.should.equal(1)
result.stderr.should.match(/skip must be an object/)
})
})
describe('standard-version', function () {
beforeEach(initInTempFolder)
afterEach(finishTemp)
describe('with mocked conventionalRecommendedBump', function () {
beforeEach(function () {
mockery.enable({ warnOnUnregistered: false, useCleanCache: true })
mockery.registerMock('conventional-recommended-bump', function (_, cb) {
cb(new Error('bump err'))
})
})
afterEach(function () {
mockery.deregisterMock('conventional-recommended-bump')
mockery.disable()
})
it('should exit on bump error', function (done) {
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')
require('./index')({ silent: true })
.catch((err) => {
err.message.should.match(/bump err/)
done()
})
})
})
describe('with mocked conventionalChangelog', function () {
beforeEach(function () {
mockery.enable({ warnOnUnregistered: false, useCleanCache: true })
mockery.registerMock('conventional-changelog', function () {
var readable = new stream.Readable({ objectMode: true })
readable._read = function () {
}
setImmediate(readable.emit.bind(readable), 'error', new Error('changelog err'))
return readable
})
})
afterEach(function () {
mockery.deregisterMock('conventional-changelog')
mockery.disable()
})
it('should exit on changelog error', function (done) {
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')
require('./index')({ silent: true })
.catch((err) => {
err.message.should.match(/changelog err/)
return done()
})
})
})
it('formats the commit and tag messages appropriately', function (done) {
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')
require('./index')({ silent: true })
.then(() => {
// check last commit message
shell.exec('git log --oneline -n1').stdout.should.match(/chore\(release\): 1\.1\.0/)
// check annotated tag message
shell.exec('git tag -l -n1 v1.1.0').stdout.should.match(/chore\(release\): 1\.1\.0/)
done()
})
})
describe('without a package file to bump', function () {
it('should exit with error', function () {
shell.rm('package.json')
return require('./index')({
silent: true,
gitTagFallback: false
})
.catch((err) => {
err.message.should.equal('no package file found')
})
})
})
describe('bower.json support', function () {
beforeEach(function () {
writeBowerJson('1.0.0')
})
it('bumps version # in bower.json', function () {
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')
return require('./index')({ silent: true })
.then(() => {
JSON.parse(fs.readFileSync('bower.json', 'utf-8')).version.should.equal('1.1.0')
getPackageVersion().should.equal('1.1.0')
})
})
})
describe('manifest.json support', function () {
beforeEach(function () {
writeManifestJson('1.0.0')
})
it('bumps version # in manifest.json', function () {
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')
return require('./index')({ silent: true })
.then(() => {
JSON.parse(fs.readFileSync('manifest.json', 'utf-8')).version.should.equal('1.1.0')
getPackageVersion().should.equal('1.1.0')
})
})
})
describe('npm-shrinkwrap.json support', function () {
beforeEach(function () {
writeNpmShrinkwrapJson('1.0.0')
})
it('bumps version # in npm-shrinkwrap.json', function (done) {
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')
require('./index')({ silent: true })
.then(() => {
JSON.parse(fs.readFileSync('npm-shrinkwrap.json', 'utf-8')).version.should.equal('1.1.0')
getPackageVersion().should.equal('1.1.0')
return done()
})
})
})
describe('package-lock.json support', function () {
beforeEach(function () {
writePackageLockJson('1.0.0')
fs.writeFileSync('.gitignore', '', 'utf-8')
})
it('bumps version # in package-lock.json', function () {
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')
return require('./index')({ silent: true })
.then(() => {
JSON.parse(fs.readFileSync('package-lock.json', 'utf-8')).version.should.equal('1.1.0')
getPackageVersion().should.equal('1.1.0')
})
})
})
describe('dry-run', function () {
it('skips all non-idempotent steps', function (done) {
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')
execCli('--dry-run').stdout.should.match(/### Features/)
shell.exec('git log --oneline -n1').stdout.should.match(/feat: new feature!/)
shell.exec('git tag').stdout.should.match(/1\.0\.0/)
getPackageVersion().should.equal('1.0.0')
return done()
})
})
describe('skip', () => {
it('allows bump and changelog generation to be skipped', function () {
let changelogContent = 'legacy header format<a name="1.0.0">\n'
writePackageJson('1.0.0')
fs.writeFileSync('CHANGELOG.md', changelogContent, 'utf-8')
commit('feat: first commit')
return execCliAsync('--skip.bump true --skip.changelog true')
.then(function () {
getPackageVersion().should.equal('1.0.0')
var content = fs.readFileSync('CHANGELOG.md', 'utf-8')
content.should.equal(changelogContent)
})
})
it('allows the commit phase to be skipped', function () {
let changelogContent = 'legacy header format<a name="1.0.0">\n'
writePackageJson('1.0.0')
fs.writeFileSync('CHANGELOG.md', changelogContent, 'utf-8')
commit('feat: new feature from branch')
return execCliAsync('--skip.commit true')
.then(function () {
getPackageVersion().should.equal('1.1.0')
var content = fs.readFileSync('CHANGELOG.md', 'utf-8')
content.should.match(/new feature from branch/)
// check last commit message
shell.exec('git log --oneline -n1').stdout.should.match(/feat: new feature from branch/)
})
})
})
describe('.gitignore', () => {
beforeEach(function () {
writeBowerJson('1.0.0')
})
it('does not update files present in .gitignore', () => {
fs.writeFileSync('.gitignore', 'bower.json', 'utf-8')
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')
return require('./index')({ silent: true })
.then(() => {
JSON.parse(fs.readFileSync('bower.json', 'utf-8')).version.should.equal('1.0.0')
getPackageVersion().should.equal('1.1.0')
})
})
})
describe('.gitignore', () => {
beforeEach(function () {
writeBowerJson('1.0.0')
})
it('does not update files present in .gitignore', () => {
fs.writeFileSync('.gitignore', 'bower.json', 'utf-8')
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')
return require('./index')({ silent: true })
.then(() => {
JSON.parse(fs.readFileSync('bower.json', 'utf-8')).version.should.equal('1.0.0')
getPackageVersion().should.equal('1.1.0')
})
})
})
describe('gitTagFallback', () => {
it('defaults to 1.0.0 if no tags in git history', () => {
shell.rm('package.json')
commit('feat: first commit')
return require('./index')({ silent: true })
.then(() => {
const output = shell.exec('git tag')
output.stdout.should.include('v1.1.0')
})
})
it('bases version on last tag, if tags are found', () => {
shell.rm('package.json')
shell.exec('git tag -a v5.0.0 -m "a release"')
shell.exec('git tag -a v3.0.0 -m "another release"')
commit('feat: another commit')
return require('./index')({ silent: true })
.then(() => {
const output = shell.exec('git tag')
output.stdout.should.include('v5.1.0')
})
})
})
})