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
package/test/core.spec.js CHANGED
@@ -1,1086 +1,1819 @@
1
- /* global describe it afterEach */
1
+ 'use strict';
2
2
 
3
- 'use strict'
3
+ const shell = require('shelljs');
4
+ const stripAnsi = require('strip-ansi');
5
+ const fs = require('fs');
4
6
 
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
- const stripAnsi = require('strip-ansi')
7
+ const mockers = require('./mocks/jest-mocks');
13
8
 
14
- const cli = require('../command')
15
- const formatCommitMessage = require('../lib/format-commit-message')
9
+ const runExecFile = require('../lib/run-execFile');
16
10
 
17
- const chai = require('chai')
18
- const should = chai.should()
19
- const expect = chai.expect
20
- chai.use(require('chai-as-promised'))
11
+ const cli = require('../command');
12
+ const formatCommitMessage = require('../lib/format-commit-message');
21
13
 
22
14
  // set by mock()
23
- let standardVersion
15
+ let standardVersion;
16
+ let readFileSyncSpy;
17
+ let lstatSyncSpy;
24
18
 
25
- function exec (opt = '', git) {
19
+ // Rather than trying to re-read something written out during tests, we can spy on writeFileSync
20
+ // we can trust fs is capable of writing the file
21
+ let writeFileSyncSpy;
22
+
23
+ const consoleErrorSpy = jest.spyOn(console, 'warn').mockImplementation();
24
+ const consoleInfoSpy = jest.spyOn(console, 'info').mockImplementation();
25
+
26
+ jest.mock('../lib/run-execFile');
27
+
28
+ const { readFileSync: readFileSyncActual, lstatSync: lstatSyncActual } = fs;
29
+
30
+ function exec(opt = '', git) {
26
31
  if (typeof opt === 'string') {
27
- opt = cli.parse(`commit-and-tag-version ${opt}`)
32
+ opt = cli.parse(`commit-and-tag-version ${opt}`);
28
33
  }
29
- if (!git) opt.skip = Object.assign({}, opt.skip, { commit: true, tag: true })
30
- return standardVersion(opt)
34
+ if (!git) opt.skip = Object.assign({}, opt.skip, { commit: true, tag: true });
35
+ return standardVersion(opt);
31
36
  }
32
37
 
33
- function getPackageVersion () {
34
- return JSON.parse(fs.readFileSync('package.json', 'utf-8')).version
38
+ function attemptingToReadPackageJson(path) {
39
+ return path.includes('package.json') || path.includes('package-lock.json');
35
40
  }
36
41
 
42
+ /**
43
+ * @param fs - reference to 'fs' - needs to be defined in the root test class so that mocking works correctly
44
+ * @param readFileSyncActual - actual implementation of fs.readFileSync - reference should be defined as a variable in root test class so we can unset spy after
45
+ * @param existingChangelog ?: string - Existing CHANGELOG.md content
46
+ * @param testFiles ?: object[] - with Path and Value fields, for mocking readFileSynch on packageFiles such as package.json, bower.json, manifest.json
47
+ * @param realTestFiles ?: object[] - with Filename (e.g. mix.exs) and Path to real file in a directory
48
+ * @return Jest spy on readFileSync
49
+ */
50
+ const mockReadFilesFromDisk = ({
51
+ fs,
52
+ readFileSyncActual,
53
+ existingChangelog,
54
+ testFiles,
55
+ realTestFiles,
56
+ }) =>
57
+ jest.spyOn(fs, 'readFileSync').mockImplementation((path, opts) => {
58
+ if (path === 'CHANGELOG.md') {
59
+ if (existingChangelog) {
60
+ return existingChangelog;
61
+ }
62
+ return '';
63
+ }
64
+
65
+ // If deliberately set to null when mocking, don't create a fake package.json
66
+ if (testFiles === null && attemptingToReadPackageJson(path)) {
67
+ return '{}';
68
+ }
69
+
70
+ if (testFiles) {
71
+ const file = testFiles.find((otherFile) => {
72
+ return path.includes(otherFile.path);
73
+ });
74
+
75
+ if (file) {
76
+ if (file.value instanceof String || typeof file.value === 'string') {
77
+ return file.value;
78
+ }
79
+ return JSON.stringify(file.value);
80
+ }
81
+
82
+ // For scenarios where we have defined testFiles such as bower.json
83
+ // Do not create a fake package.json file
84
+ if (attemptingToReadPackageJson(path)) {
85
+ return '{}';
86
+ }
87
+ }
88
+
89
+ // If no package files defined and not explicitly set to null, create a fake package json
90
+ // otherwise fs will read the real package.json in the root of this project!
91
+ if (attemptingToReadPackageJson(path)) {
92
+ return JSON.stringify({ version: '1.0.0' });
93
+ }
94
+
95
+ if (realTestFiles) {
96
+ const testFile = realTestFiles.find((testFile) => {
97
+ return path.includes(testFile.filename);
98
+ });
99
+
100
+ if (testFile) {
101
+ return readFileSyncActual(testFile.path, opts);
102
+ }
103
+ }
104
+
105
+ return readFileSyncActual(path, opts);
106
+ });
107
+
108
+ /**
109
+ * @param fs - reference to 'fs' - needs to be defined in the root test class so that mocking works correctly
110
+ * @param lstatSyncActual - actual implementation of fs.lstatSync
111
+ * @param testFiles ?: object[] - with Path and Value fields, for mocking lstatSync on packageFiles such as package.json, bower.json, manifest.json
112
+ * @param realTestFiles ?: object[] - with Filename (e.g. mix.exs) and Path to real file in a directory
113
+ * @return Jest spy on lstatSync
114
+ */
115
+ const mockFsLStat = ({ fs, lstatSyncActual, testFiles, realTestFiles }) =>
116
+ jest.spyOn(fs, 'lstatSync').mockImplementation((path) => {
117
+ if (testFiles) {
118
+ const file = testFiles.find((otherFile) => {
119
+ return path.includes(otherFile.path);
120
+ });
121
+
122
+ if (file) {
123
+ return {
124
+ isFile: () => true,
125
+ };
126
+ }
127
+ }
128
+
129
+ if (realTestFiles) {
130
+ const file = realTestFiles.find((otherFile) => {
131
+ return path.includes(otherFile.filename);
132
+ });
133
+
134
+ if (file) {
135
+ return {
136
+ isFile: () => true,
137
+ };
138
+ }
139
+ }
140
+
141
+ return lstatSyncActual(path);
142
+ });
143
+
37
144
  /**
38
145
  * Mock external conventional-changelog modules
39
146
  *
40
147
  * Mocks should be unregistered in test cleanup by calling unmock()
41
148
  *
42
149
  * bump?: 'major' | 'minor' | 'patch' | Error | (opt, parserOpts, cb) => { cb(err) | cb(null, { releaseType }) }
43
- * changelog?: string | Error | Array<string | Error | (opt) => string | null>
150
+ * changelog?: string | Error | Array<string | Error | (opt) => string | null> - Changelog to be "generated" by conventional-changelog when reading commit history
44
151
  * execFile?: ({ dryRun, silent }, cmd, cmdArgs) => Promise<string>
45
- * fs?: { [string]: string | Buffer | any }
46
- * pkg?: { [string]: any }
47
152
  * tags?: string[] | Error
153
+ * existingChangelog?: string - Existing CHANGELOG.md content
154
+ * testFiles?: object[] - with Path and Value fields, for mocking readFileSynch on packageFiles such as package.json, bower.json, manifest.json
155
+ * realTestFiles?: object[] - with Filename (e.g. mix.exs) and Path to real file in test directory
48
156
  */
49
- function mock ({ bump, changelog, execFile, fs, pkg, tags } = {}) {
50
- mockery.enable({ warnOnUnregistered: false, useCleanCache: true })
51
-
52
- mockery.registerMock('conventional-recommended-bump', function (opt, parserOpts, cb) {
53
- if (typeof bump === 'function') bump(opt, parserOpts, cb)
54
- else if (bump instanceof Error) cb(bump)
55
- else cb(null, bump ? { releaseType: bump } : {})
56
- })
57
-
58
- if (!Array.isArray(changelog)) changelog = [changelog]
59
- mockery.registerMock(
60
- 'conventional-changelog',
61
- (opt) =>
62
- new Readable({
63
- read (_size) {
64
- const next = changelog.shift()
65
- if (next instanceof Error) {
66
- this.destroy(next)
67
- } else if (typeof next === 'function') {
68
- this.push(next(opt))
69
- } else {
70
- this.push(next ? Buffer.from(next, 'utf8') : null)
71
- }
72
- }
73
- })
74
- )
75
-
76
- mockery.registerMock('git-semver-tags', function (cb) {
77
- if (tags instanceof Error) cb(tags)
78
- else cb(null, tags | [])
79
- })
80
-
81
- if (typeof execFile === 'function') {
82
- // called from commit & tag lifecycle methods
83
- mockery.registerMock('../run-execFile', execFile)
84
- }
157
+ function mock({
158
+ bump,
159
+ changelog,
160
+ tags,
161
+ existingChangelog,
162
+ testFiles,
163
+ realTestFiles,
164
+ } = {}) {
165
+ mockers.mockRecommendedBump({ bump });
166
+
167
+ if (!Array.isArray(changelog)) changelog = [changelog];
168
+
169
+ mockers.mockConventionalChangelog({
170
+ changelog,
171
+ });
172
+
173
+ mockers.mockGitSemverTags({
174
+ tags,
175
+ });
85
176
 
86
177
  // needs to be set after mockery, but before mock-fs
87
- standardVersion = require('../index')
178
+ standardVersion = require('../index');
179
+
180
+ // For fake and injected test files pretend they exist at root level when Fs queries lstat
181
+ // Package.json works without this as it'll check the one in this actual repo...
182
+ lstatSyncSpy = mockFsLStat({
183
+ fs,
184
+ lstatSyncActual,
185
+ testFiles,
186
+ realTestFiles,
187
+ });
188
+
189
+ readFileSyncSpy = mockReadFilesFromDisk({
190
+ fs,
191
+ readFileSyncActual,
192
+ existingChangelog,
193
+ testFiles,
194
+ realTestFiles,
195
+ });
196
+
197
+ // Spies on writeFileSync to capture calls and ensure we don't actually try write anything to disc
198
+ writeFileSyncSpy = jest.spyOn(fs, 'writeFileSync').mockImplementation();
199
+ }
88
200
 
89
- fs = Object.assign({}, fs)
90
- if (pkg) {
91
- fs['package.json'] = JSON.stringify(pkg)
92
- } else if (pkg === undefined && !fs['package.json']) {
93
- fs['package.json'] = JSON.stringify({ version: '1.0.0' })
94
- }
95
- mockFS(fs)
201
+ function clearCapturedSpyCalls() {
202
+ consoleInfoSpy.mockClear();
203
+ consoleErrorSpy.mockClear();
204
+ }
96
205
 
97
- stdMocks.use()
98
- return () => stdMocks.flush()
206
+ function restoreMocksToRealImplementation() {
207
+ readFileSyncSpy.mockRestore();
208
+ writeFileSyncSpy.mockRestore();
209
+ lstatSyncSpy.mockRestore();
99
210
  }
100
211
 
101
- function unmock () {
102
- mockery.deregisterAll()
103
- mockery.disable()
104
- mockFS.restore()
105
- stdMocks.restore()
106
- standardVersion = null
107
-
108
- // push out prints from the Mocha reporter
109
- const { stdout } = stdMocks.flush()
110
- for (const str of stdout) {
111
- if (str.startsWith(' ')) process.stdout.write(str)
112
- }
212
+ function unmock() {
213
+ clearCapturedSpyCalls();
214
+
215
+ restoreMocksToRealImplementation();
216
+
217
+ standardVersion = null;
113
218
  }
114
219
 
115
220
  describe('format-commit-message', function () {
116
221
  it('works for no {{currentTag}}', function () {
117
- formatCommitMessage('chore(release): 1.0.0', '1.0.0').should.equal(
118
- 'chore(release): 1.0.0'
119
- )
120
- })
222
+ expect(formatCommitMessage('chore(release): 1.0.0', '1.0.0')).toEqual(
223
+ 'chore(release): 1.0.0',
224
+ );
225
+ });
121
226
  it('works for one {{currentTag}}', function () {
122
- formatCommitMessage('chore(release): {{currentTag}}', '1.0.0').should.equal(
123
- 'chore(release): 1.0.0'
124
- )
125
- })
227
+ expect(
228
+ formatCommitMessage('chore(release): {{currentTag}}', '1.0.0'),
229
+ ).toEqual('chore(release): 1.0.0');
230
+ });
126
231
  it('works for two {{currentTag}}', function () {
127
- formatCommitMessage(
128
- 'chore(release): {{currentTag}} \n\n* CHANGELOG: https://github.com/absolute-version/commit-and-tag-version/blob/v{{currentTag}}/CHANGELOG.md',
129
- '1.0.0'
130
- ).should.equal(
131
- 'chore(release): 1.0.0 \n\n* CHANGELOG: https://github.com/absolute-version/commit-and-tag-version/blob/v1.0.0/CHANGELOG.md'
132
- )
133
- })
134
- })
232
+ expect(
233
+ formatCommitMessage(
234
+ 'chore(release): {{currentTag}} \n\n* CHANGELOG: https://github.com/absolute-version/commit-and-tag-version/blob/v{{currentTag}}/CHANGELOG.md',
235
+ '1.0.0',
236
+ ),
237
+ ).toEqual(
238
+ 'chore(release): 1.0.0 \n\n* CHANGELOG: https://github.com/absolute-version/commit-and-tag-version/blob/v1.0.0/CHANGELOG.md',
239
+ );
240
+ });
241
+ });
135
242
 
