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