Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Do not print Rush alerts when console output is restricted by \"--quiet\" or \"--json\", so that commands such as \"rush list --json\" write only JSON to stdout.",
"type": "patch"
}
]
}
4 changes: 3 additions & 1 deletion libraries/rush-lib/src/cli/RushCommandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,9 @@ export class RushCommandLineParser extends CommandLineParser {
try {
const { configuration: experiments } = this.rushConfiguration.experimentsConfiguration;

if (experiments.rushAlerts) {
// Alerts are written to stdout, so they must not be printed when console output is restricted
// (for example "--json" or "--quiet"), or they would corrupt machine-readable output.
if (experiments.rushAlerts && !this.#restrictConsoleOutput) {
// TODO: Fix this
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const actionName: string = (this as any)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import { RushCommandLineParser } from '../RushCommandLineParser';
import { EnvironmentConfiguration } from '../../api/EnvironmentConfiguration';
import { RushAlerts } from '../../utilities/RushAlerts';

describe('RushCommandLineParser alerts', () => {
const originalArgv: string[] = process.argv;
let originalExitCode: string | number | undefined;
let printAlertsAsync: jest.Mock<Promise<void>, []>;

beforeEach(() => {
originalExitCode = process.exitCode;
process.exitCode = undefined;
printAlertsAsync = jest.fn(async () => undefined);
jest
.spyOn(RushAlerts, 'loadFromConfigurationAsync')
.mockResolvedValue({ printAlertsAsync } as unknown as RushAlerts);
jest.spyOn(console, 'log').mockImplementation(() => undefined);
jest.spyOn(console, 'error').mockImplementation(() => undefined);
});

afterEach(() => {
process.exitCode = originalExitCode;
process.argv = originalArgv;
EnvironmentConfiguration.reset();
jest.restoreAllMocks();
});

async function executeAsync(args: string[]): Promise<void> {
process.argv = ['pretend-this-is-node.exe', 'pretend-this-is-rush', ...args];
const parser: RushCommandLineParser = new RushCommandLineParser({
cwd: `${__dirname}/rushAlertsRepo`,
reporterCloseAsync: async () => undefined
});
await expect(parser.executeAsync(args)).resolves.toBe(true);
}

it('prints alerts after a command when console output is not restricted', async () => {
await executeAsync(['list']);

expect(printAlertsAsync).toHaveBeenCalledTimes(1);
});

it.each(['list --json', '--quiet list', '-q list'])(
'does not print alerts for "rush %s"',
async (commandLine) => {
await executeAsync(commandLine.split(' '));

expect(RushAlerts.loadFromConfigurationAsync).not.toHaveBeenCalled();
expect(printAlertsAsync).not.toHaveBeenCalled();
}
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "a",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "rushAlerts": true }
10 changes: 10 additions & 0 deletions libraries/rush-lib/src/cli/test/rushAlertsRepo/rush.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"pnpmVersion": "4.5.0",
"rushVersion": "5.0.0",
"projects": [
{
"packageName": "a",
"projectFolder": "a"
}
]
}
Loading