Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: extract playwright finder #418

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const path = require('path');
import path from 'path';

nit


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(JSON.stringify({ error: 'Playwright installation not 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
Loading