Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Handle UNC paths in windows (fixes #23) #28

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lower case unc. all upper case only for constants.

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
5 changes: 5 additions & 0 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,9 @@ module.exports = function normalize(path) {
path = path.replace(parentDirectoryNixEndRegExp2, "");
path = path.replace(parentDirectoryNixEndRegExp3, "/");

if (doubleSlackUNCRegExp.test(path)) {
return path;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of this block, could we just do:

var doubleSlashWinRegExp = /(?!^)\\+/g;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


return path.replace(doubleSlashWinRegExp, "\\").replace(doubleSlashNixRegExp, "/");
};
1 change: 1 addition & 0 deletions test/MemoryFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,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