diff --git a/server/validate.js b/server/validate.js index e44cdfbe..082dbd90 100644 --- a/server/validate.js +++ b/server/validate.js @@ -10,29 +10,29 @@ const isString = (a) => typeof a === 'string'; module.exports.root = (dir, config, {exit = _exit, statSync = _statSync} = {}) => { if (!isString(dir)) throw Error('dir should be a string'); - + if (dir === '/') return; - + if (config('dropbox')) return; - + const [error] = tryCatch(statSync, dir); - + if (error) return exit('cloudcmd --root: %s', error.message); }; module.exports.editor = (name, {exit = _exit} = {}) => { const reg = /^(dword|edward|deepword)$/; - + if (!reg.test(name)) exit('cloudcmd --editor: could be "dword", "edward" or "deepword" only'); }; module.exports.packer = (name, {exit = _exit} = {}) => { const reg = /^(tar|zip)$/; - + if (!reg.test(name)) exit('cloudcmd --packer: could be "tar" or "zip" only'); }; @@ -42,12 +42,12 @@ module.exports.columns = (type, {exit = _exit, getColumns = _getColumns} = {}) = const all = Object .keys(getColumns()) .concat(''); - + const names = all .filter(Boolean) .map(addQuotes) .join(', '); - + if (!all.includes(type)) exit(`cloudcmd --columns: can be only one of: ${names}`); }; diff --git a/server/validate.spec.js b/server/validate.spec.js index 14869907..e0871e95 100644 --- a/server/validate.spec.js +++ b/server/validate.spec.js @@ -10,20 +10,20 @@ test('validate: root: bad', (t) => { const config = { root: Math.random(), }; - + const [e] = tryCatch(cloudcmd, { config, }); - + t.equal(e.message, 'dir should be a string', 'should throw'); t.end(); }); test('validate: root: config', (t) => { const config = stub().returns(true); - + validate.root('/hello', config); - + t.calledWith(config, ['dropbox'], 'should call config'); t.end(); }); @@ -31,7 +31,7 @@ test('validate: root: config', (t) => { test('validate: root: /', (t) => { const fn = stub(); validate.root('/', fn); - + t.notCalled(fn, 'should not call fn'); t.end(); }); @@ -41,14 +41,14 @@ test('validate: root: stat', (t) => { const error = 'ENOENT'; const statSync = stub().throws(Error(error)); const exit = stub(); - + validate.root('hello', config, { statSync, exit, }); - + const msg = 'cloudcmd --root: %s'; - + t.calledWith(exit, [msg, error], 'should call fn'); t.end(); }); @@ -56,11 +56,11 @@ test('validate: root: stat', (t) => { test('validate: packer: not valid', (t) => { const exit = stub(); const msg = 'cloudcmd --packer: could be "tar" or "zip" only'; - + validate.packer('hello', { exit, }); - + t.calledWith(exit, [msg], 'should call fn'); t.end(); }); @@ -68,22 +68,22 @@ test('validate: packer: not valid', (t) => { test('validate: editor: not valid', (t) => { const exit = stub(); const msg = 'cloudcmd --editor: could be "dword", "edward" or "deepword" only'; - + validate.editor('hello', { exit, }); - + t.calledWith(exit, [msg], 'should call fn'); t.end(); }); test('validate: columns', (t) => { const exit = stub(); - + validate.columns('name-size-date', { exit, }); - + t.notCalled(exit, 'should not call exit'); t.end(); }); @@ -93,15 +93,15 @@ test('validate: columns: wrong', (t) => { 'name-size-date': '', 'name-size': '', }); - + const exit = stub(); const msg = 'cloudcmd --columns: can be only one of: "name-size-date", "name-size"'; - + validate.columns('hello', { exit, getColumns, }); - + t.calledWith(exit, [msg], 'should call exit'); t.end(); });