|
| 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 | +import { ConsoleTerminalProvider, Terminal } from '@rushstack/terminal'; |
| 7 | + |
| 8 | +import { RushConfiguration } from '../../../api/RushConfiguration'; |
| 9 | +import { CommandLineConfiguration, type IPhasedCommandConfig } from '../../../api/CommandLineConfiguration'; |
| 10 | +import type { Operation } from '../Operation'; |
| 11 | +import type { ICommandLineJson } from '../../../api/CommandLineJson'; |
| 12 | +import { PhasedOperationPlugin } from '../PhasedOperationPlugin'; |
| 13 | +import { ShellOperationRunnerPlugin } from '../ShellOperationRunnerPlugin'; |
| 14 | +import { |
| 15 | + IgnoredParametersPlugin, |
| 16 | + RUSHSTACK_CLI_IGNORED_PARAMETER_NAMES_ENV_VAR |
| 17 | +} from '../IgnoredParametersPlugin'; |
| 18 | +import { |
| 19 | + type ICreateOperationsContext, |
| 20 | + PhasedCommandHooks |
| 21 | +} from '../../../pluginFramework/PhasedCommandHooks'; |
| 22 | +import { RushProjectConfiguration } from '../../../api/RushProjectConfiguration'; |
| 23 | +import type { IEnvironment } from '../../../utilities/Utilities'; |
| 24 | +import type { IOperationRunnerContext } from '../IOperationRunner'; |
| 25 | +import type { IOperationExecutionResult } from '../IOperationExecutionResult'; |
| 26 | + |
| 27 | +/** |
| 28 | + * Helper function to create a minimal mock record for testing the createEnvironmentForOperation hook |
| 29 | + */ |
| 30 | +function createMockRecord(operation: Operation): IOperationRunnerContext & IOperationExecutionResult { |
| 31 | + return { |
| 32 | + operation, |
| 33 | + environment: undefined |
| 34 | + } as IOperationRunnerContext & IOperationExecutionResult; |
| 35 | +} |
| 36 | + |
| 37 | +describe(IgnoredParametersPlugin.name, () => { |
| 38 | + it('should set RUSHSTACK_OPERATION_IGNORED_PARAMETERS environment variable', async () => { |
| 39 | + const rushJsonFile: string = path.resolve(__dirname, `../../test/parameterIgnoringRepo/rush.json`); |
| 40 | + const commandLineJsonFile: string = path.resolve( |
| 41 | + __dirname, |
| 42 | + `../../test/parameterIgnoringRepo/common/config/rush/command-line.json` |
| 43 | + ); |
| 44 | + |
| 45 | + const rushConfiguration = RushConfiguration.loadFromConfigurationFile(rushJsonFile); |
| 46 | + const commandLineJson: ICommandLineJson = JsonFile.load(commandLineJsonFile); |
| 47 | + |
| 48 | + const commandLineConfiguration = new CommandLineConfiguration(commandLineJson); |
| 49 | + const buildCommand: IPhasedCommandConfig = commandLineConfiguration.commands.get( |
| 50 | + 'build' |
| 51 | + )! as IPhasedCommandConfig; |
| 52 | + |
| 53 | + // Load project configurations |
| 54 | + const terminalProvider: ConsoleTerminalProvider = new ConsoleTerminalProvider(); |
| 55 | + const terminal: Terminal = new Terminal(terminalProvider); |
| 56 | + |
| 57 | + const projectConfigurations = await RushProjectConfiguration.tryLoadForProjectsAsync( |
| 58 | + rushConfiguration.projects, |
| 59 | + terminal |
| 60 | + ); |
| 61 | + |
| 62 | + const fakeCreateOperationsContext: Pick< |
| 63 | + ICreateOperationsContext, |
| 64 | + | 'phaseOriginal' |
| 65 | + | 'phaseSelection' |
| 66 | + | 'projectSelection' |
| 67 | + | 'projectsInUnknownState' |
| 68 | + | 'projectConfigurations' |
| 69 | + | 'rushConfiguration' |
| 70 | + > = { |
| 71 | + phaseOriginal: buildCommand.phases, |
| 72 | + phaseSelection: buildCommand.phases, |
| 73 | + projectSelection: new Set(rushConfiguration.projects), |
| 74 | + projectsInUnknownState: new Set(rushConfiguration.projects), |
| 75 | + projectConfigurations, |
| 76 | + rushConfiguration |
| 77 | + }; |
| 78 | + |
| 79 | + const hooks: PhasedCommandHooks = new PhasedCommandHooks(); |
| 80 | + |
| 81 | + // Apply plugins |
| 82 | + new PhasedOperationPlugin().apply(hooks); |
| 83 | + new ShellOperationRunnerPlugin().apply(hooks); |
| 84 | + new IgnoredParametersPlugin().apply(hooks); |
| 85 | + |
| 86 | + const operations: Set<Operation> = await hooks.createOperations.promise( |
| 87 | + new Set(), |
| 88 | + fakeCreateOperationsContext as ICreateOperationsContext |
| 89 | + ); |
| 90 | + |
| 91 | + // Test project 'a' which has parameterNamesToIgnore: ["--production"] |
| 92 | + const operationA = Array.from(operations).find((op) => op.name === 'a'); |
| 93 | + expect(operationA).toBeDefined(); |
| 94 | + |
| 95 | + // Create a mock operation execution result with required fields |
| 96 | + const mockRecordA = createMockRecord(operationA!); |
| 97 | + |
| 98 | + // Call the hook to get the environment |
| 99 | + const envA: IEnvironment = hooks.createEnvironmentForOperation.call({ ...process.env }, mockRecordA); |
| 100 | + |
| 101 | + // Verify the environment variable is set correctly for project 'a' |
| 102 | + expect(envA[RUSHSTACK_CLI_IGNORED_PARAMETER_NAMES_ENV_VAR]).toBe('["--production"]'); |
| 103 | + |
| 104 | + // Test project 'b' which has parameterNamesToIgnore: ["--verbose", "--config", "--mode", "--tags"] |
| 105 | + const operationB = Array.from(operations).find((op) => op.name === 'b'); |
| 106 | + expect(operationB).toBeDefined(); |
| 107 | + |
| 108 | + const mockRecordB = createMockRecord(operationB!); |
| 109 | + |
| 110 | + const envB: IEnvironment = hooks.createEnvironmentForOperation.call({ ...process.env }, mockRecordB); |
| 111 | + |
| 112 | + // Verify the environment variable is set correctly for project 'b' |
| 113 | + expect(envB[RUSHSTACK_CLI_IGNORED_PARAMETER_NAMES_ENV_VAR]).toBe( |
| 114 | + '["--verbose","--config","--mode","--tags"]' |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should not set environment variable when parameterNamesToIgnore is not specified', async () => { |
| 119 | + const rushJsonFile: string = path.resolve(__dirname, `../../test/customShellCommandinBulkRepo/rush.json`); |
| 120 | + const commandLineJsonFile: string = path.resolve( |
| 121 | + __dirname, |
| 122 | + `../../test/customShellCommandinBulkRepo/common/config/rush/command-line.json` |
| 123 | + ); |
| 124 | + |
| 125 | + const rushConfiguration = RushConfiguration.loadFromConfigurationFile(rushJsonFile); |
| 126 | + const commandLineJson: ICommandLineJson = JsonFile.load(commandLineJsonFile); |
| 127 | + |
| 128 | + const commandLineConfiguration = new CommandLineConfiguration(commandLineJson); |
| 129 | + const echoCommand: IPhasedCommandConfig = commandLineConfiguration.commands.get( |
| 130 | + 'echo' |
| 131 | + )! as IPhasedCommandConfig; |
| 132 | + |
| 133 | + const fakeCreateOperationsContext: Pick< |
| 134 | + ICreateOperationsContext, |
| 135 | + | 'phaseOriginal' |
| 136 | + | 'phaseSelection' |
| 137 | + | 'projectSelection' |
| 138 | + | 'projectsInUnknownState' |
| 139 | + | 'projectConfigurations' |
| 140 | + | 'rushConfiguration' |
| 141 | + > = { |
| 142 | + phaseOriginal: echoCommand.phases, |
| 143 | + phaseSelection: echoCommand.phases, |
| 144 | + projectSelection: new Set(rushConfiguration.projects), |
| 145 | + projectsInUnknownState: new Set(rushConfiguration.projects), |
| 146 | + projectConfigurations: new Map(), |
| 147 | + rushConfiguration |
| 148 | + }; |
| 149 | + |
| 150 | + const hooks: PhasedCommandHooks = new PhasedCommandHooks(); |
| 151 | + |
| 152 | + // Apply plugins |
| 153 | + new PhasedOperationPlugin().apply(hooks); |
| 154 | + new ShellOperationRunnerPlugin().apply(hooks); |
| 155 | + new IgnoredParametersPlugin().apply(hooks); |
| 156 | + |
| 157 | + const operations: Set<Operation> = await hooks.createOperations.promise( |
| 158 | + new Set(), |
| 159 | + fakeCreateOperationsContext as ICreateOperationsContext |
| 160 | + ); |
| 161 | + |
| 162 | + // Get any operation |
| 163 | + const operation = Array.from(operations)[0]; |
| 164 | + expect(operation).toBeDefined(); |
| 165 | + |
| 166 | + const mockRecord = createMockRecord(operation); |
| 167 | + |
| 168 | + const env: IEnvironment = hooks.createEnvironmentForOperation.call({ ...process.env }, mockRecord); |
| 169 | + |
| 170 | + // Verify the environment variable is not set |
| 171 | + expect(env[RUSHSTACK_CLI_IGNORED_PARAMETER_NAMES_ENV_VAR]).toBeUndefined(); |
| 172 | + }); |
| 173 | +}); |
0 commit comments