commit-and-tag-version 12.0.0 → 12.2.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 (54) hide show
  1. package/.eslintrc +43 -8
  2. package/.gitattributes +2 -0
  3. package/.github/workflows/ci.yaml +10 -3
  4. package/.prettierrc +3 -0
  5. package/CHANGELOG.md +14 -0
  6. package/README.md +17 -4
  7. package/bin/cli.js +6 -6
  8. package/command.js +43 -37
  9. package/defaults.js +12 -11
  10. package/index.js +44 -42
  11. package/jest.config.js +28 -0
  12. package/lib/checkpoint.js +19 -8
  13. package/lib/configuration.js +17 -17
  14. package/lib/detect-package-manager.js +20 -20
  15. package/lib/format-commit-message.js +3 -3
  16. package/lib/latest-semver-tag.js +13 -11
  17. package/lib/lifecycles/bump.js +136 -105
  18. package/lib/lifecycles/changelog.js +70 -51
  19. package/lib/lifecycles/commit.js +41 -42
  20. package/lib/lifecycles/tag.js +41 -31
  21. package/lib/preset-loader.js +12 -10
  22. package/lib/print-error.js +10 -7
  23. package/lib/run-exec.js +10 -10
  24. package/lib/run-execFile.js +10 -10
  25. package/lib/run-lifecycle-script.js +16 -11
  26. package/lib/stringify-package.js +10 -10
  27. package/lib/updaters/index.js +44 -33
  28. package/lib/updaters/types/csproj.js +9 -7
  29. package/lib/updaters/types/gradle.js +10 -8
  30. package/lib/updaters/types/json.js +14 -14
  31. package/lib/updaters/types/maven.js +44 -0
  32. package/lib/updaters/types/plain-text.js +4 -4
  33. package/lib/write-file.js +4 -4
  34. package/package.json +16 -21
  35. package/test/commit-message-config.integration-test.js +106 -0
  36. package/test/config-files.integration-test.js +114 -0
  37. package/test/config-keys.integration-test.js +83 -0
  38. package/test/core.spec.js +1438 -705
  39. package/test/git.integration-test.js +363 -0
  40. package/test/invalid-config.integration-test.js +73 -0
  41. package/test/mocks/jest-mocks.js +48 -0
  42. package/test/mocks/pom-6.3.1-crlf.xml +30 -0
  43. package/test/mocks/pom-6.3.1-lf.xml +30 -0
  44. package/test/mocks/pom-6.4.0-crlf.xml +30 -0
  45. package/test/mocks/pom-6.4.0-lf.xml +30 -0
  46. package/test/mocks/updater/customer-updater.js +5 -8
  47. package/test/mocks/updater/increment-updater.js +5 -5
  48. package/test/pre-commit-hook.integration-test.js +192 -0
  49. package/test/preset.integration-test.js +56 -0
  50. package/test/stringify-package.spec.js +18 -22
  51. package/test/utils.spec.js +25 -41
  52. package/test/config-files.spec.js +0 -176
  53. package/test/git.spec.js +0 -480
  54. package/test/preset.spec.js +0 -52
