From 2a9d8c5b614efd19b55061d1b5bbc40b5d42f422 Mon Sep 17 00:00:00 2001 From: SHIN Suzuki Date: Sat, 14 May 2016 03:32:29 +0900 Subject: [PATCH] fix: Handle environment variables in start script --- examples/hello-world/package.json | 2 +- lib/processes/application/index.js | 18 +++++++++++++----- test/processes/application.test.js | 2 ++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/examples/hello-world/package.json b/examples/hello-world/package.json index 20b8999..945dfbf 100644 --- a/examples/hello-world/package.json +++ b/examples/hello-world/package.json @@ -1,6 +1,6 @@ { "name": "testium-core.hello-world", "scripts": { - "start": "node server.js Quinn" + "start": "DEBUG=* NODE_ENV=development node server.js Quinn" } } diff --git a/lib/processes/application/index.js b/lib/processes/application/index.js index 7c4e4a3..151075b 100644 --- a/lib/processes/application/index.js +++ b/lib/processes/application/index.js @@ -66,16 +66,24 @@ function getAppOptions(config) { var commandArgs = options.command.split(/[\s]+/g); + var env = { + NODE_ENV: config.get('launchEnv', 'test'), + PORT: options.port, + PATH: './node_modules/.bin:' + process.env.PATH, + }; + + while (commandArgs.length && commandArgs[0].match('=')) { + var arg = commandArgs.shift(); + var envPair = arg.split('='); + env[envPair[0]] = envPair[1]; + } + return _.extend(options, { command: commandArgs.shift(), commandArgs: commandArgs, verifyTimeout: config.get('app.timeout', 30000), spawnOpts: { - env: _.defaults({ - NODE_ENV: config.get('launchEnv', 'test'), - PORT: options.port, - PATH: './node_modules/.bin:' + process.env.PATH, - }, process.env), + env: _.defaults(env, process.env), cwd: config.get('root'), }, }); diff --git a/test/processes/application.test.js b/test/processes/application.test.js index 5a2b214..64c2eaa 100644 --- a/test/processes/application.test.js +++ b/test/processes/application.test.js @@ -14,6 +14,8 @@ describe('App', () => { const options = await App.getOptions(config); assert.hasType('Finds an open port', Number, options.port); assert.equal('Parses command from scripts.start', 'node', options.command); + assert.equal('Parses environment variables in scripts.start', '*', options.spawnOpts.env.DEBUG); + assert.equal('Overrides NODE_ENV when set in scripts.start', 'development', options.spawnOpts.env.NODE_ENV); assert.deepEqual('Parses commandArgs from scripts.start', ['server.js', 'Quinn'], options.commandArgs); });