136
243
  describe('cli', function () {
137
- afterEach(unmock)
244
+ afterEach(unmock);
138
245
 
139
246
  describe('CHANGELOG.md does not exist', function () {
140
247
  it('populates changelog with commits since last tag by default', async function () {
141
- mock({ bump: 'patch', changelog: 'patch release\n', tags: ['v1.0.0'] })
142
- await exec()
143
- const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
144
- content.should.match(/patch release/)
145
- })
248
+ mock({ bump: 'patch', changelog: 'patch release\n', tags: ['v1.0.0'] });
249
+ await exec();
250
+ verifyNewChangelogContentMatches({
251
+ writeFileSyncSpy,
252
+ expectedContent: /patch release/,
253
+ });
254
+ });
146
255
 
147
256
  it('includes all commits if --first-release is true', async function () {
148
257
  mock({
149
258
  bump: 'minor',
150
259
  changelog: 'first commit\npatch release\n',
151
- pkg: { version: '1.0.1' }
152
- })
153
- await exec('--first-release')
154
- const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
155
- content.should.match(/patch release/)
156
- content.should.match(/first commit/)
157
- })
260
+ testFiles: [{ path: 'package.json', value: { version: '1.0.1' } }],
261
+ });
262
+ await exec('--first-release');
263
+ verifyNewChangelogContentMatches({
264
+ writeFileSyncSpy,
265
+ expectedContent: /patch release/,
266
+ });
267
+ verifyNewChangelogContentMatches({
268
+ writeFileSyncSpy,
269
+ expectedContent: /first commit/,
270
+ });
271
+ });
158
272
 
159
273
  it('skipping changelog will not create a changelog file', async function () {
160
- mock({ bump: 'minor', changelog: 'foo\n' })
161
- await exec('--skip.changelog true')
162
- getPackageVersion().should.equal('1.1.0')
163
- expect(() => fs.readFileSync('CHANGELOG.md', 'utf-8')).to.throw(/ENOENT/)
164
- })
165
- })
274
+ mock({ bump: 'minor', changelog: 'foo\n' });
275
+ await exec('--skip.changelog true');
276
+
277
+ verifyPackageVersion({ writeFileSyncSpy, expectedVersion: '1.1.0' });
278
+ expect(writeFileSyncSpy).not.toHaveBeenCalledWith('CHANGELOG.md');
279
+ });
280
+ });
166
281
 
167
282
  describe('CHANGELOG.md exists', function () {
283
+ afterEach(unmock);
284
+
168
285
  it('appends the new release above the last release, removing the old header (legacy format), and does not retain any front matter', async function () {
169
- const frontMatter =
170
- '---\nstatus: new\n---\n'
286
+ const frontMatter = '---\nstatus: new\n---\n';
171
287
  mock({
172
288
  bump: 'patch',
173
289
  changelog: 'release 1.0.1\n',
174
- fs: { 'CHANGELOG.md': frontMatter + 'legacy header format<a name="1.0.0">\n' },
175
- tags: ['v1.0.0']
176
- })
177
- await exec()
178
- const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
179
- content.should.match(/1\.0\.1/)
180
- content.should.not.match(/legacy header format/)
181
- content.should.not.match(/---status: new---/)
182
- })
290
+ existingChangelog:
291
+ frontMatter + 'legacy header format<a name="1.0.0">\n',
292
+ tags: ['v1.0.0'],
293
+ });
294
+ await exec();
295
+
296
+ verifyNewChangelogContentMatches({
297
+ writeFileSyncSpy,
298
+ expectedContent: /1\.0\.1/,
299
+ });
300
+
301
+ verifyNewChangelogContentDoesNotMatch({
302
+ writeFileSyncSpy,
303
+ expectedContent: /legacy header format/,
304
+ });
305
+ verifyNewChangelogContentDoesNotMatch({
306
+ writeFileSyncSpy,
307
+ expectedContent: /---status: new---/,
308
+ });
309
+ });
183
310
 
184
311
  it('appends the new release above the last release, replacing the old header (standard-version format) with header (new format), and retains any front matter', async function () {
185
- const { header } = require('../defaults')
312
+ const { header } = require('../defaults');
186
313
 
187
314
  const standardVersionHeader =
188
- '# Changelog\n\nAll notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.'
315
+ '# Changelog\n\nAll notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.';
189
316
 
190
- const frontMatter =
191
- '---\nstatus: new\n---\n'
317
+ const frontMatter = '---\nstatus: new\n---\n';
192
318
 
193
319
  const changelog101 =
194
- '### [1.0.1](/compare/v1.0.0...v1.0.1) (YYYY-MM-DD)\n\n\n### Bug Fixes\n\n* patch release ABCDEFXY\n'
320
+ '### [1.0.1](/compare/v1.0.0...v1.0.1) (YYYY-MM-DD)\n\n\n### Bug Fixes\n\n* patch release ABCDEFXY\n';
195
321
 
196
322
  const changelog100 =
197
- '### [1.0.0](/compare/v0.0.1...v1.0.0) (YYYY-MM-DD)\n\n\n### Features\n\n* Version one feature set\n'
323
+ '### [1.0.0](/compare/v0.0.1...v1.0.0) (YYYY-MM-DD)\n\n\n### Features\n\n* Version one feature set\n';
198
324
 
199
- const initialChangelog = frontMatter + '\n' + standardVersionHeader + '\n' + changelog100
325
+ const initialChangelog =
326
+ frontMatter + '\n' + standardVersionHeader + '\n' + changelog100;
200
327
 
201
328
  mock({
202
329
  bump: 'patch',
203
330
  changelog: changelog101,
204
- fs: { 'CHANGELOG.md': initialChangelog },
205
- tags: ['v1.0.0']
206
- })
207
- await exec()
208
- const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
209
- content.should.equal(frontMatter + '\n' + header + '\n' + changelog101 + changelog100)
210
- })
331
+ existingChangelog: initialChangelog,
332
+ tags: ['v1.0.0'],
333
+ });
334
+ await exec();
335
+
336
+ verifyNewChangelogContentEquals({
337
+ writeFileSyncSpy,
338
+ expectedContent:
339
+ frontMatter + '\n' + header + '\n' + changelog101 + changelog100,
340
+ });
341
+ });
211
342
 
212
343
  it('appends the new release above the last release, removing the old header (new format), and retains any front matter', async function () {
213
- const { header } = require('../defaults')
214
- const frontMatter =
215
- '---\nstatus: new\n---\n'
344
+ const { header } = require('../defaults');
345
+ const frontMatter = '---\nstatus: new\n---\n';
216
346
 
217
347
  const changelog101 =
218
- '### [1.0.1](/compare/v1.0.0...v1.0.1) (YYYY-MM-DD)\n\n\n### Bug Fixes\n\n* patch release ABCDEFXY\n'
348
+ '### [1.0.1](/compare/v1.0.0...v1.0.1) (YYYY-MM-DD)\n\n\n### Bug Fixes\n\n* patch release ABCDEFXY\n';
219
349
 
220
350
  const changelog100 =
221
- '### [1.0.0](/compare/v0.0.1...v1.0.0) (YYYY-MM-DD)\n\n\n### Features\n\n* Version one feature set\n'
351
+ '### [1.0.0](/compare/v0.0.1...v1.0.0) (YYYY-MM-DD)\n\n\n### Features\n\n* Version one feature set\n';
222
352
 
223
- const initialChangelog = frontMatter + '\n' + header + '\n' + changelog100
353
+ const initialChangelog =
354
+ frontMatter + '\n' + header + '\n' + changelog100;
224
355
 
225
356
  mock({
226
357
  bump: 'patch',
227
358
  changelog: changelog101,
228
- fs: { 'CHANGELOG.md': initialChangelog },
229
- tags: ['v1.0.0']
230
- })
231
- await exec()
232
-
233
- const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
234
- content.should.equal(frontMatter + '\n' + header + '\n' + changelog101 + changelog100)
235
- })
359
+ existingChangelog: initialChangelog,
360
+ tags: ['v1.0.0'],
361
+ });
362
+ await exec();
363
+
364
+ verifyNewChangelogContentEquals({
365
+ writeFileSyncSpy,
366
+ expectedContent:
367
+ frontMatter + '\n' + header + '\n' + changelog101 + changelog100,
368
+ });
369
+ });
236
370
 
237
371
  it('appends the new release above the last release, removing the old header (new format)', async function () {
238
- const { header } = require('../defaults')
372
+ const { header } = require('../defaults');
239
373
  const changelog1 =
240
- '### [1.0.1](/compare/v1.0.0...v1.0.1) (YYYY-MM-DD)\n\n\n### Bug Fixes\n\n* patch release ABCDEFXY\n'
241
- mock({ bump: 'patch', changelog: changelog1, tags: ['v1.0.0'] })
242
- await exec()
243
- let content = fs.readFileSync('CHANGELOG.md', 'utf-8')
244
- content.should.equal(header + '\n' + changelog1)
374
+ '### [1.0.1](/compare/v1.0.0...v1.0.1) (YYYY-MM-DD)\n\n\n### Bug Fixes\n\n* patch release ABCDEFXY\n';
375
+ mock({ bump: 'patch', changelog: changelog1, tags: ['v1.0.0'] });
376
+ await exec();
377
+ const content = header + '\n' + changelog1;
378
+ verifyNewChangelogContentEquals({
379
+ writeFileSyncSpy,
380
+ expectedContent: content,
381
+ });
245
382
 
246
383
  const changelog2 =
247
- '### [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'
248
- unmock()
384
+ '### [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';
385
+ unmock();
386
+
249
387
  mock({
250
388
  bump: 'patch',
251
389
  changelog: changelog2,
252
- fs: { 'CHANGELOG.md': content },
253
- tags: ['v1.0.0', 'v1.0.1']
254
- })
255
- await exec()
256
- content = fs.readFileSync('CHANGELOG.md', 'utf-8')
257
- content.should.equal(header + '\n' + changelog2 + changelog1)
258
- })
390
+ existingChangelog: content,
391
+ tags: ['v1.0.0', 'v1.0.1'],
392
+ });
393
+ await exec();
394
+ verifyNewChangelogContentEquals({
395
+ writeFileSyncSpy,
396
+ expectedContent: header + '\n' + changelog2 + changelog1,
397
+ });
398
+ });
259
399
 
260
400
  it('[DEPRECATED] (--changelogHeader) allows for a custom changelog header', async function () {
261
- const header = '# Pork Chop Log'
401
+ const header = '# Pork Chop Log';
262
402
  mock({
263
403
  bump: 'minor',
264
404
  changelog: header + '\n',
265
- fs: { 'CHANGELOG.md': '' }
266
- })
267
- await exec(`--changelogHeader="${header}"`)
268
- const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
269
- content.should.match(new RegExp(header))
270
- })
405
+ existingChangelog: '',
406
+ });
407
+ await exec(`--changelogHeader="${header}"`);
408
+ verifyNewChangelogContentMatches({
409
+ writeFileSyncSpy,
410
+ expectedContent: new RegExp(header),
411
+ });
412
+ });
271
413
 
272
414
  it('[DEPRECATED] (--changelogHeader) exits with error if changelog header matches last version search regex', async function () {
273
- mock({ bump: 'minor', fs: { 'CHANGELOG.md': '' } })
274
- await expect(exec('--changelogHeader="## 3.0.2"')).to.be.rejectedWith(/custom changelog header must not match/)
275
- })
276
- })
415
+ mock({ bump: 'minor', existingChangelog: '' });
416
+ await expect(exec('--changelogHeader="## 3.0.2"')).rejects.toThrow(
417
+ /custom changelog header must not match/,
418
+ );
419
+ });
420
+ });
277
421
 
278
422
  describe('lifecycle scripts', function () {
423
+ afterEach(unmock);
424
+
279
425
  describe('prerelease hook', function () {
280
426
  it('should run the prerelease hook when provided', async function () {
281
- const flush = mock({
427
+ mock({
282
428
  bump: 'minor',
283
- fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
284
- })
429
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
430
+ });
285
431
 
286
432
  await exec({
287
433
  scripts: {
288
- prerelease: "node -e \"console.error('prerelease' + ' ran')\""
289
- }
290
- })
291
- const { stderr } = flush()
292
- stderr.join('\n').should.match(/prerelease ran/)
293
- })
434
+ prerelease: "node -e \"console.error('prerelease' + ' ran')\"",
435
+ },
436
+ });
437
+
438
+ const expectedLog = 'prerelease ran';
439
+ verifyLogPrinted({ consoleInfoSpy: consoleErrorSpy, expectedLog });
440
+ });
294
441
 
295
442
  it('should abort if the hook returns a non-zero exit code', async function () {
296
443
  mock({
297
444
  bump: 'minor',
298
- fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
299
- })
300
-
301
- await expect(exec({
302
- scripts: {
303
- prerelease: "node -e \"throw new Error('prerelease' + ' fail')\""
304
- }
305
- })).to.be.rejectedWith(/prerelease fail/)
306
- })
307
- })
445
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
446
+ });
447
+
448
+ await expect(
449
+ exec({
450
+ scripts: {
451
+ prerelease: "node -e \"throw new Error('prerelease' + ' fail')\"",
452
+ },
453
+ }),
454
+ ).rejects.toThrow(/prerelease fail/);
455
+ });
456
+ });
308
457
 
309
458
  describe('prebump hook', function () {
310
459
  it('should allow prebump hook to return an alternate version #', async function () {
311
- const flush = mock({
460
+ mock({
312
461
  bump: 'minor',
313
- fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
314
- })
462
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
463
+ });
315
464
 
316
465
  await exec({
317
466
  scripts: {
318
- prebump: "node -e \"console.log(Array.of(9, 9, 9).join('.'))\""
319
- }
320
- })
321
- const { stdout } = flush()
322
- stdout.join('').should.match(/9\.9\.9/)
323
- getPackageVersion().should.equal('9.9.9')
324
- })
467
+ prebump: 'node -e "console.log(Array.of(9, 9, 9).join(\'.\'))"',
468
+ },
469
+ });
470
+ verifyLogPrinted({ consoleInfoSpy, expectedLog: '9.9.9' });
471
+ verifyPackageVersion({ writeFileSyncSpy, expectedVersion: '9.9.9' });
472
+ });
325
473
 
326
474
  it('should not allow prebump hook to return a releaseAs command', async function () {
327
475
  mock({
328
476
  bump: 'minor',
329
- fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
330
- })
477
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
478
+ });
331
479
 
332
480
  await exec({
333
481
  scripts: {
334
- prebump: "node -e \"console.log('major')\""
335
- }
336
- })
337
- getPackageVersion().should.equal('1.1.0')
338
- })
482
+ prebump: 'node -e "console.log(\'major\')"',
483
+ },
484
+ });
485
+ verifyPackageVersion({ writeFileSyncSpy, expectedVersion: '1.1.0' });
486
+ });
339
487
 
340
488
  it('should allow prebump hook to return an arbitrary string', async function () {
341
489
  mock({
342
490
  bump: 'minor',
343
- fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
344
- })
491
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
492
+ });
345
493
 
346
494
  await exec({
347
495
  scripts: {
348
- prebump: "node -e \"console.log('Hello World')\""
349
- }
350
- })
351
- getPackageVersion().should.equal('1.1.0')
352
- })
496
+ prebump: 'node -e "console.log(\'Hello World\')"',
497
+ },
498
+ });
499
+ verifyPackageVersion({ writeFileSyncSpy, expectedVersion: '1.1.0' });
500
+ });
353
501
 
354
502
  it('should allow prebump hook to return a version with build info', async function () {
355
503
  mock({
356
504
  bump: 'minor',
357
- fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
358
- })
505
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
506
+ });
359
507
 
360
508
  await exec({
361
509
  scripts: {
362
- prebump: "node -e \"console.log('9.9.9-test+build')\""
363
- }
364
- })
365
- getPackageVersion().should.equal('9.9.9-test+build')
366
- })
367
- })
510
+ prebump: 'node -e "console.log(\'9.9.9-test+build\')"',
511
+ },
512
+ });
513
+ verifyPackageVersion({
514
+ writeFileSyncSpy,
515
+ expectedVersion: '9.9.9-test+build',
516
+ });
517
+ });
518
+ });
368
519
 
369
520
  describe('postbump hook', function () {
370
521
  it('should run the postbump hook when provided', async function () {
371
- const flush = mock({
522
+ mock({
372
523
  bump: 'minor',
373
- fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
374
- })
524
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
525
+ });
375
526
 
376
527
  await exec({
377
528
  scripts: {
378
- postbump: "node -e \"console.error('postbump' + ' ran')\""
379
- }
380
- })
381
- const { stderr } = flush()
382
- stderr.join('\n').should.match(/postbump ran/)
383
- })
529
+ postbump: "node -e \"console.error('postbump' + ' ran')\"",
530
+ },
531
+ });
532
+
533
+ const expectedLog = 'postbump ran';
534
+ verifyLogPrinted({ consoleInfoSpy: consoleErrorSpy, expectedLog });
535
+ });
384
536
 
385
537
  it('should run the postbump and exit with error when postbump fails', async function () {
386
538
  mock({
387
539
  bump: 'minor',
388
- fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
389
- })
390
-
391
- await expect(exec({
392
- scripts: {
393
- postbump: "node -e \"throw new Error('postbump' + ' fail')\""
394
- }
395
- })).to.be.rejectedWith(/postbump fail/)
396
- })
397
- })
540
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
541
+ });
542
+
543
+ await expect(
544
+ exec({
545
+ scripts: {
546
+ postbump: "node -e \"throw new Error('postbump' + ' fail')\"",
547
+ },
548
+ }),
549
+ ).rejects.toThrow(/postbump fail/);
550
+ });
551
+ });
398
552
 
399
553
  describe('manual-release', function () {
400
554
  describe('release-types', function () {
401
- const regularTypes = ['major', 'minor', 'patch']
402
- const nextVersion = { major: '2.0.0', minor: '1.1.0', patch: '1.0.1' }
555
+ const regularTypes = ['major', 'minor', 'patch'];
556
+ const nextVersion = { major: '2.0.0', minor: '1.1.0', patch: '1.0.1' };
403
557
 
404
558
  regularTypes.forEach(function (type) {
405
559
  it('creates a ' + type + ' release', async function () {
406
560
  mock({
407
561
  bump: 'patch',
408
- fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
409
- })
410
- await exec('--release-as ' + type)
411
- getPackageVersion().should.equal(nextVersion[type])
412
- })
413
- })
562
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
563
+ });
564
+ await exec('--release-as ' + type);
565
+ verifyPackageVersion({
566
+ writeFileSyncSpy,
567
+ expectedVersion: nextVersion[type],
568
+ });
569
+ });
570
+ });
414
571
 
415
572
  // this is for pre-releases
416
573
  regularTypes.forEach(function (type) {
417
574
  it('creates a pre' + type + ' release', async function () {
418
575
  mock({
419
576
  bump: 'patch',
420
- fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
421
- })
422
- await exec('--release-as ' + type + ' --prerelease ' + type)
423
- getPackageVersion().should.equal(`${nextVersion[type]}-${type}.0`)
424
- })
425
- })
577
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
578
+ });
579
+ await exec('--release-as ' + type + ' --prerelease ' + type);
580
+ verifyPackageVersion({
581
+ writeFileSyncSpy,
582
+ expectedVersion: `${nextVersion[type]}-${type}.0`,
583
+ });
584
+ });
585
+ });
426
586
 
427
587
  it('exits with error if an invalid release type is provided', async function () {
428
- mock({ bump: 'minor', fs: { 'CHANGELOG.md': '' } })
588
+ mock({ bump: 'minor', existingChangelog: '' });
429
589
 
430
- await expect(exec('--release-as invalid')).to.be.rejectedWith(/releaseAs must be one of/)
431
- })
432
- })
590
+ await expect(exec('--release-as invalid')).rejects.toThrow(
591
+ /releaseAs must be one of/,
592
+ );
593
+ });
594
+ });
433
595
 
434
596
  describe('release-as-exact', function () {
435
597
  it('releases as v100.0.0', async function () {
436
598
  mock({
437
599
  bump: 'patch',
438
- fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
439
- })
440
- await exec('--release-as v100.0.0')
441
- getPackageVersion().should.equal('100.0.0')
442
- })
600
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
601
+ });
602
+ await exec('--release-as v100.0.0');
603
+ verifyPackageVersion({
604
+ writeFileSyncSpy,
605
+ expectedVersion: '100.0.0',
606
+ });
607
+ });
443
608
 
444
609
  it('releases as 200.0.0-amazing', async function () {
445
610
  mock({
446
611
  bump: 'patch',
447
- fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
448
- })
449
- await exec('--release-as 200.0.0-amazing')
450
- getPackageVersion().should.equal('200.0.0-amazing')
451
- })
612
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
613
+ });
614
+ await exec('--release-as 200.0.0-amazing');
615
+ verifyPackageVersion({
616
+ writeFileSyncSpy,
617
+ expectedVersion: '200.0.0-amazing',
618
+ });
619
+ });
452
620
 
453
621
  it('releases as 100.0.0 with prerelease amazing', async function () {
454
622
  mock({
455
623
  bump: 'patch',
456
- fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' },
457
- pkg: {
458
- version: '1.0.0'
459
- }
460
- })
461
- await exec('--release-as 100.0.0 --prerelease amazing')
462
- should.equal(getPackageVersion(), '100.0.0-amazing.0')
463
- })
624
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
625
+ testFiles: [
626
+ {
627
+ path: 'package.json',
628
+ value: {
629
+ version: '1.0.0',
630
+ },
631
+ },
632
+ ],
633
+ });
634
+ await exec('--release-as 100.0.0 --prerelease amazing');
635
+ verifyPackageVersion({
636
+ writeFileSyncSpy,
637
+ expectedVersion: '100.0.0-amazing.0',
638
+ });
639
+ });
464
640
 
465
641
  it('release 100.0.0 with prerelease amazing bumps build', async function () {
466
642
  mock({
467
643
  bump: 'patch',
468
- fs: { 'CHANGELOG.md': 'legacy header format<a name="100.0.0-amazing.0">\n' },
469
- pkg: {
470
- version: '100.0.0-amazing.0'
471
- }
472
- })
473
- await exec('--release-as 100.0.0 --prerelease amazing')
474
- should.equal(getPackageVersion(), '100.0.0-amazing.1')
475
- })
644
+ fs: {
645
+ 'CHANGELOG.md':
646
+ 'legacy header format<a name="100.0.0-amazing.0">\n',
647
+ },
648
+ testFiles: [
649
+ {
650
+ path: 'package.json',
651
+ value: {
652
+ version: '100.0.0-amazing.0',
653
+ },
654
+ },
655
+ ],
656
+ });
657
+ await exec('--release-as 100.0.0 --prerelease amazing');
658
+ verifyPackageVersion({
659
+ writeFileSyncSpy,
660
+ expectedVersion: '100.0.0-amazing.1',
661
+ });
662
+ });
476
663
 
