Skip to content

Commit

Permalink
Do not show prompt for cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartik Raj committed Oct 3, 2023
1 parent 30d2882 commit 3be4172
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
13 changes: 10 additions & 3 deletions src/client/terminals/envCollectionActivation/deactivatePrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

import { inject, injectable } from 'inversify';
import { Uri } from 'vscode';
import { IApplicationShell } from '../../common/application/types';
import { IApplicationEnvironment, IApplicationShell } from '../../common/application/types';
import { IBrowserService, IDisposableRegistry, IExperimentService, IPersistentStateFactory } from '../../common/types';
import { Common, Interpreters } from '../../common/utils/localize';
import { IExtensionSingleActivationService } from '../../activation/types';
import { inTerminalEnvVarExperiment } from '../../common/experiments/helpers';
import { IInterpreterService } from '../../interpreter/contracts';
import { PythonEnvType } from '../../pythonEnvironments/base/info';
import { identifyShellFromShellPath } from '../../common/terminal/shellDetectors/baseShellDetector';
import { TerminalShellType } from '../../common/terminal/types';

export const terminalDeactivationPromptKey = 'TERMINAL_DEACTIVATION_PROMPT_KEY';

Expand All @@ -23,6 +25,7 @@ export class TerminalDeactivateLimitationPrompt implements IExtensionSingleActiv
@inject(IDisposableRegistry) private readonly disposableRegistry: IDisposableRegistry,
@inject(IInterpreterService) private readonly interpreterService: IInterpreterService,
@inject(IBrowserService) private readonly browserService: IBrowserService,
@inject(IApplicationEnvironment) private readonly appEnvironment: IApplicationEnvironment,
@inject(IExperimentService) private readonly experimentService: IExperimentService,
) {}

