Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #10 from jonabc/raise-on-exec-failure
Browse files Browse the repository at this point in the history
Raise on exec failure
  • Loading branch information
jonabc authored Jan 20, 2020
2 parents 7d3224a + 3c2af2c commit bca3b46
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 19 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ The `@actions/exec` mock catches all calls to `@actions/exec.exec` and
2. output provided `stdout` using the passed in options
3. output provided `stderr` using the passed in options
4. return an exit code
- if the command doesn't match a configured mock, returns 127
- if the command matches a configured mock, returns the configured `exitCode` or 0 if not set
- if the command doesn't match a configured mock, rejects with an error for exit code 127
- if the command matches a configured mock, resolves to the configured `exitCode` if 0 or rejects with an error for non-0 values

The mocked call returns a promise that is resolved for a 0 exit code and rejected for all other exit codes. Calls to `@actions/exec.exec` that specify `options: { ignoreReturnCode: true }` will never be rejected.
The mocked call returns a promise that is resolved for a 0 exit code and rejected with an error for all other exit codes. Calls to `@actions/exec.exec` that specify `options: { ignoreReturnCode: true }` will never be rejected.

```javascript
// with a resolved mocked command
Expand Down
2 changes: 1 addition & 1 deletion lib/mocks/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ sinon.stub(exec, 'exec').callsFake(async (command, args = [], options = {}) => {
}

if (exitCode !== 0 && !options.ignoreReturnCode) {
return Promise.reject(exitCode);
return Promise.reject(new Error(`Failed with exit code ${exitCode}`));
}

return Promise.resolve(exitCode);
Expand Down
22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jonabc/actions-mocks",
"version": "1.1.0",
"version": "1.1.1",
"description": "Mocks and utilities to help when testing GitHub Actions",
"main": "index.js",
"directories": {
Expand Down
8 changes: 6 additions & 2 deletions test/mocks/exec.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ it('logs the mocked command', async () => {
it('returns a rejected promise if ignoreReturnCode is not set with an error exit code', async () => {
mocks.setLog(() => {});
mocks.mock({ command: 'command', exitCode: 2 });
await expect(exec.exec('command', ['test'])).rejects.toEqual(2);
await expect(exec.exec('command', ['test'])).rejects.toThrow(
'Failed with exit code 2'
);
});

it('includes process.env.EXEC_MOCKS on load', async () => {
Expand All @@ -49,7 +51,9 @@ it('includes process.env.EXEC_MOCKS on load', async () => {
it('returns a failure exit code if a command isn\'t mocked', async () => {
mocks.setLog(() => {});
mocks.clear();
await expect(exec.exec('command', ['test'])).rejects.toEqual(127);
await expect(exec.exec('command', ['test'])).rejects.toThrow(
'Failed with exit code 127'
);
});

describe('mock', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/runner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('run', () => {
expect(out).toMatch('test-not-mocked exited with status 1');

expect(out).toMatch('test-not-ignored');
expect(out).toMatch('test-not-ignored caught with status 3');
expect(out).toMatch('test-not-ignored caught with status Error: Failed with exit code 3');
});


Expand Down

0 comments on commit bca3b46

Please sign in to comment.