Skip to content

Commit 33f9f78

Browse files
committedJun 13, 2021
feat: refactor verify logic to drop one dependency
1 parent db9132d commit 33f9f78

9 files changed

+289
-79
lines changed
 

‎lib/die.js

-1
This file was deleted.

‎lib/verify-auth.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@ const execa = require('execa');
22
const SemanticReleaseError = require('@semantic-release/error');
33

44
module.exports = async (logger) => {
5-
logger.log('Verify authentication for vsce');
5+
logger.log('Verifying authentication for vsce');
66

77
if (!('VSCE_PAT' in process.env) && ('VSCE_TOKEN' in process.env)) {
88
logger.log('VSCE_TOKEN is deprecated and may be removed in the next major release, please use VSCE_PAT instead.');
99
process.env.VSCE_PAT = process.env.VSCE_TOKEN;
1010
}
1111

1212
if (!process.env.VSCE_PAT) {
13-
throw new SemanticReleaseError('No vsce personal access token specified (set the "VSCE_PAT" environment variable).', 'ENOVSCEPAT');
13+
throw new SemanticReleaseError('No vsce personal access token specified (set the `VSCE_PAT` environment variable).', 'ENOVSCEPAT');
1414
}
1515

1616
try {
17-
const options = ['verify-pat'];
18-
execa.sync('vsce', options);
17+
await execa('vsce', ['verify-pat']);
1918
} catch (e) {
2019
throw new SemanticReleaseError(`Invalid vsce token. Additional information:\n\n${e}`, 'EINVALIDVSCETOKEN');
2120
}

‎lib/verify-pkg.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
const SemanticReleaseError = require('@semantic-release/error');
2+
const { readJson } = require('fs-extra');
3+
const fs = require('fs');
4+
5+
module.exports = async () => {
6+
if (!fs.existsSync('./package.json')) {
7+
throw new SemanticReleaseError(
8+
'The `package.json` was not found. A `package.json` is required to release with vsce.',
9+
'ENOPKG'
10+
);
11+
}
12+
13+
let packageJson;
14+
15+
try {
16+
packageJson = await readJson('./package.json');
17+
} catch (error) {
18+
throw new SemanticReleaseError('The `package.json` seems to be invalid.', 'EINVALIDPKG');
19+
}
20+
21+
const { name, publisher } = packageJson;
222

3-
module.exports = ({ name, publisher }) => {
423
if (!name) {
5-
throw new SemanticReleaseError('No "name" found in package.json.', 'ENOPKGNAME');
24+
throw new SemanticReleaseError('No `name` found in `package.json`.', 'ENOPKGNAME');
625
}
726
if (!publisher) {
8-
throw new SemanticReleaseError('No "publisher" found in package.json.', 'ENOPUBLISHER');
27+
throw new SemanticReleaseError('No `publisher` found in `package.json`.', 'ENOPUBLISHER');
928
}
1029
};

‎lib/verify.js

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
const verifyPkg = require('./verify-pkg');
22
const verifyAuth = require('./verify-auth');
3-
const getPkg = require('read-pkg-up');
4-
const SemanticReleaseError = require('@semantic-release/error');
53

64
module.exports = async logger => {
7-
const { packageJson } = await getPkg();
8-
if (!packageJson) {
9-
throw new SemanticReleaseError(
10-
'package.json not found. A package.json is required to release with vsce.',
11-
'ENOPKG'
12-
);
13-
}
14-
verifyPkg(packageJson);
5+
await verifyPkg();
156

16-
verifyAuth(logger);
7+
await verifyAuth(logger);
178
};

‎package-lock.json

+98-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
"@semantic-release/error": "^2.2.0",
5656
"execa": "^5.0.0",
5757
"fs-extra": "^10.0.0",
58-
"read-pkg-up": "^7.0.1",
5958
"vsce": "^1.85.1"
6059
},
6160
"peerDependencies": {

‎test/verify-auth.test.js

+53-17
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,100 @@
11
const test = require('ava');
22
const sinon = require('sinon');
3-
const execa = require('execa');
3+
const proxyquire = require('proxyquire');
44
const SemanticReleaseError = require('@semantic-release/error');
5-
const verifyAuth = require('../lib/verify-auth');
65

76
const logger = {
87
log: sinon.fake()
98
};
109

11-
sinon.stub(execa, 'sync').withArgs('vsce', ['verify-pat', '--pat', process.env.VSCE_TOKEN]).returns();
12-
1310
test('VSCE_TOKEN is set', async t => {
14-
process.env.VSCE_TOKEN = 'abc123';
11+
sinon.stub(process, 'env').value({
12+
VSCE_TOKEN: 'abc123'
13+
});
14+
15+
const verifyAuth = proxyquire('../lib/verify-auth', {
16+
execa: sinon.stub().withArgs('vsce', ['verify-pat']).resolves()
17+
});
1518

1619
await t.notThrowsAsync(() => verifyAuth(logger));
1720
});
1821

1922
test('VSCE_TOKEN is not set', async t => {
20-
delete process.env.VSCE_TOKEN;
21-
delete process.env.VSCE_PAT;
23+
sinon.stub(process, 'env').value({});
24+
25+
const verifyAuth = proxyquire('../lib/verify-auth', {
26+
execa: sinon.stub().withArgs('vsce', ['verify-pat']).resolves()
27+
});
2228

2329
await t.throwsAsync(() => verifyAuth(logger), { instanceOf: SemanticReleaseError, code: 'ENOVSCEPAT' });
2430
});
2531

2632
test('VSCE_PAT is set', async t => {
27-
process.env.VSCE_PAT = 'abc123';
33+
sinon.stub(process, 'env').value({
34+
VSCE_PAT: 'abc123'
35+
});
36+
37+
const verifyAuth = proxyquire('../lib/verify-auth', {
38+
execa: sinon.stub().withArgs('vsce', ['verify-pat']).resolves()
39+
});
2840

2941
await t.notThrowsAsync(() => verifyAuth(logger));
3042
});
3143

3244
test('VSCE_PAT is not set', async t => {
33-
delete process.env.VSCE_TOKEN;
34-
delete process.env.VSCE_PAT;
45+
sinon.stub(process, 'env').value({});
46+
47+
const verifyAuth = proxyquire('../lib/verify-auth', {
48+
execa: sinon.stub().withArgs('vsce', ['verify-pat']).resolves()
49+
});
3550

3651
await t.throwsAsync(() => verifyAuth(logger), { instanceOf: SemanticReleaseError, code: 'ENOVSCEPAT' });
3752
});
3853

3954
test('VSCE_TOKEN is valid', async t => {
40-
process.env.VSCE_TOKEN = 'abc123';
55+
sinon.stub(process, 'env').value({
56+
VSCE_TOKEN: 'abc123'
57+
});
58+
59+
const verifyAuth = proxyquire('../lib/verify-auth', {
60+
execa: sinon.stub().withArgs('vsce', ['verify-pat']).resolves()
61+
});
4162

4263
await t.notThrowsAsync(() => verifyAuth(logger));
4364
});
4465

4566
test('VSCE_TOKEN is invalid', async t => {
46-
process.env.VSCE_TOKEN = 'abc123';
47-
execa.sync.restore();
67+
sinon.stub(process, 'env').value({
68+
VSCE_TOKEN: 'abc123'
69+
});
70+
71+
const verifyAuth = proxyquire('../lib/verify-auth', {
72+
execa: sinon.stub().withArgs('vsce', ['verify-pat']).rejects()
73+
});
4874

4975
await t.throwsAsync(() => verifyAuth(logger), { instanceOf: SemanticReleaseError, code: 'EINVALIDVSCETOKEN' });
5076
});
5177

5278
test('VSCE_PAT is valid', async t => {
53-
process.env.VSCE_PAT = 'abc123';
54-
sinon.stub(execa, 'sync').withArgs('vsce', ['verify-pat']).returns();
79+
sinon.stub(process, 'env').value({
80+
VSCE_PAT: 'abc123'
81+
});
82+
83+
const verifyAuth = proxyquire('../lib/verify-auth', {
84+
execa: sinon.stub().withArgs('vsce', ['verify-pat']).resolves()
85+
});
5586

5687
await t.notThrowsAsync(() => verifyAuth(logger));
5788
});
5889

5990
test('VSCE_PAT is invalid', async t => {
60-
process.env.VSCE_PAT = 'abc123';
61-
execa.sync.restore();
91+
sinon.stub(process, 'env').value({
92+
VSCE_PAT: 'abc123'
93+
});
94+
95+
const verifyAuth = proxyquire('../lib/verify-auth', {
96+
execa: sinon.stub().withArgs('vsce', ['verify-pat']).rejects()
97+
});
6298

6399
await t.throwsAsync(() => verifyAuth(logger), { instanceOf: SemanticReleaseError, code: 'EINVALIDVSCETOKEN' });
64100
});

‎test/verify-pkg.test.js

+97-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,105 @@
1+
const sinon = require('sinon');
12
const test = require('ava');
3+
const proxyquire = require('proxyquire');
24
const SemanticReleaseError = require('@semantic-release/error');
3-
const verifyPkg = require('../lib/verify-pkg');
45

5-
test('package is valid', t => {
6-
t.notThrows(() => verifyPkg({ name: 'test', publisher: 'tester' }));
6+
test('package.json is found', async t => {
7+
const name = 'test';
8+
const publisher = 'tester';
9+
10+
const verifyPkg = proxyquire('../lib/verify-pkg', {
11+
fs: {
12+
existsSync: sinon.stub().returns(true)
13+
},
14+
'fs-extra': {
15+
readJson: sinon.stub().returns({
16+
name,
17+
publisher
18+
})
19+
}
20+
});
21+
22+
await t.notThrowsAsync(() => verifyPkg());
23+
});
24+
25+
test('package.json is not found', async t => {
26+
const name = 'test';
27+
const publisher = 'tester';
28+
29+
const verifyPkg = proxyquire('../lib/verify-pkg', {
30+
fs: {
31+
existsSync: sinon.stub().returns(false)
32+
},
33+
'fs-extra': {
34+
readJson: sinon.stub().returns({
35+
name,
36+
publisher
37+
})
38+
}
39+
});
40+
41+
await t.throwsAsync(() => verifyPkg(), { instanceOf: SemanticReleaseError, code: 'ENOPKG' });
42+
});
43+
44+
test('package is valid', async t => {
45+
const name = 'test';
46+
const publisher = 'tester';
47+
const verifyPkg = proxyquire('../lib/verify-pkg', {
48+
fs: {
49+
existsSync: sinon.stub().returns(true)
50+
},
51+
'fs-extra': {
52+
readJson: sinon.stub().returns({
53+
publisher,
54+
name
55+
})
56+
}
57+
});
58+
59+
await t.notThrowsAsync(() => verifyPkg());
760
});
861

9-
test('package is missing name', t => {
10-
t.throws(() => verifyPkg({ publisher: 'tester' }), { instanceOf: SemanticReleaseError });
62+
test('package is invalid', async t => {
63+
const verifyPkg = proxyquire('../lib/verify-pkg', {
64+
fs: {
65+
existsSync: sinon.stub().returns(true)
66+
},
67+
'fs-extra': {
68+
readJson: sinon.stub().rejects()
69+
}
70+
});
71+
72+
await t.throwsAsync(() => verifyPkg(), { instanceOf: SemanticReleaseError, code: 'EINVALIDPKG' });
1173
});
1274

13-
test('package is missing publisher', t => {
14-
t.throws(() => verifyPkg({ name: 'test' }), { instanceOf: SemanticReleaseError });
75+
test('package is missing name', async t => {
76+
const publisher = 'tester';
77+
const verifyPkg = proxyquire('../lib/verify-pkg', {
78+
fs: {
79+
existsSync: sinon.stub().returns(true)
80+
},
81+
'fs-extra': {
82+
readJson: sinon.stub().returns({
83+
publisher
84+
})
85+
}
86+
});
87+
88+
await t.throwsAsync(() => verifyPkg(), { instanceOf: SemanticReleaseError, code: 'ENOPKGNAME' });
89+
});
90+
91+
test('package is missing publisher', async t => {
92+
const name = 'test';
93+
const verifyPkg = proxyquire('../lib/verify-pkg', {
94+
fs: {
95+
existsSync: sinon.stub().returns(true)
96+
},
97+
'fs-extra': {
98+
readJson: sinon.stub().returns({
99+
name
100+
})
101+
}
102+
});
103+
104+
await t.throwsAsync(() => verifyPkg(), { instanceOf: SemanticReleaseError, code: 'ENOPUBLISHER' });
15105
});

‎test/verify.test.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
const test = require('ava');
22
const sinon = require('sinon');
33
const proxyquire = require('proxyquire');
4-
const SemanticReleaseError = require('@semantic-release/error');
54

65
const logger = {
76
log: sinon.fake()
87
};
98

10-
test('package.json is found', async t => {
9+
test('resolves', async t => {
1110
const verify = proxyquire('../lib/verify', {
1211
'./verify-auth': sinon.stub().resolves(),
13-
'./verify-pkg': sinon.stub(),
14-
'read-pkg-up': sinon.stub().returns({ packageJson: {} })
12+
'./verify-pkg': sinon.stub().resolves()
1513
});
1614

1715
await t.notThrowsAsync(() => verify(logger));
1816
});
1917

20-
test('package.json is not found', async t => {
18+
test('rejects with verify-auth', async t => {
19+
const verify = proxyquire('../lib/verify', {
20+
'./verify-auth': sinon.stub().rejects(),
21+
'./verify-pkg': sinon.stub().resolves()
22+
});
23+
24+
await t.throwsAsync(() => verify(logger));
25+
});
26+
27+
test('rejects with verify-pkg', async t => {
2128
const verify = proxyquire('../lib/verify', {
2229
'./verify-auth': sinon.stub().resolves(),
23-
'./verify-pkg': sinon.stub(),
24-
'read-pkg-up': sinon.stub().returns({ })
30+
'./verify-pkg': sinon.stub().rejects()
2531
});
2632

27-
await t.throwsAsync(() => verify(logger), { instanceOf: SemanticReleaseError });
33+
await t.throwsAsync(() => verify(logger));
2834
});

0 commit comments

Comments
 (0)
Please sign in to comment.