From 8f4694f0d88a42ceaf85b581356fb177c521bcd4 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Fri, 1 Nov 2019 20:26:06 -0400 Subject: [PATCH] fix(@angular/cli): skip project analytics prompt when using update The prompt will cause the workspace configuration file to be updated which can result in an unclean repository. Fixes #16012 --- packages/angular/cli/models/command-runner.ts | 13 +++-- .../e2e/tests/misc/ask-analytics-command.ts | 51 +++++++++++++++++++ ...-analytics.ts => ask-analytics-install.ts} | 0 3 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 tests/legacy-cli/e2e/tests/misc/ask-analytics-command.ts rename tests/legacy-cli/e2e/tests/misc/{ask-analytics.ts => ask-analytics-install.ts} (100%) diff --git a/packages/angular/cli/models/command-runner.ts b/packages/angular/cli/models/command-runner.ts index acc13742f42a..4aa7100dc916 100644 --- a/packages/angular/cli/models/command-runner.ts +++ b/packages/angular/cli/models/command-runner.ts @@ -64,14 +64,19 @@ export interface CommandMapOptions { * Create the analytics instance. * @private */ -async function _createAnalytics(workspace: boolean): Promise { +async function _createAnalytics(workspace: boolean, skipPrompt = false): Promise { let config = await getGlobalAnalytics(); // If in workspace and global analytics is enabled, defer to workspace level if (workspace && config) { + const skipAnalytics = + skipPrompt || + (process.env['NG_CLI_ANALYTICS'] && + (process.env['NG_CLI_ANALYTICS'].toLowerCase() === 'false' || + process.env['NG_CLI_ANALYTICS'] === '0')); // TODO: This should honor the `no-interactive` option. // It is currently not an `ng` option but rather only an option for specific commands. // The concept of `ng`-wide options are needed to cleanly handle this. - if (!(await hasWorkspaceAnalyticsConfiguration())) { + if (!skipAnalytics && !(await hasWorkspaceAnalyticsConfiguration())) { await promptProjectAnalytics(); } config = await getWorkspaceAnalytics(); @@ -231,7 +236,9 @@ export async function runCommand( return map; }); - const analytics = options.analytics || await _createAnalytics(!!workspace.configFile); + const analytics = + options.analytics || + (await _createAnalytics(!!workspace.configFile, description.name === 'update')); const context = { workspace, analytics }; const command = new description.impl(context, description, logger); diff --git a/tests/legacy-cli/e2e/tests/misc/ask-analytics-command.ts b/tests/legacy-cli/e2e/tests/misc/ask-analytics-command.ts new file mode 100644 index 000000000000..d571b38cb671 --- /dev/null +++ b/tests/legacy-cli/e2e/tests/misc/ask-analytics-command.ts @@ -0,0 +1,51 @@ +import { execWithEnv, killAllProcesses, waitForAnyProcessOutputToMatch } from '../../utils/process'; +import { expectToFail } from '../../utils/utils'; + +export default async function() { + try { + // Execute a command with TTY force enabled + const execution = execWithEnv('ng', ['version'], { + ...process.env, + NG_FORCE_TTY: '1', + NG_CLI_ANALYTICS: 'ci', + }); + + // Check if the prompt is shown + await waitForAnyProcessOutputToMatch(/Would you like to share anonymous usage data/); + } finally { + killAllProcesses(); + } + + try { + // Execute a command with TTY force enabled + const execution = execWithEnv('ng', ['version'], { + ...process.env, + NG_FORCE_TTY: '1', + NG_CLI_ANALYTICS: 'false', + }); + + // Check if the prompt is shown + await expectToFail(() => + waitForAnyProcessOutputToMatch(/Would you like to share anonymous usage data/, 5), + ); + } finally { + killAllProcesses(); + } + + // Should not show a prompt when using update + try { + // Execute a command with TTY force enabled + const execution = execWithEnv('ng', ['update'], { + ...process.env, + NG_FORCE_TTY: '1', + NG_CLI_ANALYTICS: 'ci', + }); + + // Check if the prompt is shown + await expectToFail(() => + waitForAnyProcessOutputToMatch(/Would you like to share anonymous usage data/, 5), + ); + } finally { + killAllProcesses(); + } +} diff --git a/tests/legacy-cli/e2e/tests/misc/ask-analytics.ts b/tests/legacy-cli/e2e/tests/misc/ask-analytics-install.ts similarity index 100% rename from tests/legacy-cli/e2e/tests/misc/ask-analytics.ts rename to tests/legacy-cli/e2e/tests/misc/ask-analytics-install.ts