@@ -0,0 +1,192 @@
1
+ 'use strict';
2
+
3
+ const shell = require('shelljs');
4
+ const fs = require('fs');
5
+
6
+ const mockers = require('./mocks/jest-mocks');
7
+
8
+ // Jest swallows most standard console logs not explicitly defined into a custom logger
9
+ // see: https://stackoverflow.com/questions/51555568/remove-logging-the-origin-line-in-jest
10
+ const consoleWarnSpy = jest.spyOn(global.console, 'warn').mockImplementation();
11
+
12
+ const consoleInfoSpy = jest.spyOn(global.console, 'info').mockImplementation();
13
+
14
+ const consoleErrorSpy = jest
15
+ .spyOn(global.console, 'error')
16
+ .mockImplementation();
17
+
18
+ function exec(opt = '') {
19
+ if (typeof opt === 'string') {
20
+ const cli = require('../command');
21
+ opt = cli.parse(`commit-and-tag-version ${opt}`);
22
+ }
23
+ return require('../index')(opt);
24
+ }
25
+
26
+ function writePackageJson(version, option) {
27
+ const pkg = Object.assign({}, option, { version });
28
+ fs.writeFileSync('package.json', JSON.stringify(pkg), 'utf-8');
29
+ }
30
+
31
+ function writeHook(hookName, causeError, script) {
32
+ shell.mkdir('-p', 'scripts');
33
+ let content = script || 'console.error("' + hookName + ' ran")';
34
+ content += causeError ? '\nthrow new Error("' + hookName + '-failure")' : '';
35
+ fs.writeFileSync('scripts/' + hookName + '.js', content, 'utf-8');
36
+ fs.chmodSync('scripts/' + hookName + '.js', '755');
37
+ }
38
+
39
+ function setupTempGitRepo() {
40
+ shell.rm('-rf', 'pre-commit-hook-temp');
41
+ shell.config.silent = true;
42
+ shell.mkdir('pre-commit-hook-temp');
43
+ shell.cd('pre-commit-hook-temp');
44
+ shell.exec('git init');
45
+ shell.exec('git config commit.gpgSign false');
46
+ shell.exec('git config core.autocrlf false');
47
+ shell.exec('git commit --allow-empty -m"root-commit"');
48
+ }
49
+
50
+ function setup() {
51
+ setupTempGitRepo();
52
+ writePackageJson('1.0.0');
53
+ }
54
+
55
+ function clearCapturedSpyCalls() {
56
+ consoleInfoSpy.mockClear();
57
+ consoleWarnSpy.mockClear();
58
+ consoleErrorSpy.mockClear();
59
+ }
60
+
61
+ function resetShell() {
62
+ shell.cd('../');
63
+ shell.rm('-rf', 'pre-commit-hook-temp');
64
+ }
65
+
66
+ function reset() {
67
+ resetShell();
68
+
69
+ clearCapturedSpyCalls();
70
+ }
71
+
72
+ /**
73
+ * Mock external conventional-changelog modules
74
+ *
75
+ * bump: 'major' | 'minor' | 'patch' | Error | (opt, parserOpts, cb) => { cb(err) | cb(null, { releaseType }) }
76
+ * changelog?: string | Error | Array<string | Error | (opt) => string | null>
77
+ * tags?: string[] | Error
78
+ */
79
+ function mock({ bump, changelog, tags }) {
80
+ if (bump === undefined) throw new Error('bump must be defined for mock()');
81
+
82
+ mockers.mockRecommendedBump({ bump });
83
+
84
+ if (!Array.isArray(changelog)) changelog = [changelog];
85
+ mockers.mockConventionalChangelog({ changelog });
86
+
87
+ mockers.mockGitSemverTags({ tags });
88
+ }
89
+
90
+ function getLog(expectedLog, spy = consoleInfoSpy) {
91
+ const consoleInfoLogs = spy.mock.calls.map((args) => args[0]);
92
+ return consoleInfoLogs.find((log) => log.includes(expectedLog));
93
+ }
94
+
95
+ function verifyLogPrinted(expectedLog, spy = consoleInfoSpy) {
96
+ const logType = spy === consoleInfoSpy ? 'info' : 'warn';
97
+ const desiredLog = getLog(expectedLog, spy);
98
+ if (desiredLog) {
99
+ expect(desiredLog).toMatch(expectedLog);
100
+ } else {
101
+ expect(`no ${logType} Log printed matching`).toMatch(expectedLog);
102
+ }
103
+ }
104
+
105
+ describe('precommit hook', function () {
106
+ beforeEach(function () {
107
+ setup();
108
+ });
109
+
110
+ afterEach(function () {
111
+ reset();
112
+ });
113
+
114
+ it('should run the precommit hook when provided via .versionrc.json (#371)', async function () {
115
+ fs.writeFileSync(
116
+ '.versionrc.json',
117
+ JSON.stringify({
118
+ scripts: { precommit: 'node scripts/precommit' },
119
+ }),
120
+ 'utf-8',
121
+ );
122
+
123
+ writeHook('precommit');
124
+ fs.writeFileSync(
125
+ 'CHANGELOG.md',
126
+ 'legacy header format<a name="1.0.0">\n',
127
+ 'utf-8',
128
+ );
129
+ mock({ bump: 'minor' });
130
+ await exec('');
131
+ verifyLogPrinted('precommit ran', consoleWarnSpy);
132
+ });
133
+
134
+ it('should run the precommit hook when provided', async function () {
135
+ writePackageJson('1.0.0', {
136
+ 'commit-and-tag-version': {
137
+ scripts: { precommit: 'node scripts/precommit' },
138
+ },
139
+ });
140
+ writeHook('precommit');
141
+ fs.writeFileSync(
142
+ 'CHANGELOG.md',
143
+ 'legacy header format<a name="1.0.0">\n',
144
+ 'utf-8',
145
+ );
146
+
147
+ mock({ bump: 'minor' });
148
+ await exec('--patch');
149
+ verifyLogPrinted('precommit ran', consoleWarnSpy);
150
+ });
151
+
152
+ it('should run the precommit hook and throw error when precommit fails', async function () {
153
+ writePackageJson('1.0.0', {
154
+ 'commit-and-tag-version': {
155
+ scripts: { precommit: 'node scripts/precommit' },
156
+ },
157
+ });
158
+ writeHook('precommit', true);
159
+ fs.writeFileSync(
160
+ 'CHANGELOG.md',
161
+ 'legacy header format<a name="1.0.0">\n',
162
+ 'utf-8',
163
+ );
164
+
165
+ mock({ bump: 'minor' });
166
+ let errorMessage = '';
167
+ try {
168
+ await exec('--patch');
169
+ } catch (e) {
170
+ errorMessage = e.message;
171
+ }
172
+ expect(errorMessage).toMatch('precommit-failure');
173
+ });
174
+
175
+ it('should allow an alternate commit message to be provided by precommit script', async function () {
176
+ writePackageJson('1.0.0', {
177
+ 'commit-and-tag-version': {
178
+ scripts: { precommit: 'node scripts/precommit' },
179
+ },
180
+ });
181
+ writeHook('precommit', false, 'console.log("releasing %s delivers #222")');
182
+ fs.writeFileSync(
183
+ 'CHANGELOG.md',
184
+ 'legacy header format<a name="1.0.0">\n',
185
+ 'utf-8',
186
+ );
187
+
188
+ mock({ bump: 'minor' });
189
+ await exec('--patch');
190
+ expect(shell.exec('git log --oneline -n1').stdout).toMatch(/delivers #222/);
191
+ });
192
+ });
@@ -0,0 +1,56 @@
1
+ const shell = require('shelljs');
2
+ const fs = require('fs');
3
+
4
+ function exec(opt) {
5
+ const cli = require('../command');
6
+ opt = cli.parse(`commit-and-tag-version ${opt} --silent`);
7
+ opt.skip = { commit: true, tag: true };
8
+ return require('../index')(opt);
9
+ }
10
+
11
+ function setupTempGitRepo() {
12
+ shell.rm('-rf', 'preset-temp');
13
+ shell.config.silent = true;
14
+ shell.mkdir('preset-temp');
15
+ shell.cd('preset-temp');
16
+ shell.exec('git init');
17
+ shell.exec('git config commit.gpgSign false');
18
+ shell.exec('git config core.autocrlf false');
19
+ shell.exec('git commit --allow-empty -m "initial commit"');
20
+ shell.exec('git commit --allow-empty -m "feat: A feature commit."');
21
+ shell.exec('git commit --allow-empty -m "perf: A performance change."');
22
+ shell.exec('git commit --allow-empty -m "chore: A chore commit."');
23
+ shell.exec('git commit --allow-empty -m "ci: A ci commit."');
24
+ shell.exec('git commit --allow-empty -m "custom: A custom commit."');
25
+ }
26
+
27
+ function resetShell() {
28
+ shell.cd('../');
29
+ shell.rm('-rf', 'preset-temp');
30
+ }
31
+
32
+ describe('presets', function () {
33
+ beforeEach(function () {
34
+ setupTempGitRepo();
35
+ });
36
+
37
+ afterEach(function () {
38
+ resetShell();
39
+ });
40
+
41
+ it('Conventional Commits (default)', async function () {
42
+ await exec();
43
+ const content = fs.readFileSync('CHANGELOG.md', 'utf-8');
44
+ expect(content).toContain('### Features');
45
+ expect(content).not.toContain('### Performance Improvements');
46
+ expect(content).not.toContain('### Custom');
47
+ });
48
+
49
+ it('Angular', async function () {
50
+ await exec('--preset angular');
51
+ const content = fs.readFileSync('CHANGELOG.md', 'utf-8');
52
+ expect(content).toContain('### Features');
53
+ expect(content).toContain('### Performance Improvements');
54
+ expect(content).not.toContain('### Custom');
55
+ });
56
+ });
@@ -1,39 +1,35 @@
1
- /* global describe it */
1
+ 'use strict';
2
2
 
3
- 'use strict'
4
-
5
- const stringifyPackage = require('../lib/stringify-package')
6
-
7
- require('chai').should()
3
+ const stringifyPackage = require('../lib/stringify-package');
8
4
 
9
5
  describe('stringifyPackage()', function () {
10
- const dummy = { name: 'dummy' }
6
+ const dummy = { name: 'dummy' };
11
7
 
12
8
  it('with no params uses \\n', function () {
13
- stringifyPackage(dummy).should.match(/\n$/m)
14
- })
9
+ expect(stringifyPackage(dummy)).toMatch(/\n$/m);
10
+ });
15
11
 
16
12
  it('uses \\n', function () {
17
- stringifyPackage(dummy, 2, '\n').should.match(/\n$/m)
18
- })
13
+ expect(stringifyPackage(dummy, 2, '\n')).toMatch(/\n$/m);
14
+ });
19
15
 
