commit-and-tag-version 9.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/.editorconfig +9 -0
  2. package/.eslintrc +6 -0
  3. package/.github/ISSUE_TEMPLATE/ask-a-question.md +7 -0
  4. package/.github/ISSUE_TEMPLATE/bug-report.md +29 -0
  5. package/.github/workflows/ci.yaml +33 -0
  6. package/.github/workflows/release-please.yml +31 -0
  7. package/.versionrc +10 -0
  8. package/CHANGELOG.md +524 -0
  9. package/LICENSE.txt +16 -0
  10. package/README.md +480 -0
  11. package/bin/cli.js +14 -0
  12. package/command.js +145 -0
  13. package/defaults.js +40 -0
  14. package/index.js +92 -0
  15. package/lib/checkpoint.js +12 -0
  16. package/lib/configuration.js +41 -0
  17. package/lib/format-commit-message.js +4 -0
  18. package/lib/latest-semver-tag.js +17 -0
  19. package/lib/lifecycles/bump.js +172 -0
  20. package/lib/lifecycles/changelog.js +66 -0
  21. package/lib/lifecycles/commit.js +69 -0
  22. package/lib/lifecycles/tag.js +39 -0
  23. package/lib/preset-loader.js +17 -0
  24. package/lib/print-error.js +12 -0
  25. package/lib/run-exec.js +18 -0
  26. package/lib/run-execFile.js +18 -0
  27. package/lib/run-lifecycle-script.js +13 -0
  28. package/lib/updaters/index.js +85 -0
  29. package/lib/updaters/types/json.js +25 -0
  30. package/lib/updaters/types/plain-text.js +7 -0
  31. package/lib/write-file.js +6 -0
  32. package/package.json +69 -0
  33. package/renovate.json +9 -0
  34. package/test/config-files.spec.js +179 -0
  35. package/test/core.spec.js +850 -0
  36. package/test/git.spec.js +468 -0
  37. package/test/mocks/VERSION-1.0.0.txt +1 -0
  38. package/test/mocks/VERSION-6.3.1.txt +1 -0
  39. package/test/mocks/manifest-6.3.1.json +3 -0
  40. package/test/mocks/mix.exs +28 -0
  41. package/test/mocks/updater/customer-updater.js +12 -0
  42. package/test/mocks/version.txt +0 -0
  43. package/test/preset.spec.js +52 -0
