From 4b6fc4058868b9ebc777bfb7179548a3851e3bb4 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 27 Oct 2016 17:11:41 +0300 Subject: [PATCH] fix(rest) pack, extract: put --- lib/server/rest.js | 29 ++++++++++----- package.json | 1 + test/cloudcmd.js | 90 +++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 106 insertions(+), 14 deletions(-) diff --git a/lib/server/rest.js b/lib/server/rest.js index adcc666b..18774a1e 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -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); + } }); } diff --git a/package.json b/package.json index eedb2be2..b035e3a8 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/cloudcmd.js b/test/cloudcmd.js index 4421c41b..a4415441 100644 --- a/test/cloudcmd.js +++ b/test/cloudcmd.js @@ -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', + } + }; +} +