|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { expect } from 'chai'; |
| 5 | +import { Container } from 'inversify'; |
| 6 | +import * as TypeMoq from 'typemoq'; |
| 7 | +import { BufferDecoder } from '../../client/common/process/decoder'; |
| 8 | +import { ProcessService } from '../../client/common/process/proc'; |
| 9 | +import { IBufferDecoder, IProcessService } from '../../client/common/process/types'; |
| 10 | +import { VirtualEnvironmentManager } from '../../client/interpreter/virtualEnvs'; |
| 11 | +import { ServiceContainer } from '../../client/ioc/container'; |
| 12 | +import { ServiceManager } from '../../client/ioc/serviceManager'; |
| 13 | + |
| 14 | +suite('Virtual environment manager', () => { |
| 15 | + let serviceManager: ServiceManager; |
| 16 | + let serviceContainer: ServiceContainer; |
| 17 | + let process: TypeMoq.IMock<IProcessService>; |
| 18 | + |
| 19 | + setup(async () => { |
| 20 | + const cont = new Container(); |
| 21 | + serviceManager = new ServiceManager(cont); |
| 22 | + serviceContainer = new ServiceContainer(cont); |
| 23 | + }); |
| 24 | + |
| 25 | + test('Plain Python environment suffix', async () => await testSuffix('')); |
| 26 | + test('Venv environment suffix', async () => await testSuffix('venv')); |
| 27 | + test('Virtualenv Python environment suffix', async () => await testSuffix('virtualenv')); |
| 28 | + |
| 29 | + test('Run actual virtual env detection code', async () => { |
| 30 | + serviceManager.addSingleton<IProcessService>(IProcessService, ProcessService); |
| 31 | + serviceManager.addSingleton<IBufferDecoder>(IBufferDecoder, BufferDecoder); |
| 32 | + const venvManager = new VirtualEnvironmentManager(serviceContainer); |
| 33 | + const name = await venvManager.getEnvironmentName('python'); |
| 34 | + const result = name === '' || name === 'venv' || name === 'virtualenv'; |
| 35 | + expect(result).to.be.equal(true, 'Running venv detection code failed.'); |
| 36 | + }); |
| 37 | + |
| 38 | + async function testSuffix(expectedName: string) { |
| 39 | + process = TypeMoq.Mock.ofType<IProcessService>(); |
| 40 | + serviceManager.addSingletonInstance<IProcessService>(IProcessService, process.object); |
| 41 | + |
| 42 | + const venvManager = new VirtualEnvironmentManager(serviceContainer); |
| 43 | + process |
| 44 | + .setup(x => x.exec('python', TypeMoq.It.isAny())) |
| 45 | + .returns(() => Promise.resolve({ |
| 46 | + stdout: expectedName, |
| 47 | + stderr: '' |
| 48 | + })); |
| 49 | + |
| 50 | + const name = await venvManager.getEnvironmentName('python'); |
| 51 | + expect(name).to.be.equal(expectedName, 'Virtual envrironment name suffix is incorrect.'); |
| 52 | + } |
| 53 | +}); |
0 commit comments