commit-and-tag-version 12.7.3 → 13.1.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.
- package/.github/workflows/ci.yaml +8 -15
- package/.github/workflows/release-please.yml +2 -2
- package/CHANGELOG.md +29 -0
- package/MIGRATION.md +51 -0
- package/README.md +25 -24
- package/bin/cli.js +2 -2
- package/command.js +9 -4
- package/defaults.js +5 -3
- package/eslint.config.mjs +52 -42
- package/index.js +12 -12
- package/lib/checkpoint.js +5 -5
- package/lib/configuration.js +21 -8
- package/lib/detect-package-manager.js +4 -8
- package/lib/format-commit-message.js +2 -2
- package/lib/latest-semver-tag.js +30 -30
- package/lib/lifecycles/bump.js +97 -65
- package/lib/lifecycles/changelog.js +48 -60
- package/lib/lifecycles/commit.js +8 -8
- package/lib/lifecycles/tag.js +10 -10
- package/lib/preset-loader.js +73 -9
- package/lib/print-error.js +3 -3
- package/lib/run-exec.js +6 -5
- package/lib/run-execFile.js +6 -5
- package/lib/run-lifecycle-script.js +6 -6
- package/lib/stringify-package.js +1 -5
- package/lib/updaters/index.js +25 -12
- package/lib/updaters/types/csproj.js +4 -4
- package/lib/updaters/types/gradle.js +4 -4
- package/lib/updaters/types/json.js +9 -9
- package/lib/updaters/types/maven.js +10 -6
- package/lib/updaters/types/openapi.js +6 -6
- package/lib/updaters/types/plain-text.js +4 -4
- package/lib/updaters/types/python.js +4 -4
- package/lib/updaters/types/yaml.js +6 -6
- package/lib/write-file.js +3 -3
- package/package.json +20 -15
- package/test/commit-message-config.integration-test.js +23 -10
- package/test/config-files.integration-test.js +25 -10
- package/test/config-keys.integration-test.js +20 -7
- package/test/core.spec.js +82 -37
- package/test/esm-config.integration-test.js +97 -0
- package/test/git.integration-test.js +23 -15
- package/test/invalid-config.integration-test.js +19 -9
- package/test/mocks/pom-6.3.1-attrs-lf.xml +1 -0
- package/test/mocks/pom-6.4.0-attrs-lf.xml +1 -0
- package/test/mocks/vitest-mocks.js +260 -0
- package/test/package.json +3 -0
- package/test/pre-commit-hook.integration-test.js +22 -14
- package/test/preset.integration-test.js +7 -5
- package/test/setup-coverage.js +47 -0
- package/test/stringify-package.spec.js +1 -3
- package/test/utils.spec.js +3 -4
- package/vitest.config.js +26 -0
- package/jest.config.js +0 -28
- package/test/mocks/jest-mocks.js +0 -66
package/test/core.spec.js
CHANGED
|
@@ -1,29 +1,64 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import shell from 'shelljs';
|
|
2
|
+
import stripAnsi from 'strip-ansi';
|
|
3
|
+
|
|
4
|
+
// Set up CJS require() hook + create mock fns (must be before any source imports)
|
|
5
|
+
const mockers = vi.hoisted(() =>
|
|
6
|
+
require('./mocks/vitest-mocks').setup({ mockRunExecFile: true }),
|
|
7
|
+
);
|
|
8
|
+
|
|
9
|
+
// Register vi.mock for ESM imports in this test file
|
|
10
|
+
vi.mock('conventional-changelog', () => mockers.conventionalChangelogModule());
|
|
11
|
+
vi.mock('conventional-recommended-bump', async (importOriginal) =>
|
|
12
|
+
mockers.conventionalRecommendedBumpModule(
|
|
13
|
+
await importOriginal(),
|
|
14
|
+
await import('@conventional-changelog/git-client'),
|
|
15
|
+
),
|
|
16
|
+
);
|
|
17
|
+
vi.mock('@conventional-changelog/git-client', async (importOriginal) =>
|
|
18
|
+
mockers.gitClientModule(await importOriginal()),
|
|
19
|
+
);
|
|
20
|
+
vi.mock('../lib/run-execFile', () => ({ default: mockers.runExecFile }));
|
|
21
|
+
// Now that the source is ESM, this mock applies to the source's import of
|
|
22
|
+
// dotgitignore too (under CJS it only affected this file's import), so it
|
|
23
|
+
// needs a working default implementation rather than a bare automock.
|
|
24
|
+
vi.mock('dotgitignore', () => ({
|
|
25
|
+
default: vi.fn(() => ({ ignore: () => false })),
|
|
26
|
+
}));
|
|
27
|
+
|
|
28
|
+
// Mock fs at the Vitest ESM level so spies work for both test and source code.
|
|
29
|
+
vi.mock('fs', async (importOriginal) => {
|
|
30
|
+
const actual = await importOriginal();
|
|
31
|
+
const proxy = mockers.fsProxy;
|
|
32
|
+
return {
|
|
33
|
+
...actual,
|
|
34
|
+
default: proxy,
|
|
35
|
+
readFileSync: (...args) => proxy.readFileSync(...args),
|
|
36
|
+
writeFileSync: (...args) => proxy.writeFileSync(...args),
|
|
37
|
+
lstatSync: (...args) => proxy.lstatSync(...args),
|
|
38
|
+
chmodSync: (...args) => proxy.chmodSync(...args),
|
|
39
|
+
promises: actual.promises,
|
|
40
|
+
};
|
|
41
|
+
});
|
|
8
42
|
|
|
9
|
-
const runExecFile =
|
|
43
|
+
const runExecFile = mockers.runExecFile;
|
|
10
44
|
|
|
11
|
-
|
|
12
|
-
|
|
45
|
+
import cli from '../command';
|
|
46
|
+
import formatCommitMessage from '../lib/format-commit-message';
|
|
47
|
+
import standardVersion from '../index';
|
|
48
|
+
import { header as defaultHeader } from '../defaults';
|
|
49
|
+
import DotGitIgnore from 'dotgitignore';
|
|
50
|
+
import fs from 'fs';
|
|
13
51
|
|
|
14
52
|
// set by mock()
|
|
15
|
-
let standardVersion;
|
|
16
53
|
let readFileSyncSpy;
|
|
17
54
|
let lstatSyncSpy;
|
|
55
|
+
let writeFileSyncSpy = vi
|
|
56
|
+
.spyOn(fs, 'writeFileSync')
|
|
57
|
+
.mockImplementation(() => {});
|
|
18
58
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const consoleErrorSpy = jest.spyOn(console, 'warn').mockImplementation();
|
|
24
|
-
const consoleInfoSpy = jest.spyOn(console, 'info').mockImplementation();
|
|
25
|
-
|
|
26
|
-
jest.mock('../lib/run-execFile');
|
|
59
|
+
const consoleErrorSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
60
|
+
const consoleInfoSpy = vi.spyOn(console, 'info').mockImplementation(() => {});
|
|
61
|
+
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
27
62
|
|
|
28
63
|
const { readFileSync: readFileSyncActual, lstatSync: lstatSyncActual } = fs;
|
|
29
64
|
|
|
@@ -54,7 +89,14 @@ const mockReadFilesFromDisk = ({
|
|
|
54
89
|
testFiles,
|
|
55
90
|
realTestFiles,
|
|
56
91
|
}) =>
|
|
57
|
-
|
|
92
|
+
vi.spyOn(fs, 'readFileSync').mockImplementation((path, opts) => {
|
|
93
|
+
// The ESM module loader reads files through fs with non-string paths
|
|
94
|
+
// (URL/Buffer/fd). Those aren't test fixtures, so pass them straight to
|
|
95
|
+
// real fs; only string paths get the fixture handling below.
|
|
96
|
+
if (typeof path !== 'string') {
|
|
97
|
+
return readFileSyncActual(path, opts);
|
|
98
|
+
}
|
|
99
|
+
|
|
58
100
|
if (path === 'CHANGELOG.md') {
|
|
59
101
|
if (existingChangelog) {
|
|
60
102
|
return existingChangelog;
|
|
@@ -113,7 +155,13 @@ const mockReadFilesFromDisk = ({
|
|
|
113
155
|
* @return Jest spy on lstatSync
|
|
114
156
|
*/
|
|
115
157
|
const mockFsLStat = ({ fs, lstatSyncActual, testFiles, realTestFiles }) =>
|
|
116
|
-
|
|
158
|
+
vi.spyOn(fs, 'lstatSync').mockImplementation((path) => {
|
|
159
|
+
// See readFileSync mock: non-string paths come from the module loader,
|
|
160
|
+
// not the test fixtures, so delegate them to real fs.
|
|
161
|
+
if (typeof path !== 'string') {
|
|
162
|
+
return lstatSyncActual(path);
|
|
163
|
+
}
|
|
164
|
+
|
|
117
165
|
if (testFiles) {
|
|
118
166
|
const file = testFiles.find((otherFile) => {
|
|
119
167
|
return path.includes(otherFile.path);
|
|
@@ -179,9 +227,6 @@ function mock({
|
|
|
179
227
|
commits,
|
|
180
228
|
});
|
|
181
229
|
|
|
182
|
-
// needs to be set after mockery, but before mock-fs
|
|
183
|
-
standardVersion = require('../index');
|
|
184
|
-
|
|
185
230
|
// For fake and injected test files pretend they exist at root level when Fs queries lstat
|
|
186
231
|
// Package.json works without this as it'll check the one in this actual repo...
|
|
187
232
|
lstatSyncSpy = mockFsLStat({
|
|
@@ -200,7 +245,7 @@ function mock({
|
|
|
200
245
|
});
|
|
201
246
|
|
|
202
247
|
// Spies on writeFileSync to capture calls and ensure we don't actually try write anything to disc
|
|
203
|
-
writeFileSyncSpy =
|
|
248
|
+
writeFileSyncSpy = vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {});
|
|
204
249
|
}
|
|
205
250
|
|
|
206
251
|
function clearCapturedSpyCalls() {
|
|
@@ -212,14 +257,17 @@ function restoreMocksToRealImplementation() {
|
|
|
212
257
|
readFileSyncSpy.mockRestore();
|
|
213
258
|
writeFileSyncSpy.mockRestore();
|
|
214
259
|
lstatSyncSpy.mockRestore();
|
|
260
|
+
// Immediately re-protect against writes after restoring
|
|
261
|
+
writeFileSyncSpy = vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {});
|
|
215
262
|
}
|
|
216
263
|
|
|
217
264
|
function unmock() {
|
|
218
265
|
clearCapturedSpyCalls();
|
|
219
266
|
|
|
220
|
-
|
|
267
|
+
// Reset runExecFile mock between tests to prevent state leakage
|
|
268
|
+
runExecFile.mockReset();
|
|
221
269
|
|
|
222
|
-
|
|
270
|
+
restoreMocksToRealImplementation();
|
|
223
271
|
}
|
|
224
272
|
|
|
225
273
|
describe('format-commit-message', function () {
|
|
@@ -314,7 +362,7 @@ describe('cli', function () {
|
|
|
314
362
|
});
|
|
315
363
|
|
|
316
364
|
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 () {
|
|
317
|
-
const
|
|
365
|
+
const header = defaultHeader;
|
|
318
366
|
|
|
319
367
|
const standardVersionHeader =
|
|
320
368
|
'# 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.';
|
|
@@ -346,7 +394,7 @@ describe('cli', function () {
|
|
|
346
394
|
});
|
|
347
395
|
|
|
348
396
|
it('appends the new release above the last release, removing the old header (new format), and retains any front matter', async function () {
|
|
349
|
-
const
|
|
397
|
+
const header = defaultHeader;
|
|
350
398
|
const frontMatter = '---\nstatus: new\n---\n';
|
|
351
399
|
|
|
352
400
|
const changelog101 =
|
|
@@ -374,7 +422,7 @@ describe('cli', function () {
|
|
|
374
422
|
});
|
|
375
423
|
|
|
376
424
|
it('appends the new release above the last release, removing the old header (new format)', async function () {
|
|
377
|
-
const
|
|
425
|
+
const header = defaultHeader;
|
|
378
426
|
const changelog1 =
|
|
379
427
|
'### [1.0.1](/compare/v1.0.0...v1.0.1) (YYYY-MM-DD)\n\n\n### Bug Fixes\n\n* patch release ABCDEFXY\n';
|
|
380
428
|
mock({ bump: 'patch', changelog: changelog1, tags: ['v1.0.0'] });
|
|
@@ -921,7 +969,7 @@ describe('cli', function () {
|
|
|
921
969
|
|
|
922
970
|
it('bumps using actual recommendedBump when there are no semver relevant changes and --noBumpWhenEmptyChanges flag is not set', async function () {
|
|
923
971
|
mock({
|
|
924
|
-
bump:
|
|
972
|
+
bump: mockers.actualConventionalRecommendedBump,
|
|
925
973
|
changelog: ['chore release\n'],
|
|
926
974
|
tags: ['v1.0.0'],
|
|
927
975
|
commits: ['chore: update deps\n\n-hash-\nabc123\n'],
|
|
@@ -934,7 +982,7 @@ describe('cli', function () {
|
|
|
934
982
|
|
|
935
983
|
it('bumps using actual recommendedBump when there are semver relevant changes and --noBumpWhenEmptyChanges flag is not set', async function () {
|
|
936
984
|
mock({
|
|
937
|
-
bump:
|
|
985
|
+
bump: mockers.actualConventionalRecommendedBump,
|
|
938
986
|
changelog: ['feat release\n'],
|
|
939
987
|
tags: ['v1.0.0'],
|
|
940
988
|
commits: ['feat: shiny new stuff\n\n-hash-\nabc123\n'],
|
|
@@ -947,7 +995,7 @@ describe('cli', function () {
|
|
|
947
995
|
|
|
948
996
|
it('does not bump when there are no semver relevant changes and --noBumpWhenEmptyChanges flag is set', async function () {
|
|
949
997
|
mock({
|
|
950
|
-
bump:
|
|
998
|
+
bump: mockers.actualConventionalRecommendedBump,
|
|
951
999
|
changelog: ['chore release\n'],
|
|
952
1000
|
tags: ['v1.0.0'],
|
|
953
1001
|
commits: ['chore: update deps\n\n-hash-\nabc123\n'],
|
|
@@ -960,7 +1008,7 @@ describe('cli', function () {
|
|
|
960
1008
|
|
|
961
1009
|
it('bumps when there are semver relevant changes and --noBumpWhenEmptyChanges flag is set', async function () {
|
|
962
1010
|
mock({
|
|
963
|
-
bump:
|
|
1011
|
+
bump: mockers.actualConventionalRecommendedBump,
|
|
964
1012
|
changelog: ['feat release\n'],
|
|
965
1013
|
tags: ['v1.0.0'],
|
|
966
1014
|
commits: ['feat: shiny new stuff\n\n-hash-\nabc123\n'],
|
|
@@ -1686,9 +1734,6 @@ describe('cli', function () {
|
|
|
1686
1734
|
});
|
|
1687
1735
|
|
|
1688
1736
|
it('does not update files present in .gitignore', async function () {
|
|
1689
|
-
const DotGitIgnore = require('dotgitignore');
|
|
1690
|
-
jest.mock('dotgitignore');
|
|
1691
|
-
|
|
1692
1737
|
DotGitIgnore.mockImplementation(() => {
|
|
1693
1738
|
return {
|
|
1694
1739
|
ignore: (filename) => {
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import shell from 'shelljs';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import { execFileSync } from 'child_process';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
5
|
+
|
|
6
|
+
const CLI = fileURLToPath(new URL('../bin/cli.js', import.meta.url));
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* These tests spawn the real CLI in a child process instead of using the usual
|
|
10
|
+
* in-process harness. That is deliberate: `getConfiguration` loads ESM config
|
|
11
|
+
* files through Node's `require(esm)` (available since 22.12), and under vitest
|
|
12
|
+
* that require is intercepted by the module runner, which throws a Babel
|
|
13
|
+
* SyntaxError on ESM sources — a failure mode that does not exist in
|
|
14
|
+
* production. Only a child process observes the real behavior.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
function setupTestDirectory() {
|
|
18
|
+
shell.rm('-rf', 'esm-config-temp');
|
|
19
|
+
shell.config.silent = true;
|
|
20
|
+
shell.mkdir('esm-config-temp');
|
|
21
|
+
shell.cd('esm-config-temp');
|
|
22
|
+
shell.exec('git init');
|
|
23
|
+
shell.exec('git config commit.gpgSign false');
|
|
24
|
+
shell.exec('git config core.autocrlf false');
|
|
25
|
+
shell.exec('git commit --allow-empty -m"root-commit"');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function resetShell() {
|
|
29
|
+
shell.cd('../');
|
|
30
|
+
shell.rm('-rf', 'esm-config-temp');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function runCli() {
|
|
34
|
+
return execFileSync(process.execPath, [CLI, '--dry-run'], {
|
|
35
|
+
encoding: 'utf-8',
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
describe('ESM config files', function () {
|
|
40
|
+
beforeEach(function () {
|
|
41
|
+
setupTestDirectory();
|
|
42
|
+
|
|
43
|
+
fs.writeFileSync(
|
|
44
|
+
'package.json',
|
|
45
|
+
JSON.stringify({ version: '1.0.0' }),
|
|
46
|
+
'utf-8',
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
afterEach(function () {
|
|
51
|
+
resetShell();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('reads an ES module config-object from .versionrc.js', function () {
|
|
55
|
+
fs.writeFileSync(
|
|
56
|
+
'.versionrc.js',
|
|
57
|
+
'export default { tagPrefix: "custom-" }',
|
|
58
|
+
'utf-8',
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
const output = runCli();
|
|
62
|
+
expect(output).toContain('tagging release custom-1.0.1');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('reads an ES module config from .versionrc.mjs', function () {
|
|
66
|
+
fs.writeFileSync(
|
|
67
|
+
'.versionrc.mjs',
|
|
68
|
+
'export default { tagPrefix: "custom-" }',
|
|
69
|
+
'utf-8',
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const output = runCli();
|
|
73
|
+
expect(output).toContain('tagging release custom-1.0.1');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('evaluates an ES module config-function', function () {
|
|
77
|
+
fs.writeFileSync(
|
|
78
|
+
'.versionrc.mjs',
|
|
79
|
+
'export default function () { return { tagPrefix: "custom-" }; }',
|
|
80
|
+
'utf-8',
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const output = runCli();
|
|
84
|
+
expect(output).toContain('tagging release custom-1.0.1');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('applies the same configuration when written as CommonJS', function () {
|
|
88
|
+
fs.writeFileSync(
|
|
89
|
+
'.versionrc.js',
|
|
90
|
+
'module.exports = { tagPrefix: "custom-" }',
|
|
91
|
+
'utf-8',
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const output = runCli();
|
|
95
|
+
expect(output).toContain('tagging release custom-1.0.1');
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -1,21 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
import shell from 'shelljs';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
|
|
4
|
+
const mockers = vi.hoisted(() => require('./mocks/vitest-mocks').setup());
|
|
5
|
+
vi.mock('conventional-changelog', () => mockers.conventionalChangelogModule());
|
|
6
|
+
vi.mock('conventional-recommended-bump', async (importOriginal) =>
|
|
7
|
+
mockers.conventionalRecommendedBumpModule(
|
|
8
|
+
await importOriginal(),
|
|
9
|
+
await import('@conventional-changelog/git-client'),
|
|
10
|
+
),
|
|
11
|
+
);
|
|
12
|
+
vi.mock('@conventional-changelog/git-client', async (importOriginal) =>
|
|
13
|
+
mockers.gitClientModule(await importOriginal()),
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
17
|
+
const consoleInfoSpy = vi.spyOn(console, 'info').mockImplementation(() => {});
|
|
18
|
+
|
|
19
|
+
async function exec(opt = '') {
|
|
20
|
+
vi.resetModules();
|
|
21
|
+
const { default: cli } = await import('../command');
|
|
14
22
|
if (typeof opt === 'string') {
|
|
15
|
-
const cli = require('../command');
|
|
16
23
|
opt = cli.parse(`commit-and-tag-version ${opt}`);
|
|
17
24
|
}
|
|
18
|
-
|
|
25
|
+
const { default: standardVersion } = await import('../index');
|
|
26
|
+
return standardVersion(opt);
|
|
19
27
|
}
|
|
20
28
|
|
|
21
29
|
function writePackageJson(version, option) {
|
|
@@ -1,15 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
import shell from 'shelljs';
|
|
2
|
+
import fs from 'fs';
|
|
2
3
|
|
|
3
|
-
const
|
|
4
|
-
|
|
4
|
+
const mockers = vi.hoisted(() => require('./mocks/vitest-mocks').setup());
|
|
5
|
+
vi.mock('conventional-changelog', () => mockers.conventionalChangelogModule());
|
|
6
|
+
vi.mock('conventional-recommended-bump', async (importOriginal) =>
|
|
7
|
+
mockers.conventionalRecommendedBumpModule(
|
|
8
|
+
await importOriginal(),
|
|
9
|
+
await import('@conventional-changelog/git-client'),
|
|
10
|
+
),
|
|
11
|
+
);
|
|
12
|
+
vi.mock('@conventional-changelog/git-client', async (importOriginal) =>
|
|
13
|
+
mockers.gitClientModule(await importOriginal()),
|
|
14
|
+
);
|
|
5
15
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const cli = require('../command');
|
|
16
|
+
async function exec() {
|
|
17
|
+
vi.resetModules();
|
|
18
|
+
const { default: cli } = await import('../command');
|
|
10
19
|
const opt = cli.parse('commit-and-tag-version');
|
|
11
20
|
opt.skip = { commit: true, tag: true };
|
|
12
|
-
|
|
21
|
+
const { default: standardVersion } = await import('../index');
|
|
22
|
+
return standardVersion(opt);
|
|
13
23
|
}
|
|
14
24
|
|
|
15
25
|
/**
|
|
@@ -68,6 +78,6 @@ describe('invalid .versionrc', function () {
|
|
|
68
78
|
mock({ bump: 'minor' });
|
|
69
79
|
fs.writeFileSync('.versionrc.js', 'module.exports = 3', 'utf-8');
|
|
70
80
|
|
|
71
|
-
expect(exec).toThrow(/Invalid configuration/);
|
|
81
|
+
await expect(exec()).rejects.toThrow(/Invalid configuration/);
|
|
72
82
|
});
|
|
73
83
|
});
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Create mock state and module factories for the conventional-changelog
|
|
5
|
+
* ecosystem packages.
|
|
6
|
+
*
|
|
7
|
+
* Usage in test files:
|
|
8
|
+
*
|
|
9
|
+
* const mockers = vi.hoisted(() => require('./mocks/vitest-mocks').setup());
|
|
10
|
+
*
|
|
11
|
+
* vi.mock('conventional-changelog', () =>
|
|
12
|
+
* mockers.conventionalChangelogModule(),
|
|
13
|
+
* );
|
|
14
|
+
* vi.mock('conventional-recommended-bump', async (importOriginal) =>
|
|
15
|
+
* mockers.conventionalRecommendedBumpModule(
|
|
16
|
+
* await importOriginal(),
|
|
17
|
+
* await import('@conventional-changelog/git-client'),
|
|
18
|
+
* ),
|
|
19
|
+
* );
|
|
20
|
+
* vi.mock('@conventional-changelog/git-client', async (importOriginal) =>
|
|
21
|
+
* mockers.gitClientModule(await importOriginal()),
|
|
22
|
+
* );
|
|
23
|
+
*/
|
|
24
|
+
function setup({ mockRunExecFile = false } = {}) {
|
|
25
|
+
const runExecFile = vi.fn();
|
|
26
|
+
|
|
27
|
+
// Use the actual fs module object directly for spying
|
|
28
|
+
const fsProxy = require('fs');
|
|
29
|
+
|
|
30
|
+
// Sentinel passed as `bump` by tests that want the real Bumper
|
|
31
|
+
// implementation to run (against the mocked git client).
|
|
32
|
+
const actualConventionalRecommendedBump = Symbol(
|
|
33
|
+
'actual-conventional-recommended-bump',
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
// Shared mock data, updated by the mock* helpers below.
|
|
37
|
+
const state = {
|
|
38
|
+
tags: [], // string[] | Error - git tags yielded by the git client
|
|
39
|
+
commits: [], // string[] - raw commit bodies yielded by the git client
|
|
40
|
+
changelog: [], // Array<string | Error | fn | undefined> - changelog chunks
|
|
41
|
+
bump: undefined, // releaseType | Error | fn | actualConventionalRecommendedBump
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Module factory for '@conventional-changelog/git-client': the real
|
|
46
|
+
* ConventionalGitClient with the raw git interactions (log for commits and
|
|
47
|
+
* tags) replaced by the mock state. Parsing and semver filtering still use
|
|
48
|
+
* the real implementation.
|
|
49
|
+
*/
|
|
50
|
+
const gitClientModule = (actual) => {
|
|
51
|
+
class MockConventionalGitClient extends actual.ConventionalGitClient {
|
|
52
|
+
async *getRawCommits() {
|
|
53
|
+
for (const commit of state.commits || []) {
|
|
54
|
+
yield commit;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async *getTags() {
|
|
59
|
+
if (state.tags instanceof Error) throw state.tags;
|
|
60
|
+
for (const tag of state.tags || []) {
|
|
61
|
+
yield tag;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return { ...actual, ConventionalGitClient: MockConventionalGitClient };
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Module factory for 'conventional-changelog': a chainable stub whose
|
|
71
|
+
* write() generator yields the queued changelog chunks.
|
|
72
|
+
*/
|
|
73
|
+
const conventionalChangelogModule = () => {
|
|
74
|
+
class MockConventionalChangelog {
|
|
75
|
+
readPackage() {
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
package() {
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
loadPreset(preset) {
|
|
84
|
+
this.presetParams = preset;
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
config(config) {
|
|
89
|
+
this.configParams = config;
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
tags(params) {
|
|
94
|
+
this.tagsParams = params;
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
options(options) {
|
|
99
|
+
this.optionsParams = options;
|
|
100
|
+
return this;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
context(context) {
|
|
104
|
+
this.contextParams = context;
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
commits(params, parserOptions) {
|
|
109
|
+
this.commitsParams = params;
|
|
110
|
+
this.parserOptions = parserOptions;
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
writer(options) {
|
|
115
|
+
this.writerOptions = options;
|
|
116
|
+
return this;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async *write() {
|
|
120
|
+
// Mirrors the behaviour of the old stream mock: consume queued
|
|
121
|
+
// entries until an empty entry ends the stream; an Error entry
|
|
122
|
+
// fails it; a function entry is called with the params captured
|
|
123
|
+
// from the chained setup calls.
|
|
124
|
+
for (;;) {
|
|
125
|
+
const next = state.changelog.shift();
|
|
126
|
+
if (next instanceof Error) throw next;
|
|
127
|
+
const chunk =
|
|
128
|
+
typeof next === 'function'
|
|
129
|
+
? next({
|
|
130
|
+
preset: this.presetParams,
|
|
131
|
+
tags: this.tagsParams,
|
|
132
|
+
options: this.optionsParams,
|
|
133
|
+
context: this.contextParams,
|
|
134
|
+
commits: this.commitsParams,
|
|
135
|
+
parser: this.parserOptions,
|
|
136
|
+
writer: this.writerOptions,
|
|
137
|
+
})
|
|
138
|
+
: next;
|
|
139
|
+
if (!chunk) return;
|
|
140
|
+
yield chunk;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return { ConventionalChangelog: MockConventionalChangelog };
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Module factory for 'conventional-recommended-bump': a chainable Bumper
|
|
150
|
+
* stub driven by the `bump` mock state. When `bump` is the
|
|
151
|
+
* `actualConventionalRecommendedBump` sentinel, the real Bumper runs with
|
|
152
|
+
* the mocked git client, so real presets, parsing and whatBump logic are
|
|
153
|
+
* exercised against the mock commits/tags.
|
|
154
|
+
*/
|
|
155
|
+
const conventionalRecommendedBumpModule = (actual, gitClientMod) => {
|
|
156
|
+
class MockBumper {
|
|
157
|
+
loadPreset(preset) {
|
|
158
|
+
this.presetParams = preset;
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
config(config) {
|
|
163
|
+
this.configParams = config;
|
|
164
|
+
return this;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
options(options) {
|
|
168
|
+
this.optionsParams = options;
|
|
169
|
+
return this;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
tag(params) {
|
|
173
|
+
this.tagParams = params;
|
|
174
|
+
return this;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
commits(params, parserOptions) {
|
|
178
|
+
this.commitsParams = params;
|
|
179
|
+
this.parserOptions = parserOptions;
|
|
180
|
+
return this;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async bump(whatBump) {
|
|
184
|
+
const bump = state.bump;
|
|
185
|
+
|
|
186
|
+
if (bump === actualConventionalRecommendedBump) {
|
|
187
|
+
const bumper = new actual.Bumper(
|
|
188
|
+
new gitClientMod.ConventionalGitClient(process.cwd()),
|
|
189
|
+
);
|
|
190
|
+
if (this.presetParams) bumper.loadPreset(this.presetParams);
|
|
191
|
+
if (this.tagParams) bumper.tag(this.tagParams);
|
|
192
|
+
if (this.commitsParams || this.parserOptions) {
|
|
193
|
+
bumper.commits(this.commitsParams || {}, this.parserOptions);
|
|
194
|
+
}
|
|
195
|
+
if (this.optionsParams) bumper.options(this.optionsParams);
|
|
196
|
+
return bumper.bump(whatBump);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (bump instanceof Error) throw bump;
|
|
200
|
+
|
|
201
|
+
if (typeof bump === 'function') {
|
|
202
|
+
// Legacy callback-style test hook: (opt, parserOpts, cb)
|
|
203
|
+
return new Promise((resolve, reject) => {
|
|
204
|
+
bump(
|
|
205
|
+
{
|
|
206
|
+
preset: this.presetParams,
|
|
207
|
+
tag: this.tagParams,
|
|
208
|
+
commits: this.commitsParams,
|
|
209
|
+
},
|
|
210
|
+
this.parserOptions,
|
|
211
|
+
(err, release) => {
|
|
212
|
+
if (err) reject(err);
|
|
213
|
+
else resolve({ commits: [], ...release });
|
|
214
|
+
},
|
|
215
|
+
);
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (bump) {
|
|
220
|
+
return { releaseType: bump, commits: [] };
|
|
221
|
+
}
|
|
222
|
+
return { commits: [] };
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return { ...actual, Bumper: MockBumper };
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const mockGitSemverTags = ({ tags = [] }) => {
|
|
230
|
+
state.tags = tags;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const mockGitRawCommits = ({ commits = [] }) => {
|
|
234
|
+
state.commits = commits;
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const mockConventionalChangelog = ({ changelog }) => {
|
|
238
|
+
state.changelog = Array.isArray(changelog) ? changelog : [changelog];
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
const mockRecommendedBump = ({ bump }) => {
|
|
242
|
+
state.bump = bump;
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
return {
|
|
246
|
+
fsProxy,
|
|
247
|
+
runExecFile,
|
|
248
|
+
mockRunExecFile,
|
|
249
|
+
actualConventionalRecommendedBump,
|
|
250
|
+
gitClientModule,
|
|
251
|
+
conventionalChangelogModule,
|
|
252
|
+
conventionalRecommendedBumpModule,
|
|
253
|
+
mockGitSemverTags,
|
|
254
|
+
mockGitRawCommits,
|
|
255
|
+
mockConventionalChangelog,
|
|
256
|
+
mockRecommendedBump,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
module.exports = { setup };
|