| // Copyright 2024 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| import * as childProcess from 'node:child_process'; | |
| import * as fs from 'node:fs'; | |
| import * as os from 'node:os'; | |
| import * as path from 'node:path'; | |
| import yargs from 'yargs'; | |
| import unparse from 'yargs-unparser'; | |
| import {commandLineArgs, expandResponseFiles, normalizeForwardedOptions} from './conductor/commandline.js'; | |
| import { | |
| BUILD_ROOT, | |
| BUILD_WITH_CHROMIUM, | |
| CHECKOUT_ROOT, | |
| GEN_DIR, | |
| isContainedInDirectory, | |
| PathPair, | |
| SOURCE_ROOT, | |
| TEST_ID_REGEX, | |
| TestId, | |
| } from './conductor/paths.js'; | |
| import {isAIAgent} from './conductor/test_config.js'; | |
| const options = | |
| commandLineArgs(yargs(expandResponseFiles(process.argv.slice(2)))) | |
| .parserConfiguration({'strip-aliased': true}) | |
| .options('skip-ninja', { | |
| type: 'boolean', | |
| default: false, | |
| desc: 'Skip rebuilding', | |
| }) | |
| .options('debug-driver', { | |
| type: 'boolean', | |
| hidden: true, | |
| desc: 'Debug the driver part of tests', | |
| }) | |
| .options('bail', { | |
| type: 'boolean', | |
| alias: 'b', | |
| desc: 'Bail after first test failure', | |
| }) | |
| .options('auto-watch', { | |
| type: 'boolean', | |
| default: false, | |
| desc: 'watch changes to files and run tests automatically on file change (only for unit tests)', | |
| }) | |
| .options('node-unit-tests', | |
| {type: 'boolean', default: false, desc: 'whether to run unit tests in node (experimental)'}) | |
| .positional('tests', { | |
| type: 'string', | |
| desc: 'Path to the test suite, starting from out/Target/gen directory.', | |
| normalize: true, | |
| default: ['front_end', 'test/e2e'].map(f => path.relative(process.cwd(), path.join(SOURCE_ROOT, f))), | |
| }) | |
| .strict() | |
| .parseSync(); | |
| const CONSUMED_OPTIONS = ['tests', 'skip-ninja', 'debug-driver', 'watch', 'verbose']; | |
| let logLevel = 'error'; | |
| if (Number(options['verbose']) === 1) { | |
| logLevel = 'info'; | |
| } else if (Number(options['verbose']) >= 2) { | |
| logLevel = 'debug'; | |
| } | |
| function forwardOptions(): string[] { | |
| const forwardedOptions = {...options}; | |
| for (const consume of CONSUMED_OPTIONS) { | |
| forwardedOptions[consume] = undefined; | |
| } | |
| // @ts-expect-error yargs and unparse have slightly different types | |
| const unparsed = unparse(forwardedOptions); | |
| return normalizeForwardedOptions(unparsed); | |
| } | |
| function runProcess(exe: string, args: string[], options: childProcess.SpawnSyncOptionsWithStringEncoding) { | |
| if (os.platform() === 'win32' && !exe.endsWith('.exe')) { | |
| args = ['/c', 'vpython3.bat', '-x', exe, ...args]; | |
| exe = process.env.ComSpec ?? 'cmd.exe'; | |
| } | |
| if (logLevel !== 'error') { | |
| // eslint-disable-next-line no-console | |
| console.info(`Running '${exe}${args.length > 0 ? ` "${args.join('" "')}"` : ''}'`); | |
| } | |
| const result = childProcess.spawnSync(exe, args, options); | |
| if (result.error) { | |
| console.error(result.error); | |
| } | |
| return result; | |
| } | |
| function ninja(stdio: 'inherit'|'pipe', ...args: string[]) { | |
| let buildRoot = path.dirname(GEN_DIR); | |
| while (!fs.existsSync(path.join(buildRoot, 'args.gn'))) { | |
| const parent = path.dirname(buildRoot); | |
| if (parent === buildRoot) { | |
| throw new Error('Failed to find a build directory containing args.gn'); | |
| } | |
| buildRoot = parent; | |
| } | |
| // autoninja can't always find ninja if not run from the checkout root, so | |
| // run it from there and pass the build root as an argument. | |
| let result; | |
| if (os.platform() === 'win32') { | |
| result = runProcess( | |
| process.env.ComSpec ?? 'cmd.exe', | |
| ['/c', 'autoninja.bat', '-C', buildRoot, ...args], | |
| { | |
| encoding: 'utf-8', | |
| cwd: CHECKOUT_ROOT, | |
| stdio, | |
| }, | |
| ); | |
| } else { | |
| result = runProcess( | |
| 'autoninja', | |
| ['-C', buildRoot, ...args], | |
| { | |
| encoding: 'utf-8', | |
| cwd: CHECKOUT_ROOT, | |
| stdio, | |
| }, | |
| ); | |
| } | |
| if (result.error) { | |
| throw result.error; | |
| } | |
| const {status, output: [, output]} = result; | |
| return {status, output}; | |
| } | |
| class Tests { | |
| readonly suite: PathPair; | |
| readonly extraPaths: PathPair[]; | |
| protected readonly cwd = path.dirname(GEN_DIR); | |
| protected get executable(): string { | |
| return process.argv[0]; | |
| } | |
| constructor(suite: string, ...extraSuites: string[]) { | |
| const suitePath = PathPair.get(suite); | |
| if (!suitePath) { | |
| throw new Error(`Could not locate the test suite '${suite}'`); | |
| } | |
| this.suite = suitePath; | |
| const extraPaths = extraSuites.map(p => [p, PathPair.get(p)]); | |
| const failures = extraPaths.filter(p => p[1] === null); | |
| if (failures.length > 0) { | |
| throw new Error(`Could not resolve extra paths for ${failures.map(p => p[0]).join()}`); | |
| } | |
| this.extraPaths = extraPaths.filter((p): p is[string, PathPair] => p[1] !== null).map(p => p[1]); | |
| } | |
| match(path: TestId) { | |
| return [this.suite, ...this.extraPaths].some( | |
| pathToCheck => isContainedInDirectory(path.pathPair.buildPath, pathToCheck.buildPath)); | |
| } | |
| protected readonly useResponseFile: boolean = true; | |
| protected run(tests: TestId[], args: string[]) { | |
| const testList = tests.map(t => t.toBuildTestId()); | |
| let tmpDir: string|undefined; | |
| let testArgs: string[]; | |
| if (this.useResponseFile) { | |
| tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'devtools-test-runner-')); | |
| const rspPath = path.join(tmpDir, 'tests.rsp'); | |
| fs.writeFileSync(rspPath, testList.join('\n'), 'utf-8'); | |
| if (logLevel !== 'error') { | |
| // eslint-disable-next-line no-console | |
| console.info(`Response file (${rspPath}) content (${testList.length} test(s)):\n${ | |
| testList.map(t => ` ${t}`).join('\n')}`); | |
| } | |
| testArgs = [`@${rspPath}`]; | |
| } else { | |
| testArgs = testList; | |
| } | |
| try { | |
| const argumentsForNode = [ | |
| ...args, | |
| ...(options['auto-watch'] ? ['--auto-watch', '--no-single-run'] : []), | |
| '--', | |
| ...testArgs, | |
| ...(options['verbose'] ? [`--verbose=${options['verbose']}`] : []), | |
| ...forwardOptions(), | |
| ]; | |
| if (options['debug-driver']) { | |
| argumentsForNode.unshift('--inspect-brk'); | |
| } else if (options['debug'] && !argumentsForNode.includes('--inspect-brk')) { | |
| argumentsForNode.unshift('--inspect'); | |
| } | |
| const result = runProcess(this.executable, argumentsForNode, { | |
| encoding: 'utf-8', | |
| stdio: 'inherit', | |
| cwd: this.cwd, | |
| }); | |
| return !result.error && (result.status ?? 1) === 0; | |
| } finally { | |
| if (tmpDir) { | |
| try { | |
| fs.rmSync(tmpDir, {recursive: true, force: true}); | |
| } catch { | |
| // ignore cleanup errors | |
| } | |
| } | |
| } | |
| } | |
| } | |
| function isApiTestFile(testId: TestId): boolean { | |
| return testId.pathPair.sourcePath.endsWith('.test.api.ts') || testId.pathPair.buildPath.endsWith('.test.api.js'); | |
| } | |
| function isUnitTestFile(testId: TestId): boolean { | |
| return testId.pathPair.sourcePath.endsWith('.test.ts') && !isApiTestFile(testId); | |
| } | |
| class MochaFrontendTests extends Tests { | |
| #getWrapper(): string|undefined { | |
| const wrapper = path.join(BUILD_ROOT, 'bin', 'run_devtools_node_unit_tests'); | |
| return fs.existsSync(wrapper) ? wrapper : undefined; | |
| } | |
| override get executable() { | |
| return this.#getWrapper() ?? super.executable; | |
| } | |
| override match(path: TestId): boolean { | |
| return super.match(path) && !isApiTestFile(path); | |
| } | |
| override run(tests: TestId[]) { | |
| if (this.#getWrapper()) { | |
| return super.run(tests, []); | |
| } | |
| return super.run( | |
| tests, | |
| [ | |
| path.join(this.suite.buildPath, '..', 'test', 'unit', 'run-mocha.js'), | |
| ], | |
| ); | |
| } | |
| } | |
| class MochaApiTests extends Tests { | |
| #getWrapper(): string|undefined { | |
| const wrapper = path.join(BUILD_ROOT, 'bin', 'run_devtools_api_tests'); | |
| return fs.existsSync(wrapper) ? wrapper : undefined; | |
| } | |
| override get executable() { | |
| return this.#getWrapper() ?? super.executable; | |
| } | |
| override match(path: TestId): boolean { | |
| return super.match(path) && !isUnitTestFile(path); | |
| } | |
| override run(tests: TestId[]) { | |
| if (this.#getWrapper()) { | |
| return super.run(tests, []); | |
| } | |
| return super.run( | |
| tests, | |
| [ | |
| path.join(GEN_DIR, 'test', 'api', 'run-mocha.js'), | |
| ], | |
| ); | |
| } | |
| } | |
| class MochaTests extends Tests { | |
| readonly #wrapperName?: string; | |
| constructor(suite: string, wrapperName?: string, ...extraSuites: string[]) { | |
| super(suite, ...extraSuites); | |
| this.#wrapperName = wrapperName; | |
| } | |
| #getWrapper(): string|undefined { | |
| if (!this.#wrapperName || options['debug'] || options['debug-driver']) { | |
| return undefined; | |
| } | |
| const wrapper = path.join(BUILD_ROOT, 'bin', this.#wrapperName); | |
| return fs.existsSync(wrapper) ? wrapper : undefined; | |
| } | |
| override get executable() { | |
| return this.#getWrapper() ?? super.executable; | |
| } | |
| override run(tests: TestId[]) { | |
| if (this.#getWrapper()) { | |
| return super.run(tests, []); | |
| } | |
| const args = [ | |
| path.join(this.suite.buildPath, 'run-mocha.js'), | |
| ]; | |
| if (options['debug']) { | |
| // VSCode has issue when starting with '--inspect-brk' | |
| // Provide this in the launch.json see | |
| // .vscode/devtools-workspace-launch.json | |
| if (process.env.VSCODE_DEBUGGER === 'true') { | |
| args.unshift('--inspect'); | |
| console.warn('Attaching to VSCode Debugger automatically'); | |
| } else { | |
| args.unshift('--inspect-brk'); | |
| console.warn( | |
| '\x1b[33mYou need to attach a debugger from chrome://inspect for tests to continue the run in debug mode.\x1b[0m'); | |
| console.warn( | |
| '\x1b[33mWhen attached, resume execution in the Sources panel to begin debugging the test.\x1b[0m'); | |
| } | |
| } | |
| return super.run( | |
| tests, | |
| args, | |
| ); | |
| } | |
| } | |
| /** | |
| * Workaround the fact that these test don't have | |
| * build output in out/Default like dir. | |
| */ | |
| class ScriptPathPair extends PathPair { | |
| static getFromPair(pair: PathPair) { | |
| return new ScriptPathPair(pair.sourcePath, pair.sourcePath); | |
| } | |
| } | |
| class ScriptTestId extends TestId { | |
| static getFromTestId(testId: TestId) { | |
| return new ScriptTestId(ScriptPathPair.getFromPair(testId.pathPair), testId.subTestId); | |
| } | |
| } | |
| class ScriptTests extends Tests { | |
| override readonly cwd = SOURCE_ROOT; | |
| #getWrapper(): string|undefined { | |
| if (options['debug'] || options['debug-driver']) { | |
| return undefined; | |
| } | |
| const wrapper = path.join(BUILD_ROOT, 'bin', 'run_devtools_script_tests'); | |
| return fs.existsSync(wrapper) ? wrapper : undefined; | |
| } | |
| override get executable() { | |
| return this.#getWrapper() ?? super.executable; | |
| } | |
| override run(tests: TestId[]) { | |
| const scriptTests = tests.map(test => ScriptTestId.getFromTestId(test)); | |
| if (this.#getWrapper()) { | |
| return super.run(scriptTests, []); | |
| } | |
| return super.run( | |
| scriptTests, | |
| [ | |
| path.join(SOURCE_ROOT, 'test', 'scripts', 'run_mocha.ts'), | |
| ], | |
| ); | |
| } | |
| override match(path: TestId): boolean { | |
| return [this.suite, ...this.extraPaths].some( | |
| pathToCheck => isContainedInDirectory(path.pathPair.sourcePath, pathToCheck.sourcePath)); | |
| } | |
| } | |
| class KarmaTests extends Tests { | |
| #getWrapper(): string|undefined { | |
| if (options['auto-watch']) { | |
| return undefined; | |
| } | |
| const wrapper = path.join(BUILD_ROOT, 'bin', 'run_devtools_unit_tests'); | |
| return fs.existsSync(wrapper) ? wrapper : undefined; | |
| } | |
| override get executable() { | |
| return this.#getWrapper() ?? super.executable; | |
| } | |
| override match(path: TestId): boolean { | |
| return super.match(path) && !isApiTestFile(path); | |
| } | |
| override run(tests: TestId[]) { | |
| if (this.#getWrapper()) { | |
| return super.run(tests, []); | |
| } | |
| return super.run(tests, [ | |
| path.join(SOURCE_ROOT, 'node_modules', 'karma', 'bin', 'karma'), | |
| 'start', | |
| path.join(GEN_DIR, 'test', 'unit', 'karma.conf.js'), | |
| '--log-level', | |
| logLevel, | |
| ]); | |
| } | |
| } | |
| class MochaAiEvalsTests extends MochaTests { | |
| override match(pathToMatch: TestId): boolean { | |
| return super.match(pathToMatch) && | |
| !isContainedInDirectory( | |
| pathToMatch.pathPair.buildPath, | |
| path.join(GEN_DIR, 'test', 'ai_evals', 'report'), | |
| ); | |
| } | |
| } | |
| class ForAgentsTests extends Tests { | |
| override readonly cwd = path.join(SOURCE_ROOT, 'for_agents'); | |
| override run(tests: TestId[]) { | |
| const scriptPath = path.join(SOURCE_ROOT, 'for_agents', 'scripts', 'test.js'); | |
| const args = [scriptPath, '--build-dir', BUILD_ROOT]; | |
| const shardCount = Number(options['shard-count'] ?? 1); | |
| const shardNumber = Number(options['shard-number'] ?? 1); | |
| if (shardCount > 1) { | |
| args.push(`--test-shard=${shardNumber}/${shardCount}`); | |
| } | |
| const isWholeSuite = | |
| tests.length === 1 && path.normalize(tests[0].pathPair.buildPath) === path.normalize(this.suite.buildPath); | |
| if (!isWholeSuite) { | |
| for (const test of tests) { | |
| args.push(test.pathPair.buildPath); | |
| } | |
| } | |
| const env: NodeJS.ProcessEnv = {...process.env}; | |
| if (options['chrome-binary']) { | |
| env['PUPPETEER_EXECUTABLE_PATH'] = String(options['chrome-binary']); | |
| } | |
| const result = runProcess(this.executable, args, { | |
| encoding: 'utf-8', | |
| stdio: 'inherit', | |
| cwd: this.cwd, | |
| env, | |
| }); | |
| return !result.error && (result.status ?? 1) === 0; | |
| } | |
| } | |
| /** | |
| * TODO(333423685) | |
| * - watch | |
| **/ | |
| function main() { | |
| const tests: string[] = typeof options['tests'] === 'string' ? [options['tests']] : options['tests']; | |
| const forAgentsTestsScript = path.join(SOURCE_ROOT, 'for_agents', 'scripts', 'test.js'); | |
| const testKinds = [ | |
| new MochaApiTests(path.join(GEN_DIR, 'front_end')), | |
| new (options['node-unit-tests'] ? MochaFrontendTests : KarmaTests)( | |
| path.join(GEN_DIR, 'front_end'), path.join(GEN_DIR, 'inspector_overlay'), path.join(GEN_DIR, 'mcp')), | |
| new MochaTests(path.join(GEN_DIR, 'test/e2e'), 'run_devtools_e2e_tests'), | |
| ...(fs.existsSync(forAgentsTestsScript) ? [new ForAgentsTests(path.join(GEN_DIR, 'for_agents/tests'))] : []), | |
| new MochaAiEvalsTests(path.join(GEN_DIR, 'test/ai_evals')), | |
| new MochaTests(path.join(GEN_DIR, 'test/perf')), | |
| new ScriptTests(path.join(SOURCE_ROOT, 'scripts/eslint_rules/tests')), | |
| new ScriptTests(path.join(SOURCE_ROOT, 'scripts/stylelint_rules/tests')), | |
| new ScriptTests(path.join(SOURCE_ROOT, 'scripts/build/tests')), | |
| new ScriptTests(path.join(SOURCE_ROOT, 'scripts/gn_deps_verifier/tests')), | |
| new ScriptTests(path.join(SOURCE_ROOT, 'scripts/ai_assistance')), | |
| new ScriptTests(path.join(SOURCE_ROOT, 'test/ai_evals/report')), | |
| new ScriptTests(path.join(SOURCE_ROOT, 'test/conductor')), | |
| new ScriptTests(path.join(SOURCE_ROOT, 'scripts/verify_cl/tests')), | |
| ]; | |
| if (!options['skip-ninja']) { | |
| // For a devtools only checkout, it is fast enough to build everything. For | |
| // a chromium checkout we want to build only the targets that are needed. | |
| // The location of DevTools inside of the chromium checkout, as a GN label | |
| // prefix (e.g. `third_party/devtools`). | |
| const devtoolsDir = path.relative(CHECKOUT_ROOT, SOURCE_ROOT).split(path.sep).join('/'); | |
| const targets = BUILD_WITH_CHROMIUM ? | |
| [ | |
| 'chrome', | |
| `${devtoolsDir}/test:test`, | |
| `${devtoolsDir}/scripts/hosted_mode:hosted_mode`, | |
| `${devtoolsDir}/test/api:devtools_api_tests`, | |
| `${devtoolsDir}/test/e2e:devtools_e2e_tests`, | |
| `${devtoolsDir}/test/scripts:devtools_script_tests`, | |
| `${devtoolsDir}/test/unit:devtools_node_unit_tests`, | |
| `${devtoolsDir}/test/unit:devtools_unit_tests`, | |
| ] : | |
| []; | |
| const {status} = ninja(isAIAgent() ? 'pipe' : 'inherit', ...targets); | |
| if (status) { | |
| return status; | |
| } | |
| } | |
| const suites = new Map<(typeof testKinds)[number], TestId[]>(); | |
| const testIds = tests | |
| .flatMap(t => { | |
| if (TEST_ID_REGEX.test(t)) { | |
| return [t]; | |
| } | |
| const globbed = fs.globSync(t); | |
| return globbed.length > 0 ? globbed : [t]; | |
| }); | |
| for (const t of testIds) { | |
| const testId = TestId.create(t); | |
| if (!testId) { | |
| console.error(`Could not locate the test input for '${t}'`); | |
| continue; | |
| } | |
| const matchingSuites = testKinds.filter(kind => kind.match(testId)); | |
| if (matchingSuites.length === 0) { | |
| console.error(`Unknown test suite for '${testId.pathPair.sourcePath}'`); | |
| continue; | |
| } | |
| for (const suite of matchingSuites) { | |
| suites.get(suite)?.push(testId) ?? suites.set(suite, [testId]); | |
| } | |
| } | |
| if (suites.size > 0) { | |
| const success = Array.from(suites).every(([suite, files]) => suite.run(files)); | |
| return success ? 0 : 1; | |
| } | |
| if (tests.length > 0) { | |
| return 1; | |
| } | |
| const success = testKinds.every(kind => kind.run([TestId.create(kind.suite.sourcePath)!])); | |
| return success ? 0 : 1; | |
| } | |
| process.exit(main()); |