Skip to content

Commit

Permalink
Merge pull request #8 from oskarlh/fix-directory-separators
Browse files Browse the repository at this point in the history
Fix directory separator replacement on Windows
  • Loading branch information
TheTechsTech authored Sep 21, 2020
2 parents 6f8a4d2 + ce01f67 commit f6bb661
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 40 deletions.
10 changes: 5 additions & 5 deletions lib/add.js
Original file line number Diff line number Diff line change
@@ -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')
};

/**
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions lib/extract.js
Original file line number Diff line number Diff line change
@@ -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')
};

/**
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions lib/extractFull.js
Original file line number Diff line number Diff line change
@@ -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')
};

/**
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions lib/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
};

/**
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions lib/test.js
Original file line number Diff line number Diff line change
@@ -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')
};

/**
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions lib/update.js
Original file line number Diff line number Diff line change
@@ -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')
};

/**
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"Dannii Willis <[email protected]>",
"redx25 <[email protected]>",
"l. stubbs <[email protected]>",
"František Gič <[email protected]>"
"František Gič <[email protected]>",
"Oskar Larsson Högfeldt <[email protected]>"
],
"license": "ISC",
"bugs": {
Expand Down
54 changes: 45 additions & 9 deletions test/lib/list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
});
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/util/path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
28 changes: 28 additions & 0 deletions test/util/replaceNativeSeparator.test.js
Original file line number Diff line number Diff line change
@@ -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();
});

});
2 changes: 1 addition & 1 deletion test/util/switches.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down
Binary file modified test/zip spaces test.7z
Binary file not shown.
Binary file modified test/zip.7z
Binary file not shown.
1 change: 1 addition & 0 deletions test/zip/folder/another-folder/deep file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
content
14 changes: 14 additions & 0 deletions util/replaceNativeSeparator.js
Original file line number Diff line number Diff line change
@@ -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;
};
8 changes: 4 additions & 4 deletions util/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
Expand Down

0 comments on commit f6bb661

Please sign in to comment.