20
16
  it('uses \\r\\n', function () {
21
- stringifyPackage(dummy, 2, '\r\n').should.match(/\r\n$/m)
22
- })
17
+ expect(stringifyPackage(dummy, 2, '\r\n')).toMatch(/\r\n$/m);
18
+ });
23
19
 
24
20
  it('with no params uses 2-space indent', function () {
25
- stringifyPackage(dummy).should.match(/^ {2}"name": "dummy"/m)
26
- })
21
+ expect(stringifyPackage(dummy)).toMatch(/^ {2}"name": "dummy"/m);
22
+ });
27
23
 
28
24
  it('uses 2-space indent', function () {
29
- stringifyPackage(dummy, 2, '\n').should.match(/^ {2}"name": "dummy"/m)
30
- })
25
+ expect(stringifyPackage(dummy, 2, '\n')).toMatch(/^ {2}"name": "dummy"/m);
26
+ });
31
27
 
32
28
  it('uses 4-space indent', function () {
33
- stringifyPackage(dummy, 4, '\n').should.match(/^ {4}"name": "dummy"/m)
34
- })
29
+ expect(stringifyPackage(dummy, 4, '\n')).toMatch(/^ {4}"name": "dummy"/m);
30
+ });
35
31
 
36
32
  it('0 works', function () {
37
- stringifyPackage(dummy, 0).split(/\r\n|\r|\n/).length.should.equal(2)
38
- })
39
- })
33
+ expect(stringifyPackage(dummy, 0).split(/\r\n|\r|\n/).length).toEqual(2);
34
+ });
35
+ });
@@ -1,52 +1,36 @@
1
- /* global describe it */
1
+ const { promises: fsp } = require('fs');
2
2
 
