Skip to content

Commit 3e555cb

Browse files
committed
Throw error when attempt to spawn override script fails
1 parent 7aa2f75 commit 3e555cb

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

__tests__/cli/CLI.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,40 @@ describe('CLI', () => {
417417
expect(consoleLogMock).toHaveBeenCalledWith('Script result');
418418
});
419419

420+
test('should print error as result when attempt to spawn overriding script fails', async () => {
421+
const consoleLogMock = jest.fn();
422+
jest.spyOn(console, 'log').mockImplementation(consoleLogMock);
423+
424+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
425+
// @ts-ignore
426+
child_process.spawnSync = jest.fn(() => ({
427+
status: 1,
428+
error: new Error('spawnSync failure'),
429+
}));
430+
431+
const definition = `{
432+
"StartAt": "AddNumbers",
433+
"States": {
434+
"AddNumbers": {
435+
"Type": "Task",
436+
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:AddNumbers",
437+
"End": true
438+
}
439+
}
440+
}`;
441+
442+
const program = makeProgram();
443+
const overrideTaskMapping = 'AddNumbers:./override.sh';
444+
const inputs = ['{ "num1": 1, "num2": 2 }'];
445+
446+
await program.parseAsync(['-d', definition, '-t', overrideTaskMapping, ...inputs], { from: 'user' });
447+
448+
expect(child_process.spawnSync).toHaveBeenCalled();
449+
expect(consoleLogMock).toHaveBeenCalledWith(
450+
"Execution has failed with the following error: Attempt to run task override './override.sh' for state 'AddNumbers' failed: spawnSync failure"
451+
);
452+
});
453+
420454
test('should print error as result when overriding script terminates with non-zero exit code', async () => {
421455
const consoleLogMock = jest.fn();
422456
jest.spyOn(console, 'log').mockImplementation(consoleLogMock);
@@ -481,7 +515,7 @@ describe('CLI', () => {
481515

482516
expect(child_process.spawnSync).toHaveBeenCalled();
483517
expect(consoleLogMock).toHaveBeenCalledWith(
484-
"Execution has failed with the following error: Parsing of output '{ key: value }' in task override './override.sh' for state 'AddNumbers' failed: Unexpected token k in JSON at position 2"
518+
"Execution has failed with the following error: Parsing of output '{ key: value }' from task override './override.sh' for state 'AddNumbers' failed: Unexpected token k in JSON at position 2"
485519
);
486520
});
487521
});

src/cli/ArgumentParsers.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ function parseOverrideTaskOption(
5252
previous[taskStateName] = (input) => {
5353
const spawnResult = spawnSync(scriptPath, [JSON.stringify(input)]);
5454

55+
if (spawnResult.error) {
56+
throw new Error(
57+
`Attempt to run task override '${scriptPath}' for state '${taskStateName}' failed: ${spawnResult.error.message}`
58+
);
59+
}
60+
5561
if (spawnResult.status !== 0) {
5662
throw new Error(`${scriptPath} ('${taskStateName}'): ${spawnResult.stderr.toString()}`);
5763
}
@@ -60,7 +66,7 @@ function parseOverrideTaskOption(
6066
const jsonOrError = tryJSONParse<JSONValue>(overrideResult);
6167
if (jsonOrError instanceof Error) {
6268
throw new Error(
63-
`Parsing of output '${overrideResult}' in task override '${scriptPath}' for state '${taskStateName}' failed: ${jsonOrError.message}`
69+
`Parsing of output '${overrideResult}' from task override '${scriptPath}' for state '${taskStateName}' failed: ${jsonOrError.message}`
6470
);
6571
}
6672

0 commit comments

Comments
 (0)