Skip to content

Commit

Permalink
fix(registry): handle relative registry path (#5406)
Browse files Browse the repository at this point in the history
We get relative registry path when PLAYWRIGHT_BROWSERS_PATH or HOME is relative.
In this case, it would be good to resolve to the same absolute path
during installation and execution, and we can usually do that using INIT_CWD.
  • Loading branch information
dgozman committed Feb 12, 2021
1 parent 2a40d8e commit ac1599c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 19 deletions.
35 changes: 35 additions & 0 deletions packages/installation-tests/installation-tests.sh
Expand Up @@ -15,14 +15,22 @@ unset PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD
export PLAYWRIGHT_BROWSERS_PATH=0

# Pack all packages and put them in our output folder.
echo "Building packages..."
PACKAGE_BUILDER="../../../packages/build_package.js"
PLAYWRIGHT_CORE_TGZ="$(node ${PACKAGE_BUILDER} playwright-core ./playwright-core.tgz)"
echo "playwright-core built"
PLAYWRIGHT_TGZ="$(node ${PACKAGE_BUILDER} playwright ./playwright.tgz)"
echo "playwright built"
PLAYWRIGHT_CHROMIUM_TGZ="$(node ${PACKAGE_BUILDER} playwright-chromium ./playwright-chromium.tgz)"
echo "playwright-chromium built"
PLAYWRIGHT_WEBKIT_TGZ="$(node ${PACKAGE_BUILDER} playwright-webkit ./playwright-webkit.tgz)"
echo "playwright-webkit built"
PLAYWRIGHT_FIREFOX_TGZ="$(node ${PACKAGE_BUILDER} playwright-firefox ./playwright-firefox.tgz)"
echo "playwright-firefox built"
PLAYWRIGHT_ELECTRON_TGZ="$(node ${PACKAGE_BUILDER} playwright-electron ./playwright-electron.tgz)"
echo "playwright-electron built"
PLAYWRIGHT_ANDROID_TGZ="$(node ${PACKAGE_BUILDER} playwright-android ./playwright-android.tgz)"
echo "playwright-android built"

SCRIPTS_PATH="$(pwd -P)/.."
TEST_ROOT="$(pwd -P)"
Expand All @@ -47,6 +55,8 @@ function run_tests {
test_skip_browser_download
test_playwright_global_installation_subsequent_installs
test_playwright_should_work
test_playwright_should_work_with_relative_home_path
test_playwright_should_work_with_relative_browsers_path
test_playwright_chromium_should_work
test_playwright_webkit_should_work
test_playwright_firefox_should_work
Expand Down Expand Up @@ -210,6 +220,31 @@ function test_playwright_should_work {
echo "${FUNCNAME[0]} success"
}

function test_playwright_should_work_with_relative_home_path {
initialize_test "${FUNCNAME[0]}"
PLAYWRIGHT_BROWSERS_PATH="" HOME=. npm install ${PLAYWRIGHT_TGZ}
copy_test_scripts
echo "Running sanity.js"
# Firefox does not work with relative HOME.
PLAYWRIGHT_BROWSERS_PATH="" HOME=. node sanity.js playwright chromium webkit
echo "${FUNCNAME[0]} success"
}

function test_playwright_should_work_with_relative_browsers_path {
initialize_test "${FUNCNAME[0]}"

# Make sure that browsers path is resolved relative to the `npm install` call location.
mkdir foo
cd foo
PLAYWRIGHT_BROWSERS_PATH="../relative" npm install ${PLAYWRIGHT_TGZ}
cd ..

copy_test_scripts
echo "Running sanity.js"
PLAYWRIGHT_BROWSERS_PATH="./relative" node sanity.js playwright
echo "${FUNCNAME[0]} success"
}

function test_playwright_chromium_should_work {
initialize_test "${FUNCNAME[0]}"

Expand Down
5 changes: 4 additions & 1 deletion packages/installation-tests/sanity.js
Expand Up @@ -23,8 +23,10 @@ let success = {
}[requireName];
if (process.argv[3] === 'none')
success = [];
if (process.argv[3] === 'all')
else if (process.argv[3] === 'all')
success = ['chromium', 'firefox', 'webkit'];
else if (process.argv[3])
success = process.argv.slice(3)

const playwright = require(requireName);

Expand Down Expand Up @@ -54,6 +56,7 @@ const installer = require(requireName + '/lib/install/installer');
process.exit(1);
} catch (e) {
// All good.
console.log(`Expected error while launching ${browserType}: ${e}`);
}
}
console.log(`require SUCCESS`);
Expand Down
2 changes: 2 additions & 0 deletions src/server/firefox/firefox.ts
Expand Up @@ -40,6 +40,8 @@ export class Firefox extends BrowserType {
}

_amendEnvironment(env: Env, userDataDir: string, executable: string, browserArguments: string[]): Env {
if (!path.isAbsolute(os.homedir()))
throw new Error(`Cannot launch Firefox with relative home directory. Did you set ${os.platform() === 'win32' ? 'USERPROFILE' : 'HOME'} to a relative path?`);
return os.platform() === 'linux' ? {
...env,
// On linux Juggler ships the libstdc++ it was linked against.
Expand Down
46 changes: 28 additions & 18 deletions src/utils/registry.ts
Expand Up @@ -158,12 +158,35 @@ export const hostPlatform = ((): BrowserPlatform => {
})();

export const registryDirectory = (() => {
let result: string;

const envDefined = getFromENV('PLAYWRIGHT_BROWSERS_PATH');
if (envDefined === '0')
return path.join(__dirname, '..', '..', '.local-browsers');
if (envDefined)
return path.resolve(process.cwd(), envDefined);
return path.join(cacheDirectory(), 'ms-playwright');
if (envDefined === '0') {
result = path.join(__dirname, '..', '..', '.local-browsers');
} else if (envDefined) {
result = envDefined;
} else {
let cacheDirectory: string;
if (process.platform === 'linux')
cacheDirectory = process.env.XDG_CACHE_HOME || path.join(os.homedir(), '.cache');
else if (process.platform === 'darwin')
cacheDirectory = path.join(os.homedir(), 'Library', 'Caches');
else if (process.platform === 'win32')
cacheDirectory = process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local');
else
throw new Error('Unsupported platform: ' + process.platform);
result = path.join(cacheDirectory, 'ms-playwright');
}

if (!path.isAbsolute(result)) {
// It is important to resolve to the absolute path:
// - for unzipping to work correctly;
// - so that registry directory matches between installation and execution.
// INIT_CWD points to the root of `npm/yarn install` and is probably what
// the user meant when typing the relative path.
result = path.resolve(getFromENV('INIT_CWD') || process.cwd(), result);
}
return result;
})();

export function isBrowserDirectory(browserDirectory: string): boolean {
Expand Down Expand Up @@ -255,16 +278,3 @@ export class Registry {
return !!browser && browser.download !== false;
}
}

function cacheDirectory() {
if (process.platform === 'linux')
return process.env.XDG_CACHE_HOME || path.join(os.homedir(), '.cache');

if (process.platform === 'darwin')
return path.join(os.homedir(), 'Library', 'Caches');

if (process.platform === 'win32')
return process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local');
throw new Error('Unsupported platform: ' + process.platform);
}

0 comments on commit ac1599c

Please sign in to comment.