Skip to content

Commit

Permalink
fix: fallback to previous code if wslpath is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed May 30, 2021
1 parent 7b7e217 commit 1923f01
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/chrome-finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ export function linux() {
export function wsl() {
// Manually populate the environment variables assuming it's the default config
process.env.LOCALAPPDATA = getWSLLocalAppDataPath(`${process.env.PATH}`);
process.env.PROGRAMFILES = toWSLPath('C:/Program Files');
process.env['PROGRAMFILES(X86)'] = toWSLPath('C:/Program Files (x86)');
process.env.PROGRAMFILES = toWSLPath('C:/Program Files', '/mnt/c/Program Files');
process.env['PROGRAMFILES(X86)'] =
toWSLPath('C:/Program Files (x86)', '/mnt/c/Program Files (x86)');

return win32();
}
Expand Down
36 changes: 32 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,51 @@ export function makeTmpDir() {
}
}

function toWinDirFormat(dir: string = ''): string {
const results = /\/mnt\/([a-z])\//.exec(dir);

if (!results) {
return dir;
}

const driveLetter = results[1];
return dir.replace(`/mnt/${driveLetter}/`, `${driveLetter.toUpperCase()}:\\`)
.replace(/\//g, '\\');
}

export function toWin32Path(dir: string = ''): string {
if (/[a-z]:\\/iu.test(dir)) {
return dir;
}

return execFileSync('wslpath', ['-w', dir]).toString().trim();
try {
return execFileSync('wslpath', ['-w', dir]).toString().trim();
} catch {
return toWinDirFormat(dir);
}
}

export function toWSLPath(dir: string, fallback: string): string {
try {
return execFileSync('wslpath', ['-u', dir]).toString().trim();
} catch {
return fallback;
}
}

export function toWSLPath(dir: string = ''): string {
return execFileSync('wslpath', ['-u', dir]).toString().trim();
function getLocalAppDataPath(path: string): string {
const userRegExp = /\/mnt\/([a-z])\/Users\/([^\/:]+)\/AppData\//;
const results = userRegExp.exec(path) || [];

return `/mnt/${results[1]}/Users/${results[2]}/AppData/Local`;
}

export function getWSLLocalAppDataPath(path: string): string {
const userRegExp = /\/([a-z])\/Users\/([^\/:]+)\/AppData\//;
const results = userRegExp.exec(path) || [];

return toWSLPath(`${results[1]}:\\Users\\${results[2]}\\AppData\\Local`);
return toWSLPath(
`${results[1]}:\\Users\\${results[2]}\\AppData\\Local`, getLocalAppDataPath(path));
}

function makeUnixTmpDir() {
Expand Down
44 changes: 42 additions & 2 deletions test/utils-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ describe('toWin32Path', () => {
assert.ok(execFileSyncStub.notCalled);
});
})

describe('when wslpath is not available', () => {
beforeEach(() => execFileSyncStub.throws(new Error('oh noes!')));

it('falls back to the toWinDirFormat method', () => {
const wsl = '/mnt/c/Users/user1/AppData/';
const windows = 'C:\\Users\\user1\\AppData\\';

assert.strictEqual(toWin32Path(wsl), windows);
});

it('supports the drive letter not being C', () => {
const wsl = '/mnt/d/Users/user1/AppData';
const windows = 'D:\\Users\\user1\\AppData';

assert.strictEqual(toWin32Path(wsl), windows);
})
});
})

describe('toWSLPath', () => {
Expand All @@ -53,15 +71,26 @@ describe('toWSLPath', () => {
it('calls wslpath -u', () => {
execFileSyncStub.returns(asBuffer(''));

toWSLPath('');
toWSLPath('', '');

assert.ok(execFileSyncStub.calledWith('wslpath', ['-u', '']));
})

it('trims off the trailing newline', () => {
execFileSyncStub.returns(asBuffer('the-path\n'));

assert.strictEqual(toWSLPath(''), 'the-path');
assert.strictEqual(toWSLPath('', ''), 'the-path');
})

describe('when wslpath is not available', () => {
beforeEach(() => execFileSyncStub.throws(new Error('oh noes!')));

it('uses the fallback path', () => {
assert.strictEqual(
toWSLPath('C:/Program Files', '/mnt/c/Program Files'),
'/mnt/c/Program Files'
);
})
})
})

Expand All @@ -76,4 +105,15 @@ describe('getWSLLocalAppDataPath', () => {
assert.strictEqual(getWSLLocalAppDataPath(path), '/c/folder/');
assert.ok(execFileSyncStub.calledWith('wslpath', ['-u', 'c:\\Users\\user1\\AppData\\Local']));
});

describe('when wslpath is not available', () => {
beforeEach(() => execFileSyncStub.throws(new Error('oh noes!')));

it('falls back to the getLocalAppDataPath method', () => {
const path = '/mnt/c/Users/user1/.bin:/mnt/c/Users/user1:/mnt/c/Users/user1/AppData/';
const appDataPath = '/mnt/c/Users/user1/AppData/Local';

assert.strictEqual(getWSLLocalAppDataPath(path), appDataPath);
});
});
});

0 comments on commit 1923f01

Please sign in to comment.