|
| 1 | +import { test as base, type Page, _electron } from '@playwright/test'; |
| 2 | +import { downloadAndUnzipVSCode } from '@vscode/test-electron/out/download'; |
| 3 | +export { expect } from '@playwright/test'; |
| 4 | +import path from 'path'; |
| 5 | +import os from 'os'; |
| 6 | +import fs from 'fs'; |
| 7 | +import { spawnSync } from 'child_process'; |
| 8 | + |
| 9 | +export type TestOptions = { |
| 10 | + vscodeVersion: string; |
| 11 | +}; |
| 12 | + |
| 13 | +type TestFixtures = TestOptions & { |
| 14 | + page: Page; |
| 15 | + createTestProject: () => Promise<string>; |
| 16 | + createTmpDir: () => Promise<string>; |
| 17 | +}; |
| 18 | + |
| 19 | +let testProjectPath: string; |
| 20 | +export const test = base.extend<TestFixtures>({ |
| 21 | + vscodeVersion: ['insiders', { option: true }], |
| 22 | + page: async ({ vscodeVersion, createTestProject, createTmpDir }, use) => { |
| 23 | + const defaultCachePath = await createTmpDir(); |
| 24 | + const vscodePath = await downloadAndUnzipVSCode(vscodeVersion); |
| 25 | + testProjectPath = await createTestProject(); |
| 26 | + |
| 27 | + const electronApp = await _electron.launch({ |
| 28 | + executablePath: vscodePath, |
| 29 | + // Got it from https://github.com/microsoft/vscode-test/blob/0ec222ef170e102244569064a12898fb203e5bb7/lib/runTest.ts#L126-L160 |
| 30 | + args: [ |
| 31 | + '--no-sandbox', // https://github.com/microsoft/vscode/issues/84238 |
| 32 | + '--disable-gpu-sandbox', // https://github.com/microsoft/vscode-test/issues/221 |
| 33 | + '--disable-updates', // https://github.com/microsoft/vscode-test/issues/120 |
| 34 | + '--skip-welcome', |
| 35 | + '--skip-release-notes', |
| 36 | + '--disable-workspace-trust', |
| 37 | + `--extensionDevelopmentPath=${path.join(__dirname, '../../../')}`, |
| 38 | + `--extensions-dir=${path.join(defaultCachePath, 'extensions')}`, |
| 39 | + `--user-data-dir=${path.join(defaultCachePath, 'user-data')}`, |
| 40 | + testProjectPath, |
| 41 | + ], |
| 42 | + }); |
| 43 | + |
| 44 | + const page = await electronApp.firstWindow(); |
| 45 | + await page.context().tracing.start({ |
| 46 | + screenshots: true, |
| 47 | + snapshots: true, |
| 48 | + title: test.info().title, |
| 49 | + }); |
| 50 | + |
| 51 | + await use(page); |
| 52 | + |
| 53 | + const tracePath = test.info().outputPath('trace.zip'); |
| 54 | + await page.context().tracing.stop({ path: tracePath }); |
| 55 | + test.info().attachments.push({ name: 'trace', path: tracePath, contentType: 'application/zip' }); |
| 56 | + await electronApp.close(); |
| 57 | + |
| 58 | + const logPath = path.join(defaultCachePath, 'user-data'); |
| 59 | + if (fs.existsSync(logPath)) { |
| 60 | + const logOutputPath = test.info().outputPath('vscode-logs'); |
| 61 | + await fs.promises.cp(logPath, logOutputPath, { recursive: true }); |
| 62 | + } |
| 63 | + }, |
| 64 | + createTestProject: async ({ createTmpDir }, use) => { |
| 65 | + await use(async () => { |
| 66 | + // We want to be outside of the project directory to avoid already installed dependencies. |
| 67 | + const projectPath = await createTmpDir(); |
| 68 | + if (fs.existsSync(projectPath)) await fs.promises.rm(projectPath, { recursive: true }); |
| 69 | + console.log(`Creating project in ${projectPath}`); |
| 70 | + await fs.promises.mkdir(projectPath); |
| 71 | + spawnSync(`yarn init playwright@latest --yes -- --quiet --browser=chromium --gha --install-deps`, { |
| 72 | + cwd: projectPath, |
| 73 | + stdio: 'inherit', |
| 74 | + shell: true, |
| 75 | + }); |
| 76 | + spawnSync(`git init .`, { |
| 77 | + cwd: projectPath, |
| 78 | + }); |
| 79 | + return projectPath; |
| 80 | + }); |
| 81 | + }, |
| 82 | + createTmpDir: async ({}, use) => { |
| 83 | + const tempDirs: string[] = []; |
| 84 | + await use(async () => { |
| 85 | + const tempDir = await fs.promises.realpath(await fs.promises.mkdtemp(path.join(os.tmpdir(), 'gltest-'))); |
| 86 | + tempDirs.push(tempDir); |
| 87 | + return tempDir; |
| 88 | + }); |
| 89 | + for (const tempDir of tempDirs) await fs.promises.rm(tempDir, { recursive: true }); |
| 90 | + }, |
| 91 | +}); |
0 commit comments