shelljs/test/exit.js
Nate Fischer 39ebb71846 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
2023-11-11 18:27:54 -08:00

58 lines
1.5 KiB
JavaScript

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();
}
});