/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ /** * Test utilities for ngp tests */ import * as fs from 'node:fs/promises'; import * as fsSync from 'node:fs'; import * as os from 'node:os'; import * as path from 'path'; import {createTwoFilesPatch, diffLines} from 'diff'; import {format} from 'prettier'; import type {TestFile} from './golden_markdown.js'; import {verifyCompilerOptions} from '../src/compiler_options.js'; import {getDiagnosticPath, serializeDiagnostics, HybridCompiler} from '../src/hybrid_compiler.js'; import {createAnalyzer} from '../api.js'; import {buildTypeCheckingConfig} from '../src/tcb.js'; export { parseMarkdownContent, parseMarkdownTestCase, writeMarkdownTestCase, type TestFile, } from './golden_markdown.js'; export function pathExists(p: string): Promise { return fs.access(p).then( () => true, () => false, ); } export function resolveWasmBinding(): string { if (process.env['NGP_WASM_BINDING']) { return process.env['NGP_WASM_BINDING']; } const runfilesDir = process.env['JS_BINARY__RUNFILES'] || process.env['RUNFILES_DIR']; if (runfilesDir) { const candidates = [ path.join( runfilesDir, '_main/packages/compiler-cli/preprocessor/ng-analyze/ng_analyze_wasm/ng_analyze_wasm.js', ), path.join( runfilesDir, 'angular/packages/compiler-cli/preprocessor/ng-analyze/ng_analyze_wasm/ng_analyze_wasm.js', ), path.join( runfilesDir, 'packages/compiler-cli/preprocessor/ng-analyze/ng_analyze_wasm/ng_analyze_wasm.js', ), ]; for (const c of candidates) { if (fsSync.existsSync(c)) { return c; } } } const relativeCandidates = [ path.resolve( process.cwd(), '../../packages/compiler-cli/preprocessor/ng-analyze/ng_analyze_wasm/ng_analyze_wasm.js', ), path.resolve( process.cwd(), 'dist/bin/packages/compiler-cli/preprocessor/ng-analyze/ng_analyze_wasm/ng_analyze_wasm.js', ), ]; for (const c of relativeCandidates) { if (fsSync.existsSync(c)) { return c; } } throw new Error( `Could not find ng_analyze_wasm.js in runfiles. Checked runfilesDir=${runfilesDir}, cwd=${process.cwd()}`, ); } export function resolveSidecarBinary(): string { const ext = process.platform === 'win32' ? '.exe' : ''; const runfilesDir = process.env['JS_BINARY__RUNFILES'] || process.env['RUNFILES_DIR']; if (runfilesDir) { const candidates = [ path.join( runfilesDir, `_main/packages/compiler-cli/preprocessor/ng-analyze/ng_analyze_bin${ext}`, ), path.join( runfilesDir, `angular/packages/compiler-cli/preprocessor/ng-analyze/ng_analyze_bin${ext}`, ), path.join(runfilesDir, `packages/compiler-cli/preprocessor/ng-analyze/ng_analyze_bin${ext}`), ]; for (const c of candidates) { if (fsSync.existsSync(c)) { return c; } } } const relativeCandidates = [ path.resolve( process.cwd(), `../../packages/compiler-cli/preprocessor/ng-analyze/ng_analyze_bin${ext}`, ), path.resolve( process.cwd(), `dist/bin/packages/compiler-cli/preprocessor/ng-analyze/ng_analyze_bin${ext}`, ), ]; for (const c of relativeCandidates) { if (fsSync.existsSync(c)) { return c; } } throw new Error( `Could not find ng_analyze_bin in runfiles. Checked runfilesDir=${runfilesDir}, cwd=${process.cwd()}`, ); } function resolvePackagePath(pkg: string): string | null { const runfilesDir = process.env['JS_BINARY__RUNFILES'] || process.env['RUNFILES_DIR']; if (runfilesDir) { const candidates = [ path.join(runfilesDir, `_main/packages/${pkg}/npm_package`), path.join(runfilesDir, `angular/packages/${pkg}/npm_package`), ]; for (const c of candidates) { if (fsSync.existsSync(c)) { return c; } } } const relativeCandidates = [ path.resolve(process.cwd(), `dist/bin/packages/${pkg}/npm_package`), path.resolve(process.cwd(), `packages/${pkg}/npm_package`), ]; for (const c of relativeCandidates) { if (fsSync.existsSync(c)) { return c; } } return null; } let syntheticNodeModules: string | null = null; export function getOrCreateSyntheticNodeModules(): string | null { if (syntheticNodeModules) return syntheticNodeModules; const corePath = resolvePackagePath('core'); if (!corePath) return null; const tmpRoot = process.env['TEST_TMPDIR'] || os.tmpdir(); const dir = path.join(tmpRoot, 'ngp_synthetic_node_modules'); const angularDir = path.join(dir, '@angular'); fsSync.mkdirSync(angularDir, {recursive: true}); for (const pkg of ['core', 'common']) { const pkgPath = resolvePackagePath(pkg); if (pkgPath) { const symlink = path.join(angularDir, pkg); if (!fsSync.existsSync(symlink)) { try { fsSync.symlinkSync(pkgPath, symlink, 'junction'); } catch {} } } } const runfilesDir = process.env['JS_BINARY__RUNFILES'] || process.env['RUNFILES_DIR']; if (runfilesDir) { for (const extPkg of ['rxjs', 'tslib']) { for (const prefix of ['_main', 'angular']) { const candidate = path.join(runfilesDir, prefix, 'node_modules', extPkg); const target = path.join(dir, extPkg); if (fsSync.existsSync(candidate) && !fsSync.existsSync(target)) { try { fsSync.symlinkSync(candidate, target, 'junction'); } catch {} } } } } syntheticNodeModules = dir; return dir; } /** * Whether `nodeModules/@angular/core` is a *built* package. In this repository the workspace's * `node_modules/@angular/{core,common}` are pnpm links to `packages/{core,common}` — TypeScript * sources with no `.d.ts` metadata — which the analyzer resolves to nothing: `CommonModule` then * exports no directives, core DI tokens become unverifiable, and goldens silently change. */ function hasBuiltAngularCore(nodeModules: string): boolean { return fsSync.existsSync(path.join(nodeModules, '@angular', 'core', 'index.d.ts')); } /** * The `node_modules` fixtures resolve `@angular/*` from. * * The packages Bazel built (`//packages/{core,common}:npm_package`) always win, so `bazel test` * and `bazel run` (golden updates) see the same packages. Previously the first * `node_modules/@angular` found walking up from `cwd` won instead; under `bazel test` the sandbox * has none and the built packages were used, while under `bazel run` the walk reached the * workspace's source-linked copy, producing spurious golden diffs. The walk-up remains only for * running outside Bazel, and only accepts a built package. */ let fixtureNodeModules: string | null = null; export function resolveFixtureNodeModules(): string { if (fixtureNodeModules) return fixtureNodeModules; const synthetic = getOrCreateSyntheticNodeModules(); if (synthetic) { fixtureNodeModules = synthetic; return synthetic; } let dir = process.cwd(); while (true) { const candidate = path.join(dir, 'node_modules'); if (hasBuiltAngularCore(candidate)) { fixtureNodeModules = candidate; return candidate; } const parent = path.dirname(dir); if (parent === dir) break; dir = parent; } throw new Error( 'Could not find a built @angular/core for fixtures: neither the Bazel npm_package runfiles ' + `nor a node_modules with @angular/core/index.d.ts above cwd=${process.cwd()}.`, ); } /** * Merge actual output with expected golden file, preserving ellipses */ export async function mergeGolden(actual: TestFile[], expected: TestFile[]): Promise { const expectedMap = new Map(expected.map((f) => [f.path, f.content])); return Promise.all( actual.map(async (actualFile) => { const expectedContent = expectedMap.get(actualFile.path); if (!expectedContent) { return actualFile; } if (!expectedContent.includes('…')) { return actualFile; } // Format expected content with placeholders to match actual formatting const formattedExpected = await formatWithPlaceholders(actualFile.path, expectedContent); // Attempt to merge const mergedContent = applyEllipses(actualFile.content, formattedExpected); return { path: actualFile.path, content: mergedContent, }; }), ); } /** * Format content that might contain ellipses by using placeholders */ async function formatWithPlaceholders(filePath: string, content: string): Promise { if (!content.includes('…')) { return formatContent(filePath, content); } // Try using a comment placeholder first (valid in most places) const commentPlaceholder = '/*__ELLIPSIS__*/'; // Prettier adds spaces around comments: /* __ELLIPSIS__ */ const commentRegex = /\/\*\s*__ELLIPSIS__\s*\*\//g; try { const withComments = content.replace(/…/g, commentPlaceholder); const formatted = await formatContent(filePath, withComments); return formatted.replace(commentRegex, '…'); } catch (e) { // Return original on error } return content; } function applyEllipses(actual: string, expected: string): string { const changes = diffLines(expected, actual); let result = ''; for (let i = 0; i < changes.length; i++) { const change = changes[i]; if (change.removed) { if (change.value.includes('…')) { // Keep the ellipsis from expected result += change.value; // Skip any corresponding added block from actual const nextChange = changes[i + 1]; if (nextChange && nextChange.added) { i++; } } else { // Normal removal. If followed by an addition, it's a replacement. const nextChange = changes[i + 1]; if (nextChange && nextChange.added) { result += nextChange.value; i++; } } } else if (change.added) { // Pure addition result += change.value; } else { // Unchanged result += change.value; } } return result; } /** * Format virtual file content using Prettier */ async function formatContent(filePath: string, content: string): Promise { try { const parser = filePath.endsWith('.ts') ? 'typescript' : filePath.endsWith('.json') ? 'json' : undefined; if (!parser) return content; return await format(content, { parser, singleQuote: true, trailingComma: 'all', printWidth: 100, quoteProps: 'preserve', }); } catch (e) { console.warn(`Failed to format ${filePath}:`, e); return content; } } /** * Run the test pipeline with virtual files */ export async function runPipeline( sourceFiles: TestFile[], options: { optimize: boolean; sidecar?: boolean; templateParseOptions?: Record; errors?: string[]; format?: boolean; complianceMode?: boolean; }, ): Promise { const maybeFormat = (p: string, content: string): Promise => options.format === false ? Promise.resolve(content) : formatContent(p, content); const nodeModulesPath = resolveFixtureNodeModules(); const virtualFiles: Record = {}; for (const f of sourceFiles) { virtualFiles[f.path] = f.content; } const tsconfigFile = sourceFiles.find((f) => f.path.endsWith('tsconfig.json')); if (!tsconfigFile) { throw new Error('tsconfig.json must be provided in source files'); } let tsconfigOptions: any = {}; let compilerOptions: any = {}; let bazelOptions: any = {}; try { const parsed = JSON.parse(tsconfigFile.content); tsconfigOptions = parsed.angularCompilerOptions ?? {}; compilerOptions = parsed.compilerOptions ?? {}; bazelOptions = parsed.bazelOptions || parsed.bazelOpts || {}; } catch (e) { // Ignore } const isClosureCompilerEnabled = tsconfigOptions.annotateForClosureCompiler === true || bazelOptions.annotateForClosureCompiler === true || bazelOptions.tsickle === true; const emitDeclarationOnly = compilerOptions.emitDeclarationOnly === true && tsconfigOptions._experimentalAllowEmitDeclarationOnly === true; const onlyPublishPublicTypingsForNgModules = tsconfigOptions.onlyPublishPublicTypingsForNgModules === true; const forbidOrphanComponents = tsconfigOptions.forbidOrphanComponents === true; const supportJitMode = tsconfigOptions.supportJitMode !== false; const ngtscOptions = { preserveWhitespaces: tsconfigOptions.preserveWhitespaces, supportTestBed: tsconfigOptions.supportTestBed, enableI18nLegacyMessageIdFormat: tsconfigOptions.enableI18nLegacyMessageIdFormat ?? false, i18nUseExternalIds: tsconfigOptions.i18nUseExternalIds, i18nNormalizeLineEndingsInICUs: tsconfigOptions.i18nNormalizeLineEndingsInICUs, i18nPreserveWhitespaceForLegacyExtraction: tsconfigOptions.i18nPreserveWhitespaceForLegacyExtraction, }; const workspaceName = tsconfigOptions.workspaceName; const rootDir = path.posix.dirname(tsconfigFile.path); const rootDirs = (compilerOptions.rootDirs as string[] | undefined)?.map((d) => { const norm = d.replace(/\\/g, '/'); return norm.startsWith('/') || /^[a-zA-Z]:\//.test(norm) ? norm : path.posix.join(rootDir, norm); }); const useHostForImportGeneration = tsconfigOptions._useHostForImportGeneration || tsconfigOptions._useHostForImportAndAliasGeneration ? true : undefined; const outputs: TestFile[] = []; const normalizePathKey = (p: string) => p.replace(/\\/g, '/').toLowerCase(); const virtualFileMap = new Map(); for (const f of sourceFiles) { const key = normalizePathKey(f.path); virtualFileMap.set(key, f.content); const withoutSlash = key.replace(/^\//, ''); virtualFileMap.set(withoutSlash, f.content); virtualFileMap.set('/' + withoutSlash, f.content); } const tcbConfig = buildTypeCheckingConfig({ strictTemplates: true, checkTypeOfDomBindings: false, ...tsconfigOptions, useContextGenericType: tsconfigOptions.strictContextGenerics ?? tsconfigOptions.useContextGenericType, }); let analyzer; if (options.sidecar) { const {SidecarAnalyzer} = await import('../api.js'); const sidecar = new SidecarAnalyzer(resolveSidecarBinary()); await sidecar.initialize(tsconfigFile.path, options.optimize, virtualFiles, nodeModulesPath, { workspaceName, rootDirs, usePoisonedData: tcbConfig.usePoisonedData, useHostForImportGeneration, }); analyzer = sidecar; } else { const wasmBinding = resolveWasmBinding(); analyzer = await createAnalyzer(tsconfigFile.path, { backend: 'wasm', wasmBinding, virtualFiles, optimize: options.optimize, nodeModulesPathOverride: nodeModulesPath, workspaceName, rootDirs, usePoisonedData: tcbConfig.usePoisonedData, useHostForImportGeneration, }); } const optionDiagnostics = verifyCompilerOptions({ ...tsconfigOptions, emitDeclarationOnly: compilerOptions.emitDeclarationOnly, }); const compiler = new HybridCompiler(analyzer, { optimize: options.optimize, tcbConfig, optionDiagnostics, templateParseOptions: { ...options.templateParseOptions, }, legacyOptionalChaining: tsconfigOptions.legacyOptionalChaining, isClosureCompilerEnabled, onlyExplicitDeferDependencyImports: tsconfigOptions.onlyExplicitDeferDependencyImports, enableTemplateSourceLocations: tsconfigOptions.enableTemplateSourceLocations, emitDeclarationOnly, onlyPublishPublicTypingsForNgModules, forbidOrphanComponents, supportJitMode, ...ngtscOptions, externalRuntimeStyles: tsconfigOptions.externalRuntimeStyles ?? options.templateParseOptions?.['externalRuntimeStyles'], rootDir, complianceMode: options.complianceMode, workspaceName, rootDirs, }); let processedFiles = 0; const iterator = options.optimize && !emitDeclarationOnly ? compiler.analyzeOptimized() : compiler.analyze(); for await (const chunk of iterator) { const chunkContext = await compiler.prepareChunk(chunk); for (const result of chunk.files) { const {filePath, classes} = result; if (classes.length > 0) { processedFiles++; } const content = virtualFileMap.get(normalizePathKey(filePath)); if (!content) continue; try { const processed = await compiler.processFile(filePath, content, result, chunkContext); const emitted = processed.magicString.toString(); const outPath = '/out/' + filePath.replace(/\\/g, '/').replace(/^\//, ''); if (emitted !== content) { outputs.push({ path: outPath, content: await maybeFormat(outPath, emitted), }); } // Generate TCB file if in optimize mode if (processed.tcb) { const tcbPath = outPath.replace(/\.ts$/, '.ngtypecheck.ts'); let tcbContent = processed.tcb.code; if (processed.tcb.diagnostics && processed.tcb.diagnostics.length > 0) { tcbContent += `\n/* Diagnostics:\n`; for (const diag of processed.tcb.diagnostics) { tcbContent += ` - (${diag.start}, ${diag.end}) ${diag.message}\n`; } tcbContent += `*/\n`; } outputs.push({ path: tcbPath, content: await maybeFormat(tcbPath, tcbContent), }); } } catch (e: any) { if (options.errors) { options.errors.push(e.message || String(e)); } else { throw e; } } } } if (outputs.length === 0 && processedFiles > 0) { if (!options.errors || options.errors.length === 0) { throw new Error( 'Pipeline produced no outputs for files with classes. This usually indicates a compilation or processing error in the source.', ); } } const diagPath = getDiagnosticPath(tsconfigFile.path); const diagContent = serializeDiagnostics( tsconfigFile.path, compiler.getDiagnostics(), compiler.optionDiagnostics, ); const serializedDiagnostics = JSON.parse(diagContent) as { diagnostics: unknown[]; optionDiagnostics?: unknown[]; }; if ( serializedDiagnostics.diagnostics.length > 0 || (serializedDiagnostics.optionDiagnostics?.length ?? 0) > 0 ) { outputs.push({ path: diagPath, content: await formatContent(diagPath, diagContent), }); } compiler.analyzer.close(); return outputs; } function escapeRegExp(str: string): string { return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } export function checkMatch(actual: string, expected: string): boolean { if (!expected.includes('…')) { return normalizeWhitespace(actual) === normalizeWhitespace(expected); } const normalizedActual = normalizeWhitespace(actual); const normalizedExpected = normalizeWhitespace(expected); const parts = normalizedExpected.split('…'); const escapedParts = parts.map((part) => escapeRegExp(part).replace(/\s+/g, '\\s*')); const regexStr = '^\\s*' + escapedParts.join('[\\s\\S]*?') + '\\s*$'; const regex = new RegExp(regexStr); return regex.test(normalizedActual); } /** * Compare actual outputs with expected outputs */ export function compareOutputs(actual: TestFile[], expected: TestFile[]): void { const actualMap = new Map(actual.map((f) => [f.path, f.content])); const expectedMap = new Map(expected.map((f) => [f.path, f.content])); for (const [filePath, expectedContent] of expectedMap) { const actualContent = actualMap.get(filePath); if (actualContent === undefined) { throw new Error(`Missing file: ${filePath}`); } const normalizedActual = normalizeWhitespace(actualContent); const normalizedExpected = normalizeWhitespace(expectedContent); if (!checkMatch(actualContent, expectedContent)) { const diff = createTwoFilesPatch( `${filePath} (expected)`, `${filePath} (actual)`, normalizedExpected, normalizedActual, '', '', {context: 3}, ); throw new Error(`Content mismatch for ${filePath}:\n\n${diff}`); } } for (const filePath of actualMap.keys()) { if (!expectedMap.has(filePath)) { throw new Error(`Unexpected file: ${filePath}`); } } } function normalizeWhitespace(s: string): string { let normalized = s .split('\n') .map((line) => line.trimEnd()) .join('\n') .trim(); // Normalize Prettier 3.8 vs 3.9 semicolon placement for trailing block comments on statements. // Only consider comments on the same line as the semicolon, otherwise we'd also capture // leading comments of the next statement. while (true) { const next = normalized.replace(/;[ \t]*(\/\*[\s\S]*?\*\/)/g, ' $1;'); if (next === normalized) break; normalized = next; } // Normalize return types on functions and arrow functions (: any) so differences between // compiler versions or golden snapshots do not cause spurious mismatches. normalized = normalized.replace(/\):\s*any\s*=>/g, ') =>'); normalized = normalized.replace(/\):\s*any\s*\{/g, ') {'); return normalized; }