From fb1d2b319aaa5e9a9f42cb4bfda06b7d73c5cf5b Mon Sep 17 00:00:00 2001 From: Naomi Seyfer Date: Mon, 3 Dec 2012 15:35:18 -0800 Subject: [PATCH] Tests for --settings, and also Nick's review comments --- admin/cli-test.sh | 18 ++++++++++++++++++ app/meteor/meteor.js | 27 ++++++++++++++++++--------- app/meteor/run.js | 16 ++++++++-------- packages/meteor/server_environment.js | 7 +++---- 4 files changed, 47 insertions(+), 21 deletions(-) diff --git a/admin/cli-test.sh b/admin/cli-test.sh index 3a4217a8a9..2c15bd0e11 100755 --- a/admin/cli-test.sh +++ b/admin/cli-test.sh @@ -120,7 +120,25 @@ kill $METEOR_PID ps ax | grep -e "$MONGOMARK" | grep -v grep | awk '{print $1}' | xargs kill +echo "... settings" +cat > settings.json < settings.js < /dev/null # XXX more tests here! diff --git a/app/meteor/meteor.js b/app/meteor/meteor.js index 15b3b5a5c6..fd38153e50 100644 --- a/app/meteor/meteor.js +++ b/app/meteor/meteor.js @@ -83,6 +83,20 @@ var findCommand = function (name) { process.exit(1); }; +var getSettings = function (filename) { + var str; + try { + str = fs.readFileSync(filename); + } catch (e) { + throw new Error("Could not find settings file " + argv.settings); + } + if (str.length > 0x10000) { + throw new Error("Settings file must be less than 64 KB long"); + } + JSON.parse(str); + return str; +}; + // XXX when the pass unexpected argument or unrecognized flags, print // an error and fail out @@ -101,9 +115,9 @@ Commands.push({ .describe('debug', 'Run in debug mode for node-inspector') .boolean('debug-brk') .describe('debug-brk', 'Run in debug mode and break on first line') - .describe('settings', 'Make the given JSON file\'s contents available in Meteor.settings') + .describe('settings', 'Set Meteor.settings to the contents of a JSON file; takes the filename as an argument') .boolean('once') - .describe('once', 'Only run the project once, and return whatever exit code it returned.') + .describe('once', 'Disable automatic reloading. Only run the server once') .usage( "Usage: meteor run [options]\n" + "\n" + @@ -125,13 +139,8 @@ Commands.push({ process.stdout.write(opt.help()); process.exit(1); } - if (argv.settings) { - try { - settings = fs.readFileSync(argv.settings); - } catch (e) { - process.stdout.write("Could not file settings file " + argv.settings + "\n"); - process.exit(1); - } + if (new_argv.settings) { + settings = getSettings(new_argv.settings); } var app_dir = path.resolve(require_project("run", true)); // app or package diff --git a/app/meteor/run.js b/app/meteor/run.js index 057596cb7d..e76a671054 100644 --- a/app/meteor/run.js +++ b/app/meteor/run.js @@ -19,18 +19,16 @@ var _ = require(path.join(__dirname, '..', 'lib', 'third', 'underscore.js')); // list of log objects from the child process. var server_log = []; -// port that mongo is running on - var Status = { running: false, // is server running now? crashing: false, // does server crash whenever we start it? listening: false, // do we expect the server to be listening now. counter: 0, // how many crashes in rapid succession - code: 0, - shouldRestart: true, - shuttingDown: false, + code: 0, // exit code last returned + shouldRestart: true, // true if we should be restarting the server + shuttingDown: false, // true if we're on the way to shutting down the server - justCrash : function () { + exitNow : function () { var self = this; log_to_clients({'exit': "Your application is exiting."}); self.shuttingDown = true; @@ -49,7 +47,7 @@ var Status = { hard_crashed: function () { var self = this; if (!self.shouldRestart) { - self.justCrash(); + self.exitNow(); return; } log_to_clients({'exit': "Your application is crashing. Waiting for file change."}); @@ -59,7 +57,7 @@ var Status = { soft_crashed: function () { var self = this; if (!self.shouldRestart) { - self.justCrash(); + self.exitNow(); return; } if (this.counter === 0) @@ -554,6 +552,8 @@ exports.run = function (app_dir, bundle_opts, port, once, settings, dbg) { var watcher; var start_watching = function () { + if (!Status.shouldRestart) + return; if (deps_info) { if (watcher) watcher.destroy(); diff --git a/packages/meteor/server_environment.js b/packages/meteor/server_environment.js index 2919283832..ea1b39b227 100644 --- a/packages/meteor/server_environment.js +++ b/packages/meteor/server_environment.js @@ -4,10 +4,9 @@ Meteor = { }; try { - Meteor.settings = JSON.parse(process.env.METEOR_SETTINGS); + if (process.env.METEOR_SETTINGS) + Meteor.settings = JSON.parse(process.env.METEOR_SETTINGS); } catch (e) { - // If the settings aren't JSON, just treat them as a string, or - // undefined, or whatever they are. - Meteor.settings = process.env.METEOR_SETTINGS; + throw new Error("Settings are not valid JSON"); }