Skip to content

Commit

Permalink
feat(fslib): add support for UNC paths
Browse files Browse the repository at this point in the history
Fixes yarnpkg#1230.
Tests are included.
  • Loading branch information
paul-soporan committed Apr 23, 2020
1 parent caeaba4 commit 92a4c1e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/yarnpkg-fslib/sources/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,22 @@ export interface ConvertUtils {

const WINDOWS_PATH_REGEXP = /^[a-zA-Z]:.*$/;
const PORTABLE_PATH_REGEXP = /^\/[a-zA-Z]:.*$/;
const UNC_PORTABLE_PATH_REGEXP = /^\/\/.*$/;

// Path should look like "/N:/berry/scripts/plugin-pack.js"
// And transform to "N:\berry\scripts\plugin-pack.js"
function fromPortablePath(p: Path): NativePath {
if (process.platform !== 'win32')
return p as NativePath;

return p.match(PORTABLE_PATH_REGEXP) ? p.substring(1).replace(/\//g, `\\`) : p;
}
if (!(p.match(PORTABLE_PATH_REGEXP) || p.match(UNC_PORTABLE_PATH_REGEXP)))
return p as NativePath;

if (p.match(PORTABLE_PATH_REGEXP))
p = p.substring(1);

return p.replace(/\//g, `\\`);
};

// Path should look like "N:/berry/scripts/plugin-pack.js"
// And transform to "/N:/berry/scripts/plugin-pack.js"
Expand Down
60 changes: 60 additions & 0 deletions packages/yarnpkg-fslib/tests/npath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,36 @@ describe(`Portable paths`, () => {
const outputPath = `/C:`;
expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
});

it(`should support UNC Windows paths (//[server]/[sharename]/)`, () => {
const inputPath = '\\\\Server01\\user\\docs\\Letter.txt';
const outputPath = `//Server01/user/docs/Letter.txt`;
expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
});

it(`should support Long UNC Windows paths (//?/[server]/[sharename]/)`, () => {
const inputPath = '\\\\?\\Server01\\user\\docs\\Letter.txt';
const outputPath = `//?/Server01/user/docs/Letter.txt`;
expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
});

it(`should support Long UNC Windows paths (//?/UNC/[server]/[sharename]/)`, () => {
const inputPath = '\\\\?\\UNC\\Server01\\user\\docs\\Letter.txt';
const outputPath = `//?/UNC/Server01/user/docs/Letter.txt`;
expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
});

it(`should support Long UNC Windows paths (//?/[drive_spec]:/)`, () => {
const inputPath = '\\\\?\\C:\\user\\docs\\Letter.txt';
const outputPath = `//?/C:/user/docs/Letter.txt`;
expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
});

it(`should support Long UNC Windows paths with dot (//./[physical_device]/)`, () => {
const inputPath = '\\\\.\\PhysicalDevice\\user\\docs\\Letter.txt';
const outputPath = `//./PhysicalDevice/user/docs/Letter.txt`;
expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
});
}
});

Expand Down Expand Up @@ -127,6 +157,36 @@ describe(`Portable paths`, () => {
const outputPath = `C:`;
expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
});

it(`should transform back UNC Windows paths (//[server]/[sharename]/)`, () => {
const inputPath = `//Server01/user/docs/Letter.txt`;
const outputPath = '\\\\Server01\\user\\docs\\Letter.txt';
expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
});

it(`should transform back Long UNC Windows paths (//?/[server]/[sharename]/)`, () => {
const inputPath = `//?/Server01/user/docs/Letter.txt`;
const outputPath = '\\\\?\\Server01\\user\\docs\\Letter.txt';
expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
});

it(`should transform back Long UNC Windows paths (//?/UNC/[server]/[sharename]/)`, () => {
const inputPath = `//?/UNC/Server01/user/docs/Letter.txt`;
const outputPath = '\\\\?\\UNC\\Server01\\user\\docs\\Letter.txt';
expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
});

it(`should transform back Long UNC Windows paths (//?/[drive_spec]:/)`, () => {
const inputPath = `//?/C:/user/docs/Letter.txt`;
const outputPath = '\\\\?\\C:\\user\\docs\\Letter.txt';
expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
});

it(`should transform back Long UNC Windows paths with dot (//./[physical_device]/)`, () => {
const inputPath = `//./PhysicalDevice/user/docs/Letter.txt`;
const outputPath = '\\\\.\\PhysicalDevice\\user\\docs\\Letter.txt';
expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
});
}
});
});
Expand Down

0 comments on commit 92a4c1e

Please sign in to comment.