test(validate) rm before

This commit is contained in:
coderaiser 2018-11-12 16:13:53 +02:00
parent 8c7cd6f0de
commit 1ed504154b

View file

@ -5,45 +5,28 @@ const fs = require('fs');
const test = require('tape');
const diff = require('sinon-called-with-diff');
const sinon = diff(require('sinon'));
const tryCatch = require('try-catch');
const mockRequire = require('mock-require');
const {reRequire} = mockRequire;
const before = require('../before');
const {connect} = before;
const dir = '../..';
const validatePath = `${dir}/server/validate`;
const exitPath = `${dir}/server/exit`;
const columnsPath = `${dir}/server/columns`;
const cloudcmdPath = `${dir}/server/cloudcmd`;
const validate = require(validatePath);
const clear = require('clear-module');
const {cache, resolve} = require;
const stub = (name, exports) => {
require(name);
const resolved = resolve(name);
cache[resolved].exports = exports;
};
const cloudcmd = require(cloudcmdPath);
test('validate: root: bad', (t) => {
const config = {
root: Math.random()
};
const success = ({done}) => {
t.fail('should not create server');
t.end();
done();
};
const error = (e) => {
t.equal(e.message, 'dir should be a string', 'should throw');
t.end();
};
connect({config})
.then(success)
.catch(error);
const [e] = tryCatch(cloudcmd, {config});
t.equal(e.message, 'dir should be a string', 'should throw');
t.end();
});
test('validate: root: /', (t) => {
@ -72,66 +55,62 @@ test('validate: root: stat', (t) => {
const error = 'ENOENT';
fs.stat = (dir, fn) => fn(Error(error));
clean();
require(exitPath);
stub(exitPath, fn);
mockRequire(exitPath, fn);
const {root} = require(validatePath);
const {root} = reRequire(validatePath);
root('hello', fn);
const msg = 'cloudcmd --root: %s';
t.ok(fn.calledWith(msg, error), 'should call fn');
fs.stat = stat;
mockRequire.stop(exitPath);
t.ok(fn.calledWith(msg, error), 'should call fn');
t.end();
});
test('validate: packer: not valid', (t) => {
const fn = sinon.stub();
clean();
require(exitPath);
stub(exitPath, fn);
mockRequire(exitPath, fn);
const {packer} = require(validatePath);
const {packer} = reRequire(validatePath);
const msg = 'cloudcmd --packer: could be "tar" or "zip" only';
packer('hello');
t.ok(fn.calledWith(msg), 'should call fn');
mockRequire.stop(exitPath);
t.ok(fn.calledWith(msg), 'should call fn');
t.end();
});
test('validate: editor: not valid', (t) => {
const fn = sinon.stub();
clean();
require(exitPath);
stub(exitPath, fn);
mockRequire(exitPath, fn);
const {editor} = require(validatePath);
const {editor} = reRequire(validatePath);
const msg = 'cloudcmd --editor: could be "dword", "edward" or "deepword" only';
editor('hello');
t.ok(fn.calledWith(msg), 'should call fn');
mockRequire.stop(exitPath);
t.ok(fn.calledWith(msg), 'should call fn');
t.end();
});
test('validate: columns', (t) => {
const fn = sinon.stub();
clean();
require(exitPath);
stub(exitPath, fn);
mockRequire(exitPath, fn);
const {columns} = require(validatePath);
columns('name-size-date');
mockRequire.stop(exitPath);
t.notOk(fn.called, 'should not call exit');
t.end();
});
@ -139,26 +118,21 @@ test('validate: columns', (t) => {
test('validate: columns: wrong', (t) => {
const fn = sinon.stub();
clean();
clear(columnsPath);
require(exitPath);
stub(exitPath, fn);
stub(columnsPath, {
mockRequire(exitPath, fn);
mockRequire(columnsPath, {
'name-size-date': '',
'name-size': '',
});
const {columns} = require(validatePath);
const {columns} = reRequire(validatePath);
const msg = 'cloudcmd --columns: can be only one of: "name-size-date", "name-size"';
columns('hello');
mockRequire.stop(exitPath);
mockRequire.stop(columnsPath);
t.ok(fn.calledWith(msg), 'should call exit');
t.end();
});
function clean() {
clear(validatePath);
clear(exitPath);
}