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 all 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);
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
2 changes: 1 addition & 1 deletion lib/join.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var normalize = require("./normalize");

var absoluteWinRegExp = /^[A-Z]:([\\\/]|$)/i;
var absoluteWinRegExp = /^(?:[A-Z]:([\\\/]|$))|(\\\\)/i;
var absoluteNixRegExp = /^\//i;

module.exports = function join(path, request) {
Expand Down
8 changes: 5 additions & 3 deletions lib/normalize.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var doubleSlashWinRegExp = /\\+/g;
var doubleSlashWinRegExp = /(?!^)\\+/g;;
var doubleSlashNixRegExp = /\/+/g;
var currentDirectoryWinMiddleRegExp = /\\(\.\\)+/;
var currentDirectoryWinEndRegExp = /\\\.$/;
Expand Down Expand Up @@ -34,5 +34,7 @@ module.exports = function normalize(path) {
path = path.replace(parentDirectoryNixEndRegExp2, "");
path = path.replace(parentDirectoryNixEndRegExp3, "/");

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

return path;
};
4 changes: 4 additions & 0 deletions test/MemoryFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,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 @@ -341,6 +343,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 All @@ -363,6 +366,7 @@ describe("join", function() {
fs.join("C:\\", "a\\b").should.be.eql("C:\\a\\b");
fs.join("C:/a/b", "./../c/d").should.be.eql("C:\\a\\c\\d");
fs.join("C:\\a\\b", "./../c/d").should.be.eql("C:\\a\\c\\d");
fs.join("\\\\a\\b\\..\\c\\d\\..").should.be.eql("\\\\a\\c");
});
it("should join paths (weird cases)", function() {
var fs = new MemoryFileSystem();
Expand Down