mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 10:45:47 +00:00
feature: rest: get rid of mock-require
This commit is contained in:
parent
401a669aca
commit
1d5d22ecf8
4 changed files with 46 additions and 58 deletions
|
|
@ -26,7 +26,7 @@ const {createConfig, configPath} = require(`./config`);
|
|||
const modulas = require(`./modulas`);
|
||||
|
||||
const userMenu = require(`./user-menu`);
|
||||
const rest = require(`./rest`);
|
||||
const rest = require(`./rest/index.js`);
|
||||
const route = require(`./route`);
|
||||
const validate = require(`./validate`);
|
||||
const prefixer = require(`./prefixer`);
|
||||
|
|
@ -248,7 +248,11 @@ function cloudcmdMiddle({modules, config}) {
|
|||
userMenu({
|
||||
menuName: '.cloudcmd.menu.js',
|
||||
}),
|
||||
rest(config),
|
||||
rest({
|
||||
config,
|
||||
fs: depStore('fs'),
|
||||
moveFiles: depStore('moveFiles'),
|
||||
}),
|
||||
route(config, {
|
||||
html,
|
||||
win32: depStore('win32'),
|
||||
|
|
|
|||
|
|
@ -1,16 +1,9 @@
|
|||
'use strict';
|
||||
|
||||
const process = require('node:process');
|
||||
const DIR = '../';
|
||||
const DIR_COMMON = `${DIR}../common/`;
|
||||
|
||||
const path = require('node:path');
|
||||
const fs = require('node:fs');
|
||||
const _fs = require('node:fs');
|
||||
|
||||
const root = require(`${DIR}root`);
|
||||
const CloudFunc = require(`${DIR_COMMON}cloudfunc`);
|
||||
const markdown = require(`${DIR}markdown`);
|
||||
const info = require('./info');
|
||||
const process = require('node:process');
|
||||
|
||||
const jaguar = require('jaguar');
|
||||
const onezip = require('onezip');
|
||||
|
|
@ -22,7 +15,13 @@ const json = require('jonny');
|
|||
const ponse = require('ponse');
|
||||
|
||||
const copymitter = require('copymitter');
|
||||
const moveFiles = require('@cloudcmd/move-files');
|
||||
const _moveFiles = require('@cloudcmd/move-files');
|
||||
|
||||
const root = require(`../root`);
|
||||
const CloudFunc = require(`../../common/cloudfunc`);
|
||||
const markdown = require(`../markdown/index.js`);
|
||||
const info = require('./info');
|
||||
|
||||
const isString = (a) => typeof a === 'string';
|
||||
const isFn = (a) => typeof a === 'function';
|
||||
const swap = wraptile((fn, a, b) => fn(b, a));
|
||||
|
|
@ -37,7 +36,7 @@ const UserError = (msg) => {
|
|||
return error;
|
||||
};
|
||||
|
||||
module.exports = currify((config, request, response, next) => {
|
||||
module.exports = currify(({config, fs = _fs, moveFiles = _moveFiles}, request, response, next) => {
|
||||
const name = ponse.getPathName(request);
|
||||
const regExp = RegExp(`^${apiURL}`);
|
||||
const is = regExp.test(name);
|
||||
|
|
@ -45,10 +44,10 @@ module.exports = currify((config, request, response, next) => {
|
|||
if (!is)
|
||||
return next();
|
||||
|
||||
rest(config, request, response);
|
||||
rest({fs, config, moveFiles}, request, response);
|
||||
});
|
||||
|
||||
function rest(config, request, response) {
|
||||
function rest({fs, config, moveFiles}, request, response) {
|
||||
const name = ponse.getPathName(request);
|
||||
const params = {
|
||||
request,
|
||||
|
|
@ -56,7 +55,7 @@ function rest(config, request, response) {
|
|||
name: name.replace(apiURL, '') || '/',
|
||||
};
|
||||
|
||||
sendData(params, config, (error, options, data) => {
|
||||
sendData(params, {fs, config, moveFiles}, (error, options, data) => {
|
||||
params.gzip = !error;
|
||||
|
||||
if (!data) {
|
||||
|
|
@ -87,8 +86,10 @@ function rest(config, request, response) {
|
|||
* getting data on method and command
|
||||
*
|
||||
* @param params {name, method, body, requrest, response}
|
||||
* @param config {}
|
||||
* @param callback
|
||||
*/
|
||||
function sendData(params, config, callback) {
|
||||
function sendData(params, {fs, config, moveFiles}, callback) {
|
||||
const p = params;
|
||||
const isMD = p.name.startsWith('/markdown');
|
||||
const rootDir = config('root');
|
||||
|
|
@ -107,6 +108,8 @@ function sendData(params, config, callback) {
|
|||
.then((body) => {
|
||||
onPUT({
|
||||
name: p.name,
|
||||
fs,
|
||||
moveFiles,
|
||||
config,
|
||||
body,
|
||||
}, callback);
|
||||
|
|
@ -185,7 +188,7 @@ const getRenameMsg = (from, to) => {
|
|||
};
|
||||
|
||||
module.exports._onPUT = onPUT;
|
||||
function onPUT({name, config, body}, callback) {
|
||||
function onPUT({name, fs, moveFiles, config, body}, callback) {
|
||||
checkPut(name, body, callback);
|
||||
|
||||
const cmd = getCMD(name);
|
||||
|
|
@ -221,7 +224,7 @@ function onPUT({name, config, body}, callback) {
|
|||
}
|
||||
|
||||
case 'rename':
|
||||
return rename(rootDir, files.from, files.to, callback);
|
||||
return rename(rootDir, files.from, files.to, fs, callback);
|
||||
|
||||
case 'copy':
|
||||
if (!files.from || !files.names || !files.to)
|
||||
|
|
@ -260,7 +263,7 @@ function onPUT({name, config, body}, callback) {
|
|||
}
|
||||
}
|
||||
|
||||
function rename(rootDir, from, to, callback) {
|
||||
function rename(rootDir, from, to, fs, callback) {
|
||||
if (!from)
|
||||
return callback(UserError('"from" should be filled'));
|
||||
|
||||
|
|
|
|||
|
|
@ -1,20 +1,15 @@
|
|||
'use strict';
|
||||
|
||||
const wait = require('@iocmd/wait');
|
||||
const {EventEmitter} = require('node:events');
|
||||
const fs = require('node:fs');
|
||||
|
||||
const test = require('supertape');
|
||||
const {test, stub} = require('supertape');
|
||||
const {Volume} = require('memfs');
|
||||
const {ufs} = require('unionfs');
|
||||
|
||||
const mockRequire = require('mock-require');
|
||||
const serveOnce = require('serve-once');
|
||||
const {reRequire, stopAll} = mockRequire;
|
||||
|
||||
const cloudcmdPath = '../../';
|
||||
const dir = `${cloudcmdPath}server/`;
|
||||
const restPath = `${dir}rest`;
|
||||
|
||||
const {assign} = Object;
|
||||
const cloudcmd = require('../../server/cloudcmd.js');
|
||||
|
||||
test('cloudcmd: rest: move', async (t) => {
|
||||
const volume = {
|
||||
|
|
@ -28,17 +23,11 @@ test('cloudcmd: rest: move', async (t) => {
|
|||
.use(vol)
|
||||
.use(fs);
|
||||
|
||||
assign(unionFS, {
|
||||
promises: fs.promises,
|
||||
});
|
||||
mockRequire('fs', unionFS);
|
||||
const move = new EventEmitter();
|
||||
const moveFiles = stub().returns(move);
|
||||
|
||||
reRequire('@cloudcmd/rename-files');
|
||||
reRequire('@cloudcmd/move-files');
|
||||
reRequire(restPath);
|
||||
|
||||
const cloudcmd = reRequire(cloudcmdPath);
|
||||
const {createConfigManager} = cloudcmd;
|
||||
cloudcmd.depStore('moveFiles', moveFiles);
|
||||
|
||||
const configManager = createConfigManager();
|
||||
configManager('auth', false);
|
||||
|
|
@ -54,18 +43,20 @@ test('cloudcmd: rest: move', async (t) => {
|
|||
names: ['move.txt'],
|
||||
};
|
||||
|
||||
const {body} = await request.put(`/api/v1/move`, {
|
||||
body: files,
|
||||
});
|
||||
const emit = move.emit.bind(move);
|
||||
|
||||
stopAll();
|
||||
const [{body}] = await Promise.all([
|
||||
request.put(`/api/v1/move`, {
|
||||
body: files,
|
||||
}),
|
||||
wait(1000, emit, 'end'),
|
||||
]);
|
||||
|
||||
t.equal(body, 'move: ok("["move.txt"]")', 'should move');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudcmd: rest: move: no from', async (t) => {
|
||||
const cloudcmd = reRequire(cloudcmdPath);
|
||||
const {createConfigManager} = cloudcmd;
|
||||
|
||||
const configManager = createConfigManager();
|
||||
|
|
@ -89,7 +80,6 @@ test('cloudcmd: rest: move: no from', async (t) => {
|
|||
});
|
||||
|
||||
test('cloudcmd: rest: move: no to', async (t) => {
|
||||
const cloudcmd = reRequire(cloudcmdPath);
|
||||
const {createConfigManager} = cloudcmd;
|
||||
|
||||
const configManager = createConfigManager();
|
||||
|
|
@ -113,3 +103,4 @@ test('cloudcmd: rest: move: no to', async (t) => {
|
|||
t.equal(body, expected);
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -6,10 +6,9 @@ const test = require('supertape');
|
|||
const {Volume} = require('memfs');
|
||||
const {ufs} = require('unionfs');
|
||||
|
||||
const mockRequire = require('mock-require');
|
||||
const serveOnce = require('serve-once');
|
||||
const {reRequire, stopAll} = mockRequire;
|
||||
|
||||
const cloudcmd = require('../../server/cloudcmd.js');
|
||||
const cloudcmdPath = '../../';
|
||||
const dir = `${cloudcmdPath}server/`;
|
||||
const restPath = `${dir}rest`;
|
||||
|
|
@ -26,19 +25,13 @@ test('cloudcmd: rest: rename', async (t) => {
|
|||
.use(vol)
|
||||
.use(fs);
|
||||
|
||||
mockRequire('node:fs', unionFS);
|
||||
|
||||
reRequire('@cloudcmd/rename-files');
|
||||
reRequire('@cloudcmd/move-files');
|
||||
reRequire(restPath);
|
||||
|
||||
const cloudcmd = reRequire(cloudcmdPath);
|
||||
const {createConfigManager} = cloudcmd;
|
||||
const configManager = createConfigManager();
|
||||
|
||||
configManager('auth', false);
|
||||
configManager('root', '/');
|
||||
|
||||
cloudcmd.depStore('fs', unionFS);
|
||||
const {request} = serveOnce(cloudcmd, {
|
||||
configManager,
|
||||
});
|
||||
|
|
@ -52,18 +45,15 @@ test('cloudcmd: rest: rename', async (t) => {
|
|||
body: files,
|
||||
});
|
||||
|
||||
mockRequire.stopAll();
|
||||
cloudcmd.depStore();
|
||||
|
||||
const expected = 'rename: ok("{"from":"/fixture/mv.txt","to":"/fixture/tmp/mv.txt"}")';
|
||||
|
||||
stopAll();
|
||||
|
||||
t.equal(body, expected, 'should move');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudcmd: rest: rename: no from', async (t) => {
|
||||
const cloudcmd = reRequire(cloudcmdPath);
|
||||
const {createConfigManager} = cloudcmd;
|
||||
|
||||
const configManager = createConfigManager();
|
||||
|
|
@ -87,7 +77,6 @@ test('cloudcmd: rest: rename: no from', async (t) => {
|
|||
});
|
||||
|
||||
test('cloudcmd: rest: rename: no to', async (t) => {
|
||||
const cloudcmd = reRequire(cloudcmdPath);
|
||||
const {createConfigManager} = cloudcmd;
|
||||
|
||||
const configManager = createConfigManager();
|
||||
|
|
@ -111,3 +100,4 @@ test('cloudcmd: rest: rename: no to', async (t) => {
|
|||
t.equal(body, expected);
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue