test(rest) mv: rename

This commit is contained in:
coderaiser 2018-05-16 13:53:11 +03:00
parent 835717a084
commit 5944521719
3 changed files with 97 additions and 62 deletions

View file

@ -181,6 +181,7 @@
"html-webpack-plugin": "^3.0.7",
"inherits": "^2.0.3",
"limier": "^2.0.0",
"memfs": "^2.9.0",
"minor": "^1.2.2",
"mkdirp": "^0.5.1",
"mock-require": "^3.0.1",
@ -205,6 +206,8 @@
"supermenu": "^2.0.0",
"tape": "^4.4.0",
"tar-stream": "^1.5.2",
"try-to-catch": "^1.0.2",
"unionfs": "^3.0.2",
"url-loader": "^1.0.1",
"version-io": "^2.0.1",
"webpack": "^4.0.0",

View file

@ -1 +0,0 @@
hello

View file

@ -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();
});