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(); +}); +