477
664
  it('release 100.0.0-amazing.0 with prerelease amazing bumps build', async function () {
478
665
  mock({
479
666
  bump: 'patch',
480
- fs: { 'CHANGELOG.md': 'legacy header format<a name="100.0.0-amazing.0">\n' },
481
- pkg: {
482
- version: '100.0.0-amazing.1'
483
- }
484
- })
485
- await exec('--release-as 100.0.0-amazing.0 --prerelease amazing')
486
- should.equal(getPackageVersion(), '100.0.0-amazing.2')
487
- })
667
+ fs: {
668
+ 'CHANGELOG.md':
669
+ 'legacy header format<a name="100.0.0-amazing.0">\n',
670
+ },
671
+ testFiles: [
672
+ {
673
+ path: 'package.json',
674
+ value: {
675
+ version: '100.0.0-amazing.1',
676
+ },
677
+ },
678
+ ],
679
+ });
680
+ await exec('--release-as 100.0.0-amazing.0 --prerelease amazing');
681
+ verifyPackageVersion({
682
+ writeFileSyncSpy,
683
+ expectedVersion: '100.0.0-amazing.2',
684
+ });
685
+ });
488
686
 
489
687
  it('release 100.0.0 with prerelease amazing correctly sets version', async function () {
490
688
  mock({
491
689
  bump: 'patch',
492
- fs: { 'CHANGELOG.md': 'legacy header format<a name="100.0.0-amazing.0">\n' },
493
- pkg: {
494
- version: '99.0.0-amazing.0'
495
- }
496
- })
497
- await exec('--release-as 100.0.0 --prerelease amazing')
498
- should.equal(getPackageVersion(), '100.0.0-amazing.0')
499
- })
690
+ fs: {
691
+ 'CHANGELOG.md':
692
+ 'legacy header format<a name="100.0.0-amazing.0">\n',
693
+ },
694
+ testFiles: [
695
+ {
696
+ path: 'package.json',
697
+ value: {
698
+ version: '99.0.0-amazing.0',
699
+ },
700
+ },
701
+ ],
702
+ });
703
+ await exec('--release-as 100.0.0 --prerelease amazing');
704
+ verifyPackageVersion({
705
+ writeFileSyncSpy,
706
+ expectedVersion: '100.0.0-amazing.0',
707
+ });
708
+ });
500
709
 
501
710
  it('release 100.0.0-amazing.0 with prerelease amazing correctly sets version', async function () {
502
711
  mock({
503
712
  bump: 'patch',
504
- fs: { 'CHANGELOG.md': 'legacy header format<a name="100.0.0-amazing.0">\n' },
505
- pkg: {
506
- version: '99.0.0-amazing.0'
507
- }
508
- })
509
- await exec('--release-as 100.0.0-amazing.0 --prerelease amazing')
510
- should.equal(getPackageVersion(), '100.0.0-amazing.0')
511
- })
713
+ fs: {
714
+ 'CHANGELOG.md':
715
+ 'legacy header format<a name="100.0.0-amazing.0">\n',
716
+ },
717
+ testFiles: [
718
+ {
719
+ path: 'package.json',
720
+ value: {
721
+ version: '99.0.0-amazing.0',
722
+ },
723
+ },
724
+ ],
725
+ });
726
+ await exec('--release-as 100.0.0-amazing.0 --prerelease amazing');
727
+ verifyPackageVersion({
728
+ writeFileSyncSpy,
729
+ expectedVersion: '100.0.0-amazing.0',
730
+ });
731
+ });
512
732
 
513
733
  it('release 100.0.0-amazing.0 with prerelease amazing retains build metadata', async function () {
514
734
  mock({
515
735
  bump: 'patch',
516
- fs: { 'CHANGELOG.md': 'legacy header format<a name="100.0.0-amazing.0">\n' },
517
- pkg: {
518
- version: '100.0.0-amazing.0'
519
- }
520
- })
521
- await exec('--release-as 100.0.0-amazing.0+build.1234 --prerelease amazing')
522
- should.equal(getPackageVersion(), '100.0.0-amazing.1+build.1234')
523
- })
736
+ fs: {
737
+ 'CHANGELOG.md':
738
+ 'legacy header format<a name="100.0.0-amazing.0">\n',
739
+ },
740
+ testFiles: [
741
+ {
742
+ path: 'package.json',
743
+ value: {
744
+ version: '100.0.0-amazing.0',
745
+ },
746
+ },
747
+ ],
748
+ });
749
+ await exec(
750
+ '--release-as 100.0.0-amazing.0+build.1234 --prerelease amazing',
751
+ );
752
+ verifyPackageVersion({
753
+ writeFileSyncSpy,
754
+ expectedVersion: '100.0.0-amazing.1+build.1234',
755
+ });
756
+ });
524
757
 
525
758
  it('release 100.0.0-amazing.3 with prerelease amazing correctly sets prerelease version', async function () {
526
759
  mock({
527
760
  bump: 'patch',
528
- fs: { 'CHANGELOG.md': 'legacy header format<a name="100.0.0-amazing.0">\n' },
529
- pkg: {
530
- version: '100.0.0-amazing.0'
531
- }
532
- })
533
- await exec('--release-as 100.0.0-amazing.3 --prerelease amazing')
534
- should.equal(getPackageVersion(), '100.0.0-amazing.3')
535
- })
536
- })
761
+ fs: {
762
+ 'CHANGELOG.md':
763
+ 'legacy header format<a name="100.0.0-amazing.0">\n',
764
+ },
765
+ testFiles: [
766
+ {
767
+ path: 'package.json',
768
+ value: {
769
+ version: '100.0.0-amazing.0',
770
+ },
771
+ },
772
+ ],
773
+ });
774
+ await exec('--release-as 100.0.0-amazing.3 --prerelease amazing');
775
+ verifyPackageVersion({
776
+ writeFileSyncSpy,
777
+ expectedVersion: '100.0.0-amazing.3',
778
+ });
779
+ });
780
+ });
537
781
 
538
782
  it('creates a prerelease with a new minor version after two prerelease patches', async function () {
539
- let releaseType = 'patch'
540
- const bump = (_, __, cb) => cb(null, { releaseType })
783
+ let releaseType = 'patch';
541
784
  mock({
542
- bump,
543
- fs: { 'CHANGELOG.md': 'legacy header format<a name="1.0.0">\n' }
544
- })
785
+ bump: (_, __, cb) => cb(null, { releaseType }),
786
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
787
+ });
545
788
 
546
- await exec('--release-as patch --prerelease dev')
547
- getPackageVersion().should.equal('1.0.1-dev.0')
789
+ let version = '1.0.1-dev.0';
790
+ await exec('--release-as patch --prerelease dev');
791
+ verifyPackageVersion({ writeFileSyncSpy, expectedVersion: version });
548
792
 
549
- await exec('--prerelease dev')
550
- getPackageVersion().should.equal('1.0.1-dev.1')
793
+ unmock();
794
+ mock({
795
+ bump: (_, __, cb) => cb(null, { releaseType }),
796
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
797
+ testFiles: [{ path: 'package.json', value: { version } }],
798
+ });
799
+
800
+ version = '1.0.1-dev.1';
801
+ await exec('--prerelease dev');
802
+ verifyPackageVersion({ writeFileSyncSpy, expectedVersion: version });
551
803
 
552
- releaseType = 'minor'
553
- await exec('--release-as minor --prerelease dev')
554
- getPackageVersion().should.equal('1.1.0-dev.0')
804
+ releaseType = 'minor';
805
+ unmock();
806
+ mock({
807
+ bump: (_, __, cb) => cb(null, { releaseType }),
808
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
809
+ testFiles: [{ path: 'package.json', value: { version } }],
810
+ });
555
811
 
556
- await exec('--release-as minor --prerelease dev')
557
- getPackageVersion().should.equal('1.1.0-dev.1')
812
+ version = '1.1.0-dev.0';
813
+ await exec('--release-as minor --prerelease dev');
814
+ verifyPackageVersion({ writeFileSyncSpy, expectedVersion: version });
558
815
 
559
- await exec('--prerelease dev')
560
- getPackageVersion().should.equal('1.1.0-dev.2')
561
- })
816
+ unmock();
817
+ mock({
818
+ bump: (_, __, cb) => cb(null, { releaseType }),
819
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
820
+ testFiles: [{ path: 'package.json', value: { version } }],
821
+ });
822
+
823
+ version = '1.1.0-dev.1';
824
+ await exec('--release-as minor --prerelease dev');
825
+ verifyPackageVersion({ writeFileSyncSpy, expectedVersion: version });
826
+
827
+ unmock();
828
+ mock({
829
+ bump: (_, __, cb) => cb(null, { releaseType }),
830
+ existingChangelog: 'legacy header format<a name="1.0.0">\n',
831
+ testFiles: [{ path: 'package.json', value: { version } }],
832
+ });
833
+
834
+ version = '1.1.0-dev.2';
835
+ await exec('--prerelease dev');
836
+ verifyPackageVersion({ writeFileSyncSpy, expectedVersion: version });
837
+ });
562
838
 
563
839
  it('exits with error if an invalid release version is provided', async function () {
564
- mock({ bump: 'minor', fs: { 'CHANGELOG.md': '' } })
840
+ mock({ bump: 'minor', existingChangelog: '' });
565
841
 
566
- await expect(exec('--release-as 10.2')).to.be.rejectedWith(/releaseAs must be one of/)
567
- })
842
+ await expect(exec('--release-as 10.2')).rejects.toThrow(
843
+ /releaseAs must be one of/,
844
+ );
845
+ });
568
846
 
569
847
  it('exits with error if release version conflicts with prerelease', async function () {
570
- mock({ bump: 'minor', fs: { 'CHANGELOG.md': '' } })
848
+ mock({ bump: 'minor', existingChangelog: '' });
571
849
 
572
- await expect(exec('--release-as 1.2.3-amazing.2 --prerelease awesome')).to.be
573
- .rejectedWith(/releaseAs and prerelease have conflicting prerelease identifiers/)
574
- })
575
- })
850
+ await expect(
851
+ exec('--release-as 1.2.3-amazing.2 --prerelease awesome'),
852
+ ).rejects.toThrow(
853
+ /releaseAs and prerelease have conflicting prerelease identifiers/,
854
+ );
855
+ });
856
+ });
576
857
 
577
858
  it('appends line feed at end of package.json', async function () {
578
- mock({ bump: 'patch' })
579
- await exec()
580
- const pkgJson = fs.readFileSync('package.json', 'utf-8')
581
- pkgJson.should.equal('{\n "version": "1.0.1"\n}\n')
582
- })
859
+ mock({ bump: 'patch' });
860
+ await exec();
861
+ verifyFileContentEquals({
862
+ writeFileSyncSpy,
863
+ content: '{\n "version": "1.0.1"\n}\n',
864
+ });
865
+ });
583
866
 
584
867
  it('preserves indentation of tabs in package.json', async function () {
585
868
  mock({
586
869
  bump: 'patch',
587
- fs: { 'package.json': '{\n\t"version": "1.0.0"\n}\n' }
588
- })
589
- await exec()
590
- const pkgJson = fs.readFileSync('package.json', 'utf-8')
591
- pkgJson.should.equal('{\n\t"version": "1.0.1"\n}\n')
592
- })
870
+ testFiles: [
871
+ { path: 'package.json', value: '{\n\t"version": "1.0.0"\n}\n' },
872
+ ],
873
+ });
874
+ await exec();
875
+ // TODO: a) not bumping to 1.0.1, b) need to check how jest might handle tabbing etc
876
+ verifyFileContentEquals({
877
+ writeFileSyncSpy,
878
+ content: '{\n\t"version": "1.0.1"\n}\n',
879
+ });
880
+ });
593
881
 
594
882
  it('preserves indentation of spaces in package.json', async function () {
595
883
  mock({
596
884
  bump: 'patch',
597
- fs: { 'package.json': '{\n "version": "1.0.0"\n}\n' }
598
- })
599
- await exec()
600
- const pkgJson = fs.readFileSync('package.json', 'utf-8')
601
- pkgJson.should.equal('{\n "version": "1.0.1"\n}\n')
602
- })
885
+ testFiles: [
886
+ { path: 'package.json', value: '{\n "version": "1.0.0"\n}\n' },
887
+ ],
888
+ });
889
+ await exec();
890
+ verifyFileContentEquals({
891
+ writeFileSyncSpy,
892
+ content: '{\n "version": "1.0.1"\n}\n',
893
+ });
894
+ });
603
895
 