@@ -0,0 +1,850 @@
1
+ /* global describe it afterEach */
2
+
3
+ 'use strict'
4
+
5
+ const shell = require('shelljs')
6
+ const fs = require('fs')
7
+ const { resolve } = require('path')
8
+ const { Readable } = require('stream')
9
+ const mockFS = require('mock-fs')
10
+ const mockery = require('mockery')
11
+ const stdMocks = require('std-mocks')
12
+
13
+ const cli = require('../command')
14
+ const formatCommitMessage = require('../lib/format-commit-message')
15
+
16
+ require('chai').should()
17
+
18
+ // set by mock()
19
+ let standardVersion
20
+
21
+ function exec (opt = '', git) {
22
+ if (typeof opt === 'string') {
23
+ opt = cli.parse(`commit-and-tag-version ${opt}`)
24
+ }
25
+ if (!git) opt.skip = Object.assign({}, opt.skip, { commit: true, tag: true })
26
+ return standardVersion(opt)
27
+ }
28
+
29
+ function getPackageVersion () {
30
+ return JSON.parse(fs.readFileSync('package.json', 'utf-8')).version
31
+ }
32
+
33
+ /**
34
+ * Mock external conventional-changelog modules
35
+ *
36
+ * Mocks should be unregistered in test cleanup by calling unmock()
37
+ *
38
+ * bump?: 'major' | 'minor' | 'patch' | Error | (opt, cb) => { cb(err) | cb(null, { releaseType }) }
39
+ * changelog?: string | Error | Array<string | Error | (opt) => string | null>
40
+ * execFile?: ({ dryRun, silent }, cmd, cmdArgs) => Promise<string>
41
+ * fs?: { [string]: string | Buffer | any }
42
+ * pkg?: { [string]: any }
43
+ * tags?: string[] | Error
44
+ */
45
+ function mock ({ bump, changelog, execFile, fs, pkg, tags } = {}) {
46
+ mockery.enable({ warnOnUnregistered: false, useCleanCache: true })
47
+
48
+ mockery.registerMock('conventional-recommended-bump', function (opt, cb) {
49
+ if (typeof bump === 'function') bump(opt, cb)
50
+ else if (bump instanceof Error) cb(bump)
51
+ else cb(null, bump ? { releaseType: bump } : {})
52
+ })
53
+
54
+ if (!Array.isArray(changelog)) changelog = [changelog]
55
+ mockery.registerMock(
56
+ 'conventional-changelog',
57
+ (opt) =>
58
+ new Readable({
59
+ read (_size) {
60
+ const next = changelog.shift()
61
+ if (next instanceof Error) {
62
+ this.destroy(next)
63
+ } else if (typeof next === 'function') {
64
+ this.push(next(opt))
65
+ } else {
66
+ this.push(next ? Buffer.from(next, 'utf8') : null)
67
+ }
68
+ }
69
+ })
70
+ )
71
+
72
+ mockery.registerMock('git-semver-tags', function (cb) {
73
+ if (tags instanceof Error) cb(tags)
74
+ else cb(null, tags | [])
75
+ })
76
+
77
+ if (typeof execFile === 'function') {
78
+ // called from commit & tag lifecycle methods
79
+ mockery.registerMock('../run-execFile', execFile)
80
+ }
81
+
82
+ // needs to be set after mockery, but before mock-fs
83
+ standardVersion = require('../index')
84
+
85
+ fs = Object.assign({}, fs)
86
+ if (pkg) {
87
+ fs['package.json'] = JSON.stringify(pkg)
88
+ } else if (pkg === undefined && !fs['package.json']) {
89
+ fs['package.json'] = JSON.stringify({ version: '1.0.0' })
90
+ }
91
+ mockFS(fs)
92
+
93
+ stdMocks.use()
94
+ return () => stdMocks.flush()
95
+ }
96
+
97
+ function unmock () {
98
+ mockery.deregisterAll()
99
+ mockery.disable()
100
+ mockFS.restore()
101
+ stdMocks.restore()
102
+ standardVersion = null
103
+
104
+ // push out prints from the Mocha reporter
105
+ const { stdout } = stdMocks.flush()
106
+ for (const str of stdout) {
107
+ if (str.startsWith(' ')) process.stdout.write(str)
108
+ }
109
+ }
110
+
111
+ describe('format-commit-message', function () {
112
+ it('works for no {{currentTag}}', function () {
113
+ formatCommitMessage('chore(release): 1.0.0', '1.0.0').should.equal(
114
+ 'chore(release): 1.0.0'
115
+ )
116
+ })
117
+ it('works for one {{currentTag}}', function () {
118
+ formatCommitMessage('chore(release): {{currentTag}}', '1.0.0').should.equal(
119
+ 'chore(release): 1.0.0'
120
+ )
121
+ })
122
+ it('works for two {{currentTag}}', function () {
123
+ formatCommitMessage(
124
+ 'chore(release): {{currentTag}} \n\n* CHANGELOG: https://github.com/absolute-version/commit-and-tag-version/blob/v{{currentTag}}/CHANGELOG.md',
125
+ '1.0.0'
126
+ ).should.equal(
127
+ 'chore(release): 1.0.0 \n\n* CHANGELOG: https://github.com/absolute-version/commit-and-tag-version/blob/v1.0.0/CHANGELOG.md'
128
+ )
129
+ })
130
+ })
131
+
132
+ describe('cli', function () {
133
+ afterEach(unmock)
134
+
135
+ describe('CHANGELOG.md does not exist', function () {
136
+ it('populates changelog with commits since last tag by default', async function () {
137
+ mock({ bump: 'patch', changelog: 'patch release\n', tags: ['v1.0.0'] })
138
+ await exec()
139
+ const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
140
+ content.should.match(/patch release/)
141
+ })
142
+
143
+ it('includes all commits if --first-release is true', async function () {
144
+ mock({
145
+ bump: 'minor',
146
+ changelog: 'first commit\npatch release\n',
147
+ pkg: { version: '1.0.1' }
148
+ })
149
+ await exec('--first-release')
150
+ const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
151
+ content.should.match(/patch release/)
152
+ content.should.match(/first commit/)
153
+ })
154
+
155
+ it('skipping changelog will not create a changelog file', async function () {
156
+ mock({ bump: 'minor', changelog: 'foo\n' })
157
+ await exec('--skip.changelog true')
158
+ getPackageVersion().should.equal('1.1.0')
159
+ try {
160
+ fs.readFileSync('CHANGELOG.md', 'utf-8')
161
+ throw new Error('File should not exist')
162
+ } catch (err) {
163
+ err.code.should.equal('ENOENT')
164
+ }
165
+ })
166
+ })
167
+
168
+ describe('CHANGELOG.md exists', function () {
169
+ it('appends the new release above the last release, removing the old header (legacy format)', async function () {
170
+ mock({
171
+ bump: 'patch',
172
+ changelog: 'release 1.0.1\n',
173
+ fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' },
174
+ tags: ['v1.0.0']
175
+ })
176
+ await exec()
177
+ const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
178
+ content.should.match(/1\.0\.1/)
179
+ content.should.not.match(/legacy header format/)
180
+ })
181
+
182
+ it('appends the new release above the last release, removing the old header (new format)', async function () {
183
+ const { header } = require('../defaults')
184
+ const changelog1 =
185
+ '### [1.0.1](/compare/v1.0.0...v1.0.1) (YYYY-MM-DD)\n\n\n### Bug Fixes\n\n* patch release ABCDEFXY\n'
186
+ mock({ bump: 'patch', changelog: changelog1, tags: ['v1.0.0'] })
187
+ await exec()
188
+ let content = fs.readFileSync('CHANGELOG.md', 'utf-8')
189
+ content.should.equal(header + '\n' + changelog1)
190
+
191
+ const changelog2 =
192
+ '### [1.0.2](/compare/v1.0.1...v1.0.2) (YYYY-MM-DD)\n\n\n### Bug Fixes\n\n* another patch release ABCDEFXY\n'
193
+ unmock()
194
+ mock({
195
+ bump: 'patch',
196
+ changelog: changelog2,
197
+ fs: { 'CHANGELOG.md': content },
198
+ tags: ['v1.0.0', 'v1.0.1']
199
+ })
200
+ await exec()
201
+ content = fs.readFileSync('CHANGELOG.md', 'utf-8')
202
+ content.should.equal(header + '\n' + changelog2 + changelog1)
203
+ })
204
+
205
+ it('[DEPRECATED] (--changelogHeader) allows for a custom changelog header', async function () {
206
+ const header = '# Pork Chop Log'
207
+ mock({
208
+ bump: 'minor',
209
+ changelog: header + '\n',
210
+ fs: { 'CHANGELOG.md': '' }
211
+ })
212
+ await exec(`--changelogHeader="${header}"`)
213
+ const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
214
+ content.should.match(new RegExp(header))
215
+ })
216
+
217
+ it('[DEPRECATED] (--changelogHeader) exits with error if changelog header matches last version search regex', async function () {
218
+ mock({ bump: 'minor', fs: { 'CHANGELOG.md': '' } })
219
+ try {
220
+ await exec('--changelogHeader="## 3.0.2"')
221
+ throw new Error('That should not have worked')
222
+ } catch (error) {
223
+ error.message.should.match(/custom changelog header must not match/)
224
+ }
225
+ })
226
+ })
227
+
228
+ describe('lifecycle scripts', () => {
229
+ describe('prerelease hook', function () {
230
+ it('should run the prerelease hook when provided', async function () {
231
+ const flush = mock({
232
+ bump: 'minor',
233
+ fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
234
+ })
235
+
236
+ await exec({
237
+ scripts: {
238
+ prerelease: "node -e \"console.error('prerelease' + ' ran')\""
239
+ }
240
+ })
241
+ const { stderr } = flush()
242
+ stderr.join('\n').should.match(/prerelease ran/)
243
+ })
244
+
245
+ it('should abort if the hook returns a non-zero exit code', async function () {
246
+ mock({
247
+ bump: 'minor',
248
+ fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
249
+ })
250
+
251
+ try {
252
+ await exec({
253
+ scripts: {
254
+ prerelease: "node -e \"throw new Error('prerelease' + ' fail')\""
255
+ }
256
+ })
257
+ /* istanbul ignore next */
258
+ throw new Error('Unexpected success')
259
+ } catch (error) {
260
+ error.message.should.match(/prerelease fail/)
261
+ }
262
+ })
263
+ })
264
+
265
+ describe('prebump hook', function () {
266
+ it('should allow prebump hook to return an alternate version #', async function () {
267
+ const flush = mock({
268
+ bump: 'minor',
269
+ fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
270
+ })
271
+
272
+ await exec({
273
+ scripts: {
274
+ prebump: "node -e \"console.log(Array.of(9, 9, 9).join('.'))\""
275
+ }
276
+ })
277
+ const { stdout } = flush()
278
+ stdout.join('').should.match(/9\.9\.9/)
279
+ })
280
+ })
281
+
282
+ describe('postbump hook', function () {
283
+ it('should run the postbump hook when provided', async function () {
284
+ const flush = mock({
285
+ bump: 'minor',
286
+ fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
287
+ })
288
+
289
+ await exec({
290
+ scripts: {
291
+ postbump: "node -e \"console.error('postbump' + ' ran')\""
292
+ }
293
+ })
294
+ const { stderr } = flush()
295
+ stderr.join('\n').should.match(/postbump ran/)
296
+ })
297
+
298
+ it('should run the postbump and exit with error when postbump fails', async function () {
299
+ mock({
300
+ bump: 'minor',
301
+ fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
302
+ })
303
+
304
+ try {
305
+ await exec({
306
+ scripts: {
307
+ postbump: "node -e \"throw new Error('postbump' + ' fail')\""
308
+ }
309
+ })
310
+ await exec('--patch')
311
+ /* istanbul ignore next */
312
+ throw new Error('Unexpected success')
313
+ } catch (error) {
314
+ error.message.should.match(/postbump fail/)
315
+ }
316
+ })
317
+ })
318
+ })
319
+
320
+ describe('manual-release', function () {
321
+ describe('release-types', function () {
322
+ const regularTypes = ['major', 'minor', 'patch']
323
+ const nextVersion = { major: '2.0.0', minor: '1.1.0', patch: '1.0.1' }
324
+
325
+ regularTypes.forEach(function (type) {
326
+ it('creates a ' + type + ' release', async function () {
327
+ mock({
328
+ bump: 'patch',
329
+ fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
330
+ })
331
+ await exec('--release-as ' + type)
332
+ getPackageVersion().should.equal(nextVersion[type])
333
+ })
334
+ })
335
+
336
+ // this is for pre-releases
337
+ regularTypes.forEach(function (type) {
338
+ it('creates a pre' + type + ' release', async function () {
339
+ mock({
340
+ bump: 'patch',
341
+ fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
342
+ })
343
+ await exec('--release-as ' + type + ' --prerelease ' + type)
344
+ getPackageVersion().should.equal(`${nextVersion[type]}-${type}.0`)
345
+ })
346
+ })
347
+ })
348
+
349
+ describe('release-as-exact', function () {
350
+ it('releases as v100.0.0', async function () {
351
+ mock({
352
+ bump: 'patch',
353
+ fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
354
+ })
355
+ await exec('--release-as v100.0.0')
356
+ getPackageVersion().should.equal('100.0.0')
357
+ })
358
+
359
+ it('releases as 200.0.0-amazing', async function () {
360
+ mock({
361
+ bump: 'patch',
362
+ fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
363
+ })
364
+ await exec('--release-as 200.0.0-amazing')
365
+ getPackageVersion().should.equal('200.0.0-amazing')
366
+ })
367
+ })
368
+
369
+ it('creates a prerelease with a new minor version after two prerelease patches', async function () {
370
+ let releaseType = 'patch'
371
+ const bump = (_, cb) => cb(null, { releaseType })
372
+ mock({
373
+ bump,
374
+ fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
375
+ })
376
+
377
+ await exec('--release-as patch --prerelease dev')
378
+ getPackageVersion().should.equal('1.0.1-dev.0')
379
+
380
+ await exec('--prerelease dev')
381
+ getPackageVersion().should.equal('1.0.1-dev.1')
382
+
383
+ releaseType = 'minor'
384
+ await exec('--release-as minor --prerelease dev')
385
+ getPackageVersion().should.equal('1.1.0-dev.0')
386
+
387
+ await exec('--release-as minor --prerelease dev')
388
+ getPackageVersion().should.equal('1.1.0-dev.1')
389
+
390
+ await exec('--prerelease dev')
391
+ getPackageVersion().should.equal('1.1.0-dev.2')
392
+ })
393
+ })
394
+
395
+ it('appends line feed at end of package.json', async function () {
396
+ mock({ bump: 'patch' })
397
+ await exec()
398
+ const pkgJson = fs.readFileSync('package.json', 'utf-8')
399
+ pkgJson.should.equal('{\n "version": "1.0.1"\n}\n')
400
+ })
401
+
402
+ it('preserves indentation of tabs in package.json', async function () {
403
+ mock({
404
+ bump: 'patch',
405
+ fs: { 'package.json': '{\n\t"version": "1.0.0"\n}\n' }
406
+ })
407
+ await exec()
408
+ const pkgJson = fs.readFileSync('package.json', 'utf-8')
409
+ pkgJson.should.equal('{\n\t"version": "1.0.1"\n}\n')
410
+ })
411
+
412
+ it('preserves indentation of spaces in package.json', async function () {
413
+ mock({
414
+ bump: 'patch',
415
+ fs: { 'package.json': '{\n "version": "1.0.0"\n}\n' }
416
+ })
417
+ await exec()
418
+ const pkgJson = fs.readFileSync('package.json', 'utf-8')
419
+ pkgJson.should.equal('{\n "version": "1.0.1"\n}\n')
420
+ })
421
+
422
+ it('preserves carriage return + line feed in package.json', async function () {
423
+ mock({
424
+ bump: 'patch',
425
+ fs: { 'package.json': '{\r\n "version": "1.0.0"\r\n}\r\n' }
426
+ })
427
+ await exec()
428
+ const pkgJson = fs.readFileSync('package.json', 'utf-8')
429
+ pkgJson.should.equal('{\r\n "version": "1.0.1"\r\n}\r\n')
430
+ })
431
+
432
+ it('does not print output when the --silent flag is passed', async function () {
433
+ const flush = mock()
434
+ await exec('--silent')
435
+ flush().should.eql({ stdout: [], stderr: [] })
436
+ })
437
+ })
438
+
439
+ describe('commit-and-tag-version', function () {
440
+ afterEach(unmock)
441
+
442
+ it('should exit on bump error', async function () {
443
+ mock({ bump: new Error('bump err') })
444
+ try {
445
+ await exec()
446
+ /* istanbul ignore next */
447
+ throw new Error('Unexpected success')
448
+ } catch (err) {
449
+ err.message.should.match(/bump err/)
450
+ }
451
+ })
452
+
453
+ it('should exit on changelog error', async function () {
454
+ mock({ bump: 'minor', changelog: new Error('changelog err') })
455
+ try {
456
+ await exec()
457
+ /* istanbul ignore next */
458
+ throw new Error('Unexpected success')
459
+ } catch (err) {
460
+ err.message.should.match(/changelog err/)
461
+ }
462
+ })
463
+
464
+ it('should exit with error without a package file to bump', async function () {
465
+ mock({ bump: 'patch', pkg: false })
466
+ try {
467
+ await exec({ gitTagFallback: false })
468
+ /* istanbul ignore next */
469
+ throw new Error('Unexpected success')
470
+ } catch (err) {
471
+ err.message.should.equal('no package file found')
472
+ }
473
+ })
474
+
475
+ it('bumps version # in bower.json', async function () {
476
+ mock({
477
+ bump: 'minor',
478
+ fs: { 'bower.json': JSON.stringify({ version: '1.0.0' }) },
479
+ tags: ['v1.0.0']
480
+ })
481
+ await exec()
482
+ JSON.parse(fs.readFileSync('bower.json', 'utf-8')).version.should.equal(
483
+ '1.1.0'
484
+ )
485
+ getPackageVersion().should.equal('1.1.0')
486
+ })
487
+
488
+ it('bumps version # in manifest.json', async function () {
489
+ mock({
490
+ bump: 'minor',
491
+ fs: { 'manifest.json': JSON.stringify({ version: '1.0.0' }) },
492
+ tags: ['v1.0.0']
493
+ })
494
+ await exec()
495
+ JSON.parse(fs.readFileSync('manifest.json', 'utf-8')).version.should.equal(
496
+ '1.1.0'
497
+ )
498
+ getPackageVersion().should.equal('1.1.0')
499
+ })
500
+
501
+ describe('custom `bumpFiles` support', function () {
502
+ it('mix.exs + version.txt', async function () {
503
+ const updater = 'custom-updater.js'
504
+ const updaterModule = require('./mocks/updater/customer-updater')
505
+ mock({
506
+ bump: 'minor',
507
+ fs: {
508
+ 'mix.exs': fs.readFileSync('./test/mocks/mix.exs'),
509
+ 'version.txt': fs.readFileSync('./test/mocks/version.txt')
510
+ },
511
+ tags: ['v1.0.0']
512
+ })
513
+ mockery.registerMock(resolve(process.cwd(), updater), updaterModule)
514
+
515
+ await exec({
516
+ bumpFiles: [
517
+ 'version.txt',
518
+ { filename: 'mix.exs', updater: 'custom-updater.js' }
519
+ ]
520
+ })
521
+ fs.readFileSync('mix.exs', 'utf-8').should.contain('version: "1.1.0"')
522
+ fs.readFileSync('version.txt', 'utf-8').should.equal('1.1.0')
523
+ })
524
+
525
+ it('bumps a custom `plain-text` file', async function () {
526
+ mock({
527
+ bump: 'minor',
528
+ fs: {
529
+ 'VERSION_TRACKER.txt': fs.readFileSync(
530
+ './test/mocks/VERSION-1.0.0.txt'
531
+ )
532
+ }
533
+ })
534
+ await exec({
535
+ bumpFiles: [{ filename: 'VERSION_TRACKER.txt', type: 'plain-text' }]
536
+ })
537
+ fs.readFileSync('VERSION_TRACKER.txt', 'utf-8').should.equal('1.1.0')
538
+ })
539
+ })
540
+
541
+ describe('custom `packageFiles` support', function () {
542
+ it('reads and writes to a custom `plain-text` file', async function () {
543
+ mock({
544
+ bump: 'minor',
545
+ fs: {
546
+ 'VERSION_TRACKER.txt': fs.readFileSync(
547
+ './test/mocks/VERSION-6.3.1.txt'
548
+ )
549
+ }
550
+ })
551
+ await exec({
552
+ packageFiles: [{ filename: 'VERSION_TRACKER.txt', type: 'plain-text' }],
553
+ bumpFiles: [{ filename: 'VERSION_TRACKER.txt', type: 'plain-text' }]
554
+ })
555
+ fs.readFileSync('VERSION_TRACKER.txt', 'utf-8').should.equal('6.4.0')
556
+ })
557
+
558
+ it('allows same object to be used in packageFiles and bumpFiles', async function () {
559
+ mock({
560
+ bump: 'minor',
561
+ fs: {
562
+ 'VERSION_TRACKER.txt': fs.readFileSync(
563
+ './test/mocks/VERSION-6.3.1.txt'
564
+ )
565
+ }
566
+ })
567
+ const origWarn = console.warn
568
+ console.warn = () => {
569
+ throw new Error('console.warn should not be called')
570
+ }
571
+ const filedesc = { filename: 'VERSION_TRACKER.txt', type: 'plain-text' }
572
+ try {
573
+ await exec({ packageFiles: [filedesc], bumpFiles: [filedesc] })
574
+ fs.readFileSync('VERSION_TRACKER.txt', 'utf-8').should.equal('6.4.0')
575
+ } finally {
576
+ console.warn = origWarn
577
+ }
578
+ })
579
+ })
580
+
581
+ it('`packageFiles` are bumped along with `bumpFiles` defaults [commit-and-tag-version#533]', async function () {
582
+ mock({
583
+ bump: 'minor',
584
+ fs: {
585
+ '.gitignore': '',
586
+ 'package-lock.json': JSON.stringify({ version: '1.0.0' }),
587
+ 'manifest.json': fs.readFileSync('./test/mocks/manifest-6.3.1.json')
588
+ },
589
+ tags: ['v1.0.0']
590
+ })
591
+
592
+ await exec({
593
+ silent: true,
594
+ packageFiles: [
595
+ {
596
+ filename: 'manifest.json',
597
+ type: 'json'
598
+ }
599
+ ]
600
+ })
601
+
602
+ JSON.parse(fs.readFileSync('manifest.json', 'utf-8')).version.should.equal(
603
+ '6.4.0'
604
+ )
605
+ JSON.parse(fs.readFileSync('package.json', 'utf-8')).version.should.equal(
606
+ '6.4.0'
607
+ )
608
+ JSON.parse(
609
+ fs.readFileSync('package-lock.json', 'utf-8')
610
+ ).version.should.equal('6.4.0')
611
+ })
612
+
613
+ it('bumps version # in npm-shrinkwrap.json', async function () {
614
+ mock({
615
+ bump: 'minor',
616
+ fs: {
617
+ 'npm-shrinkwrap.json': JSON.stringify({ version: '1.0.0' })
618
+ },
619
+ tags: ['v1.0.0']
620
+ })
621
+ await exec()
622
+ JSON.parse(
623
+ fs.readFileSync('npm-shrinkwrap.json', 'utf-8')
624
+ ).version.should.equal('1.1.0')
625
+ getPackageVersion().should.equal('1.1.0')
626
+ })
627
+
628
+ it('bumps version # in package-lock.json', async function () {
629
+ mock({
630
+ bump: 'minor',
631
+ fs: {
632
+ '.gitignore': '',
633
+ 'package-lock.json': JSON.stringify({ version: '1.0.0' })
634
+ },
635
+ tags: ['v1.0.0']
636
+ })
637
+ await exec()
638
+ JSON.parse(
639
+ fs.readFileSync('package-lock.json', 'utf-8')
640
+ ).version.should.equal('1.1.0')
641
+ getPackageVersion().should.equal('1.1.0')
642
+ })
643
+
644
+ describe('skip', () => {
645
+ it('allows bump and changelog generation to be skipped', async function () {
646
+ const changelogContent = 'legacy header format<a name="1.0.0">\n'
647
+ mock({
648
+ bump: 'minor',
649
+ changelog: 'foo\n',
650
+ fs: { 'CHANGELOG.md': changelogContent }
651
+ })
652
+
653
+ await exec('--skip.bump true --skip.changelog true')
654
+ getPackageVersion().should.equal('1.0.0')
655
+ const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
656
+ content.should.equal(changelogContent)
657
+ })
658
+ })
659
+
660
+ it('does not update files present in .gitignore', async () => {
661
+ mock({
662
+ bump: 'minor',
663
+ fs: {
664
+ '.gitignore': 'package-lock.json\nbower.json',
665
+ // test a defaults.packageFiles
666
+ 'bower.json': JSON.stringify({ version: '1.0.0' }),
667
+ // test a defaults.bumpFiles
668
+ 'package-lock.json': JSON.stringify({
669
+ name: '@org/package',
670
+ version: '1.0.0',
671
+ lockfileVersion: 1
672
+ })
673
+ },
674
+ tags: ['v1.0.0']
675
+ })
676
+ await exec()
677
+ JSON.parse(
678
+ fs.readFileSync('package-lock.json', 'utf-8')
679
+ ).version.should.equal('1.0.0')
680
+ JSON.parse(fs.readFileSync('bower.json', 'utf-8')).version.should.equal(
681
+ '1.0.0'
682
+ )
683
+ getPackageVersion().should.equal('1.1.0')
684
+ })
685
+
686
+ describe('configuration', () => {
687
+ it('--header', async function () {
688
+ mock({ bump: 'minor', fs: { 'CHANGELOG.md': '' } })
689
+ await exec('--header="# Welcome to our CHANGELOG.md"')
690
+ const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
691
+ content.should.match(/# Welcome to our CHANGELOG.md/)
692
+ })
693
+
694
+ it('--issuePrefixes and --issueUrlFormat', async function () {
695
+ const format = 'http://www.foo.com/{{prefix}}{{id}}'
696
+ const prefix = 'ABC-'
697
+ const changelog = ({ preset }) =>
698
+ preset.issueUrlFormat + ':' + preset.issuePrefixes
699
+ mock({ bump: 'minor', changelog })
700
+ await exec(`--issuePrefixes="${prefix}" --issueUrlFormat="${format}"`)
701
+ const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
702
+ content.should.include(`${format}:${prefix}`)
703
+ })
704
+ })
705
+
706
+ describe('pre-major', () => {
707
+ it('bumps the minor rather than major, if version < 1.0.0', async function () {
708
+ mock({
709
+ bump: 'minor',
710
+ pkg: {
711
+ version: '0.5.0',
712
+ repository: { url: 'https://github.com/yargs/yargs.git' }
713
+ }
714
+ })
715
+ await exec()
716
+ getPackageVersion().should.equal('0.6.0')
717
+ })
718
+
719
+ it('bumps major if --release-as=major specified, if version < 1.0.0', async function () {
720
+ mock({
721
+ bump: 'major',
722
+ pkg: {
723
+ version: '0.5.0',
724
+ repository: { url: 'https://github.com/yargs/yargs.git' }
725
+ }
726
+ })
727
+ await exec('-r major')
728
+ getPackageVersion().should.equal('1.0.0')
729
+ })
730
+ })
731
+ })
732
+
733
+ describe('GHSL-2020-111', function () {
734
+ afterEach(unmock)
735
+
736
+ it('does not allow command injection via basic configuration', async function () {
737
+ mock({ bump: 'patch' })
738
+ await exec({
739
+ noVerify: true,
740
+ infile: 'foo.txt',
741
+ releaseCommitMessageFormat: 'bla `touch exploit`'
742
+ })
743
+ const stat = shell.test('-f', './exploit')
744
+ stat.should.equal(false)
745
+ })
746
+ })
747
+
748
+ describe('with mocked git', function () {
749
+ afterEach(unmock)
750
+
751
+ it('--sign signs the commit and tag', async function () {
752
+ const gitArgs = [
753
+ ['add', 'CHANGELOG.md', 'package.json'],
754
+ [
755
+ 'commit',
756
+ '-S',
757
+ 'CHANGELOG.md',
758
+ 'package.json',
759
+ '-m',
760
+ 'chore(release): 1.0.1'
761
+ ],
762
+ ['tag', '-s', 'v1.0.1', '-m', 'chore(release): 1.0.1'],
763
+ ['rev-parse', '--abbrev-ref', 'HEAD']
764
+ ]
765
+ const execFile = (_args, cmd, cmdArgs) => {
766
+ cmd.should.equal('git')
767
+ const expected = gitArgs.shift()
768
+ cmdArgs.should.deep.equal(expected)
769
+ if (expected[0] === 'rev-parse') return Promise.resolve('master')
770
+ return Promise.resolve('')
771
+ }
772
+ mock({ bump: 'patch', changelog: 'foo\n', execFile })
773
+
774
+ await exec('--sign', true)
775
+ gitArgs.should.have.lengthOf(0)
776
+ })
777
+
778
+ it('fails if git add fails', async function () {
779
+ const gitArgs = [['add', 'CHANGELOG.md', 'package.json']]
780
+ const execFile = (_args, cmd, cmdArgs) => {
781
+ cmd.should.equal('git')
782
+ const expected = gitArgs.shift()
783
+ cmdArgs.should.deep.equal(expected)
784
+ if (expected[0] === 'add') {
785
+ return Promise.reject(new Error('Command failed: git\nfailed add'))
786
+ }
787
+ return Promise.resolve('')
788
+ }
789
+ mock({ bump: 'patch', changelog: 'foo\n', execFile })
790
+
791
+ try {
792
+ await exec({}, true)
793
+ /* istanbul ignore next */
794
+ throw new Error('Unexpected success')
795
+ } catch (error) {
796
+ error.message.should.match(/failed add/)
797
+ }
798
+ })
799
+
800
+ it('fails if git commit fails', async function () {
801
+ const gitArgs = [
802
+ ['add', 'CHANGELOG.md', 'package.json'],
803
+ ['commit', 'CHANGELOG.md', 'package.json', '-m', 'chore(release): 1.0.1']
804
+ ]
805
+ const execFile = (_args, cmd, cmdArgs) => {
806
+ cmd.should.equal('git')
807
+ const expected = gitArgs.shift()
808
+ cmdArgs.should.deep.equal(expected)
809
+ if (expected[0] === 'commit') {
810
+ return Promise.reject(new Error('Command failed: git\nfailed commit'))
811
+ }
812
+ return Promise.resolve('')
813
+ }
814
+ mock({ bump: 'patch', changelog: 'foo\n', execFile })
815
+
816
+ try {
817
+ await exec({}, true)
818
+ /* istanbul ignore next */
819
+ throw new Error('Unexpected success')
820
+ } catch (error) {
821
+ error.message.should.match(/failed commit/)
822
+ }
823
+ })
824
+
825
+ it('fails if git tag fails', async function () {
826
+ const gitArgs = [
827
+ ['add', 'CHANGELOG.md', 'package.json'],
828
+ ['commit', 'CHANGELOG.md', 'package.json', '-m', 'chore(release): 1.0.1'],
829
+ ['tag', '-a', 'v1.0.1', '-m', 'chore(release): 1.0.1']
830
+ ]
831
+ const execFile = (_args, cmd, cmdArgs) => {
832
+ cmd.should.equal('git')
833
+ const expected = gitArgs.shift()
834
+ cmdArgs.should.deep.equal(expected)
835
+ if (expected[0] === 'tag') {
836
+ return Promise.reject(new Error('Command failed: git\nfailed tag'))
837
+ }
838
+ return Promise.resolve('')
839
+ }
840
+ mock({ bump: 'patch', changelog: 'foo\n', execFile })
841
+
842
+ try {
843
+ await exec({}, true)
844
+ /* istanbul ignore next */
845
+ throw new Error('Unexpected success')
846
+ } catch (error) {
847
+ error.message.should.match(/failed tag/)
848
+ }
849
+ })
850
+ })