mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 02:35:49 +00:00
test(rest) mv: rename
This commit is contained in:
parent
835717a084
commit
5944521719
3 changed files with 97 additions and 62 deletions
|
|
@ -1 +0,0 @@
|
|||
hello
|
||||
155
test/rest/mv.js
155
test/rest/mv.js
|
|
@ -1,20 +1,23 @@
|
|||
'use strict';
|
||||
|
||||
const {
|
||||
join,
|
||||
} = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
const test = require('tape');
|
||||
const {promisify} = require('es6-promisify');
|
||||
const pullout = require('pullout');
|
||||
const request = require('request');
|
||||
const before = require('../before');
|
||||
const rimraf = require('rimraf');
|
||||
const mkdirp = require('mkdirp');
|
||||
const {Volume} = require('memfs');
|
||||
const {ufs} = require('unionfs');
|
||||
const tryToCatch = require('try-to-catch');
|
||||
const mockRequire = require('mock-require');
|
||||
const clean = require('clear-module');
|
||||
|
||||
const fixtureDir = join(__dirname, '..', 'fixture') + '/';
|
||||
const root = '../../';
|
||||
const dir = root + 'server/';
|
||||
const cloudcmdPath = dir + 'cloudcmd';
|
||||
const restPath = dir + 'rest';
|
||||
const beforePath = '../before';
|
||||
|
||||
const warp = (fn, ...a) => (...b) => fn(...b, ...a);
|
||||
const _pullout = promisify(pullout);
|
||||
|
||||
const put = promisify((url, json, fn) => {
|
||||
|
|
@ -23,61 +26,91 @@ const put = promisify((url, json, fn) => {
|
|||
}));
|
||||
});
|
||||
|
||||
test('cloudcmd: rest: mv', (t) => {
|
||||
before({}, (port, after) => {
|
||||
const tmp = join(fixtureDir, 'tmp');
|
||||
const files = {
|
||||
from: '/fixture/',
|
||||
to: '/fixture/tmp/',
|
||||
names: [
|
||||
'mv.txt'
|
||||
]
|
||||
};
|
||||
|
||||
mkdirp.sync(tmp);
|
||||
|
||||
const rmTmp = () => rimraf.sync(tmp);
|
||||
|
||||
put(`http://localhost:${port}/api/v1/mv`, files)
|
||||
.then(warp(_pullout, 'string'))
|
||||
.then((body) => {
|
||||
t.equal(body, 'move: ok("["mv.txt"]")', 'should move');
|
||||
t.end();
|
||||
|
||||
fs.renameSync(`${tmp}/mv.txt`, `${fixtureDir}/mv.txt`);
|
||||
|
||||
after();
|
||||
})
|
||||
.catch(console.error)
|
||||
.then(rmTmp);
|
||||
test('cloudcmd: rest: mv', async (t) => {
|
||||
const volume = {
|
||||
'/fixture/mv.txt': 'hello',
|
||||
'/fixture/tmp/a.txt': 'a',
|
||||
};
|
||||
|
||||
const vol = Volume.fromJSON(volume, '/');
|
||||
|
||||
const unionFS = ufs
|
||||
.use(vol)
|
||||
.use(fs);
|
||||
|
||||
clean(beforePath);
|
||||
clean(cloudcmdPath);
|
||||
clean(restPath);
|
||||
clean('@cloudcmd/move-files');
|
||||
clean('@cloudcmd/rename-files');
|
||||
|
||||
mockRequire('fs', unionFS);
|
||||
|
||||
const {connect} = require(beforePath);
|
||||
const {port, done} = await connect({
|
||||
config: {
|
||||
root: '/'
|
||||
}
|
||||
});
|
||||
|
||||
const files = {
|
||||
from: '/fixture/',
|
||||
to: '/fixture/tmp/',
|
||||
names: [
|
||||
'mv.txt'
|
||||
]
|
||||
};
|
||||
|
||||
|
||||
const [, result] = await tryToCatch(put, `http://localhost:${port}/api/v1/mv`, files);
|
||||
const body = await _pullout(result, 'string');
|
||||
|
||||
done();
|
||||
mockRequire.stop('fs');
|
||||
|
||||
t.equal(body, 'move: ok("["mv.txt"]")', 'should move');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudcmd: rest: mv: rename', (t) => {
|
||||
before({}, (port, after) => {
|
||||
const tmp = join(fixtureDir, 'tmp');
|
||||
const files = {
|
||||
from: '/fixture/mv.txt',
|
||||
to: '/fixture/tmp/mv.txt',
|
||||
};
|
||||
|
||||
mkdirp.sync(tmp);
|
||||
|
||||
const rmTmp = () => rimraf.sync(tmp);
|
||||
|
||||
put(`http://localhost:${port}/api/v1/mv`, files)
|
||||
.then(warp(_pullout, 'string'))
|
||||
.then((body) => {
|
||||
const expected = 'move: ok("{"from":"/fixture/mv.txt","to":"/fixture/tmp/mv.txt"}")';
|
||||
|
||||
t.equal(body, expected, 'should move');
|
||||
t.end();
|
||||
|
||||
fs.renameSync(`${tmp}/mv.txt`, `${fixtureDir}/mv.txt`);
|
||||
|
||||
after();
|
||||
})
|
||||
.catch(console.error)
|
||||
.then(rmTmp);
|
||||
test('cloudcmd: rest: mv: rename', async (t) => {
|
||||
const volume = {
|
||||
'/fixture/mv.txt': 'hello',
|
||||
'/fixture/tmp/a.txt': 'a',
|
||||
};
|
||||
|
||||
const vol = Volume.fromJSON(volume, '/');
|
||||
|
||||
const unionFS = ufs
|
||||
.use(vol)
|
||||
.use(fs);
|
||||
|
||||
clean(beforePath);
|
||||
clean(cloudcmdPath);
|
||||
clean(restPath);
|
||||
|
||||
mockRequire('fs', unionFS);
|
||||
|
||||
const {connect} = require(beforePath);
|
||||
const {port, done} = await connect({
|
||||
config: {
|
||||
root: '/'
|
||||
}
|
||||
});
|
||||
|
||||
const files = {
|
||||
from: '/fixture/mv.txt',
|
||||
to: '/fixture/tmp/mv.txt',
|
||||
};
|
||||
|
||||
const [, result] = await tryToCatch(put, `http://localhost:${port}/api/v1/mv`, files);
|
||||
const body = await _pullout(result, 'string');
|
||||
|
||||
done();
|
||||
mockRequire.stop('fs');
|
||||
|
||||
const expected = 'move: ok("{"from":"/fixture/mv.txt","to":"/fixture/tmp/mv.txt"}")';
|
||||
|
||||
t.equal(body, expected, 'should move');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue