Skip to content

Commit

Permalink
RegExp for parsing and switches passed to run #2
Browse files Browse the repository at this point in the history
In order to clean up code and be more aware of spaces, the command (ie:
non-switches parameters) are parsed with a RegExp so they understand paths even
when they contain spaces (with the help of double quotes). Also switches are
converted into an array (`child_process.spawn` compatible) and passed to the
`run` function in each action. This avoid an useless stringify/parse of the
switches.
  • Loading branch information
q2s2t committed Oct 23, 2014
1 parent a83d04b commit b148140
Show file tree
Hide file tree
Showing 16 changed files with 194 additions and 131 deletions.
7 changes: 7 additions & 0 deletions TOTO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
TODO
====

- [x] Pass switches from each command to the `run` function.
- [x] Normalize path in `run`?
- [ ] Streaming abilities
- [ ] Multiple files in add, delete, etc.
8 changes: 2 additions & 6 deletions lib/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ module.exports = function (archive, files, options) {
options = {};
}

archive = archive.replace(/\ /g, '\\ ');
files = files.replace(/\ /g, '\\ ');
var command = '7za a "' + archive + '" "' + files + '"';

var opts = u.switches(options);
var command = '7za a ' + archive + ' ' + files + ' ' + opts;

u.run(command)
u.run(command, options)

// 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
Expand Down
12 changes: 2 additions & 10 deletions lib/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,8 @@ var u = {
module.exports = function (archive, files, options) {
return when.promise(function (fulfill, reject, progress) {

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

archive = archive.replace(/\ /g, '\\ ');
files = files.replace(/\ /g, '\\ ');

var opts = u.switches(options);
var command = '7za d ' + archive + ' ' + files + ' ' + opts;
u.run(command)
var command = '7za d "' + archive + '" "' + files + '"';
u.run(command, options)

.then(function () {
fulfill();
Expand Down
7 changes: 2 additions & 5 deletions lib/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ module.exports = function (archive, dest, options) {
options = {};
}

archive = archive.replace(/\ /g, '\\ ');
dest = dest.replace(/\ /g, '\\ ');
var opts = u.switches(options);
var command = '7za e ' + archive + ' -o' + dest + ' ' + opts;
u.run(command)
var command = '7za e "' + archive + '" -o"' + dest + '" ';
u.run(command, options)

// 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
Expand Down
8 changes: 2 additions & 6 deletions lib/extractFull.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ module.exports = function (archive, dest, options) {
options = {};
}

dest = dest.replace(/\ /g, '\\ ');
archive = archive.replace(/\ /g, '\\ ');
var command = '7za x "' + archive + '" -o"' + dest + '" ';

var opts = u.switches(options);
var command = '7za x ' + archive + ' -o' + dest + ' ' + opts;

u.run(command)
u.run(command, options)

// 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
Expand Down
5 changes: 2 additions & 3 deletions lib/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ module.exports = function (archive, options) {

archive = archive.replace(/\ /g, '\\ ');

var opts = u.switches(options);
var spec = {};
var regex = /(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) ([\.DA]+) +(\d+)[ \d]+ (.+)/;
var command = '7za l ' + archive + ' ' + opts;
u.run(command)
var command = '7za l "' + archive + '" ';
u.run(command, options)

// 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
Expand Down
11 changes: 2 additions & 9 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,8 @@ var u = {
module.exports = function (archive, options) {
return when.promise(function (fulfill, reject, progress) {

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

archive = archive.replace(/\ /g, '\\ ');

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

// 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
Expand Down
12 changes: 2 additions & 10 deletions lib/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,8 @@ var u = {
module.exports = function (archive, files, options) {
return when.promise(function (fulfill, reject, progress) {

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

archive = archive.replace(/\ /g, '\\ ');
files = files.replace(/\ /g, '\\ ');

var opts = u.switches(options);
var command = '7za u ' + archive + ' ' + files + ' ' + opts;
u.run(command)
var command = '7za u "' + archive + '" "' + files + '"';
u.run(command, options)

// 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
Expand Down
8 changes: 4 additions & 4 deletions test/method.Zip.add.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ var fs = require('fs-extra');
var add = require('../lib/add');

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

afterEach(function () { fs.removeSync('.tmp/test'); });

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

it('should return entries on progress', function (done) {
add('.tmp/test/add.zip', '*.md')
.progress(function (entries) {
expect(entries.length).to.be.at.least(1);
done();
});
});

});
14 changes: 7 additions & 7 deletions test/method.Zip.extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ var fs = require('fs-extra');
var extract = require('../lib/extract');

describe('Method: `Zip.extract`', function () {
afterEach(function () { fs.removeSync('.tmp/test'); });

//afterEach(function () { fs.removeSync('.tmp/test'); });

it('should return an error on 7z error', function (done) {
extract('test/nothere.7z', '.tmp/test')
.catch(function (err) {
Expand All @@ -22,15 +22,15 @@ describe('Method: `Zip.extract`', function () {
done();
});
});

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

it('should extract on the right path', function (done) {
extract('test/zip.7z', '.tmp/test')
.then(function () {
Expand All @@ -41,5 +41,5 @@ describe('Method: `Zip.extract`', function () {
done();
});
});
});

});
4 changes: 2 additions & 2 deletions test/method.Zip.extractFull.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Method: `Zip.extractFull`', function () {
});
});

it('should return an error on output duplticate', function (done) {
it('should return an error on output duplicate', function (done) {
extractFull('test/zip.7z', '.tmp/test', { o: '.tmp/test/duplicate' })
.catch(function (err) {
expect(err).to.be.an.instanceof(Error);
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('Method: `Zip.extractFull`', function () {
done();
});
});

it('should work with spaces in both source and destination', function (done) {
fs.copySync('test/zip.7z','.tmp/test/Folder From/Folder A/Folder B/Folder C/zip file.7z');
extractFull('.tmp/test/Folder From/Folder A/Folder B/Folder C/zip file.7z','.tmp/test/Folder To/Folder D/Folder E/Folder F')
Expand Down
10 changes: 5 additions & 5 deletions test/method.Zip.list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ var expect = require('chai').expect;
var list = require('../lib/list');

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

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

it('should return an tech spec on fulfill', function (done) {
list('test/zip.7z', { r: true })
.then(function (spec) {
Expand All @@ -23,7 +23,7 @@ describe('Method: `Zip.list`', function () {
done();
});
});

it('should return valid entries on progress', function (done) {
list('test/zip.zip')
.progress(function (entries) {
Expand All @@ -35,5 +35,5 @@ describe('Method: `Zip.list`', function () {
done();
});
});
});

});
47 changes: 45 additions & 2 deletions test/util.run.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ describe('Utility: `run`', function () {
});

it('should return an error on when 7z gets one', function (done) {
run('7za ?').catch(function (err) {
run('7za "???"').catch(function (err) {
expect(err.message).to.eql('Incorrect command line');
done();
});
});

it('should return an stdout on progress', function (done) {
run('7za')
run('7za', { h: true })
.progress(function (data) {
expect(data).to.be.a('string');
})
Expand All @@ -28,4 +28,47 @@ describe('Utility: `run`', function () {
});
});

it('should correctly parse complex commands', function (done) {
run('7za a ".tmp/test/archive.7z" "*.exe" "*.dll"', {
m0: '=BCJ',
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',
]);
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'
]);
done();
});
});

});
48 changes: 33 additions & 15 deletions test/util.switches.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,61 @@ var expect = require('chai').expect;
var switches = require('../util/switches');

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

it('should return deflaut flags with no args', function () {
expect(switches({})).to.eql('-ssc -y');
});

expect(switches({})).to.contain('-ssc');
expect(switches({})).to.contain('-y');
});

it('should return -ssc with flag { ssc: true }', function () {
expect(switches({ ssc: true })).to.eql('-ssc -y');
});

expect(switches({ ssc: true })).to.contain('-ssc');
expect(switches({ ssc: true })).to.contain('-y');
});

it('should return -ssc- with flag { ssc: false }', function () {
expect(switches({ ssc: false })).to.eql('-ssc- -y');
expect(switches({ ssc: false })).to.contain('-ssc-');
});

it('should return non default booleans when specified', function () {
var r = switches({
so : true,
spl: true,
ssw: true,
y : false
});
});
expect(r).to.contain('-so');
expect(r).to.contain('-spl');
expect(r).to.contain('-ssc');
expect(r).to.contain('-ssw');
expect(r).not.to.contain('-y');
});

it('should return complex values when needed', function () {
var r = switches({
ssc : true,
ssw : true,
m : '0=BCJ -m1=LZMA:d=21'
mx0 : true
});
expect(r).to.contain('-ssc');
expect(r).to.contain('-ssw');
expect(r).to.contain('-mx0');
expect(r).to.contain('-y');
});

it('should return complex values with spaces and quotes', function () {
var r = switches({
ssc : true,
ssw : true,
m0 : '=BCJ',
m1 : '=LZMA:d=21',
p : 'My Super Pasw,àù£*',
});
expect(r).to.contain('-ssc');
expect(r).to.contain('-ssw');
expect(r).to.contain('m0=BCJ -m1=LZMA:d=21');
expect(r).to.contain('-m0=BCJ');
expect(r).to.contain('-m1=LZMA:d=21');
expect(r).to.contain('-p"My Super Pasw,àù£*"');
expect(r).to.contain('-y');
});
});

});
Loading

0 comments on commit b148140

Please sign in to comment.