Skip to content

Commit

Permalink
Fix tests on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
q2s2t committed Oct 24, 2014
1 parent b148140 commit a87f174
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 32 deletions.
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ myTask.extractFull('myArchive.7z', 'destination', { p: 'myPassword' })
Installation
------------

You must have the `7za` executable available in your PATH or in the same
You must have the `7za` executable available in your PATH or in the same
directory of your `package.json` file.

> On Debian an Ubuntu install the `p7zip-full` package.
Expand All @@ -52,6 +52,8 @@ API
> See the [7-Zip documentation](http://sevenzip.sourceforge.jp/chm/cmdline/index.htm)
> for the full list of usages and options (switches).
> The type of the list of files can be either *String* or *Array*.
### Add: `Zip.add`

**Arguments**
Expand Down Expand Up @@ -115,13 +117,13 @@ API
* `options` An object of options.

**Progress**
* `files` A array of objects of all the extracted files *AND* directories.
* `files` A array of objects of all the extracted files *AND* directories.
The `/` character is used as a path separator on every platform. Object's
properties are: `date`, `attr`, `size` and `name`.

**Fulfill**
* `spec` An object of tech spec about the archive. Properties are: `path`,
`type`, `method`, `physicalSize` and `headersSize` (Some of them may be
* `spec` An object of tech spec about the archive. Properties are: `path`,
`type`, `method`, `physicalSize` and `headersSize` (Some of them may be
missing with non-7z archives).

**Error**
Expand Down Expand Up @@ -157,6 +159,32 @@ API
* `err` An Error object.


Advanced usage
--------------

### Compression method

With the `7za` binary compression is made like that:

```bat
# adds *.exe and *.dll files to solid archive archive.7z using LZMA method
# with 2 MB dictionary and BCJ filter.
7z a archive.7z *.exe *.dll -m0=BCJ -m1=LZMA:d=21
```

With **node-7z** you can translate it like that:

```js
var archive = new Zip();
archive.add('archive.7z', [ '*.exe' '*.dll' ], {
m0: '=BCJ',
m1: '=LZMA:d=21'
})
.then(function () {
// Do stuff...
});
```

***
With :heart: from [quentinrossetti](https://github.com/quentinrossetti)

Expand Down
1 change: 1 addition & 0 deletions TOTO.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ TODO
- [x] Normalize path in `run`?
- [ ] Streaming abilities
- [ ] Multiple files in add, delete, etc.
- [ ] Verify all doc
16 changes: 11 additions & 5 deletions lib/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@ var u = {
* Add content to an archive.
* @promise Add
* @param archive {string} Path to the archive.
* @param files {string} Files to add.
* @param options {Object} An object of acceptables options to 7z bin.
* @param files {string|Array} Files to add.
* @param options {Object} An object of acceptable options to 7za bin.
* @progress {array} Listed files and directories.
* @reject {Error} The error as issued by 7-Zip.
*/
module.exports = function (archive, files, options) {
return when.promise(function (fulfill, reject, progress) {

if (options === undefined) {
options = {};
var toAdd = '';
if (files instanceof Array) {
files.forEach(function (f) {
toAdd += '"' + f + '" ';
});
toAdd = toAdd.trim();
} else {
toAdd = '"' + files + '"';
}

var command = '7za a "' + archive + '" "' + files + '"';
var command = '7za a "' + archive + '" ' + toAdd;

u.run(command, options)

Expand Down
14 changes: 14 additions & 0 deletions test/method.Zip.add.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,18 @@ describe('Method: `Zip.add`', function () {
});
});

it('should accept array as source', function (done) {
var store = [];
add('.tmp/test/add.zip', ['*.md', '*.js'])
.progress(function (entries) {
entries.forEach(function (e) {
store.push(e)
});
})
.done(function () {
expect(store.length).to.be.at.least(5);
done();
});
});

});
40 changes: 18 additions & 22 deletions test/util.run.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*global describe, it */
var expect = require('chai').expect;
var run = require('../util/run');
var sep = require('path').sep;

describe('Utility: `run`', function () {

Expand Down Expand Up @@ -34,39 +35,34 @@ describe('Utility: `run`', function () {
m1: '=LZMA:d=21'
})
.then(function (res) {
expect(res.args).to.eql([
'a',
'.tmp/test/archive.7z',
'*.exe',
'*.dll',
'-m0=BCJ',
'-m1=LZMA:d=21',
'-ssc',
'-y',
]);
expect(res).to.contain('a');
expect(res).to.contain('.tmp'+sep+'test'+sep+'archive.7z');
expect(res).to.contain('*.exe');
expect(res).to.contain('*.dll');
expect(res).to.contain('-m0=BCJ');
expect(res).to.contain('-m1=LZMA:d=21');
expect(res).to.contain('-ssc');
expect(res).to.contain('-y');
done();
});
});

it('should correctly parse complex commands with spaces', function (done) {
var sep = require('path').sep;
run('7za a ".tmp/Folder A/Folder B\\archive.7z" "*.exe" "*.dll"', {
m0: '=BCJ',
m1: '=LZMA:d=21',
p : 'My mhjls/\\c $^é5°',
})
.then(function (res) {
expect(res.args).to.eql([
'a',
'.tmp/Folder A'+sep+'Folder B'+sep+'archive.7z',
'*.exe',
'*.dll',
'-m0=BCJ',
'-m1=LZMA:d=21',
'-p"My mhjls/\\c $^é5°"',
'-ssc',
'-y'
]);
expect(res).to.contain('a');
expect(res).to.contain('.tmp'+sep+'Folder A'+sep+'Folder B'+sep+'archive.7z');
expect(res).to.contain('*.exe');
expect(res).to.contain('*.dll');
expect(res).to.contain('-m0=BCJ');
expect(res).to.contain('-m1=LZMA:d=21');
expect(res).to.contain('-p"My mhjls/\\c $^é5°"');
expect(res).to.contain('-ssc');
expect(res).to.contain('-y');
done();
});
});
Expand Down
2 changes: 1 addition & 1 deletion util/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ module.exports = function (command, switches) {
progress(data.toString());
});
run.on('close', function (code) {
if (code === 0) return fulfill(res);
if (code === 0) return fulfill(args);
reject(err, code);
});

Expand Down

0 comments on commit a87f174

Please sign in to comment.