Skip to content

Commit

Permalink
chore: extract playwright finder
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Feb 13, 2024
1 parent 112172a commit cf062a2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
}
},
"scripts": {
"esbuild": "esbuild ./src/babelBundle.ts ./src/extension.ts ./src/oopReporter.ts ./src/debugTransform.ts --bundle --outdir=out --external:vscode --external:./babelBundle --external:./debugTransform --external:./oopReporter --format=cjs --platform=node --target=ES2019",
"esbuild": "esbuild ./src/babelBundle.ts ./src/extension.ts ./src/oopReporter.ts ./src/debugTransform.ts ./src/playwrightFinder.ts --bundle --outdir=out --external:vscode --external:./babelBundle --external:./debugTransform --external:./oopReporter --external:./playwrightFinder --format=cjs --platform=node --target=ES2019",
"build": "npm run esbuild -- --minify",
"watch": "npm run esbuild -- --sourcemap --watch",
"l10n": "npx @vscode/l10n-dev export -o ./l10n ./src",
Expand Down
50 changes: 50 additions & 0 deletions src/playwrightFinder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (c) Microsoft Corporation.
*
* 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.
*/

const path = require('path');

const packages = [
'@playwright/test',
'playwright',
'@playwright/experimental-ct-react',
'@playwright/experimental-ct-react17',
'@playwright/experimental-ct-vue',
'@playwright/experimental-ct-vue2',
'@playwright/experimental-ct-solid',
'@playwright/experimental-ct-svelte',
];

for (const packageName of packages) {
let packageJSONPath;
try {
packageJSONPath = require.resolve(path.join(packageName, 'package.json'), { paths: [process.cwd()] });
} catch (e) {
continue;
}
try {
const packageJSON = require(packageJSONPath);
const { version } = packageJSON;
const v = parseFloat(version.replace(/-(next|beta)$/, ''));
const cli = path.join(packageJSONPath, '../cli.js');
console.log(JSON.stringify({ version: v, cli }, null, 2));
process.exit(0);
} catch (e) {
console.log(JSON.stringify({ error: String(e) }, null, 2));
process.exit(0);
}
}

console.log({ error: 'Playwright installation no found for ' + process.cwd() });
39 changes: 7 additions & 32 deletions src/playwrightTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,41 +64,16 @@ export class PlaywrightTest {
}

async getPlaywrightInfo(workspaceFolder: string, configFilePath: string): Promise<{ version: number, cli: string }> {
try {
return await this._getPlaywrightInfoImpl(workspaceFolder, configFilePath, '@playwright/test');
} catch (error) {
// In order to support component testing
return await this._getPlaywrightInfoImpl(workspaceFolder, configFilePath, 'playwright');
}
}

private async _getPlaywrightInfoImpl(workspaceFolder: string, configFilePath: string, cliPackage: string): Promise<{ version: number, cli: string }> {
const pwtInfo = await this._runNode([
'-e',
`try { const pwtIndex = require.resolve("${cliPackage}"); const version = require("${cliPackage}/package.json").version; console.log(JSON.stringify({ pwtIndex, version})); } catch { console.log("null"); }`,
require.resolve('./playwrightFinder'),
], path.dirname(configFilePath));
if (!pwtInfo)
throw new Error(`Cannot find Playwright Test package`);
const { version } = JSON.parse(pwtInfo);
const v = parseFloat(version.replace(/-(next|beta)$/, ''));

// We only depend on playwright-core in 1.15+, bail out.
if (v < 1.19)
return { cli: '', version: v };

const cliInfo = await this._runNode([
'-e',
`try { const path = require('path'); const cli = path.join(path.dirname(require.resolve("${cliPackage}")), 'cli.js'); console.log(JSON.stringify({ cli })); } catch { console.log("null"); }`,
], path.dirname(configFilePath));
if (!cliInfo)
throw new Error(`Cannot find Playwright Test CLI`);
let { cli } = JSON.parse(cliInfo);

// Dogfood for 'ttest'
const { version, cli, error } = JSON.parse(pwtInfo) as { version: number, cli: string, error?: string };
if (error)
throw new Error(error);
let cliOverride = cli;
if (cli.includes('/playwright/packages/playwright-test/') && configFilePath.includes('playwright-test'))
cli = path.join(workspaceFolder, 'tests/playwright-test/stable-test-runner/node_modules/@playwright/test/cli.js');

return { cli, version: v };
cliOverride = path.join(workspaceFolder, 'tests/playwright-test/stable-test-runner/node_modules/@playwright/test/cli.js');
return { cli: cliOverride, version };
}

async listFiles(config: TestConfig): Promise<ConfigListFilesReport> {
Expand Down

0 comments on commit cf062a2

Please sign in to comment.