Skip to content

Commit

Permalink
feature: server: validate: get rid of mock-require
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Mar 20, 2024
1 parent 043ba29 commit bf90bf2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 99 deletions.
30 changes: 15 additions & 15 deletions server/validate.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
'use strict';

const {statSync: _statSync} = require('node:fs');
const tryCatch = require('try-catch');

const exit = require('./exit');
const {getColumns} = require('./columns');
const _exit = require('./exit');
const {getColumns: _getColumns} = require('./columns');
const isString = (a) => typeof a === 'string';

module.exports.root = (dir, config) => {
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 {statSync} = require('node:fs');

const [error] = tryCatch(statSync, dir);

if (error)
return exit('cloudcmd --root: %s', error.message);
};

module.exports.editor = (name) => {
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) => {
module.exports.packer = (name, {exit = _exit} = {}) => {
const reg = /^(tar|zip)$/;

if (!reg.test(name))
exit('cloudcmd --packer: could be "tar" or "zip" only');
};

module.exports.columns = (type) => {
module.exports.columns = (type, {exit = _exit, getColumns = _getColumns} = {}) => {
const addQuotes = (a) => `"${a}"`;
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}`);
};
131 changes: 47 additions & 84 deletions server/validate.spec.js
Original file line number Diff line number Diff line change
@@ -1,144 +1,107 @@
'use strict';

const fs = require('node:fs');

const {test, stub} = require('supertape');

const tryCatch = require('try-catch');
const mockRequire = require('mock-require');
const dir = '..';

const validatePath = `${dir}/server/validate`;

const cloudcmdPath = `${dir}/server/cloudcmd`;
const validate = require(validatePath);
const cloudcmd = require(cloudcmdPath);
const columnsPath = `${dir}/server/columns`;

const exitPath = `${dir}/server/exit`;
const {reRequire, stopAll} = mockRequire;
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 fn = stub();
const {statSync} = fs;

const config = stub();
const error = 'ENOENT';

fs.statSync = () => {
throw Error(error);
};

mockRequire(exitPath, fn);

const {root} = reRequire(validatePath);

root('hello', fn);

const statSync = stub().throws(Error(error));
const exit = stub();

validate.root('hello', config, {
statSync,
exit,
});

const msg = 'cloudcmd --root: %s';

fs.statSync = statSync;

stopAll();

t.calledWith(fn, [msg, error], 'should call fn');

t.calledWith(exit, [msg, error], 'should call fn');
t.end();
});

test('validate: packer: not valid', (t) => {
const fn = stub();

mockRequire(exitPath, fn);

const {packer} = reRequire(validatePath);
const exit = stub();
const msg = 'cloudcmd --packer: could be "tar" or "zip" only';
packer('hello');

stopAll();
t.calledWith(fn, [msg], 'should call fn');

validate.packer('hello', {
exit,
});

t.calledWith(exit, [msg], 'should call fn');
t.end();
});

test('validate: editor: not valid', (t) => {
const fn = stub();

mockRequire(exitPath, fn);

const {editor} = reRequire(validatePath);
const exit = stub();
const msg = 'cloudcmd --editor: could be "dword", "edward" or "deepword" only';
editor('hello');

stopAll();
t.calledWith(fn, [msg], 'should call fn');

validate.editor('hello', {
exit,
});

t.calledWith(exit, [msg], 'should call fn');
t.end();
});

test('validate: columns', (t) => {
const fn = stub();
mockRequire(exitPath, fn);

const {columns} = require(validatePath);

columns('name-size-date');

stopAll();

t.notCalled(fn, 'should not call exit');
const exit = stub();

validate.columns('name-size-date', {
exit,
});

t.notCalled(exit, 'should not call exit');
t.end();
});

test('validate: columns: wrong', (t) => {
const fn = stub();
const getColumns = stub().returns({
'name-size-date': '',
'name-size': '',
});

mockRequire(exitPath, fn);
mockRequire(columnsPath, {

const exit = stub();
const msg = 'cloudcmd --columns: can be only one of: "name-size-date", "name-size"';

validate.columns('hello', {
exit,
getColumns,
});

const {columns} = reRequire(validatePath);
const msg = 'cloudcmd --columns: can be only one of: "name-size-date", "name-size"';

columns('hello');

stopAll();

t.calledWith(fn, [msg], 'should call exit');

t.calledWith(exit, [msg], 'should call exit');
t.end();
});

0 comments on commit bf90bf2

Please sign in to comment.