Skip to content

Commit 0fee76f

Browse files
committed
fix: verify the cmd config independently for each steps
1 parent 06d1a74 commit 0fee76f

File tree

9 files changed

+155
-49
lines changed

9 files changed

+155
-49
lines changed

index.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
1-
const {castArray, isPlainObject} = require('lodash');
21
const parseJson = require('parse-json');
32
const debug = require('debug')('semantic-release:exec');
43
const SemanticReleaseError = require('@semantic-release/error');
54
const execScript = require('./lib/exec-script');
65
const verifyConfig = require('./lib/verify-config');
76

8-
const PLUGIN_TYPES = ['analyzeCommits', 'verifyRelease', 'generateNotes', 'publish', 'success', 'fail'];
9-
107
async function verifyConditions(pluginConfig, context) {
11-
for (const [option, value] of Object.entries(context.options || {})) {
12-
if (PLUGIN_TYPES.includes(option)) {
13-
for (const plugin of castArray(value)) {
14-
if (
15-
plugin === '@semantic-release/exec' ||
16-
(isPlainObject(plugin) && plugin.path === '@semantic-release/exec')
17-
) {
18-
verifyConfig(plugin);
19-
}
20-
}
21-
}
22-
}
23-
248
verifyConfig(pluginConfig);
259

2610
try {
@@ -31,11 +15,15 @@ async function verifyConditions(pluginConfig, context) {
3115
}
3216

3317
async function analyzeCommits(pluginConfig, context) {
18+
verifyConfig(pluginConfig);
19+
3420
const stdout = await execScript(pluginConfig, context);
3521
return stdout.trim() ? stdout : undefined;
3622
}
3723

3824
async function verifyRelease(pluginConfig, context) {
25+
verifyConfig(pluginConfig);
26+
3927
try {
4028
await execScript(pluginConfig, context);
4129
} catch (err) {
@@ -44,15 +32,22 @@ async function verifyRelease(pluginConfig, context) {
4432
}
4533

4634
async function generateNotes(pluginConfig, context) {
35+
verifyConfig(pluginConfig);
36+
4737
return execScript(pluginConfig, context);
4838
}
4939

5040
async function prepare(pluginConfig, context) {
41+
verifyConfig(pluginConfig);
42+
5143
await execScript(pluginConfig, context);
5244
}
5345

5446
async function publish(pluginConfig, context) {
47+
verifyConfig(pluginConfig);
48+
5549
const stdout = await execScript(pluginConfig, context);
50+
5651
try {
5752
return stdout.trim() ? parseJson(stdout) : undefined;
5853
} catch (err) {
@@ -65,10 +60,14 @@ async function publish(pluginConfig, context) {
6560
}
6661

6762
async function success(pluginConfig, context) {
63+
verifyConfig(pluginConfig);
64+
6865
await execScript(pluginConfig, context);
6966
}
7067

7168
async function fail(pluginConfig, context) {
69+
verifyConfig(pluginConfig);
70+
7271
await execScript(pluginConfig, context);
7372
}
7473

test/analyze-commits.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ test('Return "undefined" if the analyzeCommits script wrtite nothing to stdout',
3232
t.is(result, undefined);
3333
});
3434

35+
test('Throw "SemanticReleaseError" if "cmd" options is missing', async t => {
36+
const pluginConfig = {};
37+
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger};
38+
39+
const error = await t.throws(analyzeCommits(pluginConfig, context));
40+
41+
t.is(error.name, 'SemanticReleaseError');
42+
t.is(error.code, 'EINVALIDCMD');
43+
});
44+
45+
test('Throw "SemanticReleaseError" if "cmd" options is empty', async t => {
46+
const pluginConfig = {cmd: ' '};
47+
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger, options: {}};
48+
49+
const error = await t.throws(analyzeCommits(pluginConfig, context));
50+
51+
t.is(error.name, 'SemanticReleaseError');
52+
t.is(error.code, 'EINVALIDCMD');
53+
});
54+
3555
test('Throw Error if if the analyzeCommits script does not returns 0', async t => {
3656
const pluginConfig = {cmd: 'exit 1'};
3757
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger};

test/fail.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@ test('Execute script in fail step', async t => {
1919
await t.notThrows(fail(pluginConfig, context));
2020
});
2121

22+
test('Throw "SemanticReleaseError" if "cmd" options is missing', async t => {
23+
const pluginConfig = {};
24+
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger};
25+
26+
const error = await t.throws(fail(pluginConfig, context));
27+
28+
t.is(error.name, 'SemanticReleaseError');
29+
t.is(error.code, 'EINVALIDCMD');
30+
});
31+
32+
test('Throw "SemanticReleaseError" if "cmd" options is empty', async t => {
33+
const pluginConfig = {cmd: ' '};
34+
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger, options: {}};
35+
36+
const error = await t.throws(fail(pluginConfig, context));
37+
38+
t.is(error.name, 'SemanticReleaseError');
39+
t.is(error.code, 'EINVALIDCMD');
40+
});
41+
2242
test('Throw "Error" if the fail script does not returns 0', async t => {
2343
const pluginConfig = {cmd: 'exit 1'};
2444
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger};

test/generate-notes.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ test('Return the value generateNotes script wrote to stdout', async t => {
2222
t.is(result, 'Release note');
2323
});
2424

25+
test('Throw "SemanticReleaseError" if "cmd" options is missing', async t => {
26+
const pluginConfig = {};
27+
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger};
28+
29+
const error = await t.throws(generateNotes(pluginConfig, context));
30+
31+
t.is(error.name, 'SemanticReleaseError');
32+
t.is(error.code, 'EINVALIDCMD');
33+
});
34+
35+
test('Throw "SemanticReleaseError" if "cmd" options is empty', async t => {
36+
const pluginConfig = {cmd: ' '};
37+
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger, options: {}};
38+
39+
const error = await t.throws(generateNotes(pluginConfig, context));
40+
41+
t.is(error.name, 'SemanticReleaseError');
42+
t.is(error.code, 'EINVALIDCMD');
43+
});
44+
2545
test('Throw "Error" if if the generateNotes script does not returns 0', async t => {
2646
const pluginConfig = {cmd: 'exit 1'};
2747
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger};

test/prepare.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@ test('Execute script in prepare step', async t => {
1919
await t.notThrows(prepare(pluginConfig, context));
2020
});
2121

22+
test('Throw "SemanticReleaseError" if "cmd" options is missing', async t => {
23+
const pluginConfig = {};
24+
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger};
25+
26+
const error = await t.throws(prepare(pluginConfig, context));
27+
28+
t.is(error.name, 'SemanticReleaseError');
29+
t.is(error.code, 'EINVALIDCMD');
30+
});
31+
32+
test('Throw "SemanticReleaseError" if "cmd" options is empty', async t => {
33+
const pluginConfig = {cmd: ' '};
34+
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger, options: {}};
35+
36+
const error = await t.throws(prepare(pluginConfig, context));
37+
38+
t.is(error.name, 'SemanticReleaseError');
39+
t.is(error.code, 'EINVALIDCMD');
40+
});
41+
2242
test('Throw "Error" if the prepare script does not returns 0', async t => {
2343
const pluginConfig = {cmd: 'exit 1'};
2444
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger};

test/publish.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ test('Return "undefined" if the publish script wrtite nothing to stdout', async
4343
t.is(result, undefined);
4444
});
4545