604
896
  it('preserves carriage return + line feed in package.json', async function () {
605
897
  mock({
606
898
  bump: 'patch',
607
- fs: { 'package.json': '{\r\n "version": "1.0.0"\r\n}\r\n' }
608
- })
609
- await exec()
610
- const pkgJson = fs.readFileSync('package.json', 'utf-8')
611
- pkgJson.should.equal('{\r\n "version": "1.0.1"\r\n}\r\n')
612
- })
899
+ testFiles: [
900
+ { path: 'package.json', value: '{\r\n "version": "1.0.0"\r\n}\r\n' },
901
+ ],
902
+ });
903
+ await exec();
904
+ verifyFileContentEquals({
905
+ writeFileSyncSpy,
906
+ content: '{\r\n "version": "1.0.1"\r\n}\r\n',
907
+ });
908
+ });
613
909
 
614
910
  it('does not print output when the --silent flag is passed', async function () {
615
- const flush = mock()
616
- await exec('--silent')
617
- flush().should.eql({ stdout: [], stderr: [] })
618
- })
619
- })
911
+ mock();
912
+ await exec('--silent');
913
+ expect(consoleErrorSpy).not.toHaveBeenCalled();
914
+ expect(consoleInfoSpy).not.toHaveBeenCalled();
915
+ });
916
+ });
620
917
 
621
918
  describe('commit-and-tag-version', function () {
622
- afterEach(unmock)
919
+ afterEach(unmock);
623
920
 
624
921
  it('should exit on bump error', async function () {
625
- mock({ bump: new Error('bump err') })
922
+ mock({ bump: new Error('bump err') });
626
923
 
627
- await expect(exec()).to.be.rejectedWith(/bump err/)
628
- })
924
+ await expect(exec()).rejects.toThrow(/bump err/);
925
+ });
629
926
 
630
927
  it('should exit on changelog error', async function () {
631
- mock({ bump: 'minor', changelog: new Error('changelog err') })
928
+ mock({ bump: 'minor', changelog: new Error('changelog err') });
632
929
 
633
- await expect(exec()).to.be.rejectedWith(/changelog err/)
634
- })
930
+ await expect(exec()).rejects.toThrow(/changelog err/);
931
+ });
635
932
 
636
933
  it('should exit with error without a package file to bump', async function () {
637
- mock({ bump: 'patch', pkg: false })
934
+ mock({ bump: 'patch', testFiles: null });
638
935
 
639
- await expect(exec({ gitTagFallback: false })).to.be.rejectedWith('no package file found')
640
- })
936
+ await expect(exec({ gitTagFallback: false })).rejects.toThrow(
937
+ 'no package file found',
938
+ );
939
+ });
641
940
 
642
941
  it('bumps version # in bower.json', async function () {
643
942
  mock({
644
943
  bump: 'minor',
645
- fs: { 'bower.json': JSON.stringify({ version: '1.0.0' }) },
646
- tags: ['v1.0.0']
647
- })
648
- await exec()
649
- JSON.parse(fs.readFileSync('bower.json', 'utf-8')).version.should.equal(
650
- '1.1.0'
651
- )
652
- getPackageVersion().should.equal('1.1.0')
653
- })
944
+ testFiles: [{ path: 'bower.json', value: { version: '1.0.0' } }],
945
+ tags: ['v1.0.0'],
946
+ });
947
+ await exec();
948
+
949
+ verifyPackageVersion({
950
+ writeFileSyncSpy,
951
+ expectedVersion: '1.1.0',
952
+ filename: 'bower.json',
953
+ });
954
+ verifyPackageVersion({ writeFileSyncSpy, expectedVersion: '1.1.0' });
955
+ });
654
956
 
655
957
  it('bumps version # in manifest.json', async function () {
656
958
  mock({
657
959
  bump: 'minor',
658
- fs: { 'manifest.json': JSON.stringify({ version: '1.0.0' }) },
659
- tags: ['v1.0.0']
660
- })
661
- await exec()
662
- JSON.parse(fs.readFileSync('manifest.json', 'utf-8')).version.should.equal(
663
- '1.1.0'
664
- )
665
- getPackageVersion().should.equal('1.1.0')
666
- })
960
+ testFiles: [{ path: 'manifest.json', value: { version: '1.0.0' } }],
961
+ tags: ['v1.0.0'],
962
+ });
963
+ await exec();
964
+
965
+ verifyPackageVersion({
966
+ writeFileSyncSpy,
967
+ expectedVersion: '1.1.0',
968
+ filename: 'manifest.json',
969
+ });
970
+ verifyPackageVersion({ writeFileSyncSpy, expectedVersion: '1.1.0' });
971
+ });
667
972
 
668
973
  describe('custom `bumpFiles` support', function () {
974
+ afterEach(unmock);
975
+
669
976
  it('mix.exs + version.txt', async function () {
670
- const updater = 'custom-updater.js'
671
- const updaterModule = require('./mocks/updater/customer-updater')
672
977
  mock({
673
978
  bump: 'minor',
674
- fs: {
675
- 'mix.exs': fs.readFileSync('./test/mocks/mix.exs'),
676
- 'version.txt': fs.readFileSync('./test/mocks/version.txt')
677
- },
678
- tags: ['v1.0.0']
679
- })
680
- mockery.registerMock(resolve(process.cwd(), updater), updaterModule)
979
+ realTestFiles: [
980
+ { filename: 'mix.exs', path: './test/mocks/mix.exs' },
981
+ { filename: 'version.txt', path: './test/mocks/version.txt' },
982
+ ],
983
+ tags: ['v1.0.0'],
984
+ });
681
985
 
682
986
  await exec({
683
987
  bumpFiles: [
684
988
  'version.txt',
685
- { filename: 'mix.exs', updater: 'custom-updater.js' }
686
- ]
687
- })
688
- fs.readFileSync('mix.exs', 'utf-8').should.contain('version: "1.1.0"')
689
- fs.readFileSync('version.txt', 'utf-8').should.equal('1.1.0')
690
- })
989
+ {
990
+ filename: 'mix.exs',
991
+ updater: './test/mocks/updater/customer-updater',
992
+ },
993
+ ],
994
+ });
995
+
996
+ verifyPackageVersion({
997
+ writeFileSyncSpy,
998
+ expectedVersion: '1.1.0',
999
+ filename: 'mix.exs',
1000
+ asString: true,
1001
+ });
1002
+
1003
+ verifyPackageVersion({
1004
+ writeFileSyncSpy,
1005
+ expectedVersion: '1.1.0',
1006
+ filename: 'version.txt',
1007
+ asString: true,
1008
+ });
1009
+ });
691
1010
 
692
1011
  it('bumps a custom `plain-text` file', async function () {
693
1012
  mock({
694
1013
  bump: 'minor',
695
- fs: {
696
- 'VERSION_TRACKER.txt': fs.readFileSync(
697
- './test/mocks/VERSION-1.0.0.txt'
698
- )
699
- }
700
- })
1014
+ realTestFiles: [
1015
+ {
1016
+ filename: 'VERSION_TRACKER.txt',
1017
+ path: './test/mocks/VERSION-1.0.0.txt',
1018
+ },
1019
+ ],
1020
+ });
701
1021
  await exec({
702
- bumpFiles: [{ filename: 'VERSION_TRACKER.txt', type: 'plain-text' }]
703
- })
704
- fs.readFileSync('VERSION_TRACKER.txt', 'utf-8').should.equal('1.1.0')
705
- })
1022
+ bumpFiles: [{ filename: 'VERSION_TRACKER.txt', type: 'plain-text' }],
1023
+ });
1024
+ verifyPackageVersion({
1025
+ writeFileSyncSpy,
1026
+ expectedVersion: '1.1.0',
1027
+ filename: 'VERSION_TRACKER.txt',
1028
+ asString: true,
1029
+ });
1030
+ });
706
1031
 
707
1032
  it('displays the new version from custom bumper with --dry-run', async function () {
708
- const updater = 'increment-updater.js'
709
- const updaterModule = require('./mocks/updater/increment-updater')
710
1033
  mock({
711
1034
  bump: 'minor',
712
- fs: {
713
- 'increment-version.txt': fs.readFileSync(
714
- './test/mocks/increment-version.txt'
715
- )
716
- }
717
- })
718
- mockery.registerMock(resolve(process.cwd(), updater), updaterModule)
719
-
720
- const origInfo = console.info
721
- const capturedOutput = []
1035
+ realTestFiles: [
1036
+ {
1037
+ filename: 'increment-version.txt',
1038
+ path: './test/mocks/increment-version.txt',
1039
+ },
1040
+ ],
1041
+ });
1042
+
1043
+ const origInfo = console.info;
1044
+ const capturedOutput = [];
722
1045
  console.info = (...args) => {
723
- capturedOutput.push(...args)
724
- origInfo(...args)
725
- }
1046
+ capturedOutput.push(...args);
1047
+ origInfo(...args);
1048
+ };
1049
+
726
1050
  try {
727
1051
  await exec({
728
- bumpFiles: [{ filename: 'increment-version.txt', updater: 'increment-updater.js' }],
729
- dryRun: true
730
- })
731
- const logOutput = capturedOutput.join(' ')
732
- stripAnsi(logOutput).should.include('bumping version in increment-version.txt from 1 to 2')
1052
+ bumpFiles: [
1053
+ {
1054
+ filename: 'increment-version.txt',
1055
+ updater: './test/mocks/updater/increment-updater',
1056
+ },
1057
+ ],
1058
+ dryRun: true,
1059
+ });
1060
+
1061
+ const logOutput = capturedOutput.join(' ');
1062
+ expect(stripAnsi(logOutput)).toContain(
1063
+ 'bumping version in increment-version.txt from 1 to 2',
1064
+ );
733
1065
  } finally {
734
- console.info = origInfo
1066
+ console.info = origInfo;
735
1067
  }
736
- })
737
- })
1068
+ });
1069
+ });
738
1070
 
739
1071
  describe('custom `packageFiles` support', function () {
1072
+ afterEach(unmock);
1073
+
740
1074
  it('reads and writes to a custom `plain-text` file', async function () {
741
1075
  mock({
742
1076
  bump: 'minor',
743
- fs: {
744
- 'VERSION_TRACKER.txt': fs.readFileSync(
745
- './test/mocks/VERSION-6.3.1.txt'
746
- )
747
- }
748
- })
1077
+ realTestFiles: [
1078
+ {
1079
+ filename: 'VERSION_TRACKER.txt',
1080
+ path: './test/mocks/VERSION-6.3.1.txt',
1081
+ },
1082
+ ],
1083
+ });
1084
+
749
1085
  await exec({
750
- packageFiles: [{ filename: 'VERSION_TRACKER.txt', type: 'plain-text' }],
751
- bumpFiles: [{ filename: 'VERSION_TRACKER.txt', type: 'plain-text' }]
752
- })
753
- fs.readFileSync('VERSION_TRACKER.txt', 'utf-8').should.equal('6.4.0')
754
- })
1086
+ packageFiles: [
1087
+ { filename: 'VERSION_TRACKER.txt', type: 'plain-text' },
1088
+ ],
1089
+ bumpFiles: [{ filename: 'VERSION_TRACKER.txt', type: 'plain-text' }],
1090
+ });
1091
+
1092
+ verifyPackageVersion({
1093
+ writeFileSyncSpy,
1094
+ expectedVersion: '6.4.0',
1095
+ filename: 'VERSION_TRACKER.txt',
1096
+ asString: true,
1097
+ });
1098
+ });
755
1099
 
