Repository navigation
Expand file tree
/
Copy pathRushReporterHelp.test.ts
More file actions
209 lines (203 loc) · 8.86 KB
/
Copy pathRushReporterHelp.test.ts
File metadata and controls
209 lines (203 loc) · 8.86 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
// 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 type { ICommandLineJson } from '@microsoft/rush-lib/lib/api/CommandLineJson';
import { EnvironmentConfiguration } from '@microsoft/rush-lib/lib/api/EnvironmentConfiguration';
import { RushCommandLineParser } from '@microsoft/rush-lib/lib/cli/RushCommandLineParser';
import { JsonFile } from '@rushstack/node-core-library';
import { launchRushFrontendAsync } from '../RushFrontend';
import { MinimalRushConfiguration } from '../MinimalRushConfiguration';
describe('reporter help forwarding', () => {
it.each([
{ optIn: false, command: undefined, verbose: undefined },
{ optIn: true, command: undefined, verbose: undefined },
{ optIn: false, command: 'build', verbose: undefined },
{ optIn: true, command: 'build', verbose: undefined },
{ optIn: false, command: 'list', verbose: '--verbose' },
{ optIn: true, command: 'list', verbose: '--verbose' },
{ optIn: true, command: 'build', verbose: '--verbose' },
{ optIn: true, command: 'list', verbose: '-v' }
])('forwards command-owned help flags to the real engine: %j', async ({ optIn, command, verbose }) => {
const repoPath: string = path.resolve(
__dirname,
'../../../../libraries/rush-lib/src/cli/test/basicAndRunBuildActionRepo'
);
const originalArgv: string[] = process.argv;
const originalEnv: NodeJS.ProcessEnv = { ...process.env };
const originalExitCode: typeof process.exitCode = process.exitCode;
const output: string[] = [];
const errors: string[] = [];
const stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation((text) => {
output.push(String(text));
return true;
});
const stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation((text) => {
errors.push(String(text));
return true;
});
const logSpy = jest.spyOn(console, 'log').mockImplementation((text) => output.push(String(text)));
const cwdSpy = jest.spyOn(process, 'cwd').mockReturnValue(repoPath);
process.argv = [
'node',
'rush',
...(command ? [command] : []),
...(optIn ? [] : ['--reporter=json']),
'--output=json://./help-events.jsonl',
'--log-level=debug',
...(verbose ? [verbose] : []),
'--help'
];
delete process.env.RUSH_REPORTER;
process.env.RUSH_LOG_LEVEL = 'debug';
try {
EnvironmentConfiguration.reset();
await launchRushFrontendAsync({
currentPackageVersion: '5.178.1',
rushVersionToLoad: undefined,
configuration: { useRushReporter: optIn } as MinimalRushConfiguration,
launchOptions: { isManaged: true },
currentRushLib: rushLib,
executeCurrentRush: (version, selectedRushLib, options) => {
void version;
void selectedRushLib;
expect(process.argv).toEqual([
'node',
'rush',
...(command ? [command] : []),
...(verbose && (verbose === '-v' || command === 'build') ? [verbose] : []),
'--help'
]);
expect(process.env.RUSH_LOG_LEVEL).toBeUndefined();
expect(options.reporter.operationStreamEnabled).toBe(false);
const parser: RushCommandLineParser = new RushCommandLineParser({
cwd: repoPath,
reporterCloseAsync: options.reporterCloseAsync
});
return parser.executeAsync().then((succeeded) => {
expect(succeeded).toBe(true);
});
}
});
expect(output.join('')).toContain(command ? `usage: rush ${command}` : 'usage: rush');
expect(errors).toEqual([]);
} finally {
process.argv = originalArgv;
process.env = originalEnv;
process.exitCode = originalExitCode;
EnvironmentConfiguration.reset();
stdoutSpy.mockRestore();
stderrSpy.mockRestore();
logSpy.mockRestore();
cwdSpy.mockRestore();
}
});
it.each([
{ command: 'custom-output', globalHelp: false, customValueControls: ['--output', '--log-level'] },
{ command: 'custom-output', globalHelp: true, customValueControls: ['--output', '--log-level'] },
{ command: 'build', globalHelp: false, customValueControls: ['--output', '--log-level'] },
{ command: 'custom-output', globalHelp: false, customValueControls: ['--output'] },
{ command: 'custom-output', globalHelp: false, customValueControls: ['--log-level'] }
])('preserves declared reporter-shaped parameters for opted-in help: %j', async (testCase) => {
const directory: string = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'rush-custom-help-'));
const repoPath: string = path.join(directory, 'repo');
await fs.promises.cp(
path.resolve(__dirname, '../../../../libraries/rush-lib/src/cli/test/basicAndRunBuildActionRepo'),
repoPath,
{ recursive: true }
);
const commandLinePath: string = path.join(repoPath, 'common/config/rush/command-line.json');
const commandLine: ICommandLineJson = JsonFile.load(commandLinePath);
commandLine.parameters = commandLine.parameters?.filter(
(parameter) =>
(parameter.longName !== '--output' && parameter.longName !== '--log-level') ||
testCase.customValueControls.includes(parameter.longName)
);
for (const parameter of commandLine.parameters ?? []) {
if (parameter.longName === '--output' || parameter.longName === '--log-level') {
parameter.associatedCommands?.push('build');
}
}
JsonFile.save(commandLine, commandLinePath);
const originalArgv: string[] = process.argv;
const originalEnv: NodeJS.ProcessEnv = { ...process.env };
const originalExitCode: typeof process.exitCode = process.exitCode;
const output: string[] = [];
const errors: string[] = [];
const stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation((text) => {
output.push(String(text));
return true;
});
const stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation((text) => {
errors.push(String(text));
return true;
});
const logSpy = jest.spyOn(console, 'log').mockImplementation((text) => output.push(String(text)));
const cwdSpy = jest.spyOn(process, 'cwd').mockReturnValue(repoPath);
process.argv = [
'node',
'rush',
...(testCase.globalHelp ? ['--help'] : []),
testCase.command,
'--output=json://./custom-events.jsonl',
'--log-level=debug',
'--verbose',
...(testCase.globalHelp ? [] : ['--help'])
];
const expectedArgv: string[] = process.argv.filter(
(argument) =>
(!argument.startsWith('--output=') || testCase.customValueControls.includes('--output')) &&
(!argument.startsWith('--log-level=') || testCase.customValueControls.includes('--log-level'))
);
delete process.env.RUSH_REPORTER;
delete process.env.RUSH_LOG_LEVEL;
try {
EnvironmentConfiguration.reset();
await launchRushFrontendAsync({
currentPackageVersion: '5.178.1',
rushVersionToLoad: undefined,
configuration: { useRushReporter: true } as MinimalRushConfiguration,
launchOptions: { isManaged: true },
currentRushLib: rushLib,
executeCurrentRush: (version, selectedRushLib, options) => {
void version;
void selectedRushLib;
const forwardedArgv: string[] = [...process.argv];
const parser: RushCommandLineParser = new RushCommandLineParser({
cwd: repoPath,
reporterCloseAsync: options.reporterCloseAsync
});
return parser.executeAsync().then((succeeded) => {
expect(succeeded).toBe(true);
expect(forwardedArgv).toEqual(expectedArgv);
});
}
});
expect(output.join('')).toContain(
testCase.globalHelp ? 'usage: rush' : `usage: rush ${testCase.command}`
);
expect(errors).toEqual([]);
expect(fs.existsSync(path.join(repoPath, 'custom-events.jsonl'))).toBe(false);
expect(fs.existsSync(path.join(repoPath, 'custom-output-args.json'))).toBe(false);
await fs.promises.writeFile(path.join(repoPath, 'rush.json'), '{ malformed rush.json');
logSpy.mockClear();
expect(MinimalRushConfiguration.loadFromDefaultLocation()).toBeUndefined();
expect(logSpy.mock.calls).toEqual([
[`Found configuration in ${path.join(repoPath, 'rush.json')}`],
['']
]);
} finally {
process.argv = originalArgv;
process.env = originalEnv;
process.exitCode = originalExitCode;
EnvironmentConfiguration.reset();
stdoutSpy.mockRestore();
stderrSpy.mockRestore();
logSpy.mockRestore();
cwdSpy.mockRestore();
await fs.promises.rm(directory, { recursive: true, force: true });
}
});
});