-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add coverage for exit function
This adds test coverage for the shell.exit() function. This also refactors how we mock stdout/stderr and adds support for mocking process.exit() (which was needed for this change). While I was writing these tests, I realized there was an edge case I missed in PR #1122. This change fixes that edge case. Issue #1013
- Loading branch information
Showing
7 changed files
with
159 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import test from 'ava'; | ||
|
||
import shell from '..'; | ||
|
||
import mocks from './utils/mocks'; | ||
|
||
// | ||
// Valids | ||
// | ||
|
||
function runExitInSubprocess(code) { | ||
const script = code !== undefined | ||
? `var shell = require("."); shell.exit(${code});` | ||
: 'var shell = require("."); shell.exit();'; | ||
const result = shell.exec( | ||
`${JSON.stringify(shell.config.execPath)} -e ${JSON.stringify(script)}` | ||
); | ||
const actualReturnCode = result.code; | ||
return actualReturnCode; | ||
} | ||
|
||
test('exit with success status code', t => { | ||
t.is(runExitInSubprocess(0), 0); | ||
}); | ||
|
||
test('exit without explicit code should be success', t => { | ||
t.is(runExitInSubprocess(), 0); | ||
}); | ||
|
||
test('exit with failure status code', t => { | ||
t.is(runExitInSubprocess(5), 5); | ||
t.is(runExitInSubprocess(2), 2); | ||
t.is(runExitInSubprocess(25), 25); | ||
}); | ||
|
||
test('exit correctly sets the shell.errorCode()', t => { | ||
try { | ||
mocks.exit.init(); | ||
shell.exit(5); // Safe to call shell.exit() because it's mocked. | ||
t.is(shell.errorCode(), 5); | ||
t.is(mocks.exit.getValue(), 5); | ||
t.truthy(shell.error()); | ||
|
||
shell.exit(0); // Safe to call shell.exit() because it's mocked. | ||
t.is(shell.errorCode(), 0); | ||
t.falsy(mocks.exit.getValue()); | ||
t.falsy(shell.error()); | ||
|
||
// Also try it without an explicit argument. | ||
shell.exit(); // Safe to call shell.exit() because it's mocked. | ||
t.is(shell.errorCode(), 0); | ||
t.falsy(mocks.exit.getValue()); | ||
t.falsy(shell.error()); | ||
} finally { | ||
mocks.exit.restore(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.