Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Get "select virtual environment for the workspace" prompt to show up regardless of pythonpath setting #7107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2019
Merged
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
1 change: 1 addition & 0 deletions news/1 Enhancements/5499.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Get "select virtual environment for the workspace" prompt to show up regardless of pythonpath setting
10 changes: 2 additions & 8 deletions src/client/interpreter/virtualEnvs/virtualEnvPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { inject, injectable, named } from 'inversify';
import { ConfigurationTarget, Disposable, Uri } from 'vscode';
import { IExtensionActivationService } from '../../activation/types';
import { IApplicationShell, IWorkspaceService } from '../../common/application/types';
import { IApplicationShell } from '../../common/application/types';
import { traceDecorators } from '../../common/logger';
import { IDisposableRegistry, IPersistentStateFactory } from '../../common/types';
import { sleep } from '../../common/utils/async';
Expand All @@ -20,7 +20,6 @@ export class VirtualEnvironmentPrompt implements IExtensionActivationService {
constructor(
@inject(IInterpreterWatcherBuilder) private readonly builder: IInterpreterWatcherBuilder,
@inject(IPersistentStateFactory) private readonly persistentStateFactory: IPersistentStateFactory,
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService,
@inject(IInterpreterHelper) private readonly helper: IInterpreterHelper,
@inject(IPythonPathUpdaterServiceManager) private readonly pythonPathUpdaterService: IPythonPathUpdaterServiceManager,
@inject(IInterpreterLocatorService) @named(WORKSPACE_VIRTUAL_ENV_SERVICE) private readonly locator: IInterpreterLocatorService,
Expand All @@ -40,7 +39,7 @@ export class VirtualEnvironmentPrompt implements IExtensionActivationService {
await sleep(1000);
const interpreters = await this.locator.getInterpreters(resource);
const interpreter = this.helper.getBestInterpreter(interpreters);
if (!interpreter || this.hasUserDefinedPythonPath(resource)) {
if (!interpreter) {
return;
}
await this.notifyUser(interpreter, resource);
Expand All @@ -63,9 +62,4 @@ export class VirtualEnvironmentPrompt implements IExtensionActivationService {
await notificationPromptEnabled.updateValue(false);
}
}
protected hasUserDefinedPythonPath(resource?: Uri) {
const settings = this.workspaceService.getConfiguration('python', resource)!.inspect<string>('pythonPath')!;
return ((settings.workspaceFolderValue && settings.workspaceFolderValue !== 'python') ||
(settings.workspaceValue && settings.workspaceValue !== 'python')) ? true : false;
}
}
99 changes: 2 additions & 97 deletions src/test/interpreters/virtualEnvs/virtualEnvPrompt.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@

'use strict';

import { expect } from 'chai';
import { anything, instance, mock, verify, when } from 'ts-mockito';
import * as TypeMoq from 'typemoq';
import { ConfigurationTarget, Disposable, Uri, WorkspaceConfiguration } from 'vscode';
import { ConfigurationTarget, Disposable, Uri } from 'vscode';
import { ApplicationShell } from '../../../client/common/application/applicationShell';
import { IApplicationShell, IWorkspaceService } from '../../../client/common/application/types';
import { WorkspaceService } from '../../../client/common/application/workspace';
import { IApplicationShell } from '../../../client/common/application/types';
import { PersistentStateFactory } from '../../../client/common/persistentState';
import { IPersistentState, IPersistentStateFactory } from '../../../client/common/types';
import { Common, InteractiveShiftEnterBanner } from '../../../client/common/utils/localize';
Expand All @@ -32,13 +30,8 @@ suite('Virtual Environment Prompt', () => {
public async notifyUser(interpreter: PythonInterpreter, resource: Uri): Promise<void> {
await super.notifyUser(interpreter, resource);
}
// tslint:disable-next-line:no-unnecessary-override
public hasUserDefinedPythonPath(resource: Uri) {
return super.hasUserDefinedPythonPath(resource);
}
}
let builder: IInterpreterWatcherBuilder;
let workspaceService: IWorkspaceService;
let persistentStateFactory: IPersistentStateFactory;
let helper: IInterpreterHelper;
let pythonPathUpdaterService: IPythonPathUpdaterServiceManager;
Expand All @@ -47,7 +40,6 @@ suite('Virtual Environment Prompt', () => {
let appShell: IApplicationShell;
let environmentPrompt: VirtualEnvironmentPromptTest;
setup(() => {
workspaceService = mock(WorkspaceService);
builder = mock(InterpreterWatcherBuilder);
persistentStateFactory = mock(PersistentStateFactory);
helper = mock(InterpreterHelper);
Expand All @@ -58,51 +50,23 @@ suite('Virtual Environment Prompt', () => {
environmentPrompt = new VirtualEnvironmentPromptTest(
instance(builder),
instance(persistentStateFactory),
instance(workspaceService),
instance(helper),
instance(pythonPathUpdaterService),
instance(locator),
[instance(disposable)],
instance(appShell)
);
});
test('User is not notified if python path is specified in settings.json', async () => {
const resource = Uri.file('a');
const interpreter1 = { path: 'path/to/interpreter1' };
const interpreter2 = { path: 'path/to/interpreter2' };
const settings = { workspaceFolderValue: 'path/to/interpreter1' };
const workspaceConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
// tslint:disable:no-any
when(locator.getInterpreters(resource)).thenResolve([interpreter1, interpreter2] as any);
when(helper.getBestInterpreter(anything())).thenReturn(interpreter2 as any);
when(workspaceService.getConfiguration('python', resource)).thenReturn(workspaceConfig.object);
workspaceConfig.setup(c => c.inspect<string>('pythonPath'))
.returns(() => settings as any)
.verifiable(TypeMoq.Times.once());

await environmentPrompt.handleNewEnvironment(resource);

verify(locator.getInterpreters(resource)).once();
verify(helper.getBestInterpreter(anything())).once();
verify(workspaceService.getConfiguration('python', resource)).once();
workspaceConfig.verifyAll();
});

test('User is notified if interpreter exists and only python path to global interpreter is specified in settings', async () => {
const resource = Uri.file('a');
const interpreter1 = { path: 'path/to/interpreter1' };
const interpreter2 = { path: 'path/to/interpreter2' };
const settings = { workspaceFolderValue: 'python', globalValue: 'path/to/globalInterpreter' };
const prompts = [InteractiveShiftEnterBanner.bannerLabelYes(), InteractiveShiftEnterBanner.bannerLabelNo(), Common.doNotShowAgain()];
const notificationPromptEnabled = TypeMoq.Mock.ofType<IPersistentState<boolean>>();
const workspaceConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
// tslint:disable:no-any
when(locator.getInterpreters(resource)).thenResolve([interpreter1, interpreter2] as any);
when(helper.getBestInterpreter(anything())).thenReturn(interpreter2 as any);
when(workspaceService.getConfiguration('python', resource)).thenReturn(workspaceConfig.object);
workspaceConfig.setup(c => c.inspect<string>('pythonPath'))
.returns(() => settings as any)
.verifiable(TypeMoq.Times.once());
when(persistentStateFactory.createWorkspacePersistentState(anything(), true)).thenReturn(notificationPromptEnabled.object);
notificationPromptEnabled.setup(n => n.value).returns(() => true);
when(appShell.showInformationMessage(anything(), ...prompts)).thenResolve();
Expand All @@ -111,8 +75,6 @@ suite('Virtual Environment Prompt', () => {

verify(locator.getInterpreters(resource)).once();
verify(helper.getBestInterpreter(anything())).once();
verify(workspaceService.getConfiguration('python', resource)).once();
workspaceConfig.verifyAll();
verify(persistentStateFactory.createWorkspacePersistentState(anything(), true)).once();
verify(appShell.showInformationMessage(anything(), ...prompts)).once();
});
Expand Down Expand Up @@ -184,61 +146,4 @@ suite('Virtual Environment Prompt', () => {
verify(persistentStateFactory.createWorkspacePersistentState(anything(), true)).once();
verify(appShell.showInformationMessage(anything(), ...prompts)).never();
});

const testsForHasUserDefinedPath =
[
{
testName: 'Returns false when workspace folder setting equals \'python\'',
settings: { workspaceFolderValue: 'python' },
expectedResult: false
},
{
testName: 'Returns true when interpreter is provided in workspace folder setting',
settings: { workspaceFolderValue: 'path/to/interpreter' },
expectedResult: true
},
{
testName: 'Returns false when workspace setting equals \'python\'',
settings: { workspaceValue: 'python' },
expectedResult: false
},
{
testName: 'Returns true when interpreter is provided in workspace setting',
settings: { workspaceValue: 'path/to/interpreter' },
expectedResult: true
},
{
testName: 'Returns false when global setting equals \'python\'',
settings: { globalValue: 'python' },
expectedResult: false
},
{
testName: 'Returns false when interpreter is provided in global setting',
settings: { globalValue: 'path/to/interpreter' },
expectedResult: false
},
{
testName: 'Returns false when no python setting is provided',
settings: {},
expectedResult: false
}
];

suite('Function hasUserDefinedPythonPath()', () => {
testsForHasUserDefinedPath.forEach(testParams => {
test(testParams.testName, async () => {
const resource = Uri.parse('a');
const workspaceConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
when(workspaceService.getConfiguration('python', resource)).thenReturn(workspaceConfig.object);
workspaceConfig.setup(c => c.inspect<string>('pythonPath'))
.returns(() => testParams.settings as any)
.verifiable(TypeMoq.Times.once());

expect(environmentPrompt.hasUserDefinedPythonPath(resource)).to.equal(testParams.expectedResult);

verify(workspaceService.getConfiguration('python', resource)).once();
workspaceConfig.verifyAll();
});
});
});
});