756
1100
  it('allows same object to be used in packageFiles and bumpFiles', async function () {
757
1101
  mock({
758
1102
  bump: 'minor',
759
- fs: {
760
- 'VERSION_TRACKER.txt': fs.readFileSync(
761
- './test/mocks/VERSION-6.3.1.txt'
762
- )
763
- }
764
- })
765
- const origWarn = console.warn
1103
+ realTestFiles: [
1104
+ {
1105
+ filename: 'VERSION_TRACKER.txt',
1106
+ path: './test/mocks/VERSION-6.3.1.txt',
1107
+ },
1108
+ ],
1109
+ });
1110
+ const origWarn = console.warn;
1111
+
766
1112
  console.warn = () => {
767
- throw new Error('console.warn should not be called')
768
- }
769
- const filedesc = { filename: 'VERSION_TRACKER.txt', type: 'plain-text' }
1113
+ throw new Error('console.warn should not be called');
1114
+ };
1115
+
1116
+ const filedesc = {
1117
+ filename: 'VERSION_TRACKER.txt',
1118
+ type: 'plain-text',
1119
+ };
1120
+
770
1121
  try {
771
- await exec({ packageFiles: [filedesc], bumpFiles: [filedesc] })
772
- fs.readFileSync('VERSION_TRACKER.txt', 'utf-8').should.equal('6.4.0')
1122
+ await exec({ packageFiles: [filedesc], bumpFiles: [filedesc] });
1123
+ verifyPackageVersion({
1124
+ writeFileSyncSpy,
1125
+ expectedVersion: '6.4.0',
1126
+ filename: 'VERSION_TRACKER.txt',
1127
+ asString: true,
1128
+ });
773
1129
  } finally {
774
- console.warn = origWarn
1130
+ console.warn = origWarn;
775
1131
  }
776
- })
777
- })
1132
+ });
1133
+ });
778
1134
 
779
1135
  it('`packageFiles` are bumped along with `bumpFiles` defaults [commit-and-tag-version#533]', async function () {
780
1136
  mock({
781
1137
  bump: 'minor',
782
- fs: {
783
- '.gitignore': '',
784
- 'package-lock.json': JSON.stringify({ version: '1.0.0' }),
785
- 'manifest.json': fs.readFileSync('./test/mocks/manifest-6.3.1.json')
786
- },
787
- tags: ['v1.0.0']
788
- })
1138
+ testFiles: [
1139
+ {
1140
+ path: '.gitignore',
1141
+ value: '',
1142
+ },
1143
+ {
1144
+ path: 'package-lock.json',
1145
+ value: { version: '1.0.0' },
1146
+ },
1147
+ ],
1148
+ realTestFiles: [
1149
+ {
1150
+ filename: 'manifest.json',
1151
+ path: './test/mocks/manifest-6.3.1.json',
1152
+ },
1153
+ ],
1154
+ tags: ['v1.0.0'],
1155
+ });
789
1156
 
790
1157
  await exec({
791
- silent: true,
792
1158
  packageFiles: [
793
1159
  {
794
1160
  filename: 'manifest.json',
795
- type: 'json'
796
- }
797
- ]
798
- })
799
-
800
- JSON.parse(fs.readFileSync('manifest.json', 'utf-8')).version.should.equal(
801
- '6.4.0'
802
- )
803
- JSON.parse(fs.readFileSync('package.json', 'utf-8')).version.should.equal(
804
- '6.4.0'
805
- )
806
- JSON.parse(
807
- fs.readFileSync('package-lock.json', 'utf-8')
808
- ).version.should.equal('6.4.0')
809
- })
1161
+ type: 'json',
1162
+ },
1163
+ ],
1164
+ });
1165
+
1166
+ verifyPackageVersion({
1167
+ writeFileSyncSpy,
1168
+ expectedVersion: '6.4.0',
1169
+ filename: 'package.json',
1170
+ });
1171
+ verifyPackageVersion({
1172
+ writeFileSyncSpy,
1173
+ expectedVersion: '6.4.0',
1174
+ filename: 'package-lock.json',
1175
+ });
1176
+ verifyPackageVersion({
1177
+ writeFileSyncSpy,
1178
+ expectedVersion: '6.4.0',
1179
+ filename: 'manifest.json',
1180
+ });
1181
+ });
1182
+
1183
+ it('bumps version in Maven `pom.xml` file with CRLF Line Endings', async function () {
1184
+ const expected = fs.readFileSync(
1185
+ './test/mocks/pom-6.4.0-crlf.xml',
1186
+ 'utf-8',
1187
+ );
1188
+ const filename = 'pom.xml';
1189
+ mock({
1190
+ bump: 'minor',
1191
+ realTestFiles: [
1192
+ {
1193
+ filename,
1194
+ path: './test/mocks/pom-6.3.1-crlf.xml',
1195
+ },
1196
+ ],
1197
+ });
1198
+ await exec({
1199
+ packageFiles: [{ filename, type: 'maven' }],
1200
+ bumpFiles: [{ filename, type: 'maven' }],
1201
+ });
1202
+
1203
+ // filePath is the first arg passed to writeFileSync
1204
+ const packageJsonWriteFileSynchCall = findWriteFileCallForPath({
1205
+ writeFileSyncSpy,
1206
+ filename,
1207
+ });
1208
+
1209
+ if (!packageJsonWriteFileSynchCall) {
1210
+ throw new Error(`writeFileSynch not invoked with path ${filename}`);
1211
+ }
1212
+
1213
+ const calledWithContentStr = packageJsonWriteFileSynchCall[1];
1214
+ expect(calledWithContentStr).toEqual(expected);
1215
+ });
1216
+
1217
+ it('bumps version in Maven `pom.xml` file with LF Line Endings', async function () {
1218
+ const expected = fs.readFileSync(
1219
+ './test/mocks/pom-6.4.0-lf.xml',
1220
+ 'utf-8',
1221
+ );
1222
+ const filename = 'pom.xml';
1223
+ mock({
1224
+ bump: 'minor',
1225
+ realTestFiles: [
1226
+ {
1227
+ filename,
1228
+ path: './test/mocks/pom-6.3.1-lf.xml',
1229
+ },
1230
+ ],
1231
+ });
1232
+ await exec({
1233
+ packageFiles: [{ filename, type: 'maven' }],
1234
+ bumpFiles: [{ filename, type: 'maven' }],
1235
+ });
1236
+
1237
+ // filePath is the first arg passed to writeFileSync
1238
+ const packageJsonWriteFileSynchCall = findWriteFileCallForPath({
1239
+ writeFileSyncSpy,
1240
+ filename,
1241
+ });
1242
+
1243
+ if (!packageJsonWriteFileSynchCall) {
1244
+ throw new Error(`writeFileSynch not invoked with path ${filename}`);
1245
+ }
1246
+
1247
+ const calledWithContentStr = packageJsonWriteFileSynchCall[1];
1248
+ expect(calledWithContentStr).toEqual(expected);
1249
+ });
810
1250
 
811
1251
  it('bumps version in Gradle `build.gradle.kts` file', async function () {
812
- const expected = fs.readFileSync('./test/mocks/build-6.4.0.gradle.kts', 'utf-8')
1252
+ const expected = fs.readFileSync(
1253
+ './test/mocks/build-6.4.0.gradle.kts',
1254
+ 'utf-8',
1255
+ );
1256
+
1257
+ const filename = 'build.gradle.kts';
813
1258
  mock({
814
1259
  bump: 'minor',
815
- fs: {
816
- 'build.gradle.kts': fs.readFileSync('./test/mocks/build-6.3.1.gradle.kts')
817
- }
818
- })
1260
+ realTestFiles: [
1261
+ {
1262
+ filename,
1263
+ path: './test/mocks/build-6.3.1.gradle.kts',
1264
+ },
1265
+ ],
1266
+ });
1267
+
819
1268
  await exec({
820
- packageFiles: [{ filename: 'build.gradle.kts', type: 'gradle' }],
821
- bumpFiles: [{ filename: 'build.gradle.kts', type: 'gradle' }]
822
- })
823
- fs.readFileSync('build.gradle.kts', 'utf-8').should.equal(expected)
824
- })
1269
+ packageFiles: [{ filename, type: 'gradle' }],
1270
+ bumpFiles: [{ filename, type: 'gradle' }],
1271
+ });
1272
+
1273
+ // filePath is the first arg passed to writeFileSync
1274
+ const packageJsonWriteFileSynchCall = findWriteFileCallForPath({
1275
+ writeFileSyncSpy,
1276
+ filename,
1277
+ });
1278
+
1279
+ if (!packageJsonWriteFileSynchCall) {
1280
+ throw new Error(`writeFileSynch not invoked with path ${filename}`);
1281
+ }
1282
+
1283
+ const calledWithContentStr = packageJsonWriteFileSynchCall[1];
1284
+ expect(calledWithContentStr).toEqual(expected);
1285
+ });
825
1286
 
826
1287
  it('bumps version in .NET `Project.csproj` file', async function () {
827
- const expected = fs.readFileSync('./test/mocks/Project-6.4.0.csproj', 'utf-8')
828
- const filename = 'Project.csproj'
1288
+ const expected = fs.readFileSync(
1289
+ './test/mocks/Project-6.4.0.csproj',
1290
+ 'utf-8',
1291
+ );
1292
+ const filename = 'Project.csproj';
829
1293
  mock({
830
1294
  bump: 'minor',
831
- fs: {
832
- [filename]: fs.readFileSync('./test/mocks/Project-6.3.1.csproj')
833
- }
834
- })
1295
+ realTestFiles: [
1296
+ {
1297
+ filename,
1298
+ path: './test/mocks/Project-6.3.1.csproj',
1299
+ },
1300
+ ],
1301
+ });
835
1302
  await exec({
836
1303
  packageFiles: [{ filename, type: 'csproj' }],
837
- bumpFiles: [{ filename, type: 'csproj' }]
838
- })
839
- fs.readFileSync(filename, 'utf-8').should.equal(expected)
840
- })
1304
+ bumpFiles: [{ filename, type: 'csproj' }],
1305
+ });
1306
+
1307
+ // filePath is the first arg passed to writeFileSync
1308
+ const packageJsonWriteFileSynchCall = findWriteFileCallForPath({
1309
+ writeFileSyncSpy,
1310
+ filename,
1311
+ });
1312
+
1313
+ if (!packageJsonWriteFileSynchCall) {
1314
+ throw new Error(`writeFileSynch not invoked with path ${filename}`);
1315
+ }
1316
+
1317
+ const calledWithContentStr = packageJsonWriteFileSynchCall[1];
1318
+ expect(calledWithContentStr).toEqual(expected);
1319
+ });
841
1320
 
842
1321
  it('bumps version # in npm-shrinkwrap.json', async function () {
843
1322
  mock({
844
1323
  bump: 'minor',
845
- fs: {
846
- 'npm-shrinkwrap.json': JSON.stringify({ version: '1.0.0' })
847
- },
848
- tags: ['v1.0.0']
849
- })
850
- await exec()
851
- JSON.parse(
852
- fs.readFileSync('npm-shrinkwrap.json', 'utf-8')
853
- ).version.should.equal('1.1.0')
854
- getPackageVersion().should.equal('1.1.0')
855
- })
1324
+ testFiles: [
1325
+ {
1326
+ path: 'npm-shrinkwrap.json',
1327
+ value: { version: '1.0.0' },
1328
+ },
1329
+ ],
1330
+ tags: ['v1.0.0'],
1331
+ });
1332
+
1333
+ await exec();
1334
+
1335
+ verifyPackageVersion({
1336
+ writeFileSyncSpy,
1337
+ expectedVersion: '1.1.0',
1338
+ filename: 'npm-shrinkwrap.json',
1339
+ });
1340
+ verifyPackageVersion({ writeFileSyncSpy, expectedVersion: '1.1.0' });
1341
+ });
856
1342
 
857
1343
  it('bumps version # in package-lock.json', async function () {
858
1344
  mock({
859
1345
  bump: 'minor',
860
- fs: {
861
- '.gitignore': '',
862
- 'package-lock.json': JSON.stringify({ version: '1.0.0' })
863
- },
864
- tags: ['v1.0.0']
865
- })
866
- await exec()
867
- JSON.parse(
868
- fs.readFileSync('package-lock.json', 'utf-8')
869
- ).version.should.equal('1.1.0')
870
- getPackageVersion().should.equal('1.1.0')
871
- })
1346
+ testFiles: [
1347
+ {
1348
+ path: '.gitignore',
1349
+ value: '',
1350
+ },
1351
+ {
1352
+ path: 'package-lock.json',
1353
+ value: { version: '1.0.0' },
1354
+ },
1355
+ ],
1356
+ tags: ['v1.0.0'],
1357
+ });
1358
+ await exec();
1359
+
1360
+ verifyPackageVersion({
1361
+ writeFileSyncSpy,
1362
+ expectedVersion: '1.1.0',
1363
+ filename: 'package-lock.json',
1364
+ });
1365
+ verifyPackageVersion({ writeFileSyncSpy, expectedVersion: '1.1.0' });
1366
+ });
872
1367
 
