-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
index.js
113 lines (98 loc) · 3.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import test from 'ava';
import sinon from 'sinon';
import esmock from 'esmock';
import {npmConfig as packageManager} from '../source/package-manager/configs.js';
import * as util from '../source/util.js';
import np from '../source/index.js';
const defaultOptions = {
cleanup: true,
tests: true,
publish: true,
packageManager,
runPublish: true,
availability: {
isAvailable: false,
isUnknown: false,
},
renderer: 'silent',
};
const npPackageResult = await util.readPackage();
const npFails = test.macro(async (t, inputs, message) => {
await t.throwsAsync(
Promise.all(inputs.map(input => np(input, defaultOptions, npPackageResult))),
{message},
);
});
test('version is invalid', npFails,
['foo', '4.x.3'],
/New version (?:foo|4\.x\.3) should either be one of patch, minor, major, prepatch, preminor, premajor, prerelease, or a valid SemVer version\./,
);
test('version is pre-release', npFails,
['premajor', 'preminor', 'prepatch', 'prerelease', '100.0.0-0', '100.0.0-beta'],
'You must specify a dist-tag using --tag when publishing a pre-release version. This prevents accidentally tagging unstable versions as "latest". https://docs.npmjs.com/cli/dist-tag',
);
test('errors on too low version', npFails,
['1.0.0', '1.0.0-beta'],
/New version 1\.0\.0(?:-beta)? should be higher than current version \d+\.\d+\.\d+/,
);
const fakeExecaReturn = () => Object.assign(
Promise.resolve({pipe: sinon.stub()}),
{stdout: '', stderr: ''},
);
test('skip enabling 2FA if the package exists', async t => {
const enable2faStub = sinon.stub();
/** @type {typeof np} */
const npMock = await esmock('../source/index.js', {
del: {deleteAsync: sinon.stub()},
execa: {execa: sinon.stub().returns(fakeExecaReturn())},
'../source/prerequisite-tasks.js': sinon.stub(),
'../source/git-tasks.js': sinon.stub(),
'../source/git-util.js': {
hasUpstream: sinon.stub().returns(true),
pushGraceful: sinon.stub(),
verifyWorkingTreeIsClean: sinon.stub(),
},
'../source/npm/enable-2fa.js': enable2faStub,
'../source/npm/publish.js': {
getPackagePublishArguments: sinon.stub().returns([]),
runPublish: sinon.stub().returns(fakeExecaReturn()),
},
});
await t.notThrowsAsync(npMock('1.0.0', {
...defaultOptions,
availability: {
isAvailable: false,
isUnknown: false,
},
}, npPackageResult));
t.true(enable2faStub.notCalled);
});
test('skip enabling 2FA if the `2fa` option is false', async t => {
const enable2faStub = sinon.stub();
/** @type {typeof np} */
const npMock = await esmock('../source/index.js', {
del: {deleteAsync: sinon.stub()},
execa: {execa: sinon.stub().returns(fakeExecaReturn())},
'../source/prerequisite-tasks.js': sinon.stub(),
'../source/git-tasks.js': sinon.stub(),
'../source/git-util.js': {
hasUpstream: sinon.stub().returns(true),
pushGraceful: sinon.stub(),
verifyWorkingTreeIsClean: sinon.stub(),
},
'../source/npm/enable-2fa.js': enable2faStub,
'../source/npm/publish.js': {
getPackagePublishArguments: sinon.stub().returns([]),
runPublish: sinon.stub().returns(fakeExecaReturn()),
},
});
await t.notThrowsAsync(npMock('1.0.0', {
...defaultOptions,
availability: {
isAvailable: true,
isUnknown: false,
},
'2fa': false,
}, npPackageResult));
t.true(enable2faStub.notCalled);
});