Skip to content

Commit

Permalink
Feature: Test command
Browse files Browse the repository at this point in the history
  • Loading branch information
q2s2t committed Aug 25, 2014
1 parent 725c81d commit 0594c08
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 24 deletions.
59 changes: 35 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,46 @@ npm install --save 7z
API
---

### Extract with full paths: `Zip.extractFull`

**Arguments**
* `archive` The path to the archive you want to analyse.
* `dest` Where to extract the archive.
* `options` An object of options.
### Test integrity of archive: `Zip.test`

**Arguments**
* `archive` The path to the archive you want to analyse.
* `options` An object of options.

**Progress**
* `files` A array of all the extracted files *AND* directories. The `/`
character is used as a path separator on every platform.

**Error**
* `err` An Error object.

**Progress**
* `files` A array of all the extracted files *AND* directories. The `/`
character is used as a path separator on every platform.
### Extract with full paths: `Zip.extractFull`

**Error**
* `err` An Error object. Its message is the message outputed by 7-Zip.
* `code` 7-Zip [exit code](http://sevenzip.sourceforge.jp/chm/cmdline/exit_codes.htm).
**Arguments**
* `archive` The path to the archive you want to analyse.
* `dest` Where to extract the archive.
* `options` An object of options.

**Progress**
* `files` A array of all the extracted files *AND* directories. The `/`
character is used as a path separator on every platform.

**Error**
* `err` An Error object.

### Extract: `Zip.extract`

**Arguments**
* `archive` The path to the archive you want to analyse.
* `dest` Where to extract the archive.
* `options` An object of options.

**Progress**
* `files` A array of all the extracted files *AND* directories. The `/`
character is used as a path separator on every platform.

**Error**
* `err` An Error object. Its message is the message outputed by 7-Zip.
* `code` 7-Zip [exit code](http://sevenzip.sourceforge.jp/chm/cmdline/exit_codes.htm).
**Arguments**
* `archive` The path to the archive you want to analyse.
* `dest` Where to extract the archive.
* `options` An object of options.

**Progress**
* `files` A array of all the extracted files *AND* directories. The `/`
character is used as a path separator on every platform.

**Error**
* `err` An Error object.

***
With :heart: from [quentinrossetti](https://github.com/quentinrossetti)
Expand Down
2 changes: 2 additions & 0 deletions lib/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var u = {
/**
* Extract an archive.
* @promise Extract
* @param {string} archive Path to the archive.
* @param {string} dest Destination.
* @progress {array} Extracted files and directories.
* @reject {Error} The error as issued by 7-Zip.
*/
Expand Down
2 changes: 2 additions & 0 deletions lib/extractFull.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var u = {
/**
* Extract an archive with full paths.
* @promise ExtractFull
* @param {string} archive Path to the archive.
* @param {string} dest Destination.
* @progress {array} Extracted files and directories.
* @reject {Error} The error as issued by 7-Zip.
*/
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var Zip = function () {};

Zip.prototype.test = require('./test');
Zip.prototype.extract = require('./extract');
Zip.prototype.extractFull = require('./extractFull');

Expand Down
49 changes: 49 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';
var path = require('path');
var when = require('when');
var u = {
run: require('../util/run'),
switches : require('../util/switches')
};

/**
* Test integrity of archive.
* @promise Test
* @param archive {string} Path to the archive.
* @progress {array} Extracted files and directories.
* @reject {Error} The error as issued by 7-Zip.
*/
module.exports = function (archive, options) {
return when.promise(function (fulfill, reject, progress) {

if (options === undefined) {
options = {};
}

var opts = u.switches(options);
var command = '7z t ' + archive + ' ' + opts;
u.run(command)

// When a stdout is emitted, parse each line and search for a pattern. When
// the pattern is found, extract the file (or directory) name from it and
// pass it to an array. Finally returns this array.
.progress(function (data) {
var entries = [];
data.split('\n').forEach(function (line) {
if (line.substr(0, 12) === 'Testing ') {
entries.push(line.substr(12, line.length).replace(path.sep, '/'));
}
});
progress(entries);
})

.then(function () {
fulfill();
})

.catch(function (err) {
reject(err);
});

});
};
27 changes: 27 additions & 0 deletions test/lib/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*global describe, it, afterEach */
var expect = require('chai').expect;
var fs = require('fs');
var rimraf = require('rimraf');
var test = require('../../lib/test');

describe('Method: `Zip.test`', function () {

afterEach(function () { rimraf.sync('.tmp/test'); });

it('should return an error on 7z error', function (done) {
test('test/nothere.7z')
.catch(function (err) {
expect(err).to.be.an.instanceof(Error);
done();
});
});

it('should return entries on progress', function (done) {
test('test/zip.7z', { r: true })
.progress(function (entries) {
expect(entries.length).to.be.at.least(1);
done();
});
});

});

0 comments on commit 0594c08

Please sign in to comment.