Expand All @@ -35,6 +38,10 @@ export class TerminalDeactivateLimitationPrompt implements IExtensionSingleActiv
if (!e.data.includes('deactivate')) {
return;
}
const shellType = identifyShellFromShellPath(this.appEnvironment.shell);
if (shellType === TerminalShellType.commandPrompt) {
return;
}
const { terminal } = e;
const cwd =
'cwd' in terminal.creationOptions && terminal.creationOptions.cwd
Expand All @@ -59,12 +66,12 @@ export class TerminalDeactivateLimitationPrompt implements IExtensionSingleActiv
return;
}
const prompts = [Common.seeInstructions, Common.doNotShowAgain];
const selection = await this.appShell.showInformationMessage(Interpreters.terminalDeactivatePrompt, ...prompts);
const selection = await this.appShell.showWarningMessage(Interpreters.terminalDeactivatePrompt, ...prompts);
if (!selection) {
return;
}
if (selection === prompts[0]) {
const url = `https://aka.ms/AA5rjx5`;
const url = `https://aka.ms/AAmx2ft`;
this.browserService.launch(url);
}
if (selection === prompts[1]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { mock, when, anything, instance, verify, reset } from 'ts-mockito';
import { EventEmitter, Terminal, TerminalDataWriteEvent, Uri } from 'vscode';
import { IApplicationShell } from '../../../client/common/application/types';
import { IApplicationEnvironment, IApplicationShell } from '../../../client/common/application/types';
import {
IBrowserService,
IExperimentService,
Expand All @@ -19,11 +19,13 @@ import { IInterpreterService } from '../../../client/interpreter/contracts';
import { PythonEnvironment } from '../../../client/pythonEnvironments/info';
import { TerminalDeactivateLimitationPrompt } from '../../../client/terminals/envCollectionActivation/deactivatePrompt';
import { PythonEnvType } from '../../../client/pythonEnvironments/base/info';
import { TerminalShellType } from '../../../client/common/terminal/types';

suite('Terminal Deactivation Limitation Prompt', () => {
let shell: IApplicationShell;
let experimentService: IExperimentService;
let persistentStateFactory: IPersistentStateFactory;
let appEnvironment: IApplicationEnvironment;
let deactivatePrompt: TerminalDeactivateLimitationPrompt;
let terminalWriteEvent: EventEmitter<TerminalDataWriteEvent>;
let notificationEnabled: IPersistentState<boolean>;
Expand All @@ -37,6 +39,8 @@ suite('Terminal Deactivation Limitation Prompt', () => {
interpreterService = mock<IInterpreterService>();
experimentService = mock<IExperimentService>();
persistentStateFactory = mock<IPersistentStateFactory>();
appEnvironment = mock<IApplicationEnvironment>();
when(appEnvironment.shell).thenReturn('bash');
browserService = mock<IBrowserService>();
notificationEnabled = mock<IPersistentState<boolean>>();
terminalWriteEvent = new EventEmitter<TerminalDataWriteEvent>();
Expand All @@ -51,6 +55,7 @@ suite('Terminal Deactivation Limitation Prompt', () => {
[],
instance(interpreterService),
instance(browserService),
instance(appEnvironment),
instance(experimentService),
);
});
Expand All @@ -66,13 +71,35 @@ suite('Terminal Deactivation Limitation Prompt', () => {
when(interpreterService.getActiveInterpreter(anything())).thenResolve(({
type: PythonEnvType.Virtual,
} as unknown) as PythonEnvironment);
when(shell.showInformationMessage(expectedMessage, ...prompts)).thenResolve(undefined);
when(shell.showWarningMessage(expectedMessage, ...prompts)).thenResolve(undefined);

await deactivatePrompt.activate();
terminalWriteEvent.fire({ data: 'Please deactivate me', terminal });
await sleep(1);

verify(shell.showInformationMessage(expectedMessage, ...prompts)).once();
verify(shell.showWarningMessage(expectedMessage, ...prompts)).once();
});

test('When using cmd, do not show notification for the same', async () => {
const resource = Uri.file('a');
const terminal = ({
creationOptions: {
cwd: resource,
},
} as unknown) as Terminal;
reset(appEnvironment);
when(appEnvironment.shell).thenReturn(TerminalShellType.commandPrompt);
when(notificationEnabled.value).thenReturn(true);
when(interpreterService.getActiveInterpreter(anything())).thenResolve(({
type: PythonEnvType.Virtual,
} as unknown) as PythonEnvironment);
when(shell.showWarningMessage(expectedMessage, ...prompts)).thenResolve(undefined);

await deactivatePrompt.activate();
terminalWriteEvent.fire({ data: 'Please deactivate me', terminal });
await sleep(1);

verify(shell.showWarningMessage(expectedMessage, ...prompts)).once();
});

test('When not in experiment, do not show notification for the same', async () => {
Expand All @@ -88,13 +115,13 @@ suite('Terminal Deactivation Limitation Prompt', () => {
when(interpreterService.getActiveInterpreter(anything())).thenResolve(({
type: PythonEnvType.Virtual,
} as unknown) as PythonEnvironment);
when(shell.showInformationMessage(expectedMessage, ...prompts)).thenResolve(undefined);
when(shell.showWarningMessage(expectedMessage, ...prompts)).thenResolve(undefined);

await deactivatePrompt.activate();
terminalWriteEvent.fire({ data: 'Please deactivate me', terminal });
await sleep(1);

verify(shell.showInformationMessage(expectedMessage, ...prompts)).never();
verify(shell.showWarningMessage(expectedMessage, ...prompts)).never();
});

test('Do not show notification if notification is disabled', async () => {
Expand All @@ -108,13 +135,13 @@ suite('Terminal Deactivation Limitation Prompt', () => {
when(interpreterService.getActiveInterpreter(anything())).thenResolve(({
type: PythonEnvType.Virtual,
} as unknown) as PythonEnvironment);
when(shell.showInformationMessage(expectedMessage, ...prompts)).thenResolve(undefined);
when(shell.showWarningMessage(expectedMessage, ...prompts)).thenResolve(undefined);

await deactivatePrompt.activate();
terminalWriteEvent.fire({ data: 'Please deactivate me', terminal });
await sleep(1);

verify(shell.showInformationMessage(expectedMessage, ...prompts)).never();
verify(shell.showWarningMessage(expectedMessage, ...prompts)).never();
});

test('Do not show notification when virtual env is not activated for terminal', async () => {
Expand All @@ -128,13 +155,13 @@ suite('Terminal Deactivation Limitation Prompt', () => {
when(interpreterService.getActiveInterpreter(anything())).thenResolve(({
type: PythonEnvType.Conda,
} as unknown) as PythonEnvironment);
when(shell.showInformationMessage(expectedMessage, ...prompts)).thenResolve(undefined);
when(shell.showWarningMessage(expectedMessage, ...prompts)).thenResolve(undefined);

await deactivatePrompt.activate();
terminalWriteEvent.fire({ data: 'Please deactivate me', terminal });
await sleep(1);

verify(shell.showInformationMessage(expectedMessage, ...prompts)).never();
verify(shell.showWarningMessage(expectedMessage, ...prompts)).never();
});

test("Disable notification if `Don't show again` is clicked", async () => {
Expand All @@ -148,9 +175,7 @@ suite('Terminal Deactivation Limitation Prompt', () => {
when(interpreterService.getActiveInterpreter(anything())).thenResolve(({
type: PythonEnvType.Virtual,
} as unknown) as PythonEnvironment);
when(shell.showInformationMessage(expectedMessage, ...prompts)).thenReturn(
Promise.resolve(Common.doNotShowAgain),
);
when(shell.showWarningMessage(expectedMessage, ...prompts)).thenReturn(Promise.resolve(Common.doNotShowAgain));

await deactivatePrompt.activate();
terminalWriteEvent.fire({ data: 'Please deactivate me', terminal });
Expand All @@ -170,15 +195,13 @@ suite('Terminal Deactivation Limitation Prompt', () => {
when(interpreterService.getActiveInterpreter(anything())).thenResolve(({
type: PythonEnvType.Virtual,
} as unknown) as PythonEnvironment);
when(shell.showInformationMessage(expectedMessage, ...prompts)).thenReturn(
Promise.resolve(Common.seeInstructions),
);
when(shell.showWarningMessage(expectedMessage, ...prompts)).thenReturn(Promise.resolve(Common.seeInstructions));

await deactivatePrompt.activate();
terminalWriteEvent.fire({ data: 'Please deactivate me', terminal });
await sleep(1);

verify(shell.showInformationMessage(expectedMessage, ...prompts)).once();
verify(shell.showWarningMessage(expectedMessage, ...prompts)).once();
verify(browserService.launch(anything())).once();
});

Expand All @@ -193,13 +216,13 @@ suite('Terminal Deactivation Limitation Prompt', () => {
when(interpreterService.getActiveInterpreter(anything())).thenResolve(({
type: PythonEnvType.Virtual,
} as unknown) as PythonEnvironment);
when(shell.showInformationMessage(expectedMessage, ...prompts)).thenResolve(undefined);
when(shell.showWarningMessage(expectedMessage, ...prompts)).thenResolve(undefined);

await deactivatePrompt.activate();
terminalWriteEvent.fire({ data: 'Please deactivate me', terminal });
await sleep(1);

verify(shell.showInformationMessage(expectedMessage, ...prompts)).once();
verify(shell.showWarningMessage(expectedMessage, ...prompts)).once();
verify(notificationEnabled.updateValue(false)).never();
verify(browserService.launch(anything())).never();
});
Expand Down

0 comments on commit 3be4172

Please sign in to comment.