46+
test('Throw "SemanticReleaseError" if "cmd" options is missing', async t => {
47+
const pluginConfig = {};
48+
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger};
49+
50+
const error = await t.throws(publish(pluginConfig, context));
51+
52+
t.is(error.name, 'SemanticReleaseError');
53+
t.is(error.code, 'EINVALIDCMD');
54+
});
55+
56+
test('Throw "SemanticReleaseError" if "cmd" options is empty', async t => {
57+
const pluginConfig = {cmd: ' '};
58+
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger, options: {}};
59+
60+
const error = await t.throws(publish(pluginConfig, context));
61+
62+
t.is(error.name, 'SemanticReleaseError');
63+
t.is(error.code, 'EINVALIDCMD');
64+
});
65+
4666
test('Throw "Error" if the publish script does not returns 0', async t => {
4767
const pluginConfig = {cmd: 'exit 1'};
4868
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger, options: {}};

test/success.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@ test('Execute script in success step', async t => {
1919
await t.notThrows(success(pluginConfig, context));
2020
});
2121

22+
test('Throw "SemanticReleaseError" if "cmd" options is missing', async t => {
23+
const pluginConfig = {};
24+
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger};
25+
26+
const error = await t.throws(success(pluginConfig, context));
27+
28+
t.is(error.name, 'SemanticReleaseError');
29+
t.is(error.code, 'EINVALIDCMD');
30+
});
31+
32+
test('Throw "SemanticReleaseError" if "cmd" options is empty', async t => {
33+
const pluginConfig = {cmd: ' '};
34+
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger, options: {}};
35+
36+
const error = await t.throws(success(pluginConfig, context));
37+
38+
t.is(error.name, 'SemanticReleaseError');
39+
t.is(error.code, 'EINVALIDCMD');
40+
});
41+
2242
test('Throw "Error" if the success script does not returns 0', async t => {
2343
const pluginConfig = {cmd: 'exit 1'};
2444
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger};

test/verify-confitions.test.js

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,39 +39,6 @@ test('Throw "SemanticReleaseError" if "cmd" options is empty', async t => {
3939
t.is(error.code, 'EINVALIDCMD');
4040
});
4141

42-
test('Throw "SemanticReleaseError" if another exec plugin "cmd" options is missing', async t => {
43-
const pluginConfig = {cmd: './test/fixtures/echo-args.sh'};
44-
const context = {
45-
stdout: t.context.stdout,
46-
stderr: t.context.stderr,
47-
logger: t.context.logger,
48-
options: {publish: ['@semantic-release/npm', {path: '@semantic-release/exec'}]},
49-
};
50-
51-
const error = await t.throws(verifyConditions(pluginConfig, context));
52-
53-
t.is(error.name, 'SemanticReleaseError');
54-
t.is(error.code, 'EINVALIDCMD');
55-
});
56-
57-
test('Throw "SemanticReleaseError" if another exec plugin "cmd" options is empty', async t => {
58-
const pluginConfig = {cmd: './test/fixtures/echo-args.sh'};
59-
const context = {
60-
stdout: t.context.stdout,
61-
stderr: t.context.stderr,
62-
logger: t.context.logger,
63-
options: {
64-
branch: 'master',
65-
publish: ['@semantic-release/npm', {path: '@semantic-release/exec', cmd: ' '}],
66-
},
67-
};
68-
69-
const error = await t.throws(verifyConditions(pluginConfig, context));
70-
71-
t.is(error.name, 'SemanticReleaseError');
72-
t.is(error.code, 'EINVALIDCMD');
73-
});
74-
7542
test('Return if the verifyConditions script returns 0', async t => {
7643
const pluginConfig = {cmd: 'exit 0'};
7744
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger, options: {}};

test/verify-release.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@ test('Return if the verifyRelease script returns 0', async t => {
1919
await t.notThrows(verifyRelease(pluginConfig, context));
2020
});
2121

22+
test('Throw "SemanticReleaseError" if "cmd" options is missing', async t => {
23+
const pluginConfig = {};
24+
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger};
25+
26+
const error = await t.throws(verifyRelease(pluginConfig, context));
27+
28+
t.is(error.name, 'SemanticReleaseError');
29+
t.is(error.code, 'EINVALIDCMD');
30+
});
31+
32+
test('Throw "SemanticReleaseError" if "cmd" options is empty', async t => {
33+
const pluginConfig = {cmd: ' '};
34+
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger, options: {}};
35+
36+
const error = await t.throws(verifyRelease(pluginConfig, context));
37+
38+
t.is(error.name, 'SemanticReleaseError');
39+
t.is(error.code, 'EINVALIDCMD');
40+
});
41+
2242
test('Throw "SemanticReleaseError" if the verifyRelease script does not returns 0', async t => {
2343
const pluginConfig = {cmd: 'exit 1'};
2444
const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger, options: {}};

0 commit comments

Comments
 (0)