|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 4 | + |
| 5 | +import * as chaiAsPromised from 'chai-as-promised'; |
| 6 | +import * as sinon from 'sinon'; |
| 7 | +import * as typemoq from 'typemoq'; |
| 8 | +import { assert, use as chaiUse } from 'chai'; |
| 9 | +import { ConfigurationTarget, Uri } from 'vscode'; |
| 10 | +import { IDisposableRegistry, IInterpreterPathService, IPathUtils } from '../../../client/common/types'; |
| 11 | +import * as commandApis from '../../../client/common/vscodeApis/commandApis'; |
| 12 | +import { IInterpreterQuickPick } from '../../../client/interpreter/configuration/types'; |
| 13 | +import { registerCreateEnvironmentFeatures } from '../../../client/pythonEnvironments/creation/createEnvApi'; |
| 14 | +import * as windowApis from '../../../client/common/vscodeApis/windowApis'; |
| 15 | +import { CreateEnvironmentProvider } from '../../../client/pythonEnvironments/creation/types'; |
| 16 | +import { handleCreateEnvironmentCommand } from '../../../client/pythonEnvironments/creation/createEnvironment'; |
| 17 | + |
| 18 | +chaiUse(chaiAsPromised); |
| 19 | + |
| 20 | +suite('Create Environment APIs', () => { |
| 21 | + let registerCommandStub: sinon.SinonStub; |
| 22 | + let showQuickPickStub: sinon.SinonStub; |
| 23 | + let showInformationMessageStub: sinon.SinonStub; |
| 24 | + const disposables: IDisposableRegistry = []; |
| 25 | + let interpreterQuickPick: typemoq.IMock<IInterpreterQuickPick>; |
| 26 | + let interpreterPathService: typemoq.IMock<IInterpreterPathService>; |
| 27 | + let pathUtils: typemoq.IMock<IPathUtils>; |
| 28 | + |
| 29 | + setup(() => { |
| 30 | + showQuickPickStub = sinon.stub(windowApis, 'showQuickPick'); |
| 31 | + showInformationMessageStub = sinon.stub(windowApis, 'showInformationMessage'); |
| 32 | + |
| 33 | + registerCommandStub = sinon.stub(commandApis, 'registerCommand'); |
| 34 | + interpreterQuickPick = typemoq.Mock.ofType<IInterpreterQuickPick>(); |
| 35 | + interpreterPathService = typemoq.Mock.ofType<IInterpreterPathService>(); |
| 36 | + pathUtils = typemoq.Mock.ofType<IPathUtils>(); |
| 37 | + |
| 38 | + registerCommandStub.callsFake((_command: string, _callback: (...args: any[]) => any) => ({ |
| 39 | + dispose: () => { |
| 40 | + // Do nothing |
| 41 | + }, |
| 42 | + })); |
| 43 | + |
| 44 | + pathUtils.setup((p) => p.getDisplayName(typemoq.It.isAny())).returns(() => 'test'); |
| 45 | + |
| 46 | + registerCreateEnvironmentFeatures( |
| 47 | + disposables, |
| 48 | + interpreterQuickPick.object, |
| 49 | + interpreterPathService.object, |
| 50 | + pathUtils.object, |
| 51 | + ); |
| 52 | + }); |
| 53 | + teardown(() => { |
| 54 | + disposables.forEach((d) => d.dispose()); |
| 55 | + sinon.restore(); |
| 56 | + }); |
| 57 | + |
| 58 | + [true, false].forEach((selectEnvironment) => { |
| 59 | + test(`Set environment selectEnvironment == ${selectEnvironment}`, async () => { |
| 60 | + const provider = typemoq.Mock.ofType<CreateEnvironmentProvider>(); |
| 61 | + provider.setup((p) => p.name).returns(() => 'test'); |
| 62 | + provider.setup((p) => p.id).returns(() => 'test-id'); |
| 63 | + provider.setup((p) => p.description).returns(() => 'test-description'); |
| 64 | + provider |
| 65 | + .setup((p) => p.createEnvironment(typemoq.It.isAny())) |
| 66 | + .returns(() => |
| 67 | + Promise.resolve({ |
| 68 | + path: '/path/to/env', |
| 69 | + uri: Uri.file('/path/to/env'), |
| 70 | + }), |
| 71 | + ); |
| 72 | + provider.setup((p) => (p as any).then).returns(() => undefined); |
| 73 | + |
| 74 | + showQuickPickStub.resolves(provider.object); |
| 75 | + |
| 76 | + interpreterPathService |
| 77 | + .setup((p) => |
| 78 | + p.update( |
| 79 | + typemoq.It.isAny(), |
| 80 | + ConfigurationTarget.WorkspaceFolder, |
| 81 | + typemoq.It.isValue('/path/to/env'), |
| 82 | + ), |
| 83 | + ) |
| 84 | + .returns(() => Promise.resolve()) |
| 85 | + .verifiable(selectEnvironment ? typemoq.Times.once() : typemoq.Times.never()); |
| 86 | + |
| 87 | + await handleCreateEnvironmentCommand([provider.object], { selectEnvironment }); |
| 88 | + |
| 89 | + assert.ok(showQuickPickStub.calledOnce); |
| 90 | + assert.ok(selectEnvironment ? showInformationMessageStub.calledOnce : showInformationMessageStub.notCalled); |
| 91 | + interpreterPathService.verifyAll(); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments