Repository navigation
Expand file tree
/
Copy pathRushReporterControlOwnership.test.ts
More file actions
284 lines (267 loc) · 10.9 KB
/
Copy pathRushReporterControlOwnership.test.ts
File metadata and controls
284 lines (267 loc) · 10.9 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import * as rushLib from '@microsoft/rush-lib';
import { LockFile } from '@rushstack/node-core-library';
import { EnvironmentConfiguration } from '@microsoft/rush-lib/lib/api/EnvironmentConfiguration';
import { RushCommandLineParser } from '@microsoft/rush-lib/lib/cli/RushCommandLineParser';
import { launchRushFrontendAsync } from '../RushFrontend';
import {
initializeRushReporterHostAsync,
resolveRushReporterSelection,
stripReporterValueControls,
type IRushReporterSelection
} from '../RushReporterHost';
import type { MinimalRushConfiguration } from '../MinimalRushConfiguration';
describe('reporter command-line ownership', () => {
let folder: string;
let originalArgv: string[];
let originalExitCode: typeof process.exitCode;
let locks: jest.SpiedFunction<typeof LockFile.tryAcquire>;
beforeEach(async () => {
folder = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'rush-reporter-ownership-'));
await fs.promises.cp(
path.resolve(__dirname, '../../../../libraries/rush-lib/src/cli/test/basicAndRunBuildActionRepo'),
folder,
{ recursive: true }
);
originalArgv = process.argv;
originalExitCode = process.exitCode;
process.exitCode = undefined;
EnvironmentConfiguration.reset();
locks = jest.spyOn(LockFile, 'tryAcquire');
jest.spyOn(console, 'log').mockImplementation(() => undefined);
jest.spyOn(console, 'error').mockImplementation(() => undefined);
});
afterEach(async () => {
for (const result of locks.mock.results) {
if (result.type === 'return' && result.value && !result.value.isReleased) {
result.value.release();
}
}
process.argv = originalArgv;
process.exitCode = originalExitCode;
EnvironmentConfiguration.reset();
jest.restoreAllMocks();
await fs.promises.rm(folder, { recursive: true, force: true });
});
function select(argv: readonly string[], repositoryOptIn: boolean): IRushReporterSelection {
return resolveRushReporterSelection({
argv,
env: {},
cwd: folder,
commandName: 'rush',
repositoryOptIn,
stdout: { isTTY: false, write: () => undefined }
});
}
async function executeAsync(
argv: readonly string[],
repositoryOptIn: boolean
): Promise<{ selection: IRushReporterSelection; succeeded: boolean; forwarded: readonly string[] }> {
process.argv = ['node', 'rush', ...argv];
let selection: IRushReporterSelection | undefined;
let succeeded: boolean | undefined;
let forwarded: readonly string[] = [];
await launchRushFrontendAsync({
currentPackageVersion: rushLib.Rush.version,
rushVersionToLoad: undefined,
configuration: { useRushReporter: repositoryOptIn } as MinimalRushConfiguration,
launchOptions: { isManaged: false },
currentRushLib: rushLib,
initializeReporterHostAsync: async (options) => {
const initialized = await initializeRushReporterHostAsync({
...options,
argv: process.argv.slice(2),
env: {},
cwd: folder,
commandName: 'rush',
stdout: { isTTY: false, write: () => undefined },
includeDefaultFileReporter: false
});
selection = initialized.selection;
return initialized;
},
executeCurrentRush: (version, selectedRushLib, options) => {
void version;
void selectedRushLib;
forwarded = process.argv.slice(2);
const parser: RushCommandLineParser = new RushCommandLineParser({
cwd: folder,
reporterCloseAsync: options.reporterCloseAsync
});
return parser.executeAsync().then((result) => {
succeeded = result;
});
}
});
if (!selection || succeeded === undefined) {
throw new Error('Expected the real frontend and parser to execute.');
}
return { selection, succeeded, forwarded };
}
it.each([false, true])(
'runs native list with reporter --verbose (repository opt-in: %s)',
async (implicit) => {
const result = await executeAsync(
['list', ...(implicit ? [] : ['--reporter=json']), '--verbose'],
implicit
);
expect(result.succeeded).toBe(true);
expect(result.forwarded).toEqual(['list']);
expect(result.selection.logLevel).toBe('verbose');
}
);
it.each([false, true])(
'keeps native list help parseable with reporter --verbose (repository opt-in: %s)',
async (implicit) => {
const result = await executeAsync(
['list', ...(implicit ? [] : ['--reporter=json']), '--verbose', '--help'],
implicit
);
expect(result.succeeded).toBe(true);
expect(result.forwarded).toEqual(['list', '--help']);
expect(result.selection.enabled).toBe(false);
}
);
it('preserves action-owned --verbose and every -v meaning', () => {
for (const actionName of ['build', 'rebuild', 'check', 'custom-output']) {
const argv: string[] = [actionName, '--reporter=plaintext', '--verbose', '-v'];
const selection: IRushReporterSelection = select(argv, false);
expect(
stripReporterValueControls(
argv,
new Set(selection.reporterValueFlagsToStrip),
new Set(selection.reporterFlagsToStrip)
)
).toEqual([actionName, '--verbose', '-v']);
}
const argv: string[] = ['list', '--reporter=json', '-v', '--verbose', '--', '--verbose'];
const selection: IRushReporterSelection = select(argv, false);
expect(
stripReporterValueControls(
argv,
new Set(selection.reporterValueFlagsToStrip),
new Set(selection.reporterFlagsToStrip)
)
).toEqual(['list', '-v', '--', '--verbose']);
});
it('matches the native action parameter definitions instead of registering a global verbose option', () => {
const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: folder });
for (const action of parser.actions) {
if (action.actionName === 'tab-complete') continue;
const selection: IRushReporterSelection = select(
[action.actionName, '--reporter=json', '--verbose'],
false
);
const actionOwnsVerbose: boolean = action.parameters.some(
(parameter) => parameter.longName === '--verbose'
);
expect(selection.reporterFlagsToStrip ?? []).toEqual(actionOwnsVerbose ? [] : ['--verbose']);
const valueSelection: IRushReporterSelection = select(
[action.actionName, '--output=json://./events.jsonl', '--log-level=debug'],
true
);
expect(valueSelection.reporterValueFlagsToStrip).toEqual(
['--output', '--log-level'].filter(
(name) => !action.parameters.some((parameter) => parameter.longName === name)
)
);
}
expect(parser.parameters.some((parameter) => parameter.longName === '--verbose')).toBe(false);
});
it('parses and strips repository-level value controls before the native parser', async () => {
const result = await executeAsync(['list', '--output=json://./events.jsonl', '--log-level=debug'], true);
expect(result.succeeded).toBe(true);
expect(result.forwarded).toEqual(['list']);
expect(result.selection).toMatchObject({
logLevel: 'debug',
outputs: [{ reporter: 'json', target: path.join(folder, 'events.jsonl') }],
reporterValueFlagsToStrip: ['--output', '--log-level']
});
expect((await fs.promises.stat(path.join(folder, 'events.jsonl'))).isFile()).toBe(true);
});
it('preserves declared custom values even when they look like reporter controls', async () => {
const argv: string[] = [
'custom-output',
'--output=json://./custom.jsonl',
'--log-level=debug',
'--verbose'
];
const result = await executeAsync(argv, true);
expect(result.succeeded).toBe(true);
expect(result.forwarded).toEqual(argv);
expect(result.selection.outputs).toEqual([]);
expect(
JSON.parse(await fs.promises.readFile(path.join(folder, 'custom-output-args.json'), 'utf8'))
).toEqual(['--output', 'json://./custom.jsonl', '--log-level', 'debug', '--verbose']);
await expect(fs.promises.stat(path.join(folder, 'custom.jsonl'))).rejects.toMatchObject({
code: 'ENOENT'
});
});
it('claims an unowned value control without consuming a different command-owned control', async () => {
const configPath: string = path.join(folder, 'common/config/rush/command-line.json');
const config: { parameters: Array<{ longName: string }> } = JSON.parse(
await fs.promises.readFile(configPath, 'utf8')
);
config.parameters = config.parameters.filter(({ longName }) => longName !== '--log-level');
await fs.promises.writeFile(configPath, JSON.stringify(config));
const result = await executeAsync(
['custom-output', '--output=custom-artifact.zip', '--log-level=debug'],
true
);
expect(result.succeeded).toBe(true);
expect(result.selection).toMatchObject({
logLevel: 'debug',
outputs: [],
reporterValueFlagsToStrip: ['--log-level']
});
expect(
JSON.parse(await fs.promises.readFile(path.join(folder, 'custom-output-args.json'), 'utf8'))
).toEqual(['--output', 'custom-artifact.zip']);
});
it('does not claim unknown or plugin-resolved command controls', async () => {
const argv: string[] = [
'hidden-tool',
'--output=json://./hidden.jsonl',
'--log-level=debug',
'--verbose'
];
expect(select(argv, true)).toMatchObject({
outputs: [],
reporterControlsOwnedByFrontend: false,
reporterValueFlagsToStrip: []
});
await fs.promises.writeFile(
path.join(folder, 'common/config/rush/rush-plugins.json'),
JSON.stringify({
plugins: [{ packageName: '@example/plugin', pluginName: 'commands', autoinstallerName: 'plugins' }]
})
);
expect(select(['build', '--output=json://./plugin.jsonl', '--log-level=debug'], true)).toMatchObject({
outputs: [],
reporterControlsOwnedByFrontend: false
});
await fs.promises.writeFile(
path.join(folder, 'common/config/rush/rush-plugins.json'),
JSON.stringify({ plugins: [] })
);
expect(select(['build', '--log-level=debug'], true)).toMatchObject({
logLevel: 'debug',
reporterValueFlagsToStrip: ['--log-level']
});
});
it('keeps legacy inputs unchanged and rejects malformed owned values', () => {
expect(select(['list', '--verbose', '--output=json://./events.jsonl'], false)).toMatchObject({
enabled: false,
reporterControlsOwnedByFrontend: false
});
expect(() => select(['build', '--output'], true)).toThrow('--output requires a value');
expect(() => select(['build', '--log-level=unsupported'], true)).toThrow('Unsupported log level');
expect(() => select(['build', '--log-level=debug', '--quiet'], true)).toThrow(
'Contradictory reporter verbosity'
);
});
});