Skip to content

Commit 3e3c0db

Browse files
authored
Pass interpreter to shouldShowPrompt directly (microsoft#17042)
1 parent 485d4b9 commit 3e3c0db

4 files changed

Lines changed: 12 additions & 33 deletions

File tree

src/client/interpreter/contracts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,6 @@ export type GetInterpreterOptions = { ignoreCache?: boolean; onSuggestion?: bool
181181

182182
export const IPython27SupportPrompt = Symbol('IPython27SupportPrompt');
183183
export interface IPython27SupportPrompt {
184-
shouldShowPrompt(resource?: Uri): Promise<boolean>;
184+
shouldShowPrompt(interpreter: PythonEnvironment): Promise<boolean>;
185185
showPrompt(): Promise<void>;
186186
}

src/client/interpreter/display/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class InterpreterDisplay implements IInterpreterDisplay {
9393
const interpreter = await this.interpreterService.getActiveInterpreter(workspaceFolder);
9494
this.currentlySelectedWorkspaceFolder = workspaceFolder;
9595
if (interpreter) {
96-
if (await this.python27SupportPrompt.shouldShowPrompt(workspaceFolder)) {
96+
if (await this.python27SupportPrompt.shouldShowPrompt(interpreter)) {
9797
this.python27SupportPrompt.showPrompt();
9898
}
9999

src/client/interpreter/display/python27Prompt.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
'use strict';
55

66
import { inject, injectable } from 'inversify';
7-
import { Uri } from 'vscode';
87
import { IApplicationShell } from '../../common/application/types';
98
import { IPersistentStateFactory } from '../../common/types';
109
import { Common, Python27Support } from '../../common/utils/localize';
11-
import { IInterpreterService, IPython27SupportPrompt } from '../contracts';
10+
import { IPython27SupportPrompt } from '../contracts';
1211
import { sendTelemetryEvent } from '../../telemetry';
1312
import { EventName } from '../../telemetry/constants';
13+
import { PythonEnvironment } from '../../pythonEnvironments/info';
1414

1515
const doNotShowPromptStateKey = 'MESSAGE_KEY_FOR_27_SUPPORT_PROMPT';
1616

@@ -21,11 +21,10 @@ export class Python27SupportPrompt implements IPython27SupportPrompt {
2121

2222
constructor(
2323
@inject(IApplicationShell) private appShell: IApplicationShell,
24-
@inject(IInterpreterService) private readonly interpreterService: IInterpreterService,
2524
@inject(IPersistentStateFactory) private persistentState: IPersistentStateFactory,
2625
) {}
2726

28-
public async shouldShowPrompt(resource?: Uri): Promise<boolean> {
27+
public async shouldShowPrompt(interpreter: PythonEnvironment): Promise<boolean> {
2928
// Check if "Do not show again" has been selected before.
3029
const doNotShowAgain = this.persistentState.createGlobalPersistentState<boolean>(
3130
doNotShowPromptStateKey,
@@ -36,10 +35,8 @@ export class Python27SupportPrompt implements IPython27SupportPrompt {
3635
return Promise.resolve(false);
3736
}
3837

39-
// Check if current env version is Python 2.7
40-
const currentInterpreter = await this.interpreterService.getActiveInterpreter(resource);
41-
42-
if (currentInterpreter && currentInterpreter.version?.major === 2 && currentInterpreter.version?.minor === 7) {
38+
// Check if current environment version is Python 2.7
39+
if (interpreter.version?.major === 2 && interpreter.version?.minor === 7) {
4340
return Promise.resolve(true);
4441
}
4542

src/test/interpreters/display/python27Prompt.unit.test.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,20 @@ import { IApplicationShell } from '../../../client/common/application/types';
1111
import { PersistentState, PersistentStateFactory } from '../../../client/common/persistentState';
1212
import { IPersistentStateFactory } from '../../../client/common/types';
1313
import { Python27Support, Common } from '../../../client/common/utils/localize';
14-
import { IInterpreterService } from '../../../client/interpreter/contracts';
1514
import { Python27SupportPrompt } from '../../../client/interpreter/display/python27Prompt';
16-
import { InterpreterService } from '../../../client/interpreter/interpreterService';
1715
import { PythonEnvironment } from '../../../client/pythonEnvironments/info';
1816
import * as Telemetry from '../../../client/telemetry';
1917
import { EventName } from '../../../client/telemetry/constants';
2018

2119
suite('Python 2.7 support prompt', () => {
2220
let applicationShell: IApplicationShell;
23-
let interpreterService: IInterpreterService;
2421
let persistentStateFactory: IPersistentStateFactory;
2522
let state: PersistentState<boolean>;
2623
let sendTelemetryEventStub: sinon.SinonStub;
2724
let telemetryEvents: { eventName: string; properties: Record<string, unknown> }[] = [];
2825

2926
setup(() => {
3027
applicationShell = mock(ApplicationShell);
31-
interpreterService = mock(InterpreterService);
3228
persistentStateFactory = mock(PersistentStateFactory);
3329
state = mock(PersistentState) as PersistentState<boolean>;
3430

@@ -56,38 +52,33 @@ suite('Python 2.7 support prompt', () => {
5652
type TestCaseType = {
5753
doNotShow: boolean;
5854
interpreter: { version: { major: number; minor: number } };
59-
getActiveInterpreterCalled: boolean;
6055
expected: boolean;
6156
};
6257

6358
const testCases: TestCaseType[] = [
6459
{
6560
doNotShow: false,
6661
interpreter: { version: { major: 2, minor: 7 } },
67-
getActiveInterpreterCalled: true,
6862
expected: true,
6963
},
7064
{
7165
doNotShow: false,
7266
interpreter: { version: { major: 3, minor: 9 } },
73-
getActiveInterpreterCalled: true,
7467
expected: false,
7568
},
7669
{
7770
doNotShow: true,
7871
interpreter: { version: { major: 2, minor: 7 } },
79-
getActiveInterpreterCalled: false,
8072
expected: false,
8173
},
8274
{
8375
doNotShow: true,
8476
interpreter: { version: { major: 3, minor: 9 } },
85-
getActiveInterpreterCalled: false,
8677
expected: false,
8778
},
8879
];
8980

90-
testCases.forEach(({ doNotShow, interpreter, getActiveInterpreterCalled, expected }) => {
81+
testCases.forEach(({ doNotShow, interpreter, expected }) => {
9182
const testTitle = `Should${
9283
!expected ? ' not' : ''
9384
} show prompt if do not show is ${doNotShow} and interpreter is ${interpreter.version.major}.${
@@ -96,35 +87,29 @@ suite('Python 2.7 support prompt', () => {
9687

9788
test(testTitle, async () => {
9889
when(state.value).thenReturn(doNotShow);
99-
when(interpreterService.getActiveInterpreter(anything())).thenResolve(interpreter as PythonEnvironment);
10090

10191
const python27SupportPrompt = new Python27SupportPrompt(
10292
instance(applicationShell),
103-
instance(interpreterService),
10493
instance(persistentStateFactory),
10594
);
10695

107-
const result = await python27SupportPrompt.shouldShowPrompt();
96+
const result = await python27SupportPrompt.shouldShowPrompt(interpreter as PythonEnvironment);
10897

10998
assert.strictEqual(result, expected);
110-
if (getActiveInterpreterCalled) {
111-
verify(interpreterService.getActiveInterpreter(anything())).once();
112-
} else {
113-
verify(interpreterService.getActiveInterpreter(anything())).never();
114-
}
11599
});
116100
});
117101

118102
test('Should not show prompt if it has been shown earlier in the session', async () => {
119103
const python27SupportPrompt = new Python27SupportPrompt(
120104
instance(applicationShell),
121-
instance(interpreterService),
122105
instance(persistentStateFactory),
123106
);
124107

125108
await python27SupportPrompt.showPrompt();
126109

127-
const result = await python27SupportPrompt.shouldShowPrompt();
110+
const result = await python27SupportPrompt.shouldShowPrompt({
111+
version: { major: 2, minor: 7 },
112+
} as PythonEnvironment);
128113

129114
assert.strictEqual(result, false);
130115
});
@@ -146,7 +131,6 @@ suite('Python 2.7 support prompt', () => {
146131

147132
const python27SupportPrompt = new Python27SupportPrompt(
148133
instance(appShell),
149-
instance(interpreterService),
150134
instance(persistentStateFactory),
151135
);
152136

@@ -164,7 +148,6 @@ suite('Python 2.7 support prompt', () => {
164148

165149
const python27SupportPrompt = new Python27SupportPrompt(
166150
instance(appShell),
167-
instance(interpreterService),
168151
instance(persistentStateFactory),
169152
);
170153

@@ -176,7 +159,6 @@ suite('Python 2.7 support prompt', () => {
176159
test('Telemetry event should be sent when prompt is shown', async () => {
177160
const python27SupportPrompt = new Python27SupportPrompt(
178161
instance(applicationShell),
179-
instance(interpreterService),
180162
instance(persistentStateFactory),
181163
);
182164

0 commit comments

Comments
 (0)