873
1368
  describe('skip', function () {
874
1369
  it('allows bump and changelog generation to be skipped', async function () {
875
- const changelogContent = 'legacy header format<a name="1.0.0">\n'
1370
+ const changelogContent = 'legacy header format<a name="1.0.0">\n';
876
1371
  mock({
877
1372
  bump: 'minor',
878
1373
  changelog: 'foo\n',
879
- fs: { 'CHANGELOG.md': changelogContent }
880
- })
1374
+ existingChangelog: changelogContent,
1375
+ });
1376
+
1377
+ await exec('--skip.bump true --skip.changelog true');
881
1378
 
882
- await exec('--skip.bump true --skip.changelog true')
883
- getPackageVersion().should.equal('1.0.0')
884
- const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
885
- content.should.equal(changelogContent)
886
- })
887
- })
1379
+ expect(writeFileSyncSpy).not.toHaveBeenCalledWith('package.json');
1380
+ expect(writeFileSyncSpy).not.toHaveBeenCalledWith('CHANGELOG.md');
1381
+ });
1382
+ });
888
1383
 
889
1384
  it('does not update files present in .gitignore', async function () {
1385
+ const DotGitIgnore = require('dotgitignore');
1386
+ jest.mock('dotgitignore');
1387
+
1388
+ DotGitIgnore.mockImplementation(() => {
1389
+ return {
1390
+ ignore: (filename) => {
1391
+ if (filename === 'package-lock.json' || filename === 'bower.json') {
1392
+ return true;
1393
+ }
1394
+
1395
+ return false;
1396
+ },
1397
+ };
1398
+ });
1399
+
890
1400
  mock({
891
1401
  bump: 'minor',
892
- fs: {
893
- '.gitignore': 'package-lock.json\nbower.json',
894
- // test a defaults.packageFiles
895
- 'bower.json': JSON.stringify({ version: '1.0.0' }),
896
- // test a defaults.bumpFiles
897
- 'package-lock.json': JSON.stringify({
898
- name: '@org/package',
899
- version: '1.0.0',
900
- lockfileVersion: 1
901
- })
902
- },
903
- tags: ['v1.0.0']
904
- })
905
- await exec()
906
- JSON.parse(
907
- fs.readFileSync('package-lock.json', 'utf-8')
908
- ).version.should.equal('1.0.0')
909
- JSON.parse(fs.readFileSync('bower.json', 'utf-8')).version.should.equal(
910
- '1.0.0'
911
- )
912
- getPackageVersion().should.equal('1.1.0')
913
- })
1402
+ testFiles: [
1403
+ {
1404
+ path: 'bower.json',
1405
+ value: { version: '1.0.0' },
1406
+ },
1407
+ {
1408
+ path: 'package-lock.json',
1409
+ value: {
1410
+ name: '@org/package',
1411
+ version: '1.0.0',
1412
+ lockfileVersion: 1,
1413
+ },
1414
+ },
1415
+ ],
1416
+ tags: ['v1.0.0'],
1417
+ });
1418
+ await exec();
1419
+
1420
+ // does not bump these as in .gitignore
1421
+ expect(writeFileSyncSpy).not.toHaveBeenCalledWith('package-lock.json');
1422
+ expect(writeFileSyncSpy).not.toHaveBeenCalledWith('bower.json');
1423
+
1424
+ // should still bump version in package.json
1425
+ verifyPackageVersion({ writeFileSyncSpy, expectedVersion: '1.1.0' });
1426
+
1427
+ DotGitIgnore.mockRestore();
1428
+ });
914
1429
 
915
1430
  describe('configuration', function () {
916
1431
  it('--header', async function () {
917
- mock({ bump: 'minor', fs: { 'CHANGELOG.md': '' } })
918
- await exec('--header="# Welcome to our CHANGELOG.md"')
919
- const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
920
- content.should.match(/# Welcome to our CHANGELOG.md/)
921
- })
1432
+ mock({ bump: 'minor', existingChangelog: '' });
1433
+ await exec('--header="# Welcome to our CHANGELOG.md"');
1434
+
1435
+ verifyNewChangelogContentMatches({
1436
+ writeFileSyncSpy,
1437
+ expectedContent: /# Welcome to our CHANGELOG.md/,
1438
+ });
1439
+ });
922
1440
 
923
1441
  it('--issuePrefixes and --issueUrlFormat', async function () {
924
- const format = 'http://www.foo.com/{{prefix}}{{id}}'
925
- const prefix = 'ABC-'
1442
+ const format = 'http://www.foo.com/{{prefix}}{{id}}';
1443
+ const prefix = 'ABC-';
926
1444
  const changelog = ({ preset }) =>
927
- preset.issueUrlFormat + ':' + preset.issuePrefixes
928
- mock({ bump: 'minor', changelog })
929
- await exec(`--issuePrefixes="${prefix}" --issueUrlFormat="${format}"`)
930
- const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
931
- content.should.include(`${format}:${prefix}`)
932
- })
933
- })
1445
+ preset.issueUrlFormat + ':' + preset.issuePrefixes;
1446
+ mock({ bump: 'minor', changelog });
1447
+ await exec(`--issuePrefixes="${prefix}" --issueUrlFormat="${format}"`);
1448
+
1449
+ verifyNewChangelogContentMatches({
1450
+ writeFileSyncSpy,
1451
+ expectedContent: `${format}:${prefix}`,
1452
+ });
1453
+ });
1454
+ });
934
1455
 
935
1456
  describe('pre-major', function () {
936
1457
  it('bumps the minor rather than major, if version < 1.0.0', async function () {
937
1458
  mock({
938
1459
  bump: 'minor',
939
- pkg: {
940
- version: '0.5.0',
941
- repository: { url: 'https://github.com/yargs/yargs.git' }
942
- }
943
- })
944
- await exec()
945
- getPackageVersion().should.equal('0.6.0')
946
- })
1460
+ testFiles: [
1461
+ {
1462
+ path: 'package.json',
1463
+ value: {
1464
+ version: '0.5.0',
1465
+ repository: { url: 'https://github.com/yargs/yargs.git' },
1466
+ },
1467
+ },
1468
+ ],
1469
+ });
1470
+ await exec();
1471
+ verifyPackageVersion({ writeFileSyncSpy, expectedVersion: '0.6.0' });
1472
+ });
947
1473
 
948
1474
  it('bumps major if --release-as=major specified, if version < 1.0.0', async function () {
949
1475
  mock({
950
1476
  bump: 'major',
951
- pkg: {
952
- version: '0.5.0',
953
- repository: { url: 'https://github.com/yargs/yargs.git' }
954
- }
955
- })
956
- await exec('-r major')
957
- getPackageVersion().should.equal('1.0.0')
958
- })
959
- })
960
- })
1477
+ testFiles: [
1478
+ {
1479
+ path: 'package.json',
1480
+ value: {
1481
+ version: '0.5.0',
1482
+ repository: { url: 'https://github.com/yargs/yargs.git' },
1483
+ },
1484
+ },
1485
+ ],
1486
+ });
1487
+ await exec('-r major');
1488
+ verifyPackageVersion({ writeFileSyncSpy, expectedVersion: '1.0.0' });
1489
+ });
1490
+ });
1491
+ });
961
1492
 
962
1493
  describe('GHSL-2020-111', function () {
963
- afterEach(unmock)
1494
+ afterEach(unmock);
964
1495
 
965
1496
  it('does not allow command injection via basic configuration', async function () {
966
- mock({ bump: 'patch' })
1497
+ mock({ bump: 'patch' });
967
1498
  await exec({
968
1499
  noVerify: true,
969
- infile: 'foo.txt',
970
- releaseCommitMessageFormat: 'bla `touch exploit`'
971
- })
972
- const stat = shell.test('-f', './exploit')
973
- stat.should.equal(false)
974
- })
975
- })
1500
+ releaseCommitMessageFormat: 'bla `touch exploit`',
1501
+ });
1502
+ const stat = shell.test('-f', './exploit');
1503
+ expect(stat).toEqual(false);
1504
+ });
1505
+ });
976
1506
 
977
1507
  describe('with mocked git', function () {
978
- afterEach(unmock)
1508
+ afterEach(unmock);
979
1509
 
980
1510
  it('--sign signs the commit and tag', async function () {
981
1511
  const gitArgs = [
982
- ['add', 'CHANGELOG.md', 'package.json'],
1512
+ ['add', 'CHANGELOG.md', 'package.json', 'package-lock.json'],
983
1513
  [
984
1514
  'commit',
985
1515
  '-S',
986
1516
  'CHANGELOG.md',
987
1517
  'package.json',
1518
+ 'package-lock.json',
988
1519
  '-m',
989
- 'chore(release): 1.0.1'
1520
+ 'chore(release): 1.0.1',
990
1521
  ],
991
1522
  ['tag', '-s', 'v1.0.1', '-m', 'chore(release): 1.0.1'],
992
- ['rev-parse', '--abbrev-ref', 'HEAD']
993
- ]
994
- const execFile = (_args, cmd, cmdArgs) => {
995
- cmd.should.equal('git')
996
- const expected = gitArgs.shift()
997
- cmdArgs.should.deep.equal(expected)
998
- if (expected[0] === 'rev-parse') return Promise.resolve('master')
999
- return Promise.resolve('')
1000
- }
1001
- mock({ bump: 'patch', changelog: 'foo\n', execFile })
1523
+ ['rev-parse', '--abbrev-ref', 'HEAD'],
1524
+ ];
1525
+
1526
+ runExecFile.mockImplementation((_args, cmd, cmdArgs) => {
1527
+ expect(cmd).toEqual('git');
1528
+
1529
+ const expected = gitArgs.shift();
1530
+ expect(cmdArgs).toEqual(expected);
1531
+
1532
+ if (expected[0] === 'rev-parse') return Promise.resolve('master');
1533
+
1534
+ return Promise.resolve('');
1535
+ });
1536
+
1537
+ mock({
1538
+ bump: 'patch',
1539
+ changelog: 'foo\n',
1540
+ });
1002
1541
 
1003
- await exec('--sign', true)
1004
- gitArgs.should.have.lengthOf(0)
1005
- })
1542
+ await exec('--sign', true);
1543
+ expect(gitArgs).toHaveLength(0);
1544
+ });
1545
+
1546
+ it('--signedoff adds signed-off-by to the commit message', async function () {
1547
+ const gitArgs = [
1548
+ ['add', 'CHANGELOG.md', 'package.json', 'package-lock.json'],
1549
+ [
1550
+ 'commit',
1551
+ '--signoff',
1552
+ 'CHANGELOG.md',
1553
+ 'package.json',
1554
+ 'package-lock.json',
1555
+ '-m',
1556
+ 'chore(release): 1.0.1',
1557
+ ],
1558
+ ['tag', '-a', 'v1.0.1', '-m', 'chore(release): 1.0.1'],
1559
+ ['rev-parse', '--abbrev-ref', 'HEAD'],
1560
+ ];
1561
+
1562
+ runExecFile.mockImplementation((_args, cmd, cmdArgs) => {
1563
+ expect(cmd).toEqual('git');
1564
+
1565
+ const expected = gitArgs.shift();
1566
+ expect(cmdArgs).toEqual(expected);
1567
+
1568
+ if (expected[0] === 'rev-parse') return Promise.resolve('master');
1569
+
1570
+ return Promise.resolve('');
1571
+ });
1572
+
1573
+ mock({
1574
+ bump: 'patch',
1575
+ changelog: 'foo\n',
1576
+ });
1577
+
1578
+ await exec('--signoff', true);
1579
+ expect(gitArgs).toHaveLength(0);
1580
+ });
1006
1581
 
