Skip to content

Commit

Permalink
Merge 4070531 into d248b04
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Sep 7, 2016
2 parents d248b04 + 4070531 commit c12abf0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
10 changes: 9 additions & 1 deletion lib/MemoryFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ function isFile(item) {

function pathToArray(path) {
path = normalize(path);

var unc = /^\\\\/.test(path);
var nix = /^\//.test(path);
if(!nix) {

if(unc) {
path = path.slice(2);
path = path.replace(/[\\\/]+/g, "\\");
path = path.split(/[\\\/]/);
path[0] = '\\\\' + path[0];
} else if(!nix) {
if(!/^[A-Za-z]:/.test(path)) {
throw new MemoryFileSystemError(errors.code.EINVAL, path);
}
Expand Down
13 changes: 11 additions & 2 deletions lib/normalize.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var doubleSlackUNCRegExp = /^\\\\/;
var doubleSlashWinRegExp = /\\+/g;
var doubleSlashNixRegExp = /\/+/g;
var currentDirectoryWinMiddleRegExp = /\\(\.\\)+/;
Expand Down Expand Up @@ -34,5 +35,13 @@ module.exports = function normalize(path) {
path = path.replace(parentDirectoryNixEndRegExp2, "");
path = path.replace(parentDirectoryNixEndRegExp3, "/");

return path.replace(doubleSlashWinRegExp, "\\").replace(doubleSlashNixRegExp, "/");
};
var unc = doubleSlackUNCRegExp.test(path);

path = path.replace(doubleSlashWinRegExp, "\\").replace(doubleSlashNixRegExp, "/")

if (unc) {
path = '\\' + path;
}

return path;
};
3 changes: 3 additions & 0 deletions test/MemoryFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ describe("normalize", function() {
fs.normalize("C:\\a\\b\\\c\\..\\..").should.be.eql("C:\\a");
fs.normalize("C:\\a\\b\\d\\..\\c\\..\\..").should.be.eql("C:\\a");
fs.normalize("C:\\a\\b\\d\\\\.\\\\.\\c\\.\\..").should.be.eql("C:\\a\\b\\d");
fs.normalize("\\\\a\\\\b").should.be.eql("\\\\a\\b");
fs.normalize("\\\\a\\\\b\\..\\c").should.be.eql("\\\\a\\c");
});
});
describe("pathToArray", function() {
Expand All @@ -363,6 +365,7 @@ describe("pathToArray", function() {
fs.pathToArray("/a/b/c").should.be.eql(["a", "b", "c"]);
fs.pathToArray("C:/a/b").should.be.eql(["C:", "a", "b"]);
fs.pathToArray("C:\\a\\b").should.be.eql(["C:", "a", "b"]);
fs.pathToArray("\\\\a\\b\\c").should.be.eql(["\\\\a", "b", "c"]);
});
it("should fail on invalid paths", function() {
(function() {
Expand Down

0 comments on commit c12abf0

Please sign in to comment.