| // Copyright 2026 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| import {enableCompileCache} from 'node:module'; | |
| import * as path from 'node:path'; | |
| import yargs from 'yargs'; | |
| import {hideBin} from 'yargs/helpers'; | |
| import {checkOrphanTestManifest, type OrphanFinding} from './checks/orphan_test_manifest.ts'; | |
| function formatHumanReport(findings: readonly OrphanFinding[], warnings: readonly OrphanFinding[]): string { | |
| const warningSuffix = warnings.length > 0 ? `, ${warnings.length} warning(s)` : ''; | |
| const header = findings.length === 0 ? `verify-cl: PASS (0 errors${warningSuffix})` : | |
| `verify-cl: FAIL (${findings.length} error(s)${warningSuffix})`; | |
| const lines = [header]; | |
| for (const finding of findings) { | |
| lines.push(` ${finding.file} (${finding.code}): ${finding.message}`); | |
| lines.push(` Reference: ${finding.reference}`); | |
| } | |
| for (const warning of warnings) { | |
| lines.push(` WARNING ${warning.file} (${warning.code}): ${warning.message}`); | |
| lines.push(` Reference: ${warning.reference}`); | |
| } | |
| return lines.join('\n'); | |
| } | |
| async function main(): Promise<void> { | |
| enableCompileCache(); | |
| // Disable yargs' process.exit() so stdout/stderr flush cleanly and parse errors exit 2 rather | |
| // than yargs' default exit 1 (which is reserved for verification findings). | |
| let parseFailed = false; | |
| const argv = yargs(hideBin(process.argv)) | |
| .scriptName('dt verify-cl') | |
| .strict() | |
| .exitProcess(false) | |
| .version(false) | |
| .option('json', { | |
| type: 'boolean', | |
| default: false, | |
| description: 'Output machine-readable JSON report', | |
| }) | |
| .option('root', { | |
| type: 'string', | |
| default: path.resolve(import.meta.dirname, '../..'), | |
| description: 'Repository root directory', | |
| }) | |
| .fail((message, err) => { | |
| parseFailed = true; | |
| const output = err instanceof Error ? err.message : message; | |
| if (output) { | |
| process.stderr.write(`${output}\n`); | |
| } | |
| process.exitCode = 2; | |
| }) | |
| .parseSync(); | |
| if (parseFailed || ('help' in argv && Boolean(argv.help))) { | |
| return; | |
| } | |
| try { | |
| const {findings, warnings} = await checkOrphanTestManifest(argv.root); | |
| const ok = findings.length === 0; | |
| const rendered = | |
| argv.json ? JSON.stringify({ok, findings, warnings}, null, 2) : formatHumanReport(findings, warnings); | |
| process.stdout.write(`${rendered}\n`); | |
| process.exitCode = ok ? 0 : 1; | |
| } catch (error) { | |
| const message = error instanceof Error ? error.message : String(error); | |
| process.stderr.write(`${message}\n`); | |
| process.exitCode = 2; | |
| } | |
| } | |
| if (import.meta.main) { | |
| await main(); | |
| } |