cloudcmd/server/validate.spec.js
2024-03-20 11:28:14 +00:00

107 lines
2.3 KiB
JavaScript

'use strict';
const {test, stub} = require('supertape');
const tryCatch = require('try-catch');
const validate = require('./validate');
const cloudcmd = require('./cloudcmd');
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();
});
test('validate: root: /', (t) => {
const fn = stub();
validate.root('/', fn);
t.notCalled(fn, 'should not call fn');
t.end();
});
test('validate: root: stat', (t) => {
const config = stub();
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();
});
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();
});
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();
});
test('validate: columns: wrong', (t) => {
const getColumns = stub().returns({
'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();
});