feature(cloudcmd) sinon, sinon-called-with-diff -> @cloudcmd/stub

This commit is contained in:
coderaiser 2018-11-23 11:42:15 +02:00
parent 2f56d2b5ca
commit 497f7bcd40
24 changed files with 182 additions and 222 deletions

View file

@ -3,8 +3,7 @@
const path = require('path');
const test = require('tape');
const diff = require('sinon-called-with-diff');
const sinon = diff(require('sinon'));
const stub = require('@cloudcmd/stub');
const currify = require('currify');
const {reRequire} = require('mock-require');
@ -125,8 +124,8 @@ test('cloudcmd: replaceDist: !isDev', (t) => {
test('cloudcmd: auth: reject', (t) => {
const auth = config('auth');
const accept = sinon.stub();
const reject = sinon.stub();
const accept = stub();
const reject = stub();
const username = 'root';
const password = 'toor';
@ -145,8 +144,8 @@ test('cloudcmd: auth: reject', (t) => {
test('cloudcmd: auth: accept', (t) => {
const auth = config('auth');
const accept = sinon.stub();
const reject = sinon.stub();
const accept = stub();
const reject = stub();
const username = 'root';
const password = 'toor';
@ -165,8 +164,8 @@ test('cloudcmd: auth: accept', (t) => {
test('cloudcmd: auth: accept: no auth', (t) => {
const auth = config('auth');
const accept = sinon.stub();
const reject = sinon.stub();
const accept = stub();
const reject = stub();
const username = 'root';
const password = 'toor';

View file

@ -5,8 +5,7 @@ const path = require('path');
const test = require('tape');
const readjson = require('readjson');
const diff = require('sinon-called-with-diff');
const sinon = diff(require('sinon'));
const stub = require('@cloudcmd/stub');
const root = '../';
const dir = './';
@ -106,7 +105,7 @@ test('config: cryptoPass', (t) => {
test('config: middle: no', (t) => {
const {middle} = config;
const next = sinon.stub();
const next = stub();
const res = null;
const url = `${apiURL}/config`;
const method = 'POST';

View file

@ -1,7 +1,5 @@
'use strict';
const {promisify} = require('util');
const test = require('tape');
const io = require('socket.io-client');
@ -23,8 +21,6 @@ test('distribute: export', async (t) => {
const url = `http://localhost:${port}/distribute?port=${1111}`;
const socket = io.connect(url);
const name = config('name');
socket.on('connect', () => {
socket.emit('auth', 'a');
});
@ -58,8 +54,6 @@ test('distribute: export: config', async (t) => {
const url = `http://localhost:${port}/distribute?port=${1111}`;
const socket = io.connect(url);
const name = config('name');
socket.on('connect', () => {
socket.emit('auth', 'a');
});

View file

@ -3,18 +3,14 @@
const test = require('tape');
const {promisify} = require('util');
const tryToCatch = require('try-to-catch');
const io = require('socket.io-client');
const mockRequire = require('mock-require');
const {connect} = require('../../test/before');
const config = require('../config');
const distribute = {
import: promisify(require('./import')),
};
test('distribute: import: canceled', async (t) => {
const {done, port} = await connect({
const {done} = await connect({
config: {
export: false,
import: false,
@ -138,7 +134,7 @@ test('distribute: import: received: no name', async (t) => {
});
test('distribute: import: error', async (t) => {
const {done, port} = await connect({
const {done} = await connect({
config: {
import: true,
export: false,
@ -160,7 +156,7 @@ test('distribute: import: error', async (t) => {
});
test('distribute: import: config:change: no export', async (t) => {
const {done, port} = await connect({
const {done} = await connect({
config: {
import: true,
export: false,

View file

@ -6,7 +6,7 @@ const config = require('../config');
test('distribute: log: getMessage', (t) => {
const e = 'hello';
const result = log.getMessage(e)
const result = log.getMessage(e);
t.equal(e, result, 'should equal');
t.end();
@ -16,7 +16,7 @@ test('distribute: log: getMessage: message', (t) => {
const message = 'hello';
const result = log.getMessage({
message
})
});
t.equal(result, message, 'should equal');
t.end();

View file

@ -2,12 +2,11 @@
const test = require('tape');
const exit = require('./exit');
const diff = require('sinon-called-with-diff');
const sinon = diff(require('sinon'));
const stub = require('@cloudcmd/stub');
test('cloudcmd: exit: process.exit', (t) => {
const {exit:exitOriginal} = process;
process.exit = sinon.stub();
process.exit = stub();
exit();
t.ok(process.exit.calledWith(1), 'should call process.exit');
@ -20,8 +19,8 @@ test('cloudcmd: exit: console.error', (t) => {
const {exit:exitOriginal} = process;
const {error} = console;
console.error = sinon.stub();
process.exit = sinon.stub();
console.error = stub();
process.exit = stub();
exit('hello world');
t.ok(console.error.calledWith('hello world'), 'should call console.error');
@ -36,8 +35,8 @@ test('cloudcmd: exit.error: console.error: error', (t) => {
const {exit:exitOriginal} = process;
const {error} = console;
console.error = sinon.stub();
process.exit = sinon.stub();
console.error = stub();
process.exit = stub();
exit(Error('hello world'));
t.ok(console.error.calledWith('hello world'), 'should call console.error');

View file

@ -2,13 +2,12 @@
const test = require('tape');
const info = require('./info');
const sinon = require('sinon');
const stub = require('@cloudcmd/stub');
test('cloudcmd: rest: info', (t) => {
const {memoryUsage} = process;
const _memoryUsage = sinon
.stub()
const _memoryUsage = stub()
.returns({});
process.memoryUsage = _memoryUsage;

49
server/root.spec.js Normal file
View file

@ -0,0 +1,49 @@
'use strict';
const test = require('tape');
const stub = require('@cloudcmd/stub');
const mockRequire = require('mock-require');
const {reRequire} = mockRequire;
const pathConfig = './config';
const pathRoot = './root';
test('cloudcmd: root: config', (t) => {
const config = stub().returns(false);
mockRequire(pathConfig, config);
const root = reRequire(pathRoot);
root('hello');
mockRequire.stop(pathConfig);
t.ok(config.calledWith('root'), 'should call config');
t.end();
});
test('cloudcmd: root: mellow', (t) => {
const config = stub().returns('');
const pathToWin = stub();
const mellow = {
pathToWin
};
mockRequire('mellow', mellow);
mockRequire(pathConfig, config);
const root = reRequire(pathRoot);
const dir = 'hello';
const dirRoot = '/';
root(dir);
mockRequire.stop('mellow');
mockRequire.stopAll(pathConfig);
reRequire(pathRoot);
t.ok(pathToWin.calledWith(dir, dirRoot), 'should call mellow');
t.end();
});

View file

@ -262,12 +262,12 @@ test('cloudcmd: route: realpath: error', async (t) => {
const {request} = serveOnce(cloudcmd, {
config: defaultConfig
});
const {body} = await request.get('/fs/empty-file', {
options,
});
fs.realpath = realpath;
console.log(body);
t.ok(/^ENOENT/.test(body), 'should return error');
t.end();

View file

@ -3,8 +3,7 @@
const fs = require('fs');
const test = require('tape');
const diff = require('sinon-called-with-diff');
const sinon = diff(require('sinon'));
const stub = require('@cloudcmd/stub');
const tryCatch = require('try-catch');
const mockRequire = require('mock-require');
const {reRequire} = mockRequire;
@ -35,8 +34,7 @@ test('validate: root: config', (t) => {
root: Math.random()
};
const configFn = sinon
.stub()
const configFn = stub()
.returns(true);
mockRequire(configPath, configFn);
@ -51,7 +49,7 @@ test('validate: root: config', (t) => {
});
test('validate: root: /', (t) => {
const fn = sinon.stub();
const fn = stub();
validate.root('/', fn);
t.notOk(fn.called, 'should not call fn');
@ -59,7 +57,7 @@ test('validate: root: /', (t) => {
});
test('validate: root: /home', (t) => {
const fn = sinon.stub();
const fn = stub();
validate.root('/home', (...args) => {
fn(...args);
@ -70,7 +68,7 @@ test('validate: root: /home', (t) => {
});
test('validate: root: stat', (t) => {
const fn = sinon.stub();
const fn = stub();
const {statSync} = fs;
const error = 'ENOENT';
@ -93,7 +91,7 @@ test('validate: root: stat', (t) => {
});
test('validate: packer: not valid', (t) => {
const fn = sinon.stub();
const fn = stub();
mockRequire(exitPath, fn);
@ -109,7 +107,7 @@ test('validate: packer: not valid', (t) => {
});
test('validate: editor: not valid', (t) => {
const fn = sinon.stub();
const fn = stub();
mockRequire(exitPath, fn);
@ -125,7 +123,7 @@ test('validate: editor: not valid', (t) => {
});
test('validate: columns', (t) => {
const fn = sinon.stub();
const fn = stub();
mockRequire(exitPath, fn);
const {columns} = require(validatePath);
@ -139,7 +137,7 @@ test('validate: columns', (t) => {
});
test('validate: columns: wrong', (t) => {
const fn = sinon.stub();
const fn = stub();
mockRequire(exitPath, fn);
mockRequire(columnsPath, {