Skip to content
This repository has been archived by the owner on May 8, 2018. It is now read-only.

Use process.exitCode instead of process.exit() #219

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internals.wrapReporter = function (name, fn, ...args) {

console.error(`Error in reporter: ${name}`);
console.error(err.stack);
process.exit(4);
process.exitCode = 4;
});
};

Expand Down Expand Up @@ -101,16 +101,14 @@ exports.wrap = function (name, handler) {

if (name === 'check' &&
result.data.length > 0 &&
!args['warn-only']) {

if (!args.threshold ||
(args.threshold && maxCvss > args.threshold)) {

process.exit(1);
}
!args['warn-only'] &&
(!args.threshold || (args.threshold && maxCvss > args.threshold))
) {
process.exitCode = 1;
}
else {
process.exitCode = 0;
}

process.exit(0);
});
}).catch((err) => {

Expand All @@ -136,11 +134,11 @@ exports.wrap = function (name, handler) {
if (!args['warn-only']) {
if (err.isServer) {
if (!args['ignore-server-errors']) {
process.exit(2);
process.exitCode = 2;
}
}
else {
process.exit(3);
process.exitCode = 3;
}
}
});
Expand Down
55 changes: 6 additions & 49 deletions test/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,123 +106,80 @@ describe('check handler', { parallel: false }, () => {

afterEach(() => {

Mock.resetExit();
MockFs.restore();
return server.stop();
});

it('can run a clean check', () => {

let exited = false;
Mock.exit((code) => {

expect(code).to.equal(0);
exited = true;
});

return Check.handler({
path: '/clean',
quiet: true,
baseUrl: server.info.uri
}).then(() => {

expect(exited).to.equal(true);
expect(process.exitCode).to.equal(0);
});
});

it('can run a check with one finding', () => {

let exited = false;
Mock.exit((code) => {

expect(code).to.equal(1);
exited = true;
});

return Check.handler({
path: '/single',
quiet: true,
baseUrl: server.info.uri
}).then(() => {

expect(exited).to.equal(true);
expect(process.exitCode).to.equal(1);
});
});

it('can run a check with multiple findings', () => {

let exited = false;
Mock.exit((code) => {

expect(code).to.equal(1);
exited = true;
});

return Check.handler({
path: '/multiple',
quiet: true,
baseUrl: server.info.uri
}).then(() => {

expect(exited).to.equal(true);
expect(process.exitCode).to.equal(1);
});
});

it('can run an offline check', () => {

let exited = false;
Mock.exit((code) => {

expect(code).to.equal(1);
exited = true;
});

return Check.handler({
path: '/offline',
quiet: true,
offline: true,
exceptions: []
}).then(() => {

expect(exited).to.equal(true);
expect(process.exitCode).to.equal(1);
});
});

it('can run a check when package.json is missing name field', () => {

let exited = false;
Mock.exit((code) => {

expect(code).to.equal(0);
exited = true;
});

return Check.handler({
path: '/missing_name',
quiet: true,
baseUrl: server.info.uri
}).then(() => {

expect(exited).to.equal(true);
expect(process.exitCode).to.equal(0);
});
});

it('exits with status 3 when package.json is invalid', () => {

let exited = false;
Mock.exit((code) => {

expect(code).to.equal(3);
exited = true;
});

return Check.handler({
path: '/broken',
quiet: true,
baseUrl: server.info.uri
}).then(() => {

expect(exited).to.equal(true);
expect(process.exitCode).to.equal(3);
});
});
});
19 changes: 2 additions & 17 deletions test/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,20 @@ describe('login handler', () => {

afterEach(() => {

Mock.resetExit();
MockFs.restore();
return server.stop();
});

it('saves token when login succeeds', () => {

let exited = false;
Mock.exit((code) => {

expect(code).to.equal(0);
exited = true;
});

return Login.handler({
email: 'test@user.com',
password: 'testuser',
quiet: true,
baseUrl: server.info.uri
}).then(() => {

expect(exited).to.equal(true);
expect(process.exitCode).to.equal(0);
const config = JSON.parse(Fs.readFileSync(Path.join(Os.homedir(), '.nsprc')));
expect(config).to.be.an.object();
expect(config.token).to.equal('thisisafaketoken');
Expand All @@ -56,21 +48,14 @@ describe('login handler', () => {

it('exits with error when login fails', () => {

let exited = false;
Mock.exit((code) => {

expect(code).to.equal(3);
exited = true;
});

return Login.handler({
email: 'wrong@user.com',
password: 'shouldfail',
quiet: true,
baseUrl: server.info.uri
}).then(() => {

expect(exited).to.equal(true);
expect(process.exitCode).to.equal(3);
});
});
});
18 changes: 0 additions & 18 deletions test/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,6 @@
const Boom = require('boom');
const Hapi = require('hapi');

process._exit = process.exit;

exports.exit = function (fn) {

process.exit = function (code) {

try {
fn(code);
}
catch (err) {}
};
};

exports.resetExit = function () {

process.exit = process._exit;
};

exports.log = function (state) {

console._log = console.log;
Expand Down