diff --git a/lib/Common.js b/lib/Common.js index a6f341f8..fdf7d5ee 100644 --- a/lib/Common.js +++ b/lib/Common.js @@ -534,17 +534,18 @@ Common.mergeEnvironmentVariables = function(app_env, env_name, deploy_conf) { env : {} } + // Stringify possible object + for (var key in app.env) { + if (typeof app.env[key] == 'object') { + app.env[key] = JSON.stringify(app.env[key]); + } + } + /** * Extra configuration update */ util._extend(new_conf, app) - for (var key in app.env) { - if (typeof app.env[key] == 'object') { - new_conf[key] = JSON.stringify(app.env[key]); - } - } - if (env_name) { // First merge variables from deploy.production.env object as least priority. if (deploy_conf && deploy_conf[env_name] && deploy_conf[env_name]['env']) { diff --git a/test/programmatic/fixtures/json-env-passing/echo.js b/test/programmatic/fixtures/json-env-passing/echo.js new file mode 100644 index 00000000..9f2b21e0 --- /dev/null +++ b/test/programmatic/fixtures/json-env-passing/echo.js @@ -0,0 +1,4 @@ + +setInterval(function() { + console.log(process.env.JSONTEST); +}, 50); diff --git a/test/programmatic/fixtures/json-env-passing/ecosystem.config.js b/test/programmatic/fixtures/json-env-passing/ecosystem.config.js new file mode 100644 index 00000000..ad289462 --- /dev/null +++ b/test/programmatic/fixtures/json-env-passing/ecosystem.config.js @@ -0,0 +1,22 @@ +module.exports = { + apps : [{ + script: 'echo.js', + env: { + JSONTEST: { si: 'si' } + }, + env_production: { + NODE_ENV: 'production' + } + }], + + deploy : { + production : { + user : 'node', + host : '212.83.163.1', + ref : 'origin/master', + repo : 'git@github.com:repo.git', + path : '/var/www/production', + 'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production' + } + } +}; diff --git a/test/programmatic/issues/json_env_passing_4080.mocha.js b/test/programmatic/issues/json_env_passing_4080.mocha.js new file mode 100644 index 00000000..f81d258d --- /dev/null +++ b/test/programmatic/issues/json_env_passing_4080.mocha.js @@ -0,0 +1,48 @@ +var PM2 = require('../../..'); +var should = require('should'); + +describe('Programmatic log feature test', function() { + var proc1 = null; + var procs = []; + + var pm2 = new PM2.custom({ + cwd : __dirname + '/../fixtures/json-env-passing' + }); + + before(function(done) { + pm2.delete('all', function() { + done(); + }); + }); + + after(function(done) { + pm2.delete('all', function() { + pm2.disconnect(done); + }); + }); + + it('should start a process with object as environment variable', function(done) { + pm2.start({ + script: 'echo.js', + env: { + NORMAL: 'STR', + JSONTEST: { si: 'si' } + }, + env_production: { + NODE_ENV: 'production' + } + }, function(err, procs) { + should(err).be.null() + should(procs.length).eql(1) + done() + }) + }) + + it('should retrieve environment variable stringified', function(done) { + pm2.list((err, procs) => { + should(procs[0].pm2_env.JSONTEST).eql('{"si":"si"}') + should(procs[0].pm2_env.NORMAL).eql('STR') + done() + }) + }) +}) diff --git a/test/unit.sh b/test/unit.sh index e7c6fad9..7b7e55a2 100644 --- a/test/unit.sh +++ b/test/unit.sh @@ -10,8 +10,9 @@ function reset { } function runUnitTest { + echo "[~] Starting test $1" START=$(date +%s) - mocha --exit --opts ./mocha.opts $1 + mocha --exit --bail --opts ./mocha.opts $1 RET=$? if [ $RET -ne 0 ]; @@ -21,12 +22,12 @@ function runUnitTest { echo $STR >> unit_time reset - mocha --exit --opts ./mocha.opts $1 + mocha --bail --exit --opts ./mocha.opts $1 RET=$? if [ $RET -ne 0 ]; then - echo -e "######## \033[31m ✘ $1\033[0m" + echo -e "######## TEST ✘ $1 FAILED TWICE!!" exit 1 fi fi @@ -49,7 +50,7 @@ cd test/programmatic # Abort script at first error -set -e +# set -e runUnitTest ./programmatic.js runUnitTest ./instances.mocha.js @@ -80,6 +81,8 @@ runUnitTest ./configuration.mocha.js runUnitTest ./id.mocha.js runUnitTest ./god.mocha.js +runUnitTest ./issues/json_env_passing_4080.mocha.js + cd ../interface runUnitTest ./bus.spec.mocha.js