3
- const mockery = require('mockery')
4
- const { promises: fsp } = require('fs')
5
- require('chai').should()
3
+ let mockFs;
6
4
 
7
- function mockNpm () {
8
- mockery.enable({ warnOnUnregistered: false, useCleanCache: true })
9
- let lockFile = ''
10
-
11
- const fsMock = {
12
- promises: {
13
- access: async function (path) {
14
- if (lockFile && path.endsWith(lockFile)) {
15
- return true
16
- }
17
- await fsp.access(path)
18
- }
19
- }
5
+ const setLockFile = (lockFile) => {
6
+ if (mockFs) {
7
+ mockFs.mockRestore();
20
8
  }
21
- mockery.registerMock('fs', fsMock)
22
- return {
23
- setLockFile (file) {
24
- lockFile = file
9
+ mockFs = jest.spyOn(fsp, 'access').mockImplementation(async (path) => {
10
+ if (lockFile && path.endsWith(lockFile)) {
11
+ return Promise.resolve();
25
12
  }
26
- }
27
- }
13
+ return Promise.reject(new Error('Invalid lockfile'));
14
+ });
15
+ };
28
16
 
29
17
  describe('utils', function () {
30
18
  it('detectPMByLockFile should work', async function () {
31
- const { setLockFile } = mockNpm()
32
- const { detectPMByLockFile } = require('../lib/detect-package-manager')
33
-
34
- let pm = await detectPMByLockFile()
35
- pm.should.equal('npm')
19
+ const { detectPMByLockFile } = require('../lib/detect-package-manager');
36
20
 
37
- setLockFile('yarn.lock')
38
- pm = await detectPMByLockFile()
39
- pm.should.equal('yarn')
21
+ let pm = await detectPMByLockFile();
22
+ expect(pm).toEqual('npm');
40
23
 
41
- setLockFile('package-lock.json')
42
- pm = await detectPMByLockFile()
43
- pm.should.equal('npm')
24
+ setLockFile('yarn.lock');
25
+ pm = await detectPMByLockFile();
26
+ expect(pm).toEqual('yarn');
44
27
 
45
- setLockFile('pnpm-lock.yaml')
46
- pm = await detectPMByLockFile()
47
- pm.should.equal('pnpm')
28
+ setLockFile('package-lock.json');
29
+ pm = await detectPMByLockFile();
30
+ expect(pm).toEqual('npm');
48
31
 
49
- mockery.deregisterAll()
50
- mockery.disable()
51
- })
52
- })
32
+ setLockFile('pnpm-lock.yaml');
33
+ pm = await detectPMByLockFile();
34
+ expect(pm).toEqual('pnpm');
35
+ });
36
+ });
@@ -1,176 +0,0 @@
1
- /* global describe it beforeEach afterEach */
2
-
3
- 'use strict'
4
-
5
- const shell = require('shelljs')
6
- const fs = require('fs')
7
- const { Readable } = require('stream')
8
- const mockery = require('mockery')
9
- const stdMocks = require('std-mocks')
10
-
11
- const chai = require('chai')
12
- const expect = chai.expect
13
- chai.use(require('chai-as-promised'))
14
-
15
- function exec () {
16
- const cli = require('../command')
17
- const opt = cli.parse('commit-and-tag-version')
18
- opt.skip = { commit: true, tag: true }
19
- return require('../index')(opt)
20
- }
21
-
22
- /**
23
- * Mock external conventional-changelog modules
24
- *
25
- * Mocks should be unregistered in test cleanup by calling unmock()
26
- *
27
- * bump?: 'major' | 'minor' | 'patch' | Error | (opt, parserOpts, cb) => { cb(err) | cb(null, { releaseType }) }
28
- * changelog?: string | Error | Array<string | Error | (opt) => string | null>
29
- * tags?: string[] | Error
30
- */
31
- function mock ({ bump, changelog, tags } = {}) {
32
- mockery.enable({ warnOnUnregistered: false, useCleanCache: true })
33
-
34
- mockery.registerMock('conventional-recommended-bump', function (opt, parserOpts, cb) {
35
- if (typeof bump === 'function') bump(opt, parserOpts, cb)
36
- else if (bump instanceof Error) cb(bump)
37
- else cb(null, bump ? { releaseType: bump } : {})
38
- })
39
-
40
- if (!Array.isArray(changelog)) changelog = [changelog]
41
- mockery.registerMock(
42
- 'conventional-changelog',
43
- (opt) =>
44
- new Readable({
45
- read (_size) {
46
- const next = changelog.shift()
47
- if (next instanceof Error) {
48
- this.destroy(next)
49
- } else if (typeof next === 'function') {
50
- this.push(next(opt))
51
- } else {
52
- this.push(next ? Buffer.from(next, 'utf8') : null)
53
- }
54
- }
55
- })
56
- )
57
-
58
- mockery.registerMock('git-semver-tags', function (cb) {
59
- if (tags instanceof Error) cb(tags)
60
- else cb(null, tags | [])
61
- })
62
-
63
- stdMocks.use()
64
- return () => stdMocks.flush()
65
- }
66
-
67
- describe('config files', function () {
68
- beforeEach(function () {
69
- shell.rm('-rf', 'tmp')
70
- shell.config.silent = true
71
- shell.mkdir('tmp')
72
- shell.cd('tmp')
73
- fs.writeFileSync(
74
- 'package.json',
75
- JSON.stringify({ version: '1.0.0' }),
76
- 'utf-8'
77
- )
78
- })
79
-
80
- afterEach(function () {
81
- shell.cd('../')
82
- shell.rm('-rf', 'tmp')
83
-
84
- mockery.deregisterAll()
85
- mockery.disable()
86
- stdMocks.restore()
87
-
88
- // push out prints from the Mocha reporter
89
- const { stdout } = stdMocks.flush()
90
- for (const str of stdout) {
91
- if (str.startsWith(' ')) process.stdout.write(str)
92
- }
93
- })
94
-
95
- const configKeys = ['commit-and-tag-version', 'standard-version']
96
-
97
- configKeys.forEach((configKey) => {
98
- it(`reads config from package.json key '${configKey}'`, async function () {
99
- const issueUrlFormat =
100
- 'https://commit-and-tag-version.company.net/browse/{{id}}'
101
- mock({
102
- bump: 'minor',
103
- changelog: ({ preset }) => preset.issueUrlFormat
104
- })
105
- const pkg = {
106
- version: '1.0.0',
107
- repository: { url: 'git+https://company@scm.org/office/app.git' },
108
- [configKey]: { issueUrlFormat }
109
- }
110
- fs.writeFileSync('package.json', JSON.stringify(pkg), 'utf-8')
111
-
112
- await exec()
113
- const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
114
- content.should.include(issueUrlFormat)
115
- })
116
- })
117
-
118
- it('reads config from .versionrc', async function () {
119
- const issueUrlFormat = 'http://www.foo.com/{{id}}'
120
- const changelog = ({ preset }) => preset.issueUrlFormat
121
- mock({ bump: 'minor', changelog })
122
- fs.writeFileSync('.versionrc', JSON.stringify({ issueUrlFormat }), 'utf-8')
123
-
124
- await exec()
125
- const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
126
- content.should.include(issueUrlFormat)
127
- })
128
-
129
- it('reads config from .versionrc.json', async function () {
130
- const issueUrlFormat = 'http://www.foo.com/{{id}}'
131
- const changelog = ({ preset }) => preset.issueUrlFormat
132
- mock({ bump: 'minor', changelog })
133
- fs.writeFileSync(
134
- '.versionrc.json',
135
- JSON.stringify({ issueUrlFormat }),
136
- 'utf-8'
137
- )
138
-
139
- await exec()
140
- const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
141
- content.should.include(issueUrlFormat)
142
- })
143
-
144
- it('evaluates a config-function from .versionrc.js', async function () {
145
- const issueUrlFormat = 'http://www.foo.com/{{id}}'
146
- const src = `module.exports = function() { return ${JSON.stringify({
147
- issueUrlFormat
148
- })} }`
149
- const changelog = ({ preset }) => preset.issueUrlFormat
150
- mock({ bump: 'minor', changelog })
151
- fs.writeFileSync('.versionrc.js', src, 'utf-8')
152
-
153
- await exec()
154
- const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
155
- content.should.include(issueUrlFormat)
156
- })
157
-
158
- it('evaluates a config-object from .versionrc.js', async function () {
159
- const issueUrlFormat = 'http://www.foo.com/{{id}}'
160
- const src = `module.exports = ${JSON.stringify({ issueUrlFormat })}`
161
- const changelog = ({ preset }) => preset.issueUrlFormat
162
- mock({ bump: 'minor', changelog })
163
- fs.writeFileSync('.versionrc.js', src, 'utf-8')
164
-
165
- await exec()
166
- const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
167
- content.should.include(issueUrlFormat)
168
- })
169
-
170
- it('throws an error when a non-object is returned from .versionrc.js', async function () {
171
- mock({ bump: 'minor' })
172
- fs.writeFileSync('.versionrc.js', 'module.exports = 3', 'utf-8')
173
-
174
- expect(exec).to.throw(/Invalid configuration/)
175
- })
176
- })