| // Copyright 2022 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| import * as path from 'path'; | |
| import * as process from 'process'; | |
| import * as fs from 'fs'; | |
| import { spawnSync } from 'child_process'; | |
| import { runTests, SilentReporter } from '@vscode/test-electron'; | |
| /** | |
| * Checks if headless test execution is needed and relaunch with xvfb-run if necessary. | |
| */ | |
| function checkXvfb() { | |
| if (process.platform !== 'linux' || process.env.DISPLAY) { | |
| return; // Not on Linux or already have a display. | |
| } | |
| // Check if xvfb-run is available | |
| const check = spawnSync('command -v xvfb-run', { shell: true }); | |
| if (check.status !== 0) { | |
| console.error('Error: xvfb-run is not available, but required for headless testing on Linux.'); | |
| console.error( | |
| 'Please install xvfb-run and ensure it is in your PATH, ' + | |
| 'or run the tests with the DISPLAY environment variable set.', | |
| ); | |
| process.exit(1); | |
| } | |
| // Relaunch with xvfb-run | |
| const args = ['-a', process.argv[0], ...process.argv.slice(1)]; | |
| const res = spawnSync('xvfb-run', args, { stdio: 'inherit' }); | |
| if (res.error) { | |
| console.error('Failed to spawn xvfb-run:', res.error); | |
| } | |
| process.exit(res.status ?? 1); | |
| } | |
| checkXvfb(); | |
| /** | |
| * download vscode as necessary & run tests | |
| * | |
| * (needs to be a separate function because commonjs can't do top-level await) | |
| */ | |
| async function main() { | |
| try { | |
| // The folder containing the Extension Manifest package.json | |
| // Passed to `--extensionDevelopmentPath` | |
| // NB(sollyross): this must resolve to the directory containing package.json. | |
| // Since our tsconfig's rootDir is ${repo_root}, this file will end up | |
| // (when built) in ./out/src/test/, this means that's | |
| // ../../.. | |
| const extensionDevelopmentPath = path.resolve(__dirname, '../../../'); | |
| // The path to test runner | |
| // Passed to --extensionTestsPath | |
| const extensionTestsPath = path.resolve(__dirname, './suite/index'); | |
| const testDataDir = path.resolve(extensionDevelopmentPath, 'testdata'); | |
| const workspaceDir = path.resolve(testDataDir, 'workspace'); | |
| // Copy configuration to disable git since `StubbedSpawn` can otherwise | |
| // capture irrelevant git commands, resulting in flakes. | |
| const testVscodeDir = path.join(workspaceDir, '.vscode'); | |
| fs.mkdirSync(testVscodeDir, { recursive: true }); | |
| fs.copyFileSync( | |
| path.join(testDataDir, 'workspace_settings.json'), | |
| path.join(testVscodeDir, 'settings.json'), | |
| ); | |
| // Unset ELECTRON_RUN_AS_NODE to force the tests to run under electron instead of node, since | |
| // the `--disable-extensions` test arg isn't recognized when running under node. | |
| delete process.env.ELECTRON_RUN_AS_NODE; | |
| // Download VS Code, unzip it and run the integration test | |
| const settings = { | |
| extensionDevelopmentPath, | |
| extensionTestsPath, | |
| // NB(sollyross): keep this pinned so that infra runs are as reproducible | |
| // as we can make them. Due to VSCode licensing issues, we can't upload | |
| // VSCode binary distributions to CIPD, and VSCode OSS is technically | |
| // different than binary VSCode, so we've got an exception to download | |
| // from an external source -- see https://fxbug.dev/98159 | |
| version: '1.112.0', | |
| // Use a silent reporter so that we don't spam CI logs | |
| reporter: process.env.ON_CI ? new SilentReporter() : undefined, | |
| launchArgs: [ | |
| // Disable unrelated extensions to minimize flakes. | |
| '--disable-extensions', | |
| // Disable hardware acceleration to suppress irrelevant GPU errors when testing. | |
| '--disable-gpu', | |
| workspaceDir, | |
| ], | |
| }; | |
| await runTests(settings); | |
| } catch { | |
| console.error('Failed to run tests'); | |
| process.exit(1); | |
| } | |
| } | |
| void /* cause cjs prevents top-level await */ main(); |