From ee7797828b0571e91a4a5da98a1e081e654fd33b Mon Sep 17 00:00:00 2001 From: Kim-Adeline Miguel Date: Tue, 20 Jul 2021 12:49:26 -0700 Subject: [PATCH 1/2] Ignore cache when getting envs for autoselection --- src/client/interpreter/autoSelection/index.ts | 2 +- .../autoSelection/index.unit.test.ts | 77 +++++++++++-------- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/client/interpreter/autoSelection/index.ts b/src/client/interpreter/autoSelection/index.ts index 1340c0c4daee..59acef376eea 100644 --- a/src/client/interpreter/autoSelection/index.ts +++ b/src/client/interpreter/autoSelection/index.ts @@ -260,7 +260,7 @@ export class InterpreterAutoSelectionService implements IInterpreterAutoSelectio * As such, we can sort interpreters based on what it returns. */ private async autoselectInterpreterWithLocators(resource: Resource): Promise { - const interpreters = await this.interpreterService.getInterpreters(resource); + const interpreters = await this.interpreterService.getInterpreters(resource, { ignoreCache: true }); const workspaceUri = this.interpreterHelper.getActiveWorkspaceUri(resource); // When auto-selecting an intepreter for a workspace, we either want to return a local one diff --git a/src/test/interpreters/autoSelection/index.unit.test.ts b/src/test/interpreters/autoSelection/index.unit.test.ts index e56db655139b..ac54badba8a5 100644 --- a/src/test/interpreters/autoSelection/index.unit.test.ts +++ b/src/test/interpreters/autoSelection/index.unit.test.ts @@ -30,7 +30,12 @@ import { IInterpreterAutoSelectionProxyService, } from '../../../client/interpreter/autoSelection/types'; import { EnvironmentTypeComparer } from '../../../client/interpreter/configuration/environmentTypeComparer'; -import { IInterpreterHelper, IInterpreterService, WorkspacePythonPath } from '../../../client/interpreter/contracts'; +import { + GetInterpreterOptions, + IInterpreterHelper, + IInterpreterService, + WorkspacePythonPath, +} from '../../../client/interpreter/contracts'; import { InterpreterHelper } from '../../../client/interpreter/helpers'; import { InterpreterService } from '../../../client/interpreter/interpreterService'; import { EnvironmentType, PythonEnvironment } from '../../../client/pythonEnvironments/info'; @@ -203,25 +208,30 @@ suite('Interpreters - Auto Selection', () => { envPath: path.join(workspacePath, '.venv'), version: { major: 3, minor: 10, patch: 0 }, } as PythonEnvironment; - - when(interpreterService.getInterpreters(resource)).thenResolve([ - { - envType: EnvironmentType.Conda, - envPath: path.join('some', 'conda', 'env'), - version: { major: 3, minor: 7, patch: 2 }, - } as PythonEnvironment, - { - envType: EnvironmentType.System, - envPath: path.join('/', 'usr', 'bin'), - version: { major: 3, minor: 9, patch: 1 }, - } as PythonEnvironment, - localEnv, - ]); + let options: GetInterpreterOptions = {}; + + when(interpreterService.getInterpreters(resource, anything())).thenCall((_, opts) => { + options = opts; + return Promise.resolve([ + { + envType: EnvironmentType.Conda, + envPath: path.join('some', 'conda', 'env'), + version: { major: 3, minor: 7, patch: 2 }, + } as PythonEnvironment, + { + envType: EnvironmentType.System, + envPath: path.join('/', 'usr', 'bin'), + version: { major: 3, minor: 9, patch: 1 }, + } as PythonEnvironment, + localEnv, + ]); + }); await autoSelectionService.autoSelectInterpreter(resource); expect(eventFired).to.deep.equal(true, 'event not fired'); - verify(interpreterService.getInterpreters(resource)).once(); + expect(options).to.deep.equal({ ignoreCache: true }, 'getInterpreters options are different'); + verify(interpreterService.getInterpreters(resource, anything())).once(); verify(state.updateValue(localEnv)).once(); }); @@ -231,25 +241,30 @@ suite('Interpreters - Auto Selection', () => { envPath: path.join('/', 'usr', 'bin'), version: { major: 3, minor: 9, patch: 1 }, } as PythonEnvironment; - - when(interpreterService.getInterpreters(resource)).thenResolve([ - { - envType: EnvironmentType.Conda, - envPath: path.join('some', 'conda', 'env'), - version: { major: 3, minor: 7, patch: 2 }, - } as PythonEnvironment, - systemEnv, - { - envType: EnvironmentType.Pipenv, - envPath: path.join('some', 'pipenv', 'env'), - version: { major: 3, minor: 10, patch: 0 }, - } as PythonEnvironment, - ]); + let options: GetInterpreterOptions = {}; + + when(interpreterService.getInterpreters(resource, anything())).thenCall((_, opts) => { + options = opts; + return Promise.resolve([ + { + envType: EnvironmentType.Conda, + envPath: path.join('some', 'conda', 'env'), + version: { major: 3, minor: 7, patch: 2 }, + } as PythonEnvironment, + systemEnv, + { + envType: EnvironmentType.Pipenv, + envPath: path.join('some', 'pipenv', 'env'), + version: { major: 3, minor: 10, patch: 0 }, + } as PythonEnvironment, + ]); + }); await autoSelectionService.autoSelectInterpreter(resource); expect(eventFired).to.deep.equal(true, 'event not fired'); - verify(interpreterService.getInterpreters(resource)).once(); + expect(options).to.deep.equal({ ignoreCache: true }, 'getInterpreters options are different'); + verify(interpreterService.getInterpreters(resource, anything())).once(); verify(state.updateValue(systemEnv)).once(); }); }); From 86cb558d96b9f8069b1a45a4133d6c41a26d8be8 Mon Sep 17 00:00:00 2001 From: Kim-Adeline Miguel Date: Tue, 20 Jul 2021 12:49:57 -0700 Subject: [PATCH 2/2] Don't call autoSelectInterpreter twice --- src/client/activation/activationManager.ts | 1 - src/test/activation/activationManager.unit.test.ts | 9 --------- 2 files changed, 10 deletions(-) diff --git a/src/client/activation/activationManager.ts b/src/client/activation/activationManager.ts index bd7fc45f6517..002a1e60b106 100644 --- a/src/client/activation/activationManager.ts +++ b/src/client/activation/activationManager.ts @@ -61,7 +61,6 @@ export class ExtensionActivationManager implements IExtensionActivationManager { Promise.all(this.singleActivationServices.map((item) => item.activate())), this.activateWorkspace(this.activeResourceService.getActiveResource()), ]); - await this.autoSelection.autoSelectInterpreter(undefined); } @traceDecorators.error('Failed to activate a workspace') diff --git a/src/test/activation/activationManager.unit.test.ts b/src/test/activation/activationManager.unit.test.ts index 3daaec52f34b..c0baa2f5b19a 100644 --- a/src/test/activation/activationManager.unit.test.ts +++ b/src/test/activation/activationManager.unit.test.ts @@ -456,25 +456,16 @@ suite('Activation Manager', () => { .setup((s) => s.activate()) .returns(() => Promise.resolve()) .verifiable(typemoq.Times.once()); - autoSelection - .setup((a) => a.autoSelectInterpreter(undefined)) - .returns(() => Promise.resolve()) - .verifiable(typemoq.Times.once()); when(activeResourceService.getActiveResource()).thenReturn(resource); await managerTest.activate(); assert.ok(initialize.calledOnce); assert.ok(activateWorkspace.calledOnce); singleActivationService.verifyAll(); - autoSelection.verifyAll(); }); test('Throws error if execution fails', async () => { singleActivationService .setup((s) => s.activate()) - .returns(() => Promise.resolve()) - .verifiable(typemoq.Times.once()); - autoSelection - .setup((a) => a.autoSelectInterpreter(undefined)) .returns(() => Promise.reject(new Error('Kaboom'))) .verifiable(typemoq.Times.once()); when(activeResourceService.getActiveResource()).thenReturn(resource);