1007
1582
  it('--tag-force forces tag replacement', async function () {
1008
1583
  const gitArgs = [
1009
- ['add', 'CHANGELOG.md', 'package.json'],
1010
- ['commit', 'CHANGELOG.md', 'package.json', '-m', 'chore(release): 1.0.1'],
1584
+ ['add', 'CHANGELOG.md', 'package.json', 'package-lock.json'],
1585
+ [
1586
+ 'commit',
1587
+ 'CHANGELOG.md',
1588
+ 'package.json',
1589
+ 'package-lock.json',
1590
+ '-m',
1591
+ 'chore(release): 1.0.1',
1592
+ ],
1011
1593
  ['tag', '-a', '-f', 'v1.0.1', '-m', 'chore(release): 1.0.1'],
1012
- ['rev-parse', '--abbrev-ref', 'HEAD']
1013
- ]
1014
- const execFile = (_args, cmd, cmdArgs) => {
1015
- cmd.should.equal('git')
1016
- const expected = gitArgs.shift()
1017
- cmdArgs.should.deep.equal(expected)
1018
- if (expected[0] === 'rev-parse') return Promise.resolve('master')
1019
- return Promise.resolve('')
1020
- }
1021
- mock({ bump: 'patch', changelog: 'foo\n', execFile })
1594
+ ['rev-parse', '--abbrev-ref', 'HEAD'],
1595
+ ];
1596
+
1597
+ runExecFile.mockImplementation((_args, cmd, cmdArgs) => {
1598
+ expect(cmd).toEqual('git');
1599
+
1600
+ const expected = gitArgs.shift();
1601
+ expect(cmdArgs).toEqual(expected);
1602
+
1603
+ if (expected[0] === 'rev-parse') return Promise.resolve('master');
1022
1604
 
1023
- await exec('--tag-force', true)
1024
- gitArgs.should.have.lengthOf(0)
1025
- })
1605
+ return Promise.resolve('');
1606
+ });
1607
+
1608
+ mock({ bump: 'patch', changelog: 'foo\n' });
1609
+
1610
+ await exec('--tag-force', true);
1611
+ expect(gitArgs).toHaveLength(0);
1612
+ });
1026
1613
 
1027
1614
  it('fails if git add fails', async function () {
1028
- const gitArgs = [['add', 'CHANGELOG.md', 'package.json']]
1029
- const gitError = new Error('Command failed: git\nfailed add')
1030
- const execFile = (_args, cmd, cmdArgs) => {
1031
- cmd.should.equal('git')
1032
- const expected = gitArgs.shift()
1033
- cmdArgs.should.deep.equal(expected)
1615
+ const gitArgs = [
1616
+ ['add', 'CHANGELOG.md', 'package.json', 'package-lock.json'],
1617
+ ];
1618
+ const gitError = new Error('Command failed: git\nfailed add');
1619
+
1620
+ runExecFile.mockImplementation((_args, cmd, cmdArgs) => {
1621
+ expect(cmd).toEqual('git');
1622
+
1623
+ const expected = gitArgs.shift();
1624
+ expect(cmdArgs).toEqual(expected);
1034
1625
 
1035
1626
  if (expected[0] === 'add') {
1036
- return Promise.reject(gitError)
1627
+ return Promise.reject(gitError);
1037
1628
  }
1038
- return Promise.resolve('')
1039
- }
1040
- mock({ bump: 'patch', changelog: 'foo\n', execFile })
1629
+ return Promise.resolve('');
1630
+ });
1631
+
1632
+ mock({ bump: 'patch', changelog: 'foo\n' });
1041
1633
 
1042
- await expect(exec({}, true)).to.be.rejectedWith(gitError)
1043
- })
1634
+ await expect(exec({}, true)).rejects.toThrow(gitError);
1635
+ });
1044
1636
 
1045
1637
  it('fails if git commit fails', async function () {
1046
1638
  const gitArgs = [
1047
- ['add', 'CHANGELOG.md', 'package.json'],
1048
- ['commit', 'CHANGELOG.md', 'package.json', '-m', 'chore(release): 1.0.1']
1049
- ]
1050
- const gitError = new Error('Command failed: git\nfailed commit')
1051
- const execFile = (_args, cmd, cmdArgs) => {
1052
- cmd.should.equal('git')
1053
- const expected = gitArgs.shift()
1054
- cmdArgs.should.deep.equal(expected)
1639
+ ['add', 'CHANGELOG.md', 'package.json', 'package-lock.json'],
1640
+ [
1641
+ 'commit',
1642
+ 'CHANGELOG.md',
1643
+ 'package.json',
1644
+ 'package-lock.json',
1645
+ '-m',
1646
+ 'chore(release): 1.0.1',
1647
+ ],
1648
+ ];
1649
+ const gitError = new Error('Command failed: git\nfailed commit');
1650
+
1651
+ runExecFile.mockImplementation((_args, cmd, cmdArgs) => {
1652
+ expect(cmd).toEqual('git');
1653
+
1654
+ const expected = gitArgs.shift();
1655
+ expect(cmdArgs).toEqual(expected);
1656
+
1055
1657
  if (expected[0] === 'commit') {
1056
- return Promise.reject(gitError)
1658
+ return Promise.reject(gitError);
1057
1659
  }
1058
- return Promise.resolve('')
1059
- }
1060
- mock({ bump: 'patch', changelog: 'foo\n', execFile })
1660
+ return Promise.resolve('');
1661
+ });
1061
1662
 
1062
- await expect(exec({}, true)).to.be.rejectedWith(gitError)
1063
- })
1663
+ mock({ bump: 'patch', changelog: 'foo\n' });
1664
+
1665
+ await expect(exec({}, true)).rejects.toThrow(gitError);
1666
+ });
1064
1667
 
1065
1668
  it('fails if git tag fails', async function () {
1066
1669
  const gitArgs = [
1067
- ['add', 'CHANGELOG.md', 'package.json'],
1068
- ['commit', 'CHANGELOG.md', 'package.json', '-m', 'chore(release): 1.0.1'],
1069
- ['tag', '-a', 'v1.0.1', '-m', 'chore(release): 1.0.1']
1070
- ]
1071
- const gitError = new Error('Command failed: git\nfailed tag')
1072
- const execFile = (_args, cmd, cmdArgs) => {
1073
- cmd.should.equal('git')
1074
- const expected = gitArgs.shift()
1075
- cmdArgs.should.deep.equal(expected)
1670
+ ['add', 'CHANGELOG.md', 'package.json', 'package-lock.json'],
1671
+ [
1672
+ 'commit',
1673
+ 'CHANGELOG.md',
1674
+ 'package.json',
1675
+ 'package-lock.json',
1676
+ '-m',
1677
+ 'chore(release): 1.0.1',
1678
+ ],
1679
+ ['tag', '-a', 'v1.0.1', '-m', 'chore(release): 1.0.1'],
1680
+ ];
1681
+ const gitError = new Error('Command failed: git\nfailed tag');
1682
+
1683
+ runExecFile.mockImplementation((_args, cmd, cmdArgs) => {
1684
+ expect(cmd).toEqual('git');
1685
+
1686
+ const expected = gitArgs.shift();
1687
+ expect(cmdArgs).toEqual(expected);
1688
+
1076
1689
  if (expected[0] === 'tag') {
1077
- return Promise.reject(gitError)
1690
+ return Promise.reject(gitError);
1078
1691
  }
1079
- return Promise.resolve('')
1692
+ return Promise.resolve('');
1693
+ });
1694
+
1695
+ mock({ bump: 'patch', changelog: 'foo\n' });
1696
+
1697
+ await expect(exec({}, true)).rejects.toThrow(gitError);
1698
+ });
1699
+ });
1700
+
1701
+ // ------- Verifiers ------
1702
+ function findWriteFileCallForPath({ writeFileSyncSpy, filename }) {
1703
+ // filePath is the first arg passed to writeFileSync
1704
+ return writeFileSyncSpy.mock.calls.find((args) =>
1705
+ args[0].includes(filename),
1706
+ );
1707
+ }
1708
+
1709
+ function verifyPackageVersion({
1710
+ writeFileSyncSpy,
1711
+ expectedVersion,
1712
+ filename = 'package.json',
1713
+ asString = false,
1714
+ }) {
1715
+ // filePath is the first arg passed to writeFileSync
1716
+ const packageJsonWriteFileSynchCall = findWriteFileCallForPath({
1717
+ writeFileSyncSpy,
1718
+ filename,
1719
+ });
1720
+
1721
+ if (!packageJsonWriteFileSynchCall) {
1722
+ throw new Error(`writeFileSynch not invoked with path ${filename}`);
1723
+ }
1724
+
1725
+ const calledWithContentStr = packageJsonWriteFileSynchCall[1];
1726
+ if (!asString) {
1727
+ // parse to JSON and verify has property
1728
+ const calledWithContent = JSON.parse(calledWithContentStr);
1729
+
1730
+ expect(calledWithContent).toHaveProperty('version');
1731
+ expect(calledWithContent.version).toEqual(expectedVersion);
1732
+ } else {
1733
+ // for non-JSON files i.e. .exs and .txt just verify version exists
1734
+ if (filename.includes('.exs')) {
1735
+ expect(calledWithContentStr).toMatch(`version: "${expectedVersion}"`);
1736
+ } else {
1737
+ expect(calledWithContentStr).toMatch(expectedVersion);
1080
1738
  }
1081
- mock({ bump: 'patch', changelog: 'foo\n', execFile })
1739
+ }
1740
+ }
1741
+
1742
+ function verifyFileContentEquals({
1743
+ writeFileSyncSpy,
1744
+ content,
1745
+ filename = 'package.json',
1746
+ }) {
1747
+ // filePath is the first arg passed to writeFileSync
1748
+ const packageJsonWriteFileSynchCall = findWriteFileCallForPath({
1749
+ writeFileSyncSpy,
1750
+ filename,
1751
+ });
1752
+
1753
+ if (!packageJsonWriteFileSynchCall) {
1754
+ throw new Error('writeFileSynch not invoked with path package.json');
1755
+ }
1756
+
1757
+ const calledWithContentStr = packageJsonWriteFileSynchCall[1];
1758
+
1759
+ expect(calledWithContentStr).toEqual(content);
1760
+ }
1082
1761
 
1083
- await expect(exec({}, true)).to.be.rejectedWith(gitError)
1084
- })
1085
- })
1086
- })
1762
+ function verifyNewChangelogContentMatches({
1763
+ writeFileSyncSpy,
1764
+ expectedContent,
1765
+ }) {
1766
+ const changelogWriteFileSynchCall = findWriteFileCallForPath({
1767
+ writeFileSyncSpy,
1768
+ filename: 'CHANGELOG.md',
1769
+ });
1770
+
1771
+ if (!changelogWriteFileSynchCall) {
1772
+ throw new Error('writeFileSynch not invoked with path CHANGELOG.md');
1773
+ }
1774
+
1775
+ const calledWithContent = changelogWriteFileSynchCall[1];
1776
+ expect(calledWithContent).toMatch(expectedContent);
1777
+ }
1778
+
1779
+ function verifyNewChangelogContentEquals({
1780
+ writeFileSyncSpy,
1781
+ expectedContent,
1782
+ }) {
1783
+ const changelogWriteFileSynchCall = findWriteFileCallForPath({
1784
+ writeFileSyncSpy,
1785
+ filename: 'CHANGELOG.md',
1786
+ });
1787
+
1788
+ if (!changelogWriteFileSynchCall) {
1789
+ throw new Error('writeFileSynch not invoked with path CHANGELOG.md');
1790
+ }
1791
+
1792
+ const calledWithContent = changelogWriteFileSynchCall[1];
1793
+ expect(calledWithContent).toEqual(expectedContent);
1794
+ }
1795
+
1796
+ function verifyNewChangelogContentDoesNotMatch({
1797
+ writeFileSyncSpy,
1798
+ expectedContent,
1799
+ }) {
1800
+ const changelogWriteFileSynchCall = findWriteFileCallForPath({
1801
+ writeFileSyncSpy,
1802
+ filename: 'CHANGELOG.md',
1803
+ });
1804
+
1805
+ if (!changelogWriteFileSynchCall) {
1806
+ throw new Error('writeFileSynch not invoked with path CHANGELOG.md');
1807
+ }
1808
+
1809
+ const calledWithContent = changelogWriteFileSynchCall[1];
1810
+ expect(calledWithContent).not.toMatch(expectedContent);
1811
+ }
1812
+
1813
+ function verifyLogPrinted({ consoleInfoSpy, expectedLog }) {
1814
+ const consoleInfoLogs = consoleInfoSpy.mock.calls.map((args) => args[0]);
1815
+ const desiredLog = consoleInfoLogs.find((log) => log.includes(expectedLog));
1816
+ expect(desiredLog).not.toBeUndefined();
1817
+ expect(desiredLog).toMatch(expectedLog);
1818
+ }
1819
+ });