From a509f55a0784ce4df0a465d9dea791e98058b0d6 Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Thu, 27 Apr 2017 22:26:24 -0700 Subject: [PATCH] fix(start): exit forge with same status code as Electron if nonzero --- src/api/start.js | 9 +++++++++ test/fast/start_spec.js | 30 ++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/api/start.js b/src/api/start.js index e90fdb5797..e2ade86b1b 100644 --- a/src/api/start.js +++ b/src/api/start.js @@ -76,5 +76,14 @@ export default async (providedOptions = {}) => { } }); + await new Promise((resolve) => { + spawned.on('exit', (code) => { + if (code !== 0) { + process.exit(code); + } + resolve(); + }); + }); + return spawned; }; diff --git a/test/fast/start_spec.js b/test/fast/start_spec.js index 8f27e2cf83..beeba4b196 100644 --- a/test/fast/start_spec.js +++ b/test/fast/start_spec.js @@ -9,10 +9,16 @@ describe('start', () => { let start; let resolveStub; let spawnStub; + let childStub; + let childExitCode; beforeEach(() => { + childExitCode = 0; + childStub = { + on: (event, cb) => cb(childExitCode), + }; resolveStub = sinon.stub(); - spawnStub = sinon.stub(); + spawnStub = sinon.stub().returns(childStub); start = proxyquire.noCallThru().load('../../src/api/start', { '../util/forge-config': async () => ({}), '../util/resolve-dir': async dir => resolveStub(dir), @@ -102,7 +108,6 @@ describe('start', () => { it('should pass all args through to the spawned Electron instance', async () => { const args = ['magic_arg', 123, 'thingy']; resolveStub.returnsArg(0); - spawnStub.returns(0); await start({ dir: __dirname, interactive: false, @@ -113,15 +118,32 @@ describe('start', () => { expect(spawnStub.firstCall.args[1].slice(1)).to.deep.equal(args); }); + it('should exit with the status code of the spawned Electron instance if it is not zero', async () => { + resolveStub.returnsArg(0); + childExitCode = 1; + + const originalExit = process.exit; + process.exit = sinon.stub(); + + await start({ + dir: __dirname, + interactive: false, + enableLogging: true, + }); + + expect(process.exit.callCount).to.equal(1); + expect(process.exit.firstCall.args[0]).to.equal(childExitCode); + process.exit = originalExit; + }); + it('should resolve with a handle to the spawned instance', async () => { resolveStub.returnsArg(0); - spawnStub.returns('child'); await expect(start({ dir: __dirname, interactive: false, enableLogging: true, - })).to.eventually.equal('child'); + })).to.eventually.equal(childStub); }); describe('cli', () => {