Skip to content

Commit 26d5132

Browse files
authored
Add more command patterns for pip trigger (#23273)
1 parent 51cf852 commit 26d5132

File tree

2 files changed

+46
-34
lines changed

2 files changed

+46
-34
lines changed

src/client/pythonEnvironments/creation/globalPipInTerminalTrigger.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ import { onDidStartTerminalShellExecution, showWarningMessage } from '../../comm
1616
import { sendTelemetryEvent } from '../../telemetry';
1717
import { EventName } from '../../telemetry/constants';
1818

19+
function checkCommand(command: string): boolean {
20+
const lower = command.toLowerCase();
21+
return (
22+
lower.startsWith('pip install') ||
23+
lower.startsWith('pip3 install') ||
24+
lower.startsWith('python -m pip install') ||
25+
lower.startsWith('python3 -m pip install')
26+
);
27+
}
28+
1929
export function registerTriggerForPipInTerminal(disposables: Disposable[]): void {
2030
if (!shouldPromptToCreateEnv() || !inExperiment(CreateEnvOnPipInstallTrigger.experiment)) {
2131
return;
@@ -39,7 +49,7 @@ export function registerTriggerForPipInTerminal(disposables: Disposable[]): void
3949
!createEnvironmentTriggered.get(workspaceFolder.uri.fsPath) &&
4050
(await isGlobalPythonSelected(workspaceFolder))
4151
) {
42-
if (e.execution.commandLine.isTrusted && e.execution.commandLine.value.startsWith('pip install')) {
52+
if (e.execution.commandLine.isTrusted && checkCommand(e.execution.commandLine.value)) {
4353
createEnvironmentTriggered.set(workspaceFolder.uri.fsPath, true);
4454
sendTelemetryEvent(EventName.ENVIRONMENT_TERMINAL_GLOBAL_PIP);
4555
const selection = await showWarningMessage(

src/test/pythonEnvironments/creation/globalPipInTerminalTrigger.unit.test.ts

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -230,43 +230,45 @@ suite('Global Pip in Terminal Trigger', () => {
230230
sinon.assert.notCalled(showWarningMessageStub);
231231
});
232232

233-
test('Should prompt to create environment if all conditions are met', async () => {
234-
shouldPromptToCreateEnvStub.returns(true);
235-
inExperimentStub.returns(true);
236-
isGlobalPythonSelectedStub.returns(true);
237-
showWarningMessageStub.resolves(CreateEnv.Trigger.createEnvironment);
238-
239-
const disposables: Disposable[] = [];
240-
registerTriggerForPipInTerminal(disposables);
241-
242-
await handler?.({
243-
terminal: ({} as unknown) as Terminal,
244-
shellIntegration: shellIntegration.object,
245-
execution: {
246-
cwd: workspace1.uri,
247-
commandLine: {
248-
isTrusted: true,
249-
value: 'pip install',
250-
confidence: 0,
233+
['pip install', 'pip3 install', 'python -m pip install', 'python3 -m pip install'].forEach((command) => {
234+
test(`Should prompt to create environment if all conditions are met: ${command}`, async () => {
235+
shouldPromptToCreateEnvStub.returns(true);
236+
inExperimentStub.returns(true);
237+
isGlobalPythonSelectedStub.returns(true);
238+
showWarningMessageStub.resolves(CreateEnv.Trigger.createEnvironment);
239+
240+
const disposables: Disposable[] = [];
241+
registerTriggerForPipInTerminal(disposables);
242+
243+
await handler?.({
244+
terminal: ({} as unknown) as Terminal,
245+
shellIntegration: shellIntegration.object,
246+
execution: {
247+
cwd: workspace1.uri,
248+
commandLine: {
249+
isTrusted: true,
250+
value: command,
251+
confidence: 0,
252+
},
253+
read: () =>
254+
(async function* () {
255+
yield Promise.resolve(command);
256+
})(),
251257
},
252-
read: () =>
253-
(async function* () {
254-
yield Promise.resolve('pip install');
255-
})(),
256-
},
257-
});
258+
});
258259

259-
assert.strictEqual(disposables.length, 1);
260-
sinon.assert.calledOnce(shouldPromptToCreateEnvStub);
261-
sinon.assert.calledOnce(inExperimentStub);
262-
sinon.assert.calledOnce(getWorkspaceFolderStub);
263-
sinon.assert.calledOnce(isGlobalPythonSelectedStub);
264-
sinon.assert.calledOnce(showWarningMessageStub);
260+
assert.strictEqual(disposables.length, 1);
261+
sinon.assert.calledOnce(shouldPromptToCreateEnvStub);
262+
sinon.assert.calledOnce(inExperimentStub);
263+
sinon.assert.calledOnce(getWorkspaceFolderStub);
264+
sinon.assert.calledOnce(isGlobalPythonSelectedStub);
265+
sinon.assert.calledOnce(showWarningMessageStub);
265266

266-
sinon.assert.calledOnce(executeCommandStub);
267-
sinon.assert.notCalled(disableCreateEnvironmentTriggerStub);
267+
sinon.assert.calledOnce(executeCommandStub);
268+
sinon.assert.notCalled(disableCreateEnvironmentTriggerStub);
268269

269-
shellIntegration.verify((s) => s.executeCommand(typemoq.It.isAnyString()), typemoq.Times.once());
270+
shellIntegration.verify((s) => s.executeCommand(typemoq.It.isAnyString()), typemoq.Times.once());
271+
});
270272
});
271273

272274
test("Should disable create environment trigger if user selects don't show again", async () => {

0 commit comments

Comments
 (0)