@hubspot/npm-scripts 0.3.4 → 0.4.1
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/README.md +88 -11
- package/package.json +4 -2
- package/src/__tests__/release.test.js +490 -10
- package/src/constants/release.d.ts +3 -0
- package/src/constants/release.js +7 -0
- package/src/release.js +124 -39
- package/src/types.d.ts +5 -2
- package/src/utils/__tests__/git.test.js +3 -3
- package/src/utils/__tests__/logging.test.js +15 -1
- package/src/utils/__tests__/npm.test.js +18 -1
- package/src/utils/git.js +1 -1
- package/src/utils/logging.d.ts +1 -0
- package/src/utils/logging.js +6 -0
- package/src/utils/npm.d.ts +1 -0
- package/src/utils/npm.js +5 -0
- package/src/utils/release/__tests__/steps.test.js +596 -6
- package/src/utils/release/__tests__/validate.test.js +9 -1
- package/src/utils/release/__tests__/version.test.js +40 -34
- package/src/utils/release/steps.d.ts +29 -4
- package/src/utils/release/steps.js +355 -17
- package/src/utils/release/types.d.ts +22 -2
- package/src/utils/release/validate.d.ts +3 -0
- package/src/utils/release/validate.js +6 -0
- package/src/utils/release/version.d.ts +2 -1
- package/src/utils/release/version.js +22 -6
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
-
import { exec as _exec, execSync } from 'node:child_process';
|
|
2
|
+
import { exec as _exec, execFile as _execFile, execSync, } from 'node:child_process';
|
|
3
3
|
import open from 'open';
|
|
4
|
+
import fs from 'fs-extra';
|
|
4
5
|
import { logger } from '../../../utils/logging.js';
|
|
5
6
|
import { EXIT_CODES } from '../../../utils/process.js';
|
|
6
7
|
import { gitCleanup } from '../../../utils/git.js';
|
|
7
8
|
import { publish, updateNextTag, updatePreviousTag, } from '../../../utils/npm.js';
|
|
8
|
-
import { confirm } from '../../../utils/prompting.js';
|
|
9
|
+
import { confirm, select } from '../../../utils/prompting.js';
|
|
9
10
|
import { TAG, INCREMENT } from '../../../constants/release.js';
|
|
10
|
-
import { confirmRelease, runBuild, tryBuild, createTempBranch, bumpVersion, ensureNpmAuth, tryPublish, updateDistTagsForLatest, finalizeRelease, logReleaseSuccess, } from '../steps.js';
|
|
11
|
+
import { confirmRelease, runBuild, tryBuild, createTempBranch, bumpVersion, ensureNpmAuth, tryPublish, updateDistTagsForLatest, finalizeRelease, logReleaseSuccess, ensureCleanWorkingTree, ensureGhCli, ensureBranchPushed, findWorkflowRun, openReleasePr, resolveVersionIncrement, runExperimentalWorkflow, watchWorkflowRun, } from '../steps.js';
|
|
11
12
|
import { ReleaseError } from '../errors.js';
|
|
12
13
|
vi.mock('util', () => ({ promisify: vi.fn(fn => fn) }));
|
|
13
|
-
vi.mock('node:child_process', () => ({
|
|
14
|
+
vi.mock('node:child_process', () => ({
|
|
15
|
+
exec: vi.fn(),
|
|
16
|
+
execFile: vi.fn(),
|
|
17
|
+
execSync: vi.fn(),
|
|
18
|
+
}));
|
|
14
19
|
vi.mock('open', () => ({ default: vi.fn().mockResolvedValue(undefined) }));
|
|
15
20
|
vi.mock('../../../utils/logging.js', () => ({
|
|
16
|
-
logger: { log: vi.fn(), error: vi.fn(), success: vi.fn() },
|
|
21
|
+
logger: { log: vi.fn(), error: vi.fn(), success: vi.fn(), warn: vi.fn() },
|
|
22
|
+
uiLink: (text, url) => `${text}: ${url}`,
|
|
17
23
|
}));
|
|
18
24
|
vi.mock('../../../utils/process.js', () => ({
|
|
19
25
|
EXIT_CODES: { SUCCESS: 0, ERROR: 1 },
|
|
@@ -24,8 +30,15 @@ vi.mock('../../../utils/npm.js', () => ({
|
|
|
24
30
|
updateNextTag: vi.fn(),
|
|
25
31
|
updatePreviousTag: vi.fn(),
|
|
26
32
|
}));
|
|
27
|
-
vi.mock('../../../utils/prompting.js', () => ({
|
|
33
|
+
vi.mock('../../../utils/prompting.js', () => ({
|
|
34
|
+
confirm: vi.fn(),
|
|
35
|
+
select: vi.fn(),
|
|
36
|
+
}));
|
|
37
|
+
vi.mock('fs-extra', () => ({
|
|
38
|
+
default: { mkdtemp: vi.fn(), readFile: vi.fn(), remove: vi.fn() },
|
|
39
|
+
}));
|
|
28
40
|
const mockedExec = vi.mocked(_exec);
|
|
41
|
+
const mockedExecFile = vi.mocked(_execFile);
|
|
29
42
|
const mockedExecSync = vi.mocked(execSync);
|
|
30
43
|
const mockedGitCleanup = vi.mocked(gitCleanup);
|
|
31
44
|
const mockedPublish = vi.mocked(publish);
|
|
@@ -56,6 +69,30 @@ describe('release/steps', () => {
|
|
|
56
69
|
});
|
|
57
70
|
expect(logger.log).toHaveBeenCalledWith('\nDRY RUN');
|
|
58
71
|
});
|
|
72
|
+
it('should label the current version with currentVersionLabel when given', async () => {
|
|
73
|
+
await confirmRelease({
|
|
74
|
+
isDryRun: false,
|
|
75
|
+
currentVersion: '1.0.0',
|
|
76
|
+
currentVersionLabel: 'Current latest version',
|
|
77
|
+
newVersion: '1.0.0-experimental.0',
|
|
78
|
+
tag: TAG.EXPERIMENTAL,
|
|
79
|
+
localVersion: '1.0.0-beta.1',
|
|
80
|
+
versionIncrement: INCREMENT.PRERELEASE,
|
|
81
|
+
});
|
|
82
|
+
expect(logger.log).toHaveBeenCalledWith('\nCurrent latest version: 1.0.0');
|
|
83
|
+
});
|
|
84
|
+
it('should log the last experimental release when given', async () => {
|
|
85
|
+
await confirmRelease({
|
|
86
|
+
isDryRun: false,
|
|
87
|
+
currentVersion: '1.0.0',
|
|
88
|
+
lastExperimentalVersion: '0.9.0-experimental.3',
|
|
89
|
+
newVersion: '1.0.0-experimental.0',
|
|
90
|
+
tag: TAG.EXPERIMENTAL,
|
|
91
|
+
localVersion: '1.0.0-beta.1',
|
|
92
|
+
versionIncrement: INCREMENT.PRERELEASE,
|
|
93
|
+
});
|
|
94
|
+
expect(logger.log).toHaveBeenCalledWith('Last experimental release: 0.9.0-experimental.3');
|
|
95
|
+
});
|
|
59
96
|
it('should not log \\nDRY RUN when isDryRun is false', async () => {
|
|
60
97
|
await confirmRelease({
|
|
61
98
|
isDryRun: false,
|
|
@@ -121,6 +158,61 @@ describe('release/steps', () => {
|
|
|
121
158
|
localVersion: '1.0.0',
|
|
122
159
|
versionIncrement: INCREMENT.PATCH,
|
|
123
160
|
})).rejects.toMatchObject({ exitCode: EXIT_CODES.SUCCESS });
|
|
161
|
+
expect(logger.log).toHaveBeenCalledWith('\nTo release your changes on the next tag first, run the next release command instead, e.g. `yarn beta-release -v patch`');
|
|
162
|
+
});
|
|
163
|
+
it('should not prompt when skipPrompts is true', async () => {
|
|
164
|
+
await confirmRelease({
|
|
165
|
+
isDryRun: false,
|
|
166
|
+
currentVersion: '1.0.0-beta.1',
|
|
167
|
+
newVersion: '1.0.1-beta.0',
|
|
168
|
+
tag: TAG.NEXT,
|
|
169
|
+
localVersion: '1.0.0-beta.1',
|
|
170
|
+
versionIncrement: INCREMENT.PATCH,
|
|
171
|
+
skipPrompts: true,
|
|
172
|
+
});
|
|
173
|
+
expect(mockedConfirm).not.toHaveBeenCalled();
|
|
174
|
+
});
|
|
175
|
+
it('should warn about the beta release and continue when skipPrompts is true', async () => {
|
|
176
|
+
await expect(confirmRelease({
|
|
177
|
+
isDryRun: false,
|
|
178
|
+
currentVersion: '1.0.0',
|
|
179
|
+
newVersion: '1.0.1',
|
|
180
|
+
tag: TAG.LATEST,
|
|
181
|
+
localVersion: '1.0.0',
|
|
182
|
+
versionIncrement: INCREMENT.PATCH,
|
|
183
|
+
skipPrompts: true,
|
|
184
|
+
})).resolves.toBeUndefined();
|
|
185
|
+
expect(mockedConfirm).not.toHaveBeenCalled();
|
|
186
|
+
expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('have not yet been released in beta'));
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
describe('resolveVersionIncrement', () => {
|
|
190
|
+
it('should return the given increment without prompting', async () => {
|
|
191
|
+
await expect(resolveVersionIncrement(TAG.LATEST, INCREMENT.MINOR, false)).resolves.toBe(INCREMENT.MINOR);
|
|
192
|
+
expect(vi.mocked(select)).not.toHaveBeenCalled();
|
|
193
|
+
});
|
|
194
|
+
it('should prompt with patch, minor, and major for latest', async () => {
|
|
195
|
+
vi.mocked(select).mockResolvedValue(INCREMENT.PATCH);
|
|
196
|
+
await expect(resolveVersionIncrement(TAG.LATEST, undefined, false)).resolves.toBe(INCREMENT.PATCH);
|
|
197
|
+
expect(vi.mocked(select)).toHaveBeenCalledWith(expect.objectContaining({
|
|
198
|
+
choices: ['patch', 'minor', 'major'].map(value => ({
|
|
199
|
+
name: value,
|
|
200
|
+
value,
|
|
201
|
+
})),
|
|
202
|
+
}));
|
|
203
|
+
});
|
|
204
|
+
it('should include prerelease in the choices for next', async () => {
|
|
205
|
+
vi.mocked(select).mockResolvedValue(INCREMENT.PRERELEASE);
|
|
206
|
+
await resolveVersionIncrement(TAG.NEXT, undefined, false);
|
|
207
|
+
expect(vi.mocked(select)).toHaveBeenCalledWith(expect.objectContaining({
|
|
208
|
+
choices: expect.arrayContaining([
|
|
209
|
+
{ name: 'prerelease', value: 'prerelease' },
|
|
210
|
+
]),
|
|
211
|
+
}));
|
|
212
|
+
});
|
|
213
|
+
it('should throw an actionable ReleaseError instead of prompting when skipPrompts is true', async () => {
|
|
214
|
+
await expect(resolveVersionIncrement(TAG.NEXT, undefined, true)).rejects.toThrow('Pass -v <patch|minor|major|prerelease>');
|
|
215
|
+
expect(vi.mocked(select)).not.toHaveBeenCalled();
|
|
124
216
|
});
|
|
125
217
|
});
|
|
126
218
|
describe('runBuild', () => {
|
|
@@ -239,6 +331,503 @@ describe('release/steps', () => {
|
|
|
239
331
|
});
|
|
240
332
|
await expect(ensureNpmAuth(false, REGISTRY)).rejects.toThrow('Failed to authenticate with npm');
|
|
241
333
|
});
|
|
334
|
+
it('should throw instead of running npm adduser when skipPrompts is true', async () => {
|
|
335
|
+
mockedExec.mockRejectedValue(new Error('not logged in'));
|
|
336
|
+
await expect(ensureNpmAuth(false, REGISTRY, true)).rejects.toThrow(`npm login --registry ${REGISTRY}`);
|
|
337
|
+
expect(mockedExecSync).not.toHaveBeenCalled();
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
describe('ensureCleanWorkingTree', () => {
|
|
341
|
+
it('should resolve when git status is empty', async () => {
|
|
342
|
+
await expect(ensureCleanWorkingTree()).resolves.toBeUndefined();
|
|
343
|
+
expect(mockedExec).toHaveBeenCalledWith('git status --porcelain');
|
|
344
|
+
});
|
|
345
|
+
it('should throw an actionable ReleaseError listing the changes when the tree is dirty', async () => {
|
|
346
|
+
mockedExec.mockResolvedValue({
|
|
347
|
+
stdout: ' M src/index.ts\n?? notes.txt\n',
|
|
348
|
+
stderr: '',
|
|
349
|
+
});
|
|
350
|
+
const result = ensureCleanWorkingTree();
|
|
351
|
+
await expect(result).rejects.toThrow(ReleaseError);
|
|
352
|
+
await expect(result).rejects.toThrow(/uncommitted changes\. Commit or stash them[\s\S]*M src\/index\.ts/);
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
describe('ensureGhCli', () => {
|
|
356
|
+
const REPO_URL = 'https://github.com/test/repo';
|
|
357
|
+
it('should check gh is installed and logged in to the repository host', async () => {
|
|
358
|
+
await expect(ensureGhCli(REPO_URL)).resolves.toBeUndefined();
|
|
359
|
+
expect(mockedExec).toHaveBeenCalledWith('gh --version');
|
|
360
|
+
expect(mockedExec).toHaveBeenCalledWith('gh auth status --hostname github.com');
|
|
361
|
+
});
|
|
362
|
+
it('should throw an actionable ReleaseError when gh is missing', async () => {
|
|
363
|
+
mockedExec.mockRejectedValue(new Error('command not found: gh'));
|
|
364
|
+
await expect(ensureGhCli(REPO_URL)).rejects.toThrow('https://cli.github.com');
|
|
365
|
+
});
|
|
366
|
+
it('should throw an actionable ReleaseError when gh is not logged in', async () => {
|
|
367
|
+
mockedExec.mockImplementation((cmd) => cmd.startsWith('gh auth status')
|
|
368
|
+
? Promise.reject(new Error('not logged in'))
|
|
369
|
+
: Promise.resolve({ stdout: '', stderr: '' }));
|
|
370
|
+
await expect(ensureGhCli(REPO_URL)).rejects.toThrow('gh auth login --hostname github.com');
|
|
371
|
+
});
|
|
372
|
+
it('should check auth without a hostname when the repository URL cannot be parsed', async () => {
|
|
373
|
+
await ensureGhCli('not a url');
|
|
374
|
+
expect(mockedExec).toHaveBeenCalledWith('gh auth status');
|
|
375
|
+
expect(mockedExecFile).not.toHaveBeenCalled();
|
|
376
|
+
});
|
|
377
|
+
it('should check that the active gh account can access the repository', async () => {
|
|
378
|
+
await ensureGhCli(REPO_URL);
|
|
379
|
+
expect(mockedExecFile).toHaveBeenCalledWith('gh', [
|
|
380
|
+
'repo',
|
|
381
|
+
'view',
|
|
382
|
+
REPO_URL,
|
|
383
|
+
'--json',
|
|
384
|
+
'name',
|
|
385
|
+
]);
|
|
386
|
+
});
|
|
387
|
+
it('should name the active account and how to switch when it cannot access the repository', async () => {
|
|
388
|
+
mockedExecFile.mockImplementation((_file, args) => args[0] === 'repo'
|
|
389
|
+
? Promise.reject(new Error('HTTP 404: Not Found'))
|
|
390
|
+
: Promise.resolve({ stdout: 'someone-else\n', stderr: '' }));
|
|
391
|
+
await expect(ensureGhCli(REPO_URL)).rejects.toThrow(`Your active gh account (someone-else) can't access ${REPO_URL}. Switch to an account that can with \`gh auth switch --hostname github.com\``);
|
|
392
|
+
});
|
|
393
|
+
it('should still explain the access error when the active account cannot be read', async () => {
|
|
394
|
+
mockedExecFile.mockRejectedValue(new Error('HTTP 401'));
|
|
395
|
+
await expect(ensureGhCli(REPO_URL)).rejects.toThrow(`Your active gh account can't access ${REPO_URL}.`);
|
|
396
|
+
});
|
|
397
|
+
});
|
|
398
|
+
describe('ensureBranchPushed', () => {
|
|
399
|
+
const SHA = 'a'.repeat(40);
|
|
400
|
+
function mockShas(localSha, remoteSha) {
|
|
401
|
+
mockedExec.mockImplementation((cmd) => Promise.resolve({
|
|
402
|
+
stdout: cmd === 'git rev-parse HEAD'
|
|
403
|
+
? `${localSha}\n`
|
|
404
|
+
: cmd === 'git rev-parse origin/feature'
|
|
405
|
+
? `${remoteSha}\n`
|
|
406
|
+
: '',
|
|
407
|
+
stderr: '',
|
|
408
|
+
}));
|
|
409
|
+
}
|
|
410
|
+
it('should fetch the branch and return HEAD when it matches origin', async () => {
|
|
411
|
+
mockShas(SHA, SHA);
|
|
412
|
+
await expect(ensureBranchPushed('feature')).resolves.toBe(SHA);
|
|
413
|
+
expect(mockedExec).toHaveBeenCalledWith('git fetch origin feature');
|
|
414
|
+
});
|
|
415
|
+
it('should not require the branch to track origin', async () => {
|
|
416
|
+
mockShas(SHA, SHA);
|
|
417
|
+
await ensureBranchPushed('feature');
|
|
418
|
+
expect(mockedExec).not.toHaveBeenCalledWith(expect.stringContaining('@{u}'));
|
|
419
|
+
});
|
|
420
|
+
it('should throw with the push command when the branch is not on origin', async () => {
|
|
421
|
+
mockedExec.mockImplementation((cmd) => cmd === 'git fetch origin feature'
|
|
422
|
+
? Promise.reject(new Error("couldn't find remote ref feature"))
|
|
423
|
+
: Promise.resolve({ stdout: '', stderr: '' }));
|
|
424
|
+
await expect(ensureBranchPushed('feature')).rejects.toThrow('Could not fetch origin/feature. Push your branch first: git push -u origin feature');
|
|
425
|
+
});
|
|
426
|
+
it('should throw with the push command when HEAD differs from origin', async () => {
|
|
427
|
+
mockShas(SHA, 'b'.repeat(40));
|
|
428
|
+
await expect(ensureBranchPushed('feature')).rejects.toThrow('Push your branch first: git push -u origin feature');
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
describe('findWorkflowRun', () => {
|
|
432
|
+
const SHA = 'a'.repeat(40);
|
|
433
|
+
const RUN = {
|
|
434
|
+
databaseId: 7,
|
|
435
|
+
displayTitle: `Experimental release feature@${SHA}`,
|
|
436
|
+
url: 'https://github.com/test/repo/actions/runs/7',
|
|
437
|
+
};
|
|
438
|
+
it('should return the run whose title contains the sha', async () => {
|
|
439
|
+
mockedExecFile.mockResolvedValue({
|
|
440
|
+
stdout: JSON.stringify([
|
|
441
|
+
{ ...RUN, databaseId: 6, displayTitle: 'Experimental release x@b' },
|
|
442
|
+
RUN,
|
|
443
|
+
]),
|
|
444
|
+
stderr: '',
|
|
445
|
+
});
|
|
446
|
+
await expect(findWorkflowRun('w.yml', SHA, new Set(), 1, 0)).resolves.toEqual(RUN);
|
|
447
|
+
expect(mockedExecFile).toHaveBeenCalledWith('gh', [
|
|
448
|
+
'run',
|
|
449
|
+
'list',
|
|
450
|
+
'--workflow',
|
|
451
|
+
'w.yml',
|
|
452
|
+
'--event',
|
|
453
|
+
'workflow_dispatch',
|
|
454
|
+
'--json',
|
|
455
|
+
'databaseId,displayTitle,url',
|
|
456
|
+
'-L',
|
|
457
|
+
'20',
|
|
458
|
+
]);
|
|
459
|
+
});
|
|
460
|
+
it('should keep polling until the run appears', async () => {
|
|
461
|
+
mockedExecFile
|
|
462
|
+
.mockResolvedValueOnce({ stdout: '[]', stderr: '' })
|
|
463
|
+
.mockResolvedValueOnce({ stdout: JSON.stringify([RUN]), stderr: '' });
|
|
464
|
+
await expect(findWorkflowRun('w.yml', SHA, new Set(), 3, 0)).resolves.toEqual(RUN);
|
|
465
|
+
expect(mockedExecFile).toHaveBeenCalledTimes(2);
|
|
466
|
+
});
|
|
467
|
+
it('should skip runs that existed before the dispatch, even with the same title', async () => {
|
|
468
|
+
const staleRun = { ...RUN, databaseId: 5 };
|
|
469
|
+
mockedExecFile
|
|
470
|
+
.mockResolvedValueOnce({
|
|
471
|
+
stdout: JSON.stringify([staleRun]),
|
|
472
|
+
stderr: '',
|
|
473
|
+
})
|
|
474
|
+
.mockResolvedValueOnce({
|
|
475
|
+
stdout: JSON.stringify([RUN, staleRun]),
|
|
476
|
+
stderr: '',
|
|
477
|
+
});
|
|
478
|
+
await expect(findWorkflowRun('w.yml', SHA, new Set([5]), 3, 0)).resolves.toEqual(RUN);
|
|
479
|
+
expect(mockedExecFile).toHaveBeenCalledTimes(2);
|
|
480
|
+
});
|
|
481
|
+
it('should return undefined after the last attempt', async () => {
|
|
482
|
+
mockedExecFile.mockResolvedValue({ stdout: '[]', stderr: '' });
|
|
483
|
+
await expect(findWorkflowRun('w.yml', SHA, new Set(), 2, 0)).resolves.toBeUndefined();
|
|
484
|
+
expect(mockedExecFile).toHaveBeenCalledTimes(2);
|
|
485
|
+
});
|
|
486
|
+
});
|
|
487
|
+
describe('runExperimentalWorkflow', () => {
|
|
488
|
+
const SHA = 'a'.repeat(40);
|
|
489
|
+
const RUN = {
|
|
490
|
+
databaseId: 7,
|
|
491
|
+
displayTitle: `Experimental release feature@${SHA}`,
|
|
492
|
+
url: 'https://github.com/test/repo/actions/runs/7',
|
|
493
|
+
};
|
|
494
|
+
const BASE_ARGS = {
|
|
495
|
+
workflow: 'experimental-release.yml',
|
|
496
|
+
mainBranch: 'main',
|
|
497
|
+
branch: 'feature',
|
|
498
|
+
sha: SHA,
|
|
499
|
+
packageName: PACKAGE_NAME,
|
|
500
|
+
isDryRun: false,
|
|
501
|
+
};
|
|
502
|
+
const SUCCEEDED = {
|
|
503
|
+
status: 'completed',
|
|
504
|
+
conclusion: 'success',
|
|
505
|
+
jobs: [
|
|
506
|
+
{ name: 'version', status: 'completed', conclusion: 'success' },
|
|
507
|
+
{ name: 'publish', status: 'completed', conclusion: 'success' },
|
|
508
|
+
],
|
|
509
|
+
};
|
|
510
|
+
function mockRuns(runStatus) {
|
|
511
|
+
let isDispatched = false;
|
|
512
|
+
mockedExecFile.mockImplementation((_file, args) => {
|
|
513
|
+
if (args[0] === 'workflow') {
|
|
514
|
+
isDispatched = true;
|
|
515
|
+
}
|
|
516
|
+
return Promise.resolve({
|
|
517
|
+
stdout: args[1] === 'list'
|
|
518
|
+
? JSON.stringify(isDispatched ? [RUN] : [])
|
|
519
|
+
: args[1] === 'view'
|
|
520
|
+
? JSON.stringify(runStatus)
|
|
521
|
+
: '',
|
|
522
|
+
stderr: '',
|
|
523
|
+
});
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
beforeEach(() => {
|
|
527
|
+
mockRuns(SUCCEEDED);
|
|
528
|
+
vi.mocked(fs.mkdtemp).mockResolvedValue('/tmp/exp-1');
|
|
529
|
+
vi.mocked(fs.readFile).mockResolvedValue('1.0.0-experimental.3\n');
|
|
530
|
+
});
|
|
531
|
+
it('should dispatch, watch, download the version, and print the install command', async () => {
|
|
532
|
+
await runExperimentalWorkflow(BASE_ARGS);
|
|
533
|
+
expect(mockedExecFile).toHaveBeenCalledWith('gh', [
|
|
534
|
+
'run',
|
|
535
|
+
'view',
|
|
536
|
+
'7',
|
|
537
|
+
'--json',
|
|
538
|
+
'status,conclusion,jobs',
|
|
539
|
+
]);
|
|
540
|
+
expect(logger.log).toHaveBeenCalledWith('Workflow completed successfully');
|
|
541
|
+
expect(mockedExecFile).toHaveBeenCalledWith('gh', [
|
|
542
|
+
'run',
|
|
543
|
+
'download',
|
|
544
|
+
'7',
|
|
545
|
+
'-n',
|
|
546
|
+
'experimental-version',
|
|
547
|
+
'-D',
|
|
548
|
+
'/tmp/exp-1',
|
|
549
|
+
]);
|
|
550
|
+
expect(vi.mocked(fs.readFile)).toHaveBeenCalledWith('/tmp/exp-1/version.txt', 'utf8');
|
|
551
|
+
expect(vi.mocked(fs.remove)).toHaveBeenCalledWith('/tmp/exp-1');
|
|
552
|
+
expect(logger.log).toHaveBeenCalledWith(`View workflow in GitHub: ${RUN.url}`);
|
|
553
|
+
expect(logger.success).toHaveBeenCalledWith(`Published ${PACKAGE_NAME}@1.0.0-experimental.3 on the experimental tag`);
|
|
554
|
+
expect(logger.log).toHaveBeenCalledWith(`Install it with: yarn add ${PACKAGE_NAME}@1.0.0-experimental.3`);
|
|
555
|
+
expect(logger.log).toHaveBeenCalledWith(expect.stringContaining('npm can take a few minutes'));
|
|
556
|
+
});
|
|
557
|
+
it('should only print the dispatch command on dry run', async () => {
|
|
558
|
+
await runExperimentalWorkflow({ ...BASE_ARGS, isDryRun: true });
|
|
559
|
+
expect(mockedExecFile).not.toHaveBeenCalled();
|
|
560
|
+
expect(logger.log).toHaveBeenCalledWith(` gh workflow run experimental-release.yml --ref main -f branch=feature -f sha=${SHA}`);
|
|
561
|
+
});
|
|
562
|
+
it('should throw an actionable ReleaseError when listing runs fails before the dispatch', async () => {
|
|
563
|
+
mockedExecFile.mockRejectedValue(new Error('HTTP 404'));
|
|
564
|
+
await expect(runExperimentalWorkflow(BASE_ARGS)).rejects.toThrow('Failed to list runs of experimental-release.yml');
|
|
565
|
+
expect(mockedExecFile).not.toHaveBeenCalledWith('gh', expect.arrayContaining(['workflow', 'run']));
|
|
566
|
+
});
|
|
567
|
+
it('should not watch a run of the same commit that finished before the dispatch', async () => {
|
|
568
|
+
mockedExecFile.mockResolvedValue({
|
|
569
|
+
stdout: JSON.stringify([RUN]),
|
|
570
|
+
stderr: '',
|
|
571
|
+
});
|
|
572
|
+
vi.useFakeTimers();
|
|
573
|
+
const result = runExperimentalWorkflow(BASE_ARGS);
|
|
574
|
+
const assertion = expect(result).rejects.toThrow(ReleaseError);
|
|
575
|
+
await vi.runAllTimersAsync();
|
|
576
|
+
await assertion;
|
|
577
|
+
vi.useRealTimers();
|
|
578
|
+
expect(mockedExecFile).not.toHaveBeenCalledWith('gh', expect.arrayContaining(['view']));
|
|
579
|
+
});
|
|
580
|
+
it('should throw an actionable ReleaseError when the dispatch fails', async () => {
|
|
581
|
+
mockedExecFile.mockImplementation((_file, args) => args[0] === 'workflow'
|
|
582
|
+
? Promise.reject(new Error('HTTP 404'))
|
|
583
|
+
: Promise.resolve({ stdout: '[]', stderr: '' }));
|
|
584
|
+
await expect(runExperimentalWorkflow(BASE_ARGS)).rejects.toThrow('Check that the workflow exists on main');
|
|
585
|
+
});
|
|
586
|
+
it('should throw with a gh run list hint when the run cannot be found', async () => {
|
|
587
|
+
mockedExecFile.mockResolvedValue({ stdout: '[]', stderr: '' });
|
|
588
|
+
vi.useFakeTimers();
|
|
589
|
+
const result = runExperimentalWorkflow(BASE_ARGS);
|
|
590
|
+
const assertion = expect(result).rejects.toThrow('gh run list --workflow experimental-release.yml');
|
|
591
|
+
await vi.runAllTimersAsync();
|
|
592
|
+
await assertion;
|
|
593
|
+
vi.useRealTimers();
|
|
594
|
+
expect(mockedExecFile).not.toHaveBeenCalledWith('gh', expect.arrayContaining(['view']));
|
|
595
|
+
});
|
|
596
|
+
it('should name the failed job and link the run when the workflow fails', async () => {
|
|
597
|
+
mockRuns({
|
|
598
|
+
status: 'completed',
|
|
599
|
+
conclusion: 'failure',
|
|
600
|
+
jobs: [
|
|
601
|
+
{ name: 'version', status: 'completed', conclusion: 'success' },
|
|
602
|
+
{ name: 'build', status: 'completed', conclusion: 'failure' },
|
|
603
|
+
{ name: 'publish', status: 'completed', conclusion: 'skipped' },
|
|
604
|
+
],
|
|
605
|
+
});
|
|
606
|
+
await expect(runExperimentalWorkflow(BASE_ARGS)).rejects.toThrow(`The experimental release workflow failed in the build job. See ${RUN.url}`);
|
|
607
|
+
expect(logger.log).not.toHaveBeenCalledWith('Workflow completed successfully');
|
|
608
|
+
});
|
|
609
|
+
it('should report the run conclusion when no job failed', async () => {
|
|
610
|
+
mockRuns({ status: 'completed', conclusion: 'cancelled', jobs: [] });
|
|
611
|
+
await expect(runExperimentalWorkflow(BASE_ARGS)).rejects.toThrow(`The experimental release workflow finished with 'cancelled'. See ${RUN.url}`);
|
|
612
|
+
});
|
|
613
|
+
it('should warn with the run URL but succeed when the version artifact cannot be read', async () => {
|
|
614
|
+
vi.mocked(fs.readFile).mockRejectedValue(new Error('ENOENT'));
|
|
615
|
+
await expect(runExperimentalWorkflow(BASE_ARGS)).resolves.toBeUndefined();
|
|
616
|
+
expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining(RUN.url));
|
|
617
|
+
expect(vi.mocked(fs.remove)).toHaveBeenCalledWith('/tmp/exp-1');
|
|
618
|
+
});
|
|
619
|
+
});
|
|
620
|
+
describe('watchWorkflowRun', () => {
|
|
621
|
+
const job = (name, status, conclusion = '') => ({
|
|
622
|
+
name,
|
|
623
|
+
status,
|
|
624
|
+
conclusion,
|
|
625
|
+
});
|
|
626
|
+
function mockStatuses(...responses) {
|
|
627
|
+
for (const response of responses) {
|
|
628
|
+
if (response instanceof Error) {
|
|
629
|
+
mockedExecFile.mockRejectedValueOnce(response);
|
|
630
|
+
}
|
|
631
|
+
else {
|
|
632
|
+
mockedExecFile.mockResolvedValueOnce({
|
|
633
|
+
stdout: JSON.stringify(response),
|
|
634
|
+
stderr: '',
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
it('should log each job once as it finishes and return the completed run', async () => {
|
|
640
|
+
const completed = {
|
|
641
|
+
status: 'completed',
|
|
642
|
+
conclusion: 'success',
|
|
643
|
+
jobs: [
|
|
644
|
+
job('version', 'completed', 'success'),
|
|
645
|
+
job('build', 'completed', 'success'),
|
|
646
|
+
],
|
|
647
|
+
};
|
|
648
|
+
mockStatuses({
|
|
649
|
+
status: 'in_progress',
|
|
650
|
+
conclusion: '',
|
|
651
|
+
jobs: [
|
|
652
|
+
job('version', 'completed', 'success'),
|
|
653
|
+
job('build', 'in_progress'),
|
|
654
|
+
],
|
|
655
|
+
}, completed);
|
|
656
|
+
await expect(watchWorkflowRun(7, 0)).resolves.toEqual(completed);
|
|
657
|
+
const logged = vi.mocked(logger.log).mock.calls.map(([line]) => line);
|
|
658
|
+
expect(logged).toHaveLength(2);
|
|
659
|
+
expect(logged[0]).toContain('version');
|
|
660
|
+
expect(logged[1]).toContain('build');
|
|
661
|
+
});
|
|
662
|
+
it('should keep polling through transient status errors', async () => {
|
|
663
|
+
const completed = {
|
|
664
|
+
status: 'completed',
|
|
665
|
+
conclusion: 'success',
|
|
666
|
+
jobs: [],
|
|
667
|
+
};
|
|
668
|
+
mockStatuses(new Error('HTTP 502'), completed);
|
|
669
|
+
await expect(watchWorkflowRun(7, 0, 3)).resolves.toEqual(completed);
|
|
670
|
+
});
|
|
671
|
+
it('should throw after too many status errors in a row', async () => {
|
|
672
|
+
mockStatuses(new Error('HTTP 502'), new Error('HTTP 502'));
|
|
673
|
+
await expect(watchWorkflowRun(7, 0, 2)).rejects.toThrow('HTTP 502');
|
|
674
|
+
});
|
|
675
|
+
});
|
|
676
|
+
describe('openReleasePr', () => {
|
|
677
|
+
const BASE_ARGS = {
|
|
678
|
+
tag: TAG.NEXT,
|
|
679
|
+
newVersion: '1.0.1-beta.0',
|
|
680
|
+
isDryRun: false,
|
|
681
|
+
baseBranch: 'main',
|
|
682
|
+
repositoryUrl: 'https://github.com/test/repo',
|
|
683
|
+
headBranch: 'v1.0.1-beta.0-publish',
|
|
684
|
+
onTempBranch: true,
|
|
685
|
+
tempBranch: 'v1.0.1-beta.0-publish',
|
|
686
|
+
originalBranch: 'main',
|
|
687
|
+
};
|
|
688
|
+
const PR_URL = 'https://github.com/test/repo/pull/42';
|
|
689
|
+
function failOn(prefix) {
|
|
690
|
+
mockedExec.mockImplementation((cmd) => {
|
|
691
|
+
if (cmd.startsWith(prefix)) {
|
|
692
|
+
return Promise.reject(new Error(`${prefix} failed`));
|
|
693
|
+
}
|
|
694
|
+
return Promise.resolve({ stdout: '', stderr: '' });
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
beforeEach(() => {
|
|
698
|
+
mockedExecFile.mockResolvedValue({ stdout: `${PR_URL}\n`, stderr: '' });
|
|
699
|
+
});
|
|
700
|
+
it('should bump without a git tag, commit, and push in order', async () => {
|
|
701
|
+
await openReleasePr(BASE_ARGS);
|
|
702
|
+
const calls = mockedExec.mock.calls.map(args => args[0]);
|
|
703
|
+
expect(calls).toEqual([
|
|
704
|
+
'yarn version --no-git-tag-version --new-version 1.0.1-beta.0',
|
|
705
|
+
'git add package.json',
|
|
706
|
+
'git commit -m "chore: release v1.0.1-beta.0"',
|
|
707
|
+
'git push -u origin v1.0.1-beta.0-publish',
|
|
708
|
+
]);
|
|
709
|
+
});
|
|
710
|
+
it('should note that npm takes a few minutes to make the version available', async () => {
|
|
711
|
+
await openReleasePr(BASE_ARGS);
|
|
712
|
+
expect(logger.log).toHaveBeenCalledWith(expect.stringContaining('npm can take a few minutes'));
|
|
713
|
+
});
|
|
714
|
+
it('should open the PR with gh using an argument list', async () => {
|
|
715
|
+
await openReleasePr({
|
|
716
|
+
...BASE_ARGS,
|
|
717
|
+
tag: TAG.LATEST,
|
|
718
|
+
newVersion: '1.0.1',
|
|
719
|
+
headBranch: 'v1.0.1-publish',
|
|
720
|
+
tempBranch: 'v1.0.1-publish',
|
|
721
|
+
});
|
|
722
|
+
expect(mockedExecFile).toHaveBeenCalledWith('gh', [
|
|
723
|
+
'pr',
|
|
724
|
+
'create',
|
|
725
|
+
'--base',
|
|
726
|
+
'main',
|
|
727
|
+
'--head',
|
|
728
|
+
'v1.0.1-publish',
|
|
729
|
+
'--title',
|
|
730
|
+
'chore: release v1.0.1',
|
|
731
|
+
'--body',
|
|
732
|
+
'Merging this PR publishes v1.0.1 to npm on the latest tag. The publish runs in CI after the merge.',
|
|
733
|
+
]);
|
|
734
|
+
});
|
|
735
|
+
it('should open the PR against baseBranch and name the tag in the body', async () => {
|
|
736
|
+
await openReleasePr({
|
|
737
|
+
...BASE_ARGS,
|
|
738
|
+
tag: TAG.PREVIOUS,
|
|
739
|
+
newVersion: '0.9.1',
|
|
740
|
+
baseBranch: 'previous-branch',
|
|
741
|
+
headBranch: 'v0.9.1-publish',
|
|
742
|
+
tempBranch: 'v0.9.1-publish',
|
|
743
|
+
originalBranch: 'previous-branch',
|
|
744
|
+
});
|
|
745
|
+
expect(mockedExecFile).toHaveBeenCalledWith('gh', expect.arrayContaining([
|
|
746
|
+
'--base',
|
|
747
|
+
'previous-branch',
|
|
748
|
+
'--body',
|
|
749
|
+
'Merging this PR publishes v0.9.1 to npm on the previous tag. The publish runs in CI after the merge.',
|
|
750
|
+
]));
|
|
751
|
+
});
|
|
752
|
+
it('should log the PR URL', async () => {
|
|
753
|
+
await openReleasePr(BASE_ARGS);
|
|
754
|
+
expect(logger.success).toHaveBeenCalledWith(`Release PR opened: ${PR_URL}`);
|
|
755
|
+
});
|
|
756
|
+
it('should push and open the PR from headBranch', async () => {
|
|
757
|
+
await openReleasePr({
|
|
758
|
+
...BASE_ARGS,
|
|
759
|
+
headBranch: 'feature-branch',
|
|
760
|
+
onTempBranch: false,
|
|
761
|
+
originalBranch: 'feature-branch',
|
|
762
|
+
});
|
|
763
|
+
expect(mockedExec).toHaveBeenCalledWith('git push -u origin feature-branch');
|
|
764
|
+
expect(mockedExecFile).toHaveBeenCalledWith('gh', expect.arrayContaining(['--head', 'feature-branch']));
|
|
765
|
+
});
|
|
766
|
+
it('should never build, publish, or open a browser', async () => {
|
|
767
|
+
await openReleasePr(BASE_ARGS);
|
|
768
|
+
expect(mockedExec).not.toHaveBeenCalledWith('yarn build');
|
|
769
|
+
expect(mockedPublish).not.toHaveBeenCalled();
|
|
770
|
+
expect(vi.mocked(open)).not.toHaveBeenCalled();
|
|
771
|
+
});
|
|
772
|
+
it('should not commit, push, or open a PR on dry run and should revert the bump', async () => {
|
|
773
|
+
await openReleasePr({
|
|
774
|
+
...BASE_ARGS,
|
|
775
|
+
isDryRun: true,
|
|
776
|
+
onTempBranch: false,
|
|
777
|
+
});
|
|
778
|
+
expect(mockedExec).not.toHaveBeenCalledWith(expect.stringMatching(/^(git commit|git push)/));
|
|
779
|
+
expect(mockedExecFile).not.toHaveBeenCalled();
|
|
780
|
+
expect(mockedGitCleanup).toHaveBeenCalledWith('1.0.1-beta.0', false);
|
|
781
|
+
});
|
|
782
|
+
it('should preview the headBranch on dry run', async () => {
|
|
783
|
+
await openReleasePr({
|
|
784
|
+
...BASE_ARGS,
|
|
785
|
+
isDryRun: true,
|
|
786
|
+
onTempBranch: false,
|
|
787
|
+
});
|
|
788
|
+
expect(logger.log).toHaveBeenCalledWith(' git push -u origin v1.0.1-beta.0-publish');
|
|
789
|
+
expect(logger.log).toHaveBeenCalledWith(expect.stringContaining('--head v1.0.1-beta.0-publish'));
|
|
790
|
+
});
|
|
791
|
+
it('should roll back to the original branch when the push fails', async () => {
|
|
792
|
+
failOn('git push');
|
|
793
|
+
await expect(openReleasePr(BASE_ARGS)).rejects.toMatchObject({
|
|
794
|
+
message: '',
|
|
795
|
+
});
|
|
796
|
+
expect(mockedExec).toHaveBeenCalledWith('git reset HEAD~');
|
|
797
|
+
expect(mockedGitCleanup).toHaveBeenCalledWith('1.0.1-beta.0', false, 'v1.0.1-beta.0-publish', 'main');
|
|
798
|
+
});
|
|
799
|
+
it('should not reset HEAD when the failure happens before the commit', async () => {
|
|
800
|
+
failOn('yarn version');
|
|
801
|
+
await expect(openReleasePr(BASE_ARGS)).rejects.toThrow(ReleaseError);
|
|
802
|
+
expect(mockedExec).not.toHaveBeenCalledWith('git reset HEAD~');
|
|
803
|
+
expect(mockedGitCleanup).toHaveBeenCalled();
|
|
804
|
+
});
|
|
805
|
+
it('should print manual recovery commands and still throw ReleaseError when the rollback fails', async () => {
|
|
806
|
+
mockedExec.mockImplementation((cmd) => {
|
|
807
|
+
if (cmd.startsWith('git push') || cmd === 'git reset HEAD~') {
|
|
808
|
+
return Promise.reject(new Error(`${cmd} failed`));
|
|
809
|
+
}
|
|
810
|
+
return Promise.resolve({ stdout: '', stderr: '' });
|
|
811
|
+
});
|
|
812
|
+
await expect(openReleasePr(BASE_ARGS)).rejects.toMatchObject({
|
|
813
|
+
name: 'ReleaseError',
|
|
814
|
+
message: '',
|
|
815
|
+
});
|
|
816
|
+
expect(mockedGitCleanup).not.toHaveBeenCalled();
|
|
817
|
+
expect(logger.error).toHaveBeenCalledWith('Automatic cleanup failed. To restore your repository, run: git reset HEAD~ && git checkout HEAD -- . && git checkout main && git branch -D v1.0.1-beta.0-publish');
|
|
818
|
+
});
|
|
819
|
+
it('should print recovery commands when gitCleanup fails', async () => {
|
|
820
|
+
failOn('yarn version');
|
|
821
|
+
mockedGitCleanup.mockRejectedValue(new Error('unmerged paths'));
|
|
822
|
+
await expect(openReleasePr(BASE_ARGS)).rejects.toThrow(ReleaseError);
|
|
823
|
+
expect(logger.error).toHaveBeenCalledWith('Automatic cleanup failed. To restore your repository, run: git checkout HEAD -- . && git checkout main && git branch -D v1.0.1-beta.0-publish');
|
|
824
|
+
});
|
|
825
|
+
it('should tell the user how to finish or undo when PR creation fails after the push', async () => {
|
|
826
|
+
mockedExecFile.mockRejectedValue(new Error('gh pr create failed'));
|
|
827
|
+
await expect(openReleasePr(BASE_ARGS)).rejects.toThrow(ReleaseError);
|
|
828
|
+
expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('git push origin --delete v1.0.1-beta.0-publish'));
|
|
829
|
+
expect(mockedGitCleanup).toHaveBeenCalledWith('1.0.1-beta.0', false, 'v1.0.1-beta.0-publish', 'main');
|
|
830
|
+
});
|
|
242
831
|
});
|
|
243
832
|
describe('tryPublish', () => {
|
|
244
833
|
const BASE_ARGS = {
|
|
@@ -367,6 +956,7 @@ describe('release/steps', () => {
|
|
|
367
956
|
await logReleaseSuccess(BASE_ARGS);
|
|
368
957
|
expect(logger.success).toHaveBeenCalledWith(`${PACKAGE_NAME} version 1.0.1-beta.0 published successfully`);
|
|
369
958
|
expect(logger.log).toHaveBeenCalledWith(`https://www.npmjs.com/package/${PACKAGE_NAME}?activeTab=versions`);
|
|
959
|
+
expect(logger.log).toHaveBeenCalledWith(expect.stringContaining('npm can take a few minutes'));
|
|
370
960
|
});
|
|
371
961
|
it('should not log the GitHub release reminder for next tag', async () => {
|
|
372
962
|
await logReleaseSuccess(BASE_ARGS);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { validatePreviousBranchConfigured, validateBranchForTag, validateTagIncrementCombination, validateVersionSync, validateNextReleaseIsNotOverIncrement, } from '../validate.js';
|
|
2
|
+
import { validatePreviousBranchConfigured, validateBranchForTag, validateTagIncrementCombination, validateVersionSync, validateNextReleaseIsNotOverIncrement, rejectVersionIncrement, } from '../validate.js';
|
|
3
3
|
import { ReleaseError } from '../errors.js';
|
|
4
4
|
import { TAG, INCREMENT } from '../../../constants/release.js';
|
|
5
5
|
const DIST_TAGS = {
|
|
@@ -273,4 +273,12 @@ describe('release/validate', () => {
|
|
|
273
273
|
})).toThrow('prerelease');
|
|
274
274
|
});
|
|
275
275
|
});
|
|
276
|
+
describe('rejectVersionIncrement', () => {
|
|
277
|
+
it('should pass when no increment is given', () => {
|
|
278
|
+
expect(rejectVersionIncrement({})).toBe(true);
|
|
279
|
+
});
|
|
280
|
+
it('should explain that experimental versions come from latest when -v is given', () => {
|
|
281
|
+
expect(() => rejectVersionIncrement({ versionIncrement: 'minor' })).toThrow("Experimental releases don't take -v");
|
|
282
|
+
});
|
|
283
|
+
});
|
|
276
284
|
});
|