mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 02:35:49 +00:00
feature: rest: get rid of mock-require
This commit is contained in:
parent
98e0011e8e
commit
25d8faea63
4 changed files with 46 additions and 58 deletions
|
|
@ -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