From acab75308e3529d383109e7efb602991b64fb6db Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 15 May 2020 11:42:01 +1200 Subject: [PATCH] fix: use `wslpath` to resolve Windows paths --- src/chrome-finder.ts | 6 +++--- src/utils.ts | 15 ++++----------- test/utils-test.ts | 39 --------------------------------------- 3 files changed, 7 insertions(+), 53 deletions(-) delete mode 100644 test/utils-test.ts diff --git a/src/chrome-finder.ts b/src/chrome-finder.ts index 90bce777b..94eb6418f 100644 --- a/src/chrome-finder.ts +++ b/src/chrome-finder.ts @@ -12,7 +12,7 @@ const {execSync, execFileSync} = require('child_process'); const escapeRegExp = require('escape-string-regexp'); const log = require('lighthouse-logger'); -import {getLocalAppDataPath, ChromePathNotSetError} from './utils'; +import {getLocalAppDataPath, toWinDirFormat, ChromePathNotSetError} from './utils'; const newLineRegex = /\r?\n/; @@ -158,8 +158,8 @@ export function linux() { export function wsl() { // Manually populate the environment variables assuming it's the default config process.env.LOCALAPPDATA = getLocalAppDataPath(`${process.env.PATH}`); - process.env.PROGRAMFILES = '/mnt/c/Program Files'; - process.env['PROGRAMFILES(X86)'] = '/mnt/c/Program Files (x86)'; + process.env.PROGRAMFILES = toWinDirFormat('C:/Program Files'); + process.env['PROGRAMFILES(X86)'] = toWinDirFormat('C:/Program Files (x86)'); return win32(); } diff --git a/src/utils.ts b/src/utils.ts index 58681e965..a0d11b2b9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -6,7 +6,7 @@ 'use strict'; import {join} from 'path'; -import {execSync} from 'child_process'; +import {execSync, execFileSync} from 'child_process'; import * as mkdirp from 'mkdirp'; const isWsl = require('is-wsl'); @@ -74,21 +74,14 @@ export function makeTmpDir() { } export 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, '\\'); + return execFileSync('wslpath', [dir]).toString().trim(); } export function getLocalAppDataPath(path: string): string { - const userRegExp = /\/mnt\/([a-z])\/Users\/([^\/:]+)\/AppData\//; + const userRegExp = /\/([a-z])\/Users\/([^\/:]+)\/AppData\//; const results = userRegExp.exec(path) || []; - return `/mnt/${results[1]}/Users/${results[2]}/AppData/Local`; + return toWinDirFormat(`C:\\${results[1]}\\Users\\${results[2]}\\AppData\\Local`); } function makeUnixTmpDir() { diff --git a/test/utils-test.ts b/test/utils-test.ts deleted file mode 100644 index 22e12e506..000000000 --- a/test/utils-test.ts +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright 2017 Google Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -'use strict'; - -import * as assert from 'assert'; -import {toWinDirFormat, getLocalAppDataPath} from '../src/utils'; - -describe('WSL path format to Windows', () => { - it('transforms basic path', () => { - const wsl = '/mnt/c/Users/user1/AppData/'; - const windows = 'C:\\Users\\user1\\AppData\\'; - assert.equal(toWinDirFormat(wsl), windows); - }); - - it('transforms if drive letter is different than c', () => { - const wsl = '/mnt/d/Users/user1/AppData'; - const windows = 'D:\\Users\\user1\\AppData'; - assert.equal(toWinDirFormat(wsl), windows); - }); - - it('getLocalAppDataPath returns a correct path', () => { - 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.equal(getLocalAppDataPath(path), appDataPath); - }); -});