fix(rest) pack, extract: put

This commit is contained in:
coderaiser 2016-10-27 17:11:41 +03:00
parent 8aff5a7f8b
commit 4b6fc40588
3 changed files with 106 additions and 14 deletions

View file

@ -23,7 +23,7 @@
check = require('checkup'),
isWin32 = process.platform === 'win32';
/**
* rest interface
*
@ -123,7 +123,7 @@
if (/^pack/.test(cmd)) {
cmd = cmd.replace(/^pack/, '');
streamPack(root(cmd), p.response, callback);
streamPack(root(cmd), p.response);
} else {
switch(cmd) {
case '':
@ -143,14 +143,15 @@
}
}
function streamPack(cmd, response, fn) {
var filename = cmd.replace(/\.tar\.gz$/, ''),
dir = path.dirname(filename),
names = [
path.basename(filename)
];
function streamPack(cmd, response) {
var noop = function() {};
var filename = cmd.replace(/\.tar\.gz$/, '');
var dir = path.dirname(filename);
var names = [
path.basename(filename)
];
operation('pack', dir, response, names, fn);
operation('pack', dir, response, names, noop);
}
/**
@ -262,6 +263,7 @@
}
function operation(op, from, to, names, fn) {
var wasError;
var packer;
if (!fn) {
@ -274,6 +276,7 @@
packer = jaguar[op](from, to, names);
packer.on('error', function(error) {
wasError = true;
fn(error);
});
@ -286,7 +289,15 @@
});
packer.on('end', function() {
var name, msg;
process.stdout.write('\n');
if (!wasError) {
name = path.basename(from);
msg = formatMsg(op, name);
fn(null, msg);
}
});
}

View file

@ -140,6 +140,7 @@
"pullout": "^1.0.0",
"recess": "^1.1.9",
"redrun": "^5.0.0",
"request": "^2.76.0",
"shortdate": "^1.0.1",
"stylelint": "^7.0.2",
"stylelint-config-standard": "^14.0.0",

View file

@ -1,10 +1,12 @@
const http = require('http');
const fs = require('fs');
const path = require('path');
const test = require('tape');
const express = require('express');
const promisify = require('es6-promisify');
const pullout = require('pullout');
const request = require('request');
const wrap = (fn, ...a) => (...b) => fn(...a, ...b);
const warp = (fn, ...a) => (...b) => fn(...b, ...a);
@ -14,7 +16,11 @@ const freeport = promisify(require('freeport'));
const _pullout = promisify(pullout);
const get = promisify((url, fn) => {
http.get(url, success(fn));
fn(null, request(url));
});
const put = promisify((options, fn) => {
fn(null, request.put(options));
});
const cloudcmd = require('..');
@ -45,7 +51,6 @@ const before = (fn) => {
test('cloudcmd: rest: fs: path', (t) => {
before((port, after) => {
console.log(port);
get(`http://${host}:${port}/api/v1/fs`)
.then(warp(_pullout, 'string'))
.then(JSON.parse)
@ -60,11 +65,10 @@ test('cloudcmd: rest: fs: path', (t) => {
});
});
test('cloudcmd: rest: pack', (t) => {
test('cloudcmd: rest: pack: get', (t) => {
before((port, after) => {
console.log(port);
get(`http://${host}:${port}/api/v1/pack/fixture/pack`)
.then(warp(_pullout, 'buffer'))
.then(_pullout)
.then((pack) => {
const fixture = fs.readFileSync(__dirname + '/fixture/pack.tar.gz');
t.ok(fixture.compare(pack), 'should pack data');
@ -77,3 +81,79 @@ test('cloudcmd: rest: pack', (t) => {
});
});
test('cloudcmd: rest: pack: put: file', (t) => {
before((port, after) => {
const name = String(Math.random()) + '.tar.gz';
const options = getPackOptions(host, port, name);
put(options)
.then(warp(_pullout, 'string'))
.then((pack) => {
const file = fs.readFileSync(__dirname + '/' + name);
const fixture = fs.readFileSync(__dirname + '/fixture/pack.tar.gz');
fs.unlinkSync(`${__dirname}/${name}`);
t.ok(fixture.compare(file), 'should create archive');
t.end();
after();
})
.catch((error) => {
console.log(error);
});
});
});
test('cloudcmd: rest: pack: put: response', (t) => {
before((port, after) => {
const name = String(Math.random()) + '.tar.gz';
const options = getPackOptions(host, port, name);
put(options)
.then(warp(_pullout, 'string'))
.then((msg) => {
t.equal(msg, 'pack: ok("fixture")', 'should return result message');
fs.unlinkSync(`${__dirname}/${name}`);
t.end();
after();
})
.catch((error) => {
console.log(error);
});
});
});
test('cloudcmd: rest: pack: put: error', (t) => {
before((port, after) => {
const name = String(Math.random()) + '.tar.gz';
const options = getPackOptions(host, port, 'name', [
'not found'
]);
put(options)
.then(warp(_pullout, 'string'))
.then((msg) => {
t.ok(/^ENOENT: no such file or directory/.test(msg), 'should return error');
t.end();
after();
})
.catch((error) => {
console.log(error);
});
});
});
function getPackOptions(host, port, to, names = ['pack']) {
return {
url: `http://${host}:${port}/api/v1/pack`,
json: {
to,
names,
from: '/fixture',
}
};
}