|
| 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 os from 'node:os'; |
| 5 | + |
| 6 | +import type { IPhase } from '../../../api/CommandLineConfiguration'; |
| 7 | +import type { RushConfigurationProject } from '../../../api/RushConfigurationProject'; |
| 8 | +import type { IOperationSettings, RushProjectConfiguration } from '../../../api/RushProjectConfiguration'; |
| 9 | +import { |
| 10 | + type IExecuteOperationsContext, |
| 11 | + PhasedCommandHooks |
| 12 | +} from '../../../pluginFramework/PhasedCommandHooks'; |
| 13 | +import type { IOperationExecutionResult } from '../IOperationExecutionResult'; |
| 14 | +import { Operation } from '../Operation'; |
| 15 | +import { WeightedOperationPlugin } from '../WeightedOperationPlugin'; |
| 16 | +import { MockOperationRunner } from './MockOperationRunner'; |
| 17 | + |
| 18 | +const MOCK_PHASE: IPhase = { |
| 19 | + name: '_phase:test', |
| 20 | + allowWarningsOnSuccess: false, |
| 21 | + associatedParameters: new Set(), |
| 22 | + dependencies: { |
| 23 | + self: new Set(), |
| 24 | + upstream: new Set() |
| 25 | + }, |
| 26 | + isSynthetic: false, |
| 27 | + logFilenameIdentifier: '_phase_test', |
| 28 | + missingScriptBehavior: 'silent' |
| 29 | +}; |
| 30 | + |
| 31 | +function createProject(packageName: string): RushConfigurationProject { |
| 32 | + return { |
| 33 | + packageName |
| 34 | + } as RushConfigurationProject; |
| 35 | +} |
| 36 | + |
| 37 | +function createOperation(options: { |
| 38 | + project: RushConfigurationProject; |
| 39 | + settings?: IOperationSettings; |
| 40 | + isNoOp?: boolean; |
| 41 | +}): Operation { |
| 42 | + const { project, settings, isNoOp } = options; |
| 43 | + return new Operation({ |
| 44 | + phase: MOCK_PHASE, |
| 45 | + project, |
| 46 | + settings, |
| 47 | + runner: new MockOperationRunner(`${project.packageName} (${MOCK_PHASE.name})`, undefined, false, isNoOp), |
| 48 | + logFilenameIdentifier: `${project.packageName}_phase_test` |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +function createExecutionRecords(operation: Operation): Map<Operation, IOperationExecutionResult> { |
| 53 | + return new Map([ |
| 54 | + [ |
| 55 | + operation, |
| 56 | + { |
| 57 | + operation, |
| 58 | + runner: operation.runner |
| 59 | + } as unknown as IOperationExecutionResult |
| 60 | + ] |
| 61 | + ]); |
| 62 | +} |
| 63 | + |
| 64 | +function createContext( |
| 65 | + projectConfigurations: ReadonlyMap<RushConfigurationProject, RushProjectConfiguration>, |
| 66 | + parallelism: number = os.availableParallelism() |
| 67 | +): IExecuteOperationsContext { |
| 68 | + return { |
| 69 | + projectConfigurations, |
| 70 | + parallelism |
| 71 | + } as IExecuteOperationsContext; |
| 72 | +} |
| 73 | + |
| 74 | +async function applyWeightPluginAsync( |
| 75 | + operations: Map<Operation, IOperationExecutionResult>, |
| 76 | + context: IExecuteOperationsContext |
| 77 | +): Promise<void> { |
| 78 | + const hooks: PhasedCommandHooks = new PhasedCommandHooks(); |
| 79 | + new WeightedOperationPlugin().apply(hooks); |
| 80 | + await hooks.beforeExecuteOperations.promise(operations, context); |
| 81 | +} |
| 82 | + |
| 83 | +function mockAvailableParallelism(value: number): jest.SpyInstance<number, []> { |
| 84 | + return jest.spyOn(os, 'availableParallelism').mockReturnValue(value); |
| 85 | +} |
| 86 | + |
| 87 | +describe(WeightedOperationPlugin.name, () => { |
| 88 | + afterEach(() => { |
| 89 | + jest.restoreAllMocks(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('applies numeric weight from operation settings', async () => { |
| 93 | + const project: RushConfigurationProject = createProject('project-number'); |
| 94 | + const operation: Operation = createOperation({ |
| 95 | + project, |
| 96 | + settings: { |
| 97 | + operationName: MOCK_PHASE.name, |
| 98 | + weight: 7 |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + await applyWeightPluginAsync( |
| 103 | + createExecutionRecords(operation), |
| 104 | + createContext(new Map(), /* Set parallelism to ensure -p does not affect weight calculation */ 1) |
| 105 | + ); |
| 106 | + |
| 107 | + expect(operation.weight).toBe(7); |
| 108 | + }); |
| 109 | + |
| 110 | + it('converts percentage weight using available parallelism', async () => { |
| 111 | + mockAvailableParallelism(10); |
| 112 | + |
| 113 | + const project: RushConfigurationProject = createProject('project-percent'); |
| 114 | + const operation: Operation = createOperation({ |
| 115 | + project, |
| 116 | + settings: { |
| 117 | + operationName: MOCK_PHASE.name, |
| 118 | + weight: '25%' |
| 119 | + } as IOperationSettings |
| 120 | + }); |
| 121 | + |
| 122 | + await applyWeightPluginAsync(createExecutionRecords(operation), createContext(new Map())); |
| 123 | + |
| 124 | + expect(operation.weight).toBe(2); |
| 125 | + }); |
| 126 | + |
| 127 | + it('reads weight from rush-project configuration when operation settings are undefined', async () => { |
| 128 | + mockAvailableParallelism(8); |
| 129 | + |
| 130 | + const project: RushConfigurationProject = createProject('project-config'); |
| 131 | + const operation: Operation = createOperation({ project }); |
| 132 | + const projectConfiguration: RushProjectConfiguration = { |
| 133 | + operationSettingsByOperationName: new Map([ |
| 134 | + [ |
| 135 | + MOCK_PHASE.name, |
| 136 | + { |
| 137 | + operationName: MOCK_PHASE.name, |
| 138 | + weight: '50%' |
| 139 | + } as IOperationSettings |
| 140 | + ] |
| 141 | + ]) |
| 142 | + } as unknown as RushProjectConfiguration; |
| 143 | + |
| 144 | + await applyWeightPluginAsync( |
| 145 | + createExecutionRecords(operation), |
| 146 | + createContext(new Map([[project, projectConfiguration]])) |
| 147 | + ); |
| 148 | + |
| 149 | + expect(operation.weight).toBe(4); |
| 150 | + }); |
| 151 | + |
| 152 | + it('use ceiling when converting percentage weight to avoid zero weight', async () => { |
| 153 | + mockAvailableParallelism(16); |
| 154 | + |
| 155 | + const project: RushConfigurationProject = createProject('project-ceiling'); |
| 156 | + const operation: Operation = createOperation({ |
| 157 | + project, |
| 158 | + settings: { |
| 159 | + operationName: MOCK_PHASE.name, |
| 160 | + weight: '33.3333%' |
| 161 | + } as IOperationSettings |
| 162 | + }); |
| 163 | + |
| 164 | + await applyWeightPluginAsync(createExecutionRecords(operation), createContext(new Map())); |
| 165 | + |
| 166 | + expect(operation.weight).toBe(5); |
| 167 | + }); |
| 168 | + |
| 169 | + it('forces NO-OP operation weight to zero ignore weight settings', async () => { |
| 170 | + const project: RushConfigurationProject = createProject('project-no-op'); |
| 171 | + const operation: Operation = createOperation({ |
| 172 | + project, |
| 173 | + isNoOp: true, |
| 174 | + settings: { |
| 175 | + operationName: MOCK_PHASE.name, |
| 176 | + weight: 100 |
| 177 | + } |
| 178 | + }); |
| 179 | + |
| 180 | + await applyWeightPluginAsync(createExecutionRecords(operation), createContext(new Map())); |
| 181 | + |
| 182 | + expect(operation.weight).toBe(0); |
| 183 | + }); |
| 184 | + |
| 185 | + it('throws for invalid percentage weight format', async () => { |
| 186 | + mockAvailableParallelism(16); |
| 187 | + |
| 188 | + const project: RushConfigurationProject = createProject('project-invalid'); |
| 189 | + const operation: Operation = createOperation({ |
| 190 | + project, |
| 191 | + // @ts-expect-error Testing invalid input |
| 192 | + settings: { |
| 193 | + operationName: MOCK_PHASE.name, |
| 194 | + weight: '12.5a%' |
| 195 | + } as IOperationSettings |
| 196 | + }); |
| 197 | + |
| 198 | + await expect( |
| 199 | + applyWeightPluginAsync(createExecutionRecords(operation), createContext(new Map())) |
| 200 | + ).rejects.toThrow(/invalid weight: 12.5a%/i); |
| 201 | + }); |
| 202 | +}); |
0 commit comments