Skip to content

Commit 0d6c464

Browse files
author
Kartik Raj
committed
Intialize prompt in background
1 parent bed2760 commit 0d6c464

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/client/interpreter/virtualEnvs/condaInheritEnvPrompt.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,19 @@ export class CondaInheritEnvPrompt implements IExtensionActivationService {
2525
) { }
2626

2727
public async activate(resource: Uri): Promise<void> {
28+
this.initializeInBackground(resource).ignoreErrors();
29+
}
30+
31+
@traceDecorators.error('Failed to intialize conda inherit env prompt')
32+
public async initializeInBackground(resource: Uri): Promise<void> {
2833
const show = await this.shouldShowPrompt(resource);
2934
if (!show) {
3035
return;
3136
}
3237
await this.promptAndUpdate();
3338
}
3439

40+
@traceDecorators.error('Failed to display conda inherit env prompt')
3541
public async promptAndUpdate() {
3642
const notificationPromptEnabled = this.persistentStateFactory.createGlobalPersistentState(condaInheritEnvPromptKey, true);
3743
if (!notificationPromptEnabled.value) {

src/test/interpreters/virtualEnvs/condaInheritEnvPrompt.unit.test.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ConfigurationTarget, Uri, WorkspaceConfiguration } from 'vscode';
1111
import { IApplicationShell, IWorkspaceService } from '../../../client/common/application/types';
1212
import { PersistentStateFactory } from '../../../client/common/persistentState';
1313
import { IPersistentState, IPersistentStateFactory } from '../../../client/common/types';
14+
import { createDeferred, createDeferredFromPromise, sleep } from '../../../client/common/utils/async';
1415
import { InteractiveShiftEnterBanner, Interpreters } from '../../../client/common/utils/localize';
1516
import { IInterpreterService, InterpreterType } from '../../../client/interpreter/contracts';
1617
import { CondaInheritEnvPrompt, condaInheritEnvPromptKey } from '../../../client/interpreter/virtualEnvs/condaInheritEnvPrompt';
@@ -185,8 +186,47 @@ suite('Conda Inherit Env Prompt', async () => {
185186
verifyAll();
186187
});
187188
});
188-
189189
suite('Method activate()', () => {
190+
let initializeInBackground: sinon.SinonStub<any>;
191+
setup(() => {
192+
workspaceService = TypeMoq.Mock.ofType<IWorkspaceService>();
193+
appShell = TypeMoq.Mock.ofType<IApplicationShell>();
194+
interpreterService = TypeMoq.Mock.ofType<IInterpreterService>();
195+
persistentStateFactory = mock(PersistentStateFactory);
196+
});
197+
198+
teardown(() => {
199+
sinon.restore();
200+
});
201+
202+
test('Invokes initializeInBackground() in the background', async () => {
203+
const initializeInBackgroundDeferred = createDeferred<void>();
204+
initializeInBackground = sinon.stub(CondaInheritEnvPrompt.prototype, 'initializeInBackground');
205+
initializeInBackground.callsFake(() => initializeInBackgroundDeferred.promise);
206+
condaInheritEnvPrompt = new CondaInheritEnvPrompt(interpreterService.object, workspaceService.object, appShell.object, instance(persistentStateFactory));
207+
208+
const promise = condaInheritEnvPrompt.activate(resource);
209+
const deferred = createDeferredFromPromise(promise);
210+
await sleep(1);
211+
212+
// Ensure activate() function has completed while initializeInBackground() is still not resolved
213+
assert.equal(deferred.completed, true);
214+
215+
initializeInBackgroundDeferred.resolve();
216+
await sleep(1);
217+
assert.ok(initializeInBackground.calledOnce);
218+
});
219+
220+
test('Ignores errors raised by initializeInBackground()', async () => {
221+
initializeInBackground = sinon.stub(CondaInheritEnvPrompt.prototype, 'initializeInBackground');
222+
initializeInBackground.rejects(new Error('Kaboom'));
223+
condaInheritEnvPrompt = new CondaInheritEnvPrompt(interpreterService.object, workspaceService.object, appShell.object, instance(persistentStateFactory));
224+
await condaInheritEnvPrompt.activate(resource);
225+
assert.ok(initializeInBackground.calledOnce);
226+
});
227+
});
228+
229+
suite('Method initializeInBackground()', () => {
190230
let shouldShowPrompt: sinon.SinonStub<any>;
191231
let promptAndUpdate: sinon.SinonStub<any>;
192232
setup(() => {
@@ -206,7 +246,7 @@ suite('Conda Inherit Env Prompt', async () => {
206246
promptAndUpdate = sinon.stub(CondaInheritEnvPrompt.prototype, 'promptAndUpdate');
207247
promptAndUpdate.callsFake(() => Promise.resolve(undefined));
208248
condaInheritEnvPrompt = new CondaInheritEnvPrompt(interpreterService.object, workspaceService.object, appShell.object, instance(persistentStateFactory));
209-
await condaInheritEnvPrompt.activate(resource);
249+
await condaInheritEnvPrompt.initializeInBackground(resource);
210250
assert.ok(shouldShowPrompt.calledOnce);
211251
assert.ok(promptAndUpdate.calledOnce);
212252
});
@@ -217,7 +257,7 @@ suite('Conda Inherit Env Prompt', async () => {
217257
promptAndUpdate = sinon.stub(CondaInheritEnvPrompt.prototype, 'promptAndUpdate');
218258
promptAndUpdate.callsFake(() => Promise.resolve(undefined));
219259
condaInheritEnvPrompt = new CondaInheritEnvPrompt(interpreterService.object, workspaceService.object, appShell.object, instance(persistentStateFactory));
220-
await condaInheritEnvPrompt.activate(resource);
260+
await condaInheritEnvPrompt.initializeInBackground(resource);
221261
assert.ok(shouldShowPrompt.calledOnce);
222262
assert.ok(promptAndUpdate.notCalled);
223263
});

0 commit comments

Comments
 (0)