From 6980b1350c7aa7419ac1d436081579ce0a8e0367 Mon Sep 17 00:00:00 2001 From: Quentin Rossetti Date: Mon, 11 Aug 2014 15:42:27 +0200 Subject: [PATCH] Test command implemented with Promises --- index.js | 26 ++++++++++++++++++++++++++ test/index.js | 23 +++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 index.js create mode 100644 test/index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..cb3742c --- /dev/null +++ b/index.js @@ -0,0 +1,26 @@ +'use strict'; + +var Promise = require('promise'); +var process = require('child_process'); +var os = require('os'); +var path = require('path'); + +module.exports.test = function test(archivePath) { + + return new Promise(function (fulfill, reject){ + var command = '7z t ' + archivePath; + process.exec(command, function (err, stdout, stderr) { + //TODO: Custom error + if (err) return reject(err); + if (stderr) return reject(stderr); + var r = []; + stdout.split(os.EOL).forEach(function (_line) { + if (_line.substr(0, 12) === 'Testing ') { + return r.push(_line.substr(12, _line.length).replace(path.sep, '/')); + } + }); + return fulfill(r); + }); + }); + +}; diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..07f33c1 --- /dev/null +++ b/test/index.js @@ -0,0 +1,23 @@ +'use strict'; +var sevenzip = require('../index'); +var expect = require('chai').expect; + +describe('Specifications for the `test` function', function(){ + + it('should get an error when 7z gets an error', function (done) { + sevenzip.test('test/resource/nothere.7z') + .then(null, function (err) { + expect(err.message.substr(0, 14)).to.eql('Command failed'); + done(); + }); + }); + + it('should get a list of files and directories', function (done) { + sevenzip.test('test/resource/resource.7z') + .then(function (files) { + expect(files.length).to.be.at.least(6); + done(); + }); + }); + +});