|
| 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 path from 'node:path'; |
| 5 | +import { JsonFile } from '@rushstack/node-core-library'; |
| 6 | + |
| 7 | +import { RushConfiguration } from '../../../api/RushConfiguration'; |
| 8 | +import { CommandLineConfiguration, type IPhasedCommandConfig } from '../../../api/CommandLineConfiguration'; |
| 9 | +import type { Operation } from '../Operation'; |
| 10 | +import type { ICommandLineJson } from '../../../api/CommandLineJson'; |
| 11 | +import { PhasedOperationPlugin } from '../PhasedOperationPlugin'; |
| 12 | +import { ShellOperationRunnerPlugin } from '../ShellOperationRunnerPlugin'; |
| 13 | +import { TrimRushEnvironmentVariablesPlugin } from '../TrimRushEnvironmentVariablesPlugin'; |
| 14 | +import { |
| 15 | + type ICreateOperationsContext, |
| 16 | + type IOperationGraphContext, |
| 17 | + PhasedCommandHooks |
| 18 | +} from '../../../pluginFramework/PhasedCommandHooks'; |
| 19 | +import type { IOperationGraph } from '../IOperationGraph'; |
| 20 | +import { OperationGraphHooks } from '../../../pluginFramework/OperationGraphHooks'; |
| 21 | +import type { IEnvironment } from '../../../utilities/Utilities'; |
| 22 | +import type { IOperationRunnerContext } from '../IOperationRunner'; |
| 23 | +import type { IOperationExecutionResult } from '../IOperationExecutionResult'; |
| 24 | + |
| 25 | +/** |
| 26 | + * Helper function to create a minimal mock record for testing the createEnvironmentForOperation hook |
| 27 | + */ |
| 28 | +function createMockRecord(operation: Operation): IOperationRunnerContext & IOperationExecutionResult { |
| 29 | + return { |
| 30 | + operation, |
| 31 | + environment: undefined |
| 32 | + } as IOperationRunnerContext & IOperationExecutionResult; |
| 33 | +} |
| 34 | + |
| 35 | +describe(TrimRushEnvironmentVariablesPlugin.name, () => { |
| 36 | + it('should remove RUSH_-prefixed environment variables while preserving others', async () => { |
| 37 | + const rushJsonFile: string = path.resolve(__dirname, `../../test/parameterIgnoringRepo/rush.json`); |
| 38 | + const commandLineJsonFile: string = path.resolve( |
| 39 | + __dirname, |
| 40 | + `../../test/parameterIgnoringRepo/common/config/rush/command-line.json` |
| 41 | + ); |
| 42 | + |
| 43 | + const rushConfiguration = RushConfiguration.loadFromConfigurationFile(rushJsonFile); |
| 44 | + const commandLineJson: ICommandLineJson = JsonFile.load(commandLineJsonFile); |
| 45 | + |
| 46 | + const commandLineConfiguration = new CommandLineConfiguration(commandLineJson); |
| 47 | + const buildCommand: IPhasedCommandConfig = commandLineConfiguration.commands.get( |
| 48 | + 'build' |
| 49 | + )! as IPhasedCommandConfig; |
| 50 | + |
| 51 | + const fakeCreateOperationsContext: Pick< |
| 52 | + ICreateOperationsContext, |
| 53 | + 'phaseSelection' | 'projectSelection' | 'projectConfigurations' | 'rushConfiguration' |
| 54 | + > = { |
| 55 | + phaseSelection: buildCommand.phases, |
| 56 | + projectSelection: new Set(rushConfiguration.projects), |
| 57 | + projectConfigurations: new Map(), |
| 58 | + rushConfiguration |
| 59 | + }; |
| 60 | + |
| 61 | + const hooks: PhasedCommandHooks = new PhasedCommandHooks(); |
| 62 | + |
| 63 | + // Apply plugins |
| 64 | + new PhasedOperationPlugin().apply(hooks); |
| 65 | + new ShellOperationRunnerPlugin().apply(hooks); |
| 66 | + new TrimRushEnvironmentVariablesPlugin().apply(hooks); |
| 67 | + |
| 68 | + const operations: Set<Operation> = await hooks.createOperationsAsync.promise( |
| 69 | + new Set(), |
| 70 | + fakeCreateOperationsContext as unknown as ICreateOperationsContext |
| 71 | + ); |
| 72 | + |
| 73 | + // Set up a mock graph and invoke onGraphCreatedAsync so the plugin registers its graph hooks |
| 74 | + const graphHooks: OperationGraphHooks = new OperationGraphHooks(); |
| 75 | + const fakeGraph: IOperationGraph = { hooks: graphHooks } as unknown as IOperationGraph; |
| 76 | + await hooks.onGraphCreatedAsync.promise( |
| 77 | + fakeGraph, |
| 78 | + fakeCreateOperationsContext as unknown as IOperationGraphContext |
| 79 | + ); |
| 80 | + |
| 81 | + const operation = Array.from(operations)[0]; |
| 82 | + expect(operation).toBeDefined(); |
| 83 | + |
| 84 | + const mockRecord = createMockRecord(operation); |
| 85 | + |
| 86 | + const initialEnvironment: IEnvironment = { |
| 87 | + ...process.env, |
| 88 | + RUSH_TEMP_FOLDER: 'some-temp-folder', |
| 89 | + Rush_Some_Mixed_Case_Var: 'should also be trimmed', |
| 90 | + RUSHSTACK_FILE_ERROR_BASE_FOLDER: 'should be preserved', |
| 91 | + PATH: process.env.PATH, |
| 92 | + SOME_OTHER_VAR: 'should be preserved' |
| 93 | + }; |
| 94 | + |
| 95 | + const env: IEnvironment = graphHooks.createEnvironmentForOperation.call(initialEnvironment, mockRecord); |
| 96 | + |
| 97 | + expect(env.RUSH_TEMP_FOLDER).toBeUndefined(); |
| 98 | + expect(env.Rush_Some_Mixed_Case_Var).toBeUndefined(); |
| 99 | + expect(env.RUSHSTACK_FILE_ERROR_BASE_FOLDER).toBe('should be preserved'); |
| 100 | + expect(env.SOME_OTHER_VAR).toBe('should be preserved'); |
| 101 | + }); |
| 102 | +}); |
0 commit comments