|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import child_process from 'node:child_process'; |
| 5 | +import type { |
| 6 | + HeftConfiguration, |
| 7 | + IHeftTaskPlugin, |
| 8 | + IHeftTaskRunHookOptions, |
| 9 | + IHeftTaskSession |
| 10 | +} from '@rushstack/heft'; |
| 11 | + |
| 12 | +/** @alpha */ |
| 13 | +export interface ICargoTestPluginOptions { |
| 14 | + release?: boolean | undefined; |
| 15 | +} |
| 16 | + |
| 17 | +const PLUGIN_NAME: 'cargo-test-plugin' = 'cargo-test-plugin'; |
| 18 | +const RELEASE_PARAMETER_LONG_NAME: '--release' = '--release'; |
| 19 | + |
| 20 | +/** |
| 21 | + * @internal |
| 22 | + */ |
| 23 | +export default class CargoTestPlugin implements IHeftTaskPlugin<ICargoTestPluginOptions> { |
| 24 | + private _releaseMode: boolean = false; |
| 25 | + |
| 26 | + public apply( |
| 27 | + taskSession: IHeftTaskSession, |
| 28 | + heftConfiguration: HeftConfiguration, |
| 29 | + options: ICargoTestPluginOptions = {} |
| 30 | + ): void { |
| 31 | + // Check for CLI parameter first, then fall back to options |
| 32 | + this._releaseMode = |
| 33 | + taskSession.parameters.getFlagParameter(RELEASE_PARAMETER_LONG_NAME).value || |
| 34 | + options.release || |
| 35 | + false; |
| 36 | + |
| 37 | + taskSession.hooks.run.tapPromise(PLUGIN_NAME, async (runOptions: IHeftTaskRunHookOptions) => { |
| 38 | + await this._runCargoTestAsync(taskSession, heftConfiguration, runOptions.abortSignal); |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + private async _runCargoTestAsync( |
| 43 | + taskSession: IHeftTaskSession, |
| 44 | + heftConfiguration: HeftConfiguration, |
| 45 | + abortSignal: AbortSignal |
| 46 | + ): Promise<void> { |
| 47 | + taskSession.logger.terminal.writeLine('Running Cargo tests...'); |
| 48 | + |
| 49 | + if (this._releaseMode) { |
| 50 | + taskSession.logger.terminal.writeLine('Using release mode'); |
| 51 | + } |
| 52 | + |
| 53 | + await this._executeCargoTest( |
| 54 | + heftConfiguration.buildFolderPath, |
| 55 | + this._releaseMode, |
| 56 | + abortSignal, |
| 57 | + taskSession |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + private async _executeCargoTest( |
| 62 | + buildFolderPath: string, |
| 63 | + releaseMode: boolean, |
| 64 | + abortSignal: AbortSignal, |
| 65 | + taskSession: IHeftTaskSession |
| 66 | + ): Promise<void> { |
| 67 | + return new Promise<void>((resolve, reject) => { |
| 68 | + const args: string[] = ['test']; |
| 69 | + |
| 70 | + if (releaseMode) { |
| 71 | + args.push('--release'); |
| 72 | + } |
| 73 | + |
| 74 | + taskSession.logger.terminal.writeDebugLine(`Executing: cargo ${args.join(' ')}`); |
| 75 | + |
| 76 | + const child = child_process.spawn('cargo', args, { |
| 77 | + stdio: ['inherit', 'inherit', 1], // Redirect stderr to stdout |
| 78 | + cwd: buildFolderPath, |
| 79 | + env: process.env |
| 80 | + }); |
| 81 | + |
| 82 | + // Handle abort signal |
| 83 | + const abortHandler = (): void => { |
| 84 | + child.kill('SIGTERM'); |
| 85 | + }; |
| 86 | + |
| 87 | + if (abortSignal.aborted) { |
| 88 | + child.kill('SIGTERM'); |
| 89 | + reject(new Error('Operation aborted')); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + abortSignal.addEventListener('abort', abortHandler); |
| 94 | + |
| 95 | + child.on('error', (error) => { |
| 96 | + abortSignal.removeEventListener('abort', abortHandler); |
| 97 | + reject(new Error(`Failed to spawn Cargo test process: ${error.message}`)); |
| 98 | + }); |
| 99 | + |
| 100 | + child.on('exit', (code, signal) => { |
| 101 | + abortSignal.removeEventListener('abort', abortHandler); |
| 102 | + if (signal === 'SIGTERM' && abortSignal.aborted) { |
| 103 | + reject(new Error('Operation aborted')); |
| 104 | + } else if (code === 0) { |
| 105 | + taskSession.logger.terminal.writeLine('Cargo tests completed successfully.'); |
| 106 | + resolve(); |
| 107 | + } else { |
| 108 | + reject(new Error(`Cargo test process exited with code ${code}`)); |
| 109 | + } |
| 110 | + }); |
| 111 | + }); |
| 112 | + } |
| 113 | +} |
0 commit comments