|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (C) 2025 Posit Software, PBC. All rights reserved. |
| 3 | + * Licensed under the Elastic License 2.0. See LICENSE.txt for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import path = require('path'); |
| 7 | +import { join } from 'path'; |
| 8 | +import * as fs from 'fs'; |
| 9 | +import { constants, access, rm, mkdir, rename } from 'fs/promises'; |
| 10 | +import * as os from 'os'; |
| 11 | +import * as playwright from '@playwright/test'; |
| 12 | +import { Application, ApplicationOptions, MultiLogger, createApp, copyFixtureFile, Quality, getRandomUserDataDir } from '../../infra'; |
| 13 | +import { SPEC_NAME, setFixtureScreenshot, ROOT_PATH, TEMP_DIR } from './constants'; |
| 14 | + |
| 15 | +export interface CustomTestOptions { |
| 16 | + web: boolean; |
| 17 | + artifactDir: string; |
| 18 | + headless?: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +export interface AppFixtureOptions { |
| 22 | + options: ApplicationOptions; |
| 23 | + logsPath: string; |
| 24 | + logger: MultiLogger; |
| 25 | + workerInfo: playwright.WorkerInfo; |
| 26 | +} |
| 27 | + |
| 28 | +export function OptionsFixture() { |
| 29 | + return async (logsPath: string, logger: MultiLogger, snapshots: boolean, project: CustomTestOptions) => { |
| 30 | + const TEST_DATA_PATH = join(os.tmpdir(), 'vscsmoke'); |
| 31 | + const EXTENSIONS_PATH = join(TEST_DATA_PATH, 'extensions-dir'); |
| 32 | + const WORKSPACE_PATH = join(TEST_DATA_PATH, 'qa-example-content'); |
| 33 | + const SPEC_CRASHES_PATH = join(ROOT_PATH, '.build', 'crashes', project.artifactDir, TEMP_DIR); |
| 34 | + |
| 35 | + // get the version from package.json |
| 36 | + const packageJsonPath = join(ROOT_PATH, 'package.json'); |
| 37 | + const packageJson = JSON.parse(await fs.promises.readFile(packageJsonPath, 'utf-8')); |
| 38 | + const packageVersion = packageJson.version || '0.0.0'; |
| 39 | + const version = { |
| 40 | + major: parseInt(packageVersion.split('.')[0], 10), |
| 41 | + minor: parseInt(packageVersion.split('.')[1], 10), |
| 42 | + patch: parseInt(packageVersion.split('.')[2], 10), |
| 43 | + }; |
| 44 | + |
| 45 | + const options: ApplicationOptions = { |
| 46 | + codePath: process.env.BUILD, |
| 47 | + workspacePath: WORKSPACE_PATH, |
| 48 | + userDataDir: join(TEST_DATA_PATH, 'd'), |
| 49 | + extensionsPath: EXTENSIONS_PATH, |
| 50 | + logger, |
| 51 | + logsPath, |
| 52 | + crashesPath: SPEC_CRASHES_PATH, |
| 53 | + verbose: !!process.env.VERBOSE, |
| 54 | + remote: !!process.env.REMOTE, |
| 55 | + web: project.web, |
| 56 | + headless: project.headless, |
| 57 | + tracing: true, |
| 58 | + snapshots, |
| 59 | + quality: Quality.Dev, |
| 60 | + version |
| 61 | + }; |
| 62 | + |
| 63 | + options.userDataDir = getRandomUserDataDir(options); |
| 64 | + |
| 65 | + return options; |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +export function UserDataDirFixture() { |
| 70 | + return async (options: ApplicationOptions) => { |
| 71 | + const userDir = options.web ? join(options.userDataDir, 'data', 'User') : join(options.userDataDir, 'User'); |
| 72 | + process.env.PLAYWRIGHT_USER_DATA_DIR = userDir; |
| 73 | + |
| 74 | + // Copy keybindings and settings fixtures to the user data directory |
| 75 | + await copyFixtureFile('keybindings.json', userDir, true); |
| 76 | + |
| 77 | + const settingsFileName = 'settings.json'; |
| 78 | + if (fs.existsSync('/.dockerenv')) { |
| 79 | + |
| 80 | + const fixturesDir = path.join(ROOT_PATH, 'test/e2e/fixtures'); |
| 81 | + const settingsFile = path.join(fixturesDir, 'settings.json'); |
| 82 | + |
| 83 | + const mergedSettings = { |
| 84 | + ...JSON.parse(fs.readFileSync(settingsFile, 'utf8')), |
| 85 | + ...JSON.parse(fs.readFileSync(path.join(fixturesDir, 'settingsDocker.json'), 'utf8')), |
| 86 | + }; |
| 87 | + |
| 88 | + // Overwrite file |
| 89 | + fs.writeFileSync(settingsFile, JSON.stringify(mergedSettings, null, 2)); |
| 90 | + } |
| 91 | + |
| 92 | + await copyFixtureFile(settingsFileName, userDir); |
| 93 | + |
| 94 | + return userDir; |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +export function AppFixture() { |
| 99 | + return async (fixtureOptions: AppFixtureOptions, use: (arg0: Application) => Promise<void>) => { |
| 100 | + const { options, logsPath, logger, workerInfo } = fixtureOptions; |
| 101 | + const app = createApp(options); |
| 102 | + |
| 103 | + try { |
| 104 | + await app.start(); |
| 105 | + await app.workbench.sessions.expectNoStartUpMessaging(); |
| 106 | + |
| 107 | + await use(app); |
| 108 | + } catch (error) { |
| 109 | + // capture a screenshot on failure |
| 110 | + const screenshotPath = path.join(logsPath, 'app-start-failure.png'); |
| 111 | + try { |
| 112 | + const page = app.code?.driver?.page; |
| 113 | + if (page) { |
| 114 | + const screenshot = await page.screenshot({ path: screenshotPath }); |
| 115 | + setFixtureScreenshot(screenshot); |
| 116 | + } |
| 117 | + } catch { |
| 118 | + // ignore |
| 119 | + } |
| 120 | + |
| 121 | + throw error; // re-throw the error to ensure test failure |
| 122 | + } finally { |
| 123 | + await app.stop(); |
| 124 | + |
| 125 | + // rename the temp logs dir to the spec name (if available) |
| 126 | + const specLogsPath = path.join(path.dirname(logsPath), SPEC_NAME || `worker-${workerInfo.workerIndex}`); |
| 127 | + await moveAndOverwrite(logger, logsPath, specLogsPath); |
| 128 | + } |
| 129 | + }; |
| 130 | +} |
| 131 | + |
| 132 | +async function moveAndOverwrite(logger: MultiLogger, sourcePath: string, destinationPath: string) { |
| 133 | + try { |
| 134 | + await access(sourcePath, constants.F_OK); |
| 135 | + } catch { |
| 136 | + console.error(`moveAndOverwrite: source path does not exist: ${sourcePath}`); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + // check if the destination exists and delete it if so |
| 141 | + try { |
| 142 | + await access(destinationPath, constants.F_OK); |
| 143 | + await rm(destinationPath, { recursive: true, force: true }); |
| 144 | + } catch (err) { } |
| 145 | + |
| 146 | + // ensure parent directory of destination path exists |
| 147 | + const destinationDir = path.dirname(destinationPath); |
| 148 | + await mkdir(destinationDir, { recursive: true }); |
| 149 | + |
| 150 | + // rename source to destination |
| 151 | + try { |
| 152 | + await rename(sourcePath, destinationPath); |
| 153 | + logger.setPath(destinationPath); |
| 154 | + logger.log('Logger path updated to:', destinationPath); |
| 155 | + } catch (err) { |
| 156 | + logger.log(`moveAndOverwrite: failed to move ${sourcePath} to ${destinationPath}:`, err); |
| 157 | + } |
| 158 | +} |
0 commit comments