diff --git a/client/client.js b/client/client.js index f58ccb03..f5d4325d 100644 --- a/client/client.js +++ b/client/client.js @@ -205,9 +205,10 @@ function CloudCmdProto(DOM) { this.logOut = () => { const url = CloudCmd.prefix + '/logout'; const error = () => document.location.reload(); + const {prefix} = CloudCmd; DOM.Storage.clear(); - unregisterSW(); + unregisterSW(prefix); DOM.load.ajax({ url, error, diff --git a/client/dom/current-file.spec.js b/client/dom/current-file.spec.js index c274117c..780a31c5 100644 --- a/client/dom/current-file.spec.js +++ b/client/dom/current-file.spec.js @@ -1,8 +1,7 @@ 'use strict'; const test = require('tape'); -const diff = require('sinon-called-with-diff'); -const sinon = diff(require('sinon')); +const stub = require('@cloudcmd/stub'); const id = (a) => a; const wraptile = require('wraptile'); const returns = wraptile(id); @@ -19,7 +18,7 @@ test('current-file: setCurrentName: setAttribute', (t) => { global.DOM = getDOM(); global.CloudCmd = getCloudCmd(); - const setAttribute = sinon.stub(); + const setAttribute = stub(); const current = { setAttribute }; @@ -43,7 +42,7 @@ test('current-file: setCurrentName: setAttribute: cyrillic', (t) => { global.DOM = getDOM(); global.CloudCmd = getCloudCmd(); - const setAttribute = sinon.stub(); + const setAttribute = stub(); const current = { setAttribute }; @@ -64,8 +63,8 @@ test('current-file: emit', (t) => { CloudCmd, } = global; - const emit = sinon.stub(); - const setAttribute = sinon.stub(); + const emit = stub(); + const setAttribute = stub(); global.DOM = getDOM(); global.CloudCmd = getCloudCmd({ @@ -92,7 +91,7 @@ test('current-file: setCurrentName: return', (t) => { CloudCmd, } = global; - const setAttribute = sinon.stub(); + const setAttribute = stub(); const link = {}; global.DOM = getDOM({ @@ -162,7 +161,7 @@ test('current-file: isCurrentFile', (t) => { CloudCmd, } = global; - const isContainClass = sinon.stub(); + const isContainClass = stub(); global.DOM = getDOM({ isContainClass @@ -183,16 +182,16 @@ test('current-file: isCurrentFile', (t) => { function getCloudCmd({emit} = {}) { return { prefix: '', - emit: emit || sinon.stub(), + emit: emit || stub(), }; } function getDOM({ link = {}, - getCurrentDirPath = sinon.stub(), - getCurrentDirName = sinon.stub(), - getByDataName = sinon.stub(), - isContainClass = sinon.stub(), + getCurrentDirPath = stub(), + getCurrentDirName = stub(), + getByDataName = stub(), + isContainClass = stub(), } = {}) { return { getCurrentDirPath, diff --git a/client/dom/dom-tree.spec.js b/client/dom/dom-tree.spec.js index b9226166..0acb6302 100644 --- a/client/dom/dom-tree.spec.js +++ b/client/dom/dom-tree.spec.js @@ -1,8 +1,7 @@ 'use strict'; 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 { @@ -22,7 +21,7 @@ test('dom: isContainClass: no className', (t) => { }); test('dom: isContainClass: contains', (t) => { - const contains = sinon.stub(); + const contains = stub(); const el = { classList: { contains, @@ -37,7 +36,7 @@ test('dom: isContainClass: contains', (t) => { }); test('dom: isContainClass: contains: array', (t) => { - const contains = sinon.stub(); + const contains = stub(); const el = { classList: { contains, diff --git a/client/key/vim/find.spec.js b/client/key/vim/find.spec.js index f437e30f..66143498 100644 --- a/client/key/vim/find.spec.js +++ b/client/key/vim/find.spec.js @@ -1,8 +1,7 @@ 'use strict'; const test = require('tape'); -const diff = require('sinon-called-with-diff'); -const sinon = diff(require('sinon')); +const stub = require('@cloudcmd/stub'); const dir = './'; const { @@ -21,7 +20,7 @@ const { test('cloudcmd: client: vim: find', (t) => { const {DOM} = global; - const setCurrentByName = sinon.stub(); + const setCurrentByName = stub(); DOM.setCurrentByName = setCurrentByName; DOM.Dialog.prompt = Promise.resolve.bind(Promise); @@ -34,7 +33,7 @@ test('cloudcmd: client: vim: find', (t) => { test('cloudcmd: client: vim: findNext', (t) => { const {DOM} = global; - const setCurrentByName = sinon.stub(); + const setCurrentByName = stub(); DOM.setCurrentByName = setCurrentByName; findNext(); @@ -45,7 +44,7 @@ test('cloudcmd: client: vim: findNext', (t) => { test('cloudcmd: client: vim: findPrevious', (t) => { const {DOM} = global; - const setCurrentByName = sinon.stub(); + const setCurrentByName = stub(); DOM.setCurrentByName = setCurrentByName; findPrevious(); diff --git a/client/key/vim/index.spec.js b/client/key/vim/index.spec.js index 32ec407f..ce2f8a7c 100644 --- a/client/key/vim/index.spec.js +++ b/client/key/vim/index.spec.js @@ -1,10 +1,17 @@ 'use strict'; +const {join} = require('path'); + const test = require('tape'); -const diff = require('sinon-called-with-diff'); -const sinon = diff(require('sinon')); +const stub = require('@cloudcmd/stub'); +const mockRequire = require('mock-require'); +const {reRequire} = mockRequire; + const dir = '../'; -const KEY = require(dir + 'key'); + +const pathKey = join(dir, 'key'); +const pathVim = join(dir, 'vim'); +const pathFind = join(dir, 'vim', 'find'); const { getDOM, @@ -17,13 +24,14 @@ global.CloudCmd = getCloudCmd(); const DOM = global.DOM; const Buffer = DOM.Buffer; -const vim = require(dir + 'vim'); +const KEY = require(pathKey); +const vim = require(pathVim); test('cloudcmd: client: key: set next file: no', (t) => { const element = { }; - const setCurrentFile = sinon.stub(); + const setCurrentFile = stub(); global.DOM.CurrentInfo.element = element; global.DOM.setCurrentFile = setCurrentFile; @@ -31,7 +39,6 @@ test('cloudcmd: client: key: set next file: no', (t) => { vim('j', {}); t.ok(setCurrentFile.calledWith(element), 'should set next file'); - t.end(); }); @@ -41,7 +48,7 @@ test('cloudcmd: client: key: set next file current', (t) => { nextSibling }; - const setCurrentFile = sinon.stub(); + const setCurrentFile = stub(); global.DOM.CurrentInfo.element = element; global.DOM.setCurrentFile = setCurrentFile; @@ -59,7 +66,7 @@ test('cloudcmd: client: key: set next file current', (t) => { nextSibling }; - const setCurrentFile = sinon.stub(); + const setCurrentFile = stub(); global.DOM.CurrentInfo.element = element; global.DOM.setCurrentFile = setCurrentFile; @@ -79,7 +86,7 @@ test('cloudcmd: client: key: set next file current: g', (t) => { nextSibling }; - const setCurrentFile = sinon.stub(); + const setCurrentFile = stub(); global.DOM.CurrentInfo.element = element; global.DOM.setCurrentFile = setCurrentFile; @@ -101,7 +108,7 @@ test('cloudcmd: client: key: set +2 file current', (t) => { nextSibling }; - const setCurrentFile = sinon.stub(); + const setCurrentFile = stub(); global.DOM.CurrentInfo.element = element; global.DOM.setCurrentFile = setCurrentFile; @@ -125,13 +132,13 @@ test('cloudcmd: client: key: select +2 files from current before delete', (t) => nextSibling }; - const setCurrentFile = sinon.stub(); + const setCurrentFile = stub(); global.DOM.CurrentInfo.element = element; global.DOM.setCurrentFile = setCurrentFile; - global.DOM.selectFile = sinon.stub(); + global.DOM.selectFile = stub(); global.DOM.getCurrentName = () => false; - global.CloudCmd.Operation.show = sinon.stub(); + global.CloudCmd.Operation.show = stub(); const event = {}; @@ -153,12 +160,12 @@ test('cloudcmd: client: key: delete +2 files from current', (t) => { nextSibling }; - const setCurrentFile = sinon.stub(); - const show = sinon.stub(); + const setCurrentFile = stub(); + const show = stub(); global.DOM.CurrentInfo.element = element; global.DOM.setCurrentFile = setCurrentFile; - global.DOM.selectFile = sinon.stub(); + global.DOM.selectFile = stub(); global.DOM.getCurrentName = () => false; global.CloudCmd.Operation.show = show; @@ -179,7 +186,7 @@ test('cloudcmd: client: key: set previous file current', (t) => { previousSibling }; - const setCurrentFile = sinon.stub(); + const setCurrentFile = stub(); global.DOM.CurrentInfo.element = element; global.DOM.setCurrentFile = setCurrentFile; @@ -192,7 +199,7 @@ test('cloudcmd: client: key: set previous file current', (t) => { }); test('cloudcmd: client: key: copy: no', (t) => { - const copy = sinon.stub(); + const copy = stub(); Buffer.copy = copy; @@ -204,7 +211,7 @@ test('cloudcmd: client: key: copy: no', (t) => { }); test('cloudcmd: client: key: copy', (t) => { - const copy = sinon.stub(); + const copy = stub(); Buffer.copy = copy; @@ -216,7 +223,7 @@ test('cloudcmd: client: key: copy', (t) => { }); test('cloudcmd: client: key: copy: unselectFiles', (t) => { - const unselectFiles = sinon.stub(); + const unselectFiles = stub(); DOM.unselectFiles = unselectFiles; @@ -228,7 +235,7 @@ test('cloudcmd: client: key: copy: unselectFiles', (t) => { }); test('cloudcmd: client: key: paste', (t) => { - const paste = sinon.stub(); + const paste = stub(); Buffer.paste = paste; @@ -240,8 +247,8 @@ test('cloudcmd: client: key: paste', (t) => { }); test('cloudcmd: client: key: selectFile: ..', (t) => { - const selectFile = sinon.stub(); - const getCurrentName = sinon.stub(); + const selectFile = stub(); + const getCurrentName = stub(); DOM.selectFile = selectFile; DOM.getCurrentName = () => '..'; @@ -254,7 +261,7 @@ test('cloudcmd: client: key: selectFile: ..', (t) => { }); test('cloudcmd: client: key: selectFile', (t) => { - const selectFile = sinon.stub(); + const selectFile = stub(); DOM.selectFile = selectFile; DOM.getCurrentName = (a) => a.name; @@ -276,7 +283,7 @@ test('cloudcmd: client: key: set last file current', (t) => { nextSibling }; - const setCurrentFile = sinon.stub(); + const setCurrentFile = stub(); global.DOM.CurrentInfo.element = element; global.DOM.setCurrentFile = setCurrentFile; @@ -298,7 +305,7 @@ test('cloudcmd: client: key: set first file current', (t) => { previousSibling }; - const setCurrentFile = sinon.stub(); + const setCurrentFile = stub(); global.DOM.CurrentInfo.element = element; global.DOM.setCurrentFile = setCurrentFile; @@ -315,7 +322,7 @@ test('cloudcmd: client: key: visual', (t) => { const element = { }; - const toggleSelectedFile = sinon.stub(); + const toggleSelectedFile = stub(); global.DOM.CurrentInfo.element = element; global.DOM.toggleSelectedFile = toggleSelectedFile; @@ -331,7 +338,7 @@ test('cloudcmd: client: key: ESC', (t) => { const element = { }; - const unselectFiles = sinon.stub(); + const unselectFiles = stub(); global.DOM.CurrentInfo.element = element; global.DOM.unselectFiles = unselectFiles ; @@ -351,7 +358,7 @@ test('cloudcmd: client: key: Enter', (t) => { nextSibling }; - const setCurrentFile = sinon.stub(); + const setCurrentFile = stub(); DOM.CurrentInfo.element = element; DOM.setCurrentFile = setCurrentFile; @@ -368,7 +375,7 @@ test('cloudcmd: client: key: Enter', (t) => { }); test('cloudcmd: client: key: /', (t) => { - const preventDefault = sinon.stub(); + const preventDefault = stub(); const element = {}; DOM.CurrentInfo.element = element; @@ -383,44 +390,38 @@ test('cloudcmd: client: key: /', (t) => { }); test('cloudcmd: client: key: n', (t) => { - const findNext = sinon.stub(); + const findNext = stub(); - clean(dir + 'vim'); - stub(dir + 'vim/find', { - findNext + mockRequire(pathFind, { + findNext, }); - const vim = require(dir + 'vim'); + const vim = reRequire(pathVim); const event = {}; vim('n', event); + mockRequire.stop(pathFind); + t.ok(findNext.calledWith(), 'should call findNext'); t.end(); }); test('cloudcmd: client: key: N', (t) => { - const findPrevious = sinon.stub(); + const findPrevious = stub(); - clean(dir + 'vim'); - stub(dir + 'vim/find', { + mockRequire(pathFind, { findPrevious, }); - const vim = require(dir + 'vim'); + const vim = reRequire(dir + 'vim'); const event = {}; vim('N', event); + mockRequire.stop(pathFind); + t.ok(findPrevious.calledWith(), 'should call findPrevious'); t.end(); }); -function clean(path) { - delete require.cache[require.resolve(path)]; -} - -function stub(name, fn) { - require.cache[require.resolve(name)].exports = fn; -} - diff --git a/client/modules/polyfill.spec.js b/client/modules/polyfill.spec.js index 607b0cb2..482fc287 100644 --- a/client/modules/polyfill.spec.js +++ b/client/modules/polyfill.spec.js @@ -2,12 +2,11 @@ const test = require('tape'); const mockRequire = require('mock-require'); -const diff = require('sinon-called-with-diff'); -const sinon = diff(require('sinon')); +const stub = require('@cloudcmd/stub'); test('cloudcmd: client: polyfill: scrollIntoViewIfNeaded', (t) => { const {DOM} = global; - const scroll = sinon.stub(); + const scroll = stub(); const el = {}; global.DOM = {}; diff --git a/client/sw/register.js b/client/sw/register.js index 1ea669a9..90c307f7 100644 --- a/client/sw/register.js +++ b/client/sw/register.js @@ -19,8 +19,8 @@ async function registerSW(prefix) { return navigator.serviceWorker.register(`${prefix}/sw.js`); } -async function unregisterSW() { - const reg = await registerSW(); - reg && reg.unregister(); +async function unregisterSW(prefix) { + const reg = await registerSW(prefix); + reg && reg.unregister(prefix); } diff --git a/client/sw/register.spec.js b/client/sw/register.spec.js index 7ac44560..9af45834 100644 --- a/client/sw/register.spec.js +++ b/client/sw/register.spec.js @@ -1,13 +1,13 @@ 'use strict'; const test = require('tape'); -const sinon = require('sinon'); +const stub = require('@cloudcmd/stub'); const tryCatch = require('try-catch'); const {reRequire} = require('mock-require'); test('sw: listen', (t) => { const {listenSW} = reRequire('./register'); - const addEventListener = sinon.stub(); + const addEventListener = stub(); const sw = { addEventListener, }; @@ -48,7 +48,7 @@ test('sw: register: registerSW: no https', async (t) => { location, } = global; - const register = sinon.stub(); + const register = stub(); global.navigator = getNavigator({ register, @@ -80,7 +80,7 @@ test('sw: register: registerSW: no localhost', async (t) => { hostname: 'cloudcmd.io', }; - const register = sinon.stub(); + const register = stub(); global.navigator = getNavigator({ register, @@ -107,7 +107,7 @@ test('sw: register: registerSW', async (t) => { hostname: 'localhost', }; - const register = sinon.stub(); + const register = stub(); global.navigator = getNavigator({ register, @@ -135,10 +135,10 @@ test('sw: register: unregisterSW', async (t) => { }; const reg = { - unregister: sinon.stub() + unregister: stub() }; - const register = sinon.stub() + const register = stub() .returns(Promise.resolve(reg)); global.navigator = getNavigator({ @@ -147,18 +147,18 @@ test('sw: register: unregisterSW', async (t) => { const {unregisterSW} = reRequire('./register'); - await unregisterSW(); + await unregisterSW('/hello'); global.location = location; global.navigator = navigator; - t.ok(register.calledWith(), 'should call register'); + t.ok(register.calledWith('/hello/sw.js'), 'should call register'); t.end(); }); function getNavigator({register, unregister}) { - unregister = unregister || sinon.stub(); + unregister = unregister || stub(); return { serviceWorker: { diff --git a/common/datetime.spec.js b/common/datetime.spec.js index 25e17c39..96a72b4d 100644 --- a/common/datetime.spec.js +++ b/common/datetime.spec.js @@ -1,8 +1,6 @@ 'use strict'; const test = require('tape'); -const sinon = require('sinon'); - const datetime = require('./datetime'); test('common: datetime', (t) => { diff --git a/package.json b/package.json index 51af495a..834882a8 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ }, "config": { "dirs": "bin/cloudcmd.js common server", - "dirs_test": "test bin/release.js webpack.config.js cssnano.config.js .webpack" + "dirs_test": "test bin/release.js webpack.config.js cssnano.config.js .webpack client/**/*.spec.js server/**/*.spec.js common/**/*.spec.js" }, "scripts": { "start": "node bin/cloudcmd.js", @@ -175,6 +175,7 @@ "@cloudcmd/create-element": "^1.0.0", "@cloudcmd/modal": "^1.0.0", "@cloudcmd/olark": "^2.0.1", + "@cloudcmd/stub": "^2.0.0", "babel-loader": "^8.0.0", "babel-plugin-macros": "^2.2.1", "clean-css-loader": "^1.0.1", @@ -215,8 +216,6 @@ "scroll-into-view-if-needed": "^2.2.5", "serve-once": "^1.5.0", "serviceworker-webpack-plugin": "^1.0.1", - "sinon": "^7.0.0", - "sinon-called-with-diff": "^3.0.0", "smalltalk": "^3.1.0", "style-loader": "^0.23.0", "stylelint": "^9.0.0", diff --git a/server/cloudcmd.spec.js b/server/cloudcmd.spec.js index e3da3de7..9fe11dde 100644 --- a/server/cloudcmd.spec.js +++ b/server/cloudcmd.spec.js @@ -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'; diff --git a/server/config.spec.js b/server/config.spec.js index bf5343b7..c17f5169 100644 --- a/server/config.spec.js +++ b/server/config.spec.js @@ -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'; diff --git a/server/distribute/export.spec.js b/server/distribute/export.spec.js index 0d50061a..987b64bb 100644 --- a/server/distribute/export.spec.js +++ b/server/distribute/export.spec.js @@ -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'); }); diff --git a/server/distribute/import.spec.js b/server/distribute/import.spec.js index f1ff761c..a7949a44 100644 --- a/server/distribute/import.spec.js +++ b/server/distribute/import.spec.js @@ -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, diff --git a/server/distribute/log.spec.js b/server/distribute/log.spec.js index 650de213..a5763106 100644 --- a/server/distribute/log.spec.js +++ b/server/distribute/log.spec.js @@ -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(); diff --git a/server/exit.spec.js b/server/exit.spec.js index 49718259..169c445e 100644 --- a/server/exit.spec.js +++ b/server/exit.spec.js @@ -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'); diff --git a/server/rest/info.spec.js b/server/rest/info.spec.js index a5bbf34a..5a70e4bd 100644 --- a/server/rest/info.spec.js +++ b/server/rest/info.spec.js @@ -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; diff --git a/server/root.spec.js b/server/root.spec.js new file mode 100644 index 00000000..72dc97ec --- /dev/null +++ b/server/root.spec.js @@ -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(); +}); + diff --git a/server/route.spec.js b/server/route.spec.js index 3856f37c..c315e51b 100644 --- a/server/route.spec.js +++ b/server/route.spec.js @@ -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(); diff --git a/server/validate.spec.js b/server/validate.spec.js index 3dffd946..e1464d56 100644 --- a/server/validate.spec.js +++ b/server/validate.spec.js @@ -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, { diff --git a/test/common/btoa.js b/test/common/btoa.js index 4d4bbe46..a18e8947 100644 --- a/test/common/btoa.js +++ b/test/common/btoa.js @@ -1,8 +1,7 @@ 'use strict'; const test = require('tape'); -const diff = require('sinon-called-with-diff'); -const sinon = diff(require('sinon')); +const stub = require('@cloudcmd/stub'); const btoa = require('../../common/btoa'); @@ -10,7 +9,7 @@ test('btoa: browser', (t) => { const btoaOriginal = global.btoa; const str = 'hello'; - global.btoa = sinon.stub(); + global.btoa = stub(); btoa(str); diff --git a/test/server/modulas.js b/test/server/modulas.js index 52d28e99..afafc2b9 100644 --- a/test/server/modulas.js +++ b/test/server/modulas.js @@ -1,10 +1,9 @@ 'use strict'; const {join} = require('path'); -const test = require('tape'); - -const diff = require('sinon-called-with-diff'); -const sinon = diff(require('sinon')); +const tryTo = require('try-to-tape'); +const test = tryTo(require('tape')); +const stub = require('@cloudcmd/stub'); const dir = join(__dirname, '..', '..'); const modulesPath = join(dir, 'json', 'modules.json'); @@ -14,7 +13,12 @@ const localModules = require(modulesPath); const modulas = require(`${dir}/server/modulas`); const cloudcmd = require(cloudcmdPath); -const {request} = require('serve-once')(cloudcmd); +const {request} = require('serve-once')(cloudcmd, { + config: { + auth: false, + dropbox: false, + } +}); test('cloudcmd: modules', async (t) => { const modules = { @@ -68,7 +72,7 @@ test('cloudcmd: modules: wrong route', async (t) => { test('cloudcmd: modules: no', (t) => { const fn = modulas(); const url = '/json/modules.json'; - const send = sinon.stub(); + const send = stub(); fn({url}, {send}); diff --git a/test/server/root.js b/test/server/root.js deleted file mode 100644 index e3dcbeb5..00000000 --- a/test/server/root.js +++ /dev/null @@ -1,70 +0,0 @@ -'use strict'; - -const path = require('path'); -const test = require('tape'); - -const diff = require('sinon-called-with-diff'); -const sinon = diff(require('sinon')); - -const dir = path.join(__dirname, '..', '..', 'server'); - -const pathConfig = path.join(dir, 'config'); -const pathRoot = `${dir}/root`; - -const clean = require('clear-module'); - -const {cache, resolve} = require; -const stub = (name, exports) => { - require(name); - - const resolved = resolve(name); - cache[resolved].exports = exports; -}; - -test('cloudcmd: root: config', (t) => { - clean(pathRoot); - - const config = sinon.stub().returns(false); - - stub(pathConfig, config); - - const root = require(pathRoot); - - root('hello'); - - t.ok(config.calledWith('root'), 'should call config'); - - clean(pathConfig); - clean(pathRoot); - - t.end(); -}); - -test('cloudcmd: root: mellow', (t) => { - clean(pathRoot); - - const config = sinon.stub().returns(''); - const pathToWin = sinon.stub(); - - const mellow = { - pathToWin - }; - - stub('mellow', mellow); - stub(pathConfig, config); - - const root = require(pathRoot); - const dir = 'hello'; - const dirRoot = '/'; - - root(dir); - - t.ok(pathToWin.calledWith(dir, dirRoot), 'should call mellow'); - - clean('mellow'); - clean(pathConfig); - clean(pathRoot); - - t.end(); -}); - diff --git a/test/server/terminal.js b/test/server/terminal.js index 1b596047..b47a46c9 100644 --- a/test/server/terminal.js +++ b/test/server/terminal.js @@ -1,8 +1,7 @@ 'use strict'; const test = require('tape'); -const diff = require('sinon-called-with-diff'); -const sinon = diff(require('sinon')); +const stub = require('@cloudcmd/stub'); const mockRequire = require('mock-require'); const {reRequire} = mockRequire; @@ -38,7 +37,7 @@ test('cloudcmd: terminal: disabled: listen', (t) => { }); test('cloudcmd: terminal: enabled', (t) => { - const term = sinon.stub(); + const term = stub(); const arg = 'hello'; mockRequire(configPath, () => '/terminal'); @@ -56,7 +55,7 @@ test('cloudcmd: terminal: enabled', (t) => { test('cloudcmd: terminal: enabled: no string', (t) => { const {log:originalLog} = console; - const log = sinon.stub(); + const log = stub(); mockRequire(configPath, () => 'hello');