From 383f0ce39ff7750b3e6e1f53b438805c0c23fdad Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 6 Dec 2016 11:14:40 +0200 Subject: [PATCH] fix(validate) root: crash when --no-root used --- bin/cloudcmd.js | 2 +- lib/server/validate.js | 10 +++++++--- test/lib/validate.js | 24 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 test/lib/validate.js diff --git a/bin/cloudcmd.js b/bin/cloudcmd.js index fbb7ee44..9139f9b1 100755 --- a/bin/cloudcmd.js +++ b/bin/cloudcmd.js @@ -97,7 +97,7 @@ if (args.version) { readConfig(args.config); const options = { - root: args.root, + root: args.root || '/', /* --no-root */ editor: args.editor, packer: args.packer, prefix: args.prefix diff --git a/lib/server/validate.js b/lib/server/validate.js index 6db4c245..bc1317bd 100644 --- a/lib/server/validate.js +++ b/lib/server/validate.js @@ -9,12 +9,16 @@ module.exports.packer = packer; function root(dir, fn) { var fs; + if (typeof dir !== 'string') + throw Error('dir should be a string'); + if (dir !== '/') { - fs = require('fs'); + fs = require('fs'); fs.stat(dir, function(error) { if (error) - exit('cloudcmd --root: %s', error.message); - else if (typeof fn === 'function') + return exit('cloudcmd --root: %s', error.message); + + if (typeof fn === 'function') fn('root:', dir); }); } diff --git a/test/lib/validate.js b/test/lib/validate.js new file mode 100644 index 00000000..a70e1fc7 --- /dev/null +++ b/test/lib/validate.js @@ -0,0 +1,24 @@ +'use strict'; + +const test = require('tape'); +const root = '../../'; +const validate = require(root + 'lib/server/validate'); + +const before = require('../before'); + +test('validate: root: bad', (t) => { + const config = { + root: Math.random() + }; + const fn = () => { + before({config}, (port, after) => { + t.fail('should not create server'); + after(); + t.end(); + }); + }; + + t.throws(fn, /dir should be a string/, 'should throw'); + t.end(); +}); +