// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. import fs from 'node:fs'; import * as os from 'node:os'; import * as path from 'node:path'; import { execFileSync } from 'node:child_process'; import { once } from 'node:events'; import { Async, JsonFile, LockFile } from '@rushstack/node-core-library'; import type { IReporterEmitEventInput, IReporterEventSink, IRushDiagnostic } from '@rushstack/rush-reporter'; import { EnvironmentConfiguration } from '../../api/EnvironmentConfiguration'; import type { IRushConfigurationJson } from '../../api/RushConfiguration'; import { _getRushSessionDerivedExitStatus, _getRushSessionTelemetryAggregate, _isRushSessionErrorRepresented } from '../../pluginFramework/RushSession'; import { RushCommandLineParser } from '../RushCommandLineParser'; import { PhasedScriptAction } from '../scriptActions/PhasedScriptAction'; import { FlagFile } from '../../api/FlagFile'; import { RushConstants } from '../../logic/RushConstants'; // These integration tests run real commands and join their finalization in afterEach. jest.setTimeout(30000); class CapturingReporterSink implements IReporterEventSink { public readonly events: IReporterEmitEventInput[] = []; public emit(event: IReporterEmitEventInput): string { this.events.push(event); return `event-${this.events.length}`; } } function isCompletion(event: IReporterEmitEventInput): boolean { return ( event.type === 'commandResult' || event.type === 'commandCompleted' || event.type === 'sessionCompleted' ); } describe('RushCommandLineParser reporter lifecycle', () => { const temporaryFolders: string[] = []; let originalExitCode: string | number | undefined; let originalArgv: string[]; let stdoutSpy: jest.SpyInstance; let stderrSpy: jest.SpyInstance; let lockSpy: jest.SpiedFunction; let watchTest: Promise | undefined; let cancelWatchTest: (() => void) | undefined; function ownWatchTest(testAsync: (signal: AbortSignal) => Promise): Promise { const controller: AbortController = new AbortController(); cancelWatchTest = () => controller.abort(); watchTest = testAsync(controller.signal); return watchTest; } async function stopWatchTestAsync(): Promise { const pending: Promise | undefined = watchTest; cancelWatchTest?.(); watchTest = undefined; cancelWatchTest = undefined; await pending; } async function copyRepositoryAsync(): Promise { const directory: string = await fs.promises.mkdtemp( path.join(fs.realpathSync.native(os.tmpdir()), 'rush-reporter-lifecycle-') ); temporaryFolders.push(directory); const repoPath: string = path.join(directory, 'repo'); await fs.promises.cp(path.join(__dirname, 'basicAndRunBuildActionRepo'), repoPath, { recursive: true }); return repoPath; } beforeEach(() => { originalExitCode = process.exitCode; originalArgv = process.argv; process.exitCode = undefined; process.argv = ['node', 'rush', 'custom-output']; EnvironmentConfiguration.reset(); stdoutSpy = jest.spyOn(console, 'log').mockImplementation(() => undefined); stderrSpy = jest.spyOn(console, 'error').mockImplementation(() => undefined); lockSpy = jest.spyOn(LockFile, 'tryAcquire'); }); afterEach(async () => { try { // Jest timeouts do not cancel the test promise or run its finally block. await stopWatchTestAsync(); } finally { for (const result of lockSpy.mock.results) { if (result.type === 'return' && result.value && !result.value.isReleased) { result.value.release(); } } try { // On Windows, a process that the watch session started can keep the folder busy for a moment after it ends. await Promise.all( temporaryFolders .splice(0) .map((directory) => fs.promises.rm(directory, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }) ) ); } finally { process.exitCode = originalExitCode; process.argv = originalArgv; EnvironmentConfiguration.reset(); jest.restoreAllMocks(); } } }); it.each([false, true])( 'joins a cancelled watch test through setup and finalization (reject: %s)', async (reject) => { const [setup, releaseSetup] = Async.getSignal(); const [finalizing, markFinalizing] = Async.getSignal(); const [finalized, releaseFinalization] = Async.getSignal(); const failure: Error = new Error('watch test failed'); let signal: AbortSignal | undefined; let joined: boolean = false; const execution: Promise = ownWatchTest(async (testSignal) => { signal = testSignal; await setup; try { if (reject) { throw failure; } } finally { markFinalizing(); await finalized; } }); const stopping: Promise = stopWatchTestAsync().finally(() => { joined = true; }); const settled: Promise[]> = Promise.allSettled([execution, stopping]); try { expect(signal?.aborted).toBe(true); expect(joined).toBe(false); releaseSetup(); await finalizing; expect(joined).toBe(false); } finally { releaseSetup(); releaseFinalization(); await settled; } expect(joined).toBe(true); expect(await settled).toEqual( [0, 1].map(() => reject ? { status: 'rejected', reason: failure } : { status: 'fulfilled', value: undefined } ) ); } ); it.each([ { file: 'rush.json', withClose: false }, { file: 'rush.json', withClose: true }, { file: 'common/config/rush/command-line.json', withClose: false }, { file: 'common/config/rush/command-line.json', withClose: true } ])('reports invalid $file before fatal exit (close callback: $withClose)', async ({ file, withClose }) => { const repoPath: string = await copyRepositoryAsync(); await fs.promises.writeFile(path.join(repoPath, file), '{'); const visibleOutput: unknown[] = []; for (const reporting of [false, true]) { process.exitCode = undefined; EnvironmentConfiguration.reset(); stdoutSpy.mockClear(); stderrSpy.mockClear(); const sink: CapturingReporterSink = new CapturingReporterSink(); let eventsAtExit: readonly IReporterEmitEventInput[] = []; let eventsAtClose: readonly IReporterEmitEventInput[] = []; const exitSpy: jest.SpyInstance = jest.spyOn(process, 'exit').mockImplementation(() => { eventsAtExit = [...sink.events]; return undefined as never; }); const closeAsync: jest.Mock, []> = jest.fn(async () => { eventsAtClose = [...sink.events]; }); const flushAsync: jest.Mock, []> = jest.fn(async () => undefined); const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: repoPath, reporter: reporting ? { eventSink: sink, sessionId: 'initialization-failure', flushAsync } : undefined, reporterCloseAsync: withClose ? closeAsync : undefined }); await expect(parser.executeAsync(['custom-output'])).resolves.toBe(false); await new Promise((resolve) => setImmediate(resolve)); expect(process.exitCode).toBe(1); expect(exitSpy).toHaveBeenCalledTimes(1); expect(exitSpy).toHaveBeenCalledWith(1); expect(closeAsync).toHaveBeenCalledTimes(withClose ? 1 : 0); expect(flushAsync).toHaveBeenCalledTimes(reporting && !withClose ? 1 : 0); expect(sink.events.map(({ type }) => type)).toEqual( reporting ? ['sessionStarted', 'diagnosticEmitted', 'sessionCompleted'] : [] ); expect(eventsAtExit).toEqual(sink.events); if (withClose) { expect(eventsAtClose).toEqual(sink.events); } if (reporting) { expect(sink.events[1].payload).toMatchObject({ code: 'RUSH_COMMAND_FAILED', diagnosticId: expect.any(String) }); expect(sink.events[2].payload).toMatchObject({ exitCode: 1 }); expect(_getRushSessionDerivedExitStatus(parser.rushSession)).toEqual({ exitCode: 1, outcome: 'failed' }); } visibleOutput.push({ stdout: stdoutSpy.mock.calls.map((args) => [...args]), stderr: stderrSpy.mock.calls.map((args) => [...args]) }); exitSpy.mockRestore(); } expect(visibleOutput[1]).toEqual(visibleOutput[0]); }); it('binds the native reporter terminal before configuration failure and waits for its sink flush', async () => { const repoPath: string = await copyRepositoryAsync(); await fs.promises.writeFile(path.join(repoPath, 'rush.json'), '{'); const sink: CapturingReporterSink = new CapturingReporterSink(); const exitSpy: jest.SpyInstance = jest .spyOn(process, 'exit') .mockImplementation(() => undefined as never); let releaseFlush: (() => void) | undefined; const flushGate: Promise = new Promise((resolve) => { releaseFlush = resolve; }); const flushAsync: jest.Mock, []> = jest.fn(() => flushGate); const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: repoPath, reporter: { eventSink: sink, sessionId: 'native-initialization-failure', operationStreamEnabled: true, flushAsync } }); await expect(parser.executeAsync(['custom-output'])).resolves.toBe(false); const exitsBeforeFlush: number = exitSpy.mock.calls.length; const flushCalls: number = flushAsync.mock.calls.length; releaseFlush!(); await new Promise((resolve) => setImmediate(resolve)); expect(exitsBeforeFlush).toBe(0); expect(flushCalls).toBe(1); expect(sink.events[0].type).toBe('sessionStarted'); expect(sink.events[1].type).toBe('diagnosticEmitted'); expect(sink.events.slice(2, -1)).toContainEqual( expect.objectContaining({ type: 'messageEmitted', payload: expect.objectContaining({ severity: 'error', text: expect.stringContaining('rush.json') }) }) ); expect(sink.events.at(-1)).toMatchObject({ type: 'sessionCompleted', payload: { exitCode: 1 } }); expect(stdoutSpy).not.toHaveBeenCalled(); expect(stderrSpy).not.toHaveBeenCalled(); expect(exitSpy).toHaveBeenCalledTimes(1); expect(exitSpy).toHaveBeenCalledWith(1); }); it.each([false, true])('correlates a plugin initialization failure (frozen: %s)', async (frozen) => { const repoPath: string = await copyRepositoryAsync(); const sink: CapturingReporterSink = new CapturingReporterSink(); const closeAsync: jest.Mock, []> = jest.fn(async () => undefined); const exitSpy: jest.SpyInstance = jest .spyOn(process, 'exit') .mockImplementation(() => undefined as never); const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: repoPath, reporter: { eventSink: sink, sessionId: 'plugin-initialization-failure' }, reporterCloseAsync: closeAsync }); const error: Error = new Error('plugin initialization failed'); if (frozen) { Object.freeze(error); } jest.spyOn(parser.pluginManager, 'tryInitializeUnassociatedPluginsAsync').mockRejectedValue(error); await expect(parser.executeAsync(['custom-output'])).resolves.toBe(false); await new Promise((resolve) => setImmediate(resolve)); expect(sink.events.map(({ type }) => type)).toEqual([ 'sessionStarted', 'diagnosticEmitted', 'sessionCompleted' ]); expect(sink.events[1].scope?.commandName).toBeUndefined(); expect(_isRushSessionErrorRepresented(parser.rushSession, error)).toBe(true); expect(sink.events[2].payload).toMatchObject({ exitCode: 1 }); expect(closeAsync).toHaveBeenCalledTimes(1); expect(exitSpy).toHaveBeenCalledTimes(1); expect(exitSpy).toHaveBeenCalledWith(1); expect(stderrSpy.mock.calls.flat().join('\n')).toContain(error.message); expect(stderrSpy.mock.calls.flat().join('\n')).not.toContain('TypeError'); }); it.each([ { args: ['not-a-rush-command'], message: 'not-a-rush-command' }, { args: ['list', '--not-a-rush-option'], message: '--not-a-rush-option' } ])('reports one real pre-execution parse diagnostic for $args', async ({ args, message }) => { const repoPath: string = await copyRepositoryAsync(); const visibleOutput: unknown[] = []; const stdoutWriteSpy: jest.SpyInstance = jest.spyOn(process.stdout, 'write').mockReturnValue(true); const stderrWriteSpy: jest.SpyInstance = jest.spyOn(process.stderr, 'write').mockReturnValue(true); for (const reporting of [false, true]) { process.exitCode = undefined; EnvironmentConfiguration.reset(); jest.clearAllMocks(); const sink: CapturingReporterSink = new CapturingReporterSink(); const closeAsync: jest.Mock, []> = jest.fn(async () => undefined); const exitSpy: jest.SpyInstance = jest .spyOn(process, 'exit') .mockImplementation(() => undefined as never); const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: repoPath, reporter: reporting ? { eventSink: sink, sessionId: 'parse-failure' } : undefined, reporterCloseAsync: closeAsync }); await expect(parser.executeAsync(args)).resolves.toBe(false); expect(process.exitCode).toBe(2); expect(exitSpy).not.toHaveBeenCalled(); expect(closeAsync).toHaveBeenCalledTimes(1); const stderr: string = stderrSpy.mock.calls.flat().join('\n'); expect(stderr).toContain(message); expect(sink.events.map(({ type }) => type)).toEqual( reporting ? ['sessionStarted', 'diagnosticEmitted', 'sessionCompleted'] : [] ); if (reporting) { const diagnosticEvent: IReporterEmitEventInput = sink.events[1]; const diagnostic: IRushDiagnostic = diagnosticEvent.payload as IRushDiagnostic; expect(diagnostic.code).toBe('RUSH_COMMAND_FAILED'); expect(diagnosticEvent.scope?.commandName).toBeUndefined(); expect(diagnostic.parameters?.message).toEqual({ value: expect.stringContaining(message), privacy: 'local-sensitive' }); expect(stderr).toContain(diagnostic.parameters?.message.value); expect(_getRushSessionDerivedExitStatus(parser.rushSession)).toEqual({ exitCode: 1, outcome: 'failed' }); } visibleOutput.push({ stdout: stdoutSpy.mock.calls.map((call) => [...call]), stderr: stderrSpy.mock.calls.map((call) => [...call]), stdoutWrites: stdoutWriteSpy.mock.calls.map(([chunk]) => chunk), stderrWrites: stderrWriteSpy.mock.calls.map(([chunk]) => chunk) }); exitSpy.mockRestore(); } expect(visibleOutput[1]).toEqual(visibleOutput[0]); }); it('does not diagnose a successful help request as a parse failure', async () => { jest.spyOn(process.stdout, 'write').mockReturnValue(true); const sink: CapturingReporterSink = new CapturingReporterSink(); const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: await copyRepositoryAsync(), reporter: { eventSink: sink, sessionId: 'help' } }); await expect(parser.executeAsync(['--help'])).resolves.toBe(true); expect(sink.events.filter(({ type }) => type === 'diagnosticEmitted')).toEqual([]); expect(sink.events.at(-1)?.payload).toMatchObject({ exitCode: 0 }); }); it.each([false, true])('awaits a real delayed public telemetry hook (reject: %s)', async (reject) => { const visibleErrors: unknown[] = []; for (const reporting of [false, true]) { process.exitCode = undefined; EnvironmentConfiguration.reset(); stdoutSpy.mockClear(); stderrSpy.mockClear(); const repoPath: string = await copyRepositoryAsync(); const rushJsonPath: string = path.join(repoPath, 'rush.json'); const rushJson: IRushConfigurationJson = JsonFile.load(rushJsonPath); rushJson.telemetryEnabled = true; JsonFile.save(rushJson, rushJsonPath); const sink: CapturingReporterSink = new CapturingReporterSink(); const closeAsync: jest.Mock, []> = jest.fn(async () => undefined); const exitSpy: jest.SpyInstance = jest .spyOn(process, 'exit') .mockImplementation(() => undefined as never); const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: repoPath, reporter: reporting ? { eventSink: sink, sessionId: 'telemetry-finalization' } : undefined, reporterCloseAsync: reporting ? closeAsync : undefined }); let markHookStarted: (() => void) | undefined; const hookStarted: Promise = new Promise((resolve) => { markHookStarted = resolve; }); let releaseHook: (() => void) | undefined; const hookReleased: Promise = new Promise((resolve) => { releaseHook = resolve; }); const failure: Error = new Error('delayed telemetry flush failed'); const flushTelemetry: jest.Mock, []> = jest.fn(async () => { markHookStarted!(); await hookReleased; if (reject) { throw failure; } }); parser.rushSession.hooks.flushTelemetry.tapPromise('DelayedTelemetry', flushTelemetry); const execution: Promise = parser.executeAsync(['custom-output', '--reporter=junit']); await hookStarted; await new Promise((resolve) => setImmediate(resolve)); const prematureCompletions: IReporterEmitEventInput[] = sink.events.filter(isCompletion); releaseHook!(); const succeeded: boolean = await execution; expect(JsonFile.load(path.join(repoPath, 'custom-output-args.json'))).toEqual(['--reporter', 'junit']); expect(prematureCompletions).toEqual([]); expect(succeeded).toBe(!reject); expect(process.exitCode).toBe(reject ? 1 : 0); expect(exitSpy).not.toHaveBeenCalled(); expect(flushTelemetry).toHaveBeenCalledTimes(1); expect(closeAsync).toHaveBeenCalledTimes(reporting ? 1 : 0); if (reporting) { const completions: IReporterEmitEventInput[] = sink.events.filter(isCompletion); expect(completions.map(({ type }) => type)).toEqual([ 'commandResult', 'commandCompleted', 'sessionCompleted' ]); for (const event of completions) { expect(event.payload).toMatchObject({ exitCode: reject ? 1 : 0 }); } expect(completions[0].payload).toMatchObject({ succeeded: !reject }); expect(_getRushSessionDerivedExitStatus(parser.rushSession)).toEqual({ exitCode: reject ? 1 : 0, outcome: reject ? 'failed' : 'succeeded' }); expect(_isRushSessionErrorRepresented(parser.rushSession, failure)).toBe(reject); expect(sink.events.filter(({ type }) => type === 'diagnosticEmitted')).toHaveLength(reject ? 1 : 0); } else { expect(sink.events).toEqual([]); } visibleErrors.push(stderrSpy.mock.calls.map((args) => [...args])); exitSpy.mockRestore(); } expect(visibleErrors[1]).toEqual(visibleErrors[0]); }); it.each([ { reporting: false, useAlias: false }, { reporting: true, useAlias: false }, { reporting: false, useAlias: true }, { reporting: true, useAlias: true } ])( 'observes a real watch cancellation without changing legacy exit (shadow: $reporting, alias: $useAlias)', ({ reporting, useAlias }) => ownWatchTest(async (signal) => { const repoPath: string = await copyRepositoryAsync(); JsonFile.save( { commands: [ { commandKind: 'bulk', name: 'watch-test', summary: 'Watch cancellation fixture', watchForChanges: true, enableParallelism: false, disableBuildCache: true, safeForSimultaneousRushProcesses: true } ] }, path.join(repoPath, 'common/config/rush/command-line.json') ); JsonFile.save({}, path.join(repoPath, 'common/config/rush/npm-shrinkwrap.json')); for (const name of ['a', 'b']) { JsonFile.save( { name, version: '1.0.0', scripts: { 'watch-test': 'node watch-test.js' } }, path.join(repoPath, name, 'package.json') ); await fs.promises.writeFile( path.join(repoPath, name, 'watch-test.js'), 'process.stdout.write("watch child output\\n");\n' ); } // Capture successful fixture setup diagnostics; failed Git commands still throw with their stderr. execFileSync('git', ['init', '--quiet'], { cwd: repoPath, stdio: 'pipe' }); execFileSync('git', ['add', '.'], { cwd: repoPath, stdio: 'pipe' }); execFileSync( 'git', [ '-c', 'user.name=Rush test', '-c', 'user.email=rush-test@example.com', '-c', 'commit.gpgSign=false', 'commit', '--quiet', '-m', 'Initialize watch fixture' ], { cwd: repoPath, stdio: 'pipe' } ); const sink: CapturingReporterSink = new CapturingReporterSink(); const exitSpy: jest.SpyInstance = jest .spyOn(process, 'exit') .mockImplementation(() => undefined as never); const watchSpy: jest.SpyInstance = jest.spyOn(fs, 'watch'); const cwd: string = useAlias ? path.join(path.dirname(repoPath), 'repo-alias') : repoPath; if (useAlias) { await fs.promises.symlink( await fs.promises.realpath(repoPath), cwd, process.platform === 'win32' ? 'junction' : 'dir' ); } signal.throwIfAborted(); const parser: RushCommandLineParser = new RushCommandLineParser({ cwd, reporter: reporting ? { eventSink: sink, sessionId: 'real-watch-cancellation' } : undefined }); await new FlagFile( parser.rushConfiguration.defaultSubspace.getSubspaceTempFolderPath(), RushConstants.lastLinkFlagFilename, {} ).createAsync(); const action = parser.getAction('watch-test'); if (!(action instanceof PhasedScriptAction)) { throw new Error('Expected the production phased watch action'); } let reachedWatchIdle: boolean = false; let closedWatchers: Promise[] = []; const abort = (): void => action.sessionAbortController.abort(); parser.rushSession.hooks.runPhasedCommand.for('watch-test').tap('CancelRealWatch', (command) => { command.hooks.onGraphCreatedAsync.tap('CancelRealWatch', (graph) => { // Wait until the action and watcher have installed their abort listeners. graph.hooks.beforeExecuteIterationAsync.tap('CancelRealWatch', () => { signal.addEventListener('abort', abort, { once: true }); if (signal.aborted) { abort(); } }); graph.hooks.onIdle.tap({ name: 'CancelRealWatch', stage: Number.MAX_SAFE_INTEGER }, () => { reachedWatchIdle = true; closedWatchers = watchSpy.mock.results.map(({ value }) => once(value as fs.FSWatcher, 'close')); action.sessionAbortController.abort(); }); }); }); signal.throwIfAborted(); const execution: Promise = parser.executeAsync(['watch-test', '--verbose']); try { await expect(execution).resolves.toBe(true); await Promise.all(closedWatchers); expect(reachedWatchIdle).toBe(true); expect(parser.cwd).toBe(await fs.promises.realpath(repoPath)); expect(watchSpy.mock.calls.length).toBeGreaterThan(0); expect(action.sessionAbortController.signal.aborted).toBe(true); expect(exitSpy).not.toHaveBeenCalled(); expect(process.exitCode).toBe(0); expect(_getRushSessionDerivedExitStatus(parser.rushSession)).toEqual( reporting ? { exitCode: 1, outcome: 'cancelled' } : undefined ); if (reporting) { expect(sink.events.filter(isCompletion).map(({ payload }) => payload)).toEqual([ expect.objectContaining({ succeeded: true, exitCode: 0 }), expect.objectContaining({ exitCode: 0 }), expect.objectContaining({ exitCode: 0 }) ]); expect(_getRushSessionTelemetryAggregate(parser.rushSession)).toMatchObject({ result: 'succeeded', exitCode: 0, operationStatusCounts: { success: 2 } }); expect(sink.events.filter(({ type }) => type === 'diagnosticEmitted')).toEqual([]); } } finally { abort(); try { await execution; await Promise.all(closedWatchers); } finally { signal.removeEventListener('abort', abort); } } }) ); });