diff --git a/lib/add.js b/lib/add.js index de71d04..69b080a 100644 --- a/lib/add.js +++ b/lib/add.js @@ -1,10 +1,10 @@ 'use strict'; -var path = require('path'); var when = require('when'); var u = { - files : require('../util/files'), - run : require('../util/run'), - switches: require('../util/switches'), + files : require('../util/files'), + replaceNativeSeparator: require('../util/replaceNativeSeparator'), + run : require('../util/run'), + switches : require('../util/switches') }; /** @@ -36,7 +36,7 @@ module.exports = function (archive, files, options) { var entries = []; data.split('\n').forEach(function (line) { if (line.substr(0, 1) === '+') { - entries.push(line.substr(2, line.length).replace(path.sep, '/')); + entries.push(u.replaceNativeSeparator(line.substr(2, line.length))); } }); return progress(entries); diff --git a/lib/extract.js b/lib/extract.js index 08914df..7f720ee 100644 --- a/lib/extract.js +++ b/lib/extract.js @@ -1,9 +1,9 @@ 'use strict'; -var path = require('path'); var when = require('when'); var u = { - run : require('../util/run'), - switches: require('../util/switches') + replaceNativeSeparator: require('../util/replaceNativeSeparator'), + run : require('../util/run'), + switches : require('../util/switches') }; /** @@ -32,7 +32,7 @@ module.exports = function (archive, dest, options) { var entries = []; data.split('\n').forEach(function (line) { if (line.substr(0, 1) === '-') { - entries.push(line.substr(2, line.length).replace(path.sep, '/')); + entries.push(u.replaceNativeSeparator(line.substr(2, line.length))); } }); return progress(entries); diff --git a/lib/extractFull.js b/lib/extractFull.js index c6d00a9..a0b74fb 100644 --- a/lib/extractFull.js +++ b/lib/extractFull.js @@ -1,9 +1,9 @@ 'use strict'; -var path = require('path'); var when = require('when'); var u = { - run : require('../util/run'), - switches: require('../util/switches') + replaceNativeSeparator: require('../util/replaceNativeSeparator'), + run : require('../util/run'), + switches : require('../util/switches') }; /** @@ -32,7 +32,7 @@ module.exports = function (archive, dest, options) { var entries = []; data.split('\n').forEach(function (line) { if (line.substr(0, 1) === '-') { - entries.push(line.substr(2, line.length).replace(path.sep, '/')); + entries.push(u.replaceNativeSeparator(line.substr(2, line.length))); } }); return progress(entries); diff --git a/lib/list.js b/lib/list.js index 55339a1..6dd65a1 100644 --- a/lib/list.js +++ b/lib/list.js @@ -2,8 +2,9 @@ var path = require('path'); var when = require('when'); var u = { - run : require('../util/run'), - switches: require('../util/switches') + replaceNativeSeparator: require('../util/replaceNativeSeparator'), + run : require('../util/run'), + switches : require('../util/switches') }; /** @@ -65,7 +66,7 @@ module.exports = function (archive, options) { date: new Date(res[1]), attr: res[2], size: parseInt(res[3], 10), - name: res[5].replace(path.sep, '/') + name: u.replaceNativeSeparator(res[5]) }; entries.push(e); diff --git a/lib/test.js b/lib/test.js index 014a7cd..4079bf1 100644 --- a/lib/test.js +++ b/lib/test.js @@ -1,9 +1,9 @@ 'use strict'; -var path = require('path'); var when = require('when'); var u = { - run : require('../util/run'), - switches: require('../util/switches') + replaceNativeSeparator: require('../util/replaceNativeSeparator'), + run : require('../util/run'), + switches : require('../util/switches') }; /** @@ -31,7 +31,7 @@ module.exports = function (archive, options) { var entries = []; data.split('\n').forEach(function (line) { if (line.substr(0, 1) === 'T') { - entries.push(line.substr(2, line.length).replace(path.sep, '/')); + entries.push(u.replaceNativeSeparator(line.substr(2, line.length))); } }); return progress(entries); diff --git a/lib/update.js b/lib/update.js index 80402cf..9b83b9a 100644 --- a/lib/update.js +++ b/lib/update.js @@ -1,9 +1,9 @@ 'use strict'; -var path = require('path'); var when = require('when'); var u = { - run: require('../util/run'), - switches : require('../util/switches') + replaceNativeSeparator: require('../util/replaceNativeSeparator'), + run : require('../util/run'), + switches : require('../util/switches') }; /** @@ -32,7 +32,7 @@ module.exports = function (archive, files, options) { var entries = []; data.split('\n').forEach(function (line) { if (line.substr(0, 1) === 'U') { - entries.push(line.substr(2, line.length).replace(path.sep, '/')); + entries.push(u.replaceNativeSeparator(line.substr(2, line.length))); } }); return progress(entries); diff --git a/package.json b/package.json index f3b18dc..3576158 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "Dannii Willis ", "redx25 ", "l. stubbs ", - "František Gič " + "František Gič ", + "Oskar Larsson Högfeldt " ], "license": "ISC", "bugs": { diff --git a/test/lib/list.test.js b/test/lib/list.test.js index ec53703..e3d9dd7 100644 --- a/test/lib/list.test.js +++ b/test/lib/list.test.js @@ -3,6 +3,28 @@ var expect = require('chai').expect; var list = require('../../lib/list'); +function testListProgress (file, done) { + var error; + list('test/zip.7z') + .progress(function (entries) { + try { + expect(entries.length).to.be.at.least(1); + entries.forEach(function (entry) { + expect(entry.date).to.be.an.instanceof(Date); + expect(entry.attr.length).to.eql(5); + expect(entry.name).to.be.a('string'); + expect(entry.name).to.not.contain('\\'); + }); + } catch(e) { + error = e; + } + }) + .then(function () { + done(error) + }) + .done(next); +} + describe('Method: `Zip.list`', function () { it('should return an error on 7z error', function (done) { @@ -25,15 +47,29 @@ describe('Method: `Zip.list`', function () { }); }); - it('should return valid entries on progress', function (done) { - list('test/zip.zip') - .progress(function (entries) { - expect(entries.length).to.be.at.least(1); - expect(entries[0].date).to.be.an.instanceof(Date); - expect(entries[0].attr.length).to.eql(5); - expect(entries[0].name).to.be.a('string'); - expect(entries[0].name).to.not.contain('\\'); - done(); + ['zip.zip', 'zip.7z'].forEach(function (file) { + it('should return valid entries on progress (' + file + ')', function (done) { + var expectFailure; + list('test/' + file) + .progress(function (entries) { + try { + expect(entries.length).to.be.at.least(1); + entries.forEach(function (entry) { + expect(entry.date).to.be.an.instanceof(Date); + expect(entry.attr.length).to.eql(5); + expect(entry.name).to.be.a('string'); + expect(entry.name).to.not.contain('\\'); + }); + } catch(e) { + expectFailure = e; + } + }) + .then(function () { + done(expectFailure); + }) + .catch(function (listError) { + done(listError || expectFailure); + }); }); }); diff --git a/test/util/path.test.js b/test/util/path.test.js index 2759362..5e8cddc 100644 --- a/test/util/path.test.js +++ b/test/util/path.test.js @@ -6,7 +6,7 @@ var path = require('../../util/path'); describe('Utility: `path`', function () { - it('should return deflaut flags with no args', function () { + it('should return default flags with no args', function () { var _7zcmd = path(); var pathInSystem = exec('which ' + _7zcmd.fullpath).toString(); }); diff --git a/test/util/replaceNativeSeparator.test.js b/test/util/replaceNativeSeparator.test.js new file mode 100644 index 0000000..2649d57 --- /dev/null +++ b/test/util/replaceNativeSeparator.test.js @@ -0,0 +1,28 @@ +/*global describe, it */ +'use strict'; +var expect = require('chai').expect; +var replaceNativeSeparator = require('../../util/replaceNativeSeparator'); +var sep = require('path').sep; + +describe('Utility: `replaceNativeSeparator`', function () { + + it('should replace the native directory separator (' + sep + ')' + + ' with / and allows / in input', + function (done) { + [ + ['abc', 'abc'], + ['å/Ö', 'å/Ö'], + ['3' + sep + 'π' + sep + '1' + sep + '4.txt', '3/π/1/4.txt'], + ['abc/def' + sep + 'g', 'abc/def/g'], + ['directory' + sep + 'file', 'directory/file'], + ['a' + sep + 'b' + sep + 'c' + sep + 'd.txt', 'a/b/c/d.txt'], + ] + .forEach(function (inputAndExpectedOutput) { + var input = inputAndExpectedOutput[0]; + var expectedOutput = inputAndExpectedOutput[1]; + expect(replaceNativeSeparator(input)).to.eql(expectedOutput); + }); + done(); + }); + +}); diff --git a/test/util/switches.test.js b/test/util/switches.test.js index 0a5456e..f1669a6 100644 --- a/test/util/switches.test.js +++ b/test/util/switches.test.js @@ -5,7 +5,7 @@ var switches = require('../../util/switches'); describe('Utility: `switches`', function () { - it('should return deflaut flags with no args', function () { + it('should return default flags with no args', function () { expect(switches({})).to.contain('-ssc'); expect(switches({})).to.contain('-y'); }); diff --git a/test/zip spaces test.7z b/test/zip spaces test.7z index c0db972..3fc8f75 100644 Binary files a/test/zip spaces test.7z and b/test/zip spaces test.7z differ diff --git a/test/zip.7z b/test/zip.7z index c0db972..3fc8f75 100644 Binary files a/test/zip.7z and b/test/zip.7z differ diff --git a/test/zip/folder/another-folder/deep file.txt b/test/zip/folder/another-folder/deep file.txt new file mode 100644 index 0000000..6b584e8 --- /dev/null +++ b/test/zip/folder/another-folder/deep file.txt @@ -0,0 +1 @@ +content \ No newline at end of file diff --git a/util/replaceNativeSeparator.js b/util/replaceNativeSeparator.js new file mode 100644 index 0000000..b92ec0e --- /dev/null +++ b/util/replaceNativeSeparator.js @@ -0,0 +1,14 @@ +'use strict'; +var nativeSeparator = require('path').sep; + +/** + * @param {string} path A path with the native directory separator. + * @return {string} A path with / for directory separator. + */ +module.exports = function (path) { + var result = path, next; + while ((next = result.replace(nativeSeparator, '/')) !== result) { + result = next; + } + return result; +}; diff --git a/util/run.js b/util/run.js index c8700f8..24d98f1 100644 --- a/util/run.js +++ b/util/run.js @@ -34,8 +34,8 @@ module.exports = function (command, switches) { var commands = command.match(regexpCommands); if (commands) { commands.forEach(function (c) { - c = c.replace(/\//, path.sep); - c = c.replace(/\\/, path.sep); + c = c.replace(/\//g, path.sep); + c = c.replace(/\\/g, path.sep); c = path.normalize(c); args.push(c); }); @@ -48,8 +48,8 @@ module.exports = function (command, switches) { if (output) { args.pop(); var o = output[0]; - o = o.replace(/\//, path.sep); - o = o.replace(/\\/, path.sep); + o = o.replace(/\//g, path.sep); + o = o.replace(/\\/g, path.sep); o = o.replace(/"/g, ''); o = path.normalize(o); args.push(o);