|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { expect, use } from 'chai'; |
| 5 | +import * as chaiAsPromised from 'chai-as-promised'; |
| 6 | +import * as TypeMoq from 'typemoq'; |
| 7 | +import { Disposable, OutputChannel, Uri } from 'vscode'; |
| 8 | +import { EnumEx } from '../../../client/common/enumUtils'; |
| 9 | +import { Installer } from '../../../client/common/installer/installer'; |
| 10 | +import { ProductInstaller } from '../../../client/common/installer/productInstaller'; |
| 11 | +import { IInstallationChannelManager, IModuleInstaller } from '../../../client/common/installer/types'; |
| 12 | +import { IDisposableRegistry, ILogger, InstallerResponse, ModuleNamePurpose, Product } from '../../../client/common/types'; |
| 13 | +import { IServiceContainer } from '../../../client/ioc/types'; |
| 14 | + |
| 15 | +use(chaiAsPromised); |
| 16 | + |
| 17 | +// tslint:disable-next-line:max-func-body-length |
| 18 | +suite('Module Installerx', () => { |
| 19 | + [undefined, Uri.file('resource')].forEach(resource => { |
| 20 | + EnumEx.getNamesAndValues<Product>(Product).forEach(product => { |
| 21 | + let disposables: Disposable[] = []; |
| 22 | + let installer: Installer; |
| 23 | + let installationChannel: TypeMoq.IMock<IInstallationChannelManager>; |
| 24 | + let moduleInstaller: TypeMoq.IMock<IModuleInstaller>; |
| 25 | + let serviceContainer: TypeMoq.IMock<IServiceContainer>; |
| 26 | + let productInstallerFactory: ProductInstaller; |
| 27 | + setup(() => { |
| 28 | + serviceContainer = TypeMoq.Mock.ofType<IServiceContainer>(); |
| 29 | + const outputChannel = TypeMoq.Mock.ofType<OutputChannel>(); |
| 30 | + |
| 31 | + installer = new Installer(serviceContainer.object, outputChannel.object); |
| 32 | + |
| 33 | + disposables = []; |
| 34 | + serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IDisposableRegistry), TypeMoq.It.isAny())).returns(() => disposables); |
| 35 | + |
| 36 | + installationChannel = TypeMoq.Mock.ofType<IInstallationChannelManager>(); |
| 37 | + serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IInstallationChannelManager), TypeMoq.It.isAny())).returns(() => installationChannel.object); |
| 38 | + |
| 39 | + moduleInstaller = TypeMoq.Mock.ofType<IModuleInstaller>(); |
| 40 | + // tslint:disable-next-line:no-any |
| 41 | + moduleInstaller.setup((x: any) => x.then).returns(() => undefined); |
| 42 | + installationChannel.setup(i => i.getInstallationChannel(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve(moduleInstaller.object)); |
| 43 | + installationChannel.setup(i => i.getInstallationChannel(TypeMoq.It.isAny())).returns(() => Promise.resolve(moduleInstaller.object)); |
| 44 | + |
| 45 | + productInstallerFactory = new ProductInstaller(serviceContainer.object, outputChannel.object); |
| 46 | + }); |
| 47 | + teardown(() => { |
| 48 | + disposables.forEach(disposable => { |
| 49 | + if (disposable) { |
| 50 | + disposable.dispose(); |
| 51 | + } |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + switch (product.value) { |
| 56 | + case Product.isort: |
| 57 | + case Product.ctags: { |
| 58 | + return; |
| 59 | + } |
| 60 | + case Product.unittest: { |
| 61 | + test(`Ensure resource info is passed into the module installer ${product.name} (${resource ? 'With a resource' : 'without a resource'})`, async () => { |
| 62 | + const response = await installer.install(product.value, resource); |
| 63 | + expect(response).to.be.equal(InstallerResponse.Installed); |
| 64 | + }); |
| 65 | + test(`Ensure resource info is passed into the module installer (created using ProductInstaller) ${product.name} (${resource ? 'With a resource' : 'without a resource'})`, async () => { |
| 66 | + const response = await installer.install(product.value, resource); |
| 67 | + expect(response).to.be.equal(InstallerResponse.Installed); |
| 68 | + }); |
| 69 | + } |
| 70 | + default: { |
| 71 | + test(`Ensure resource info is passed into the module installer ${product.name} (${resource ? 'With a resource' : 'without a resource'})`, async () => { |
| 72 | + const moduleName = installer.translateProductToModuleName(product.value, ModuleNamePurpose.install); |
| 73 | + const logger = TypeMoq.Mock.ofType<ILogger>(); |
| 74 | + logger.setup(l => l.logError(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => new Error('UnitTesting')); |
| 75 | + serviceContainer.setup(c => c.get(TypeMoq.It.isValue(ILogger), TypeMoq.It.isAny())).returns(() => logger.object); |
| 76 | + |
| 77 | + moduleInstaller.setup(m => m.installModule(TypeMoq.It.isValue(moduleName), TypeMoq.It.isValue(resource))).returns(() => Promise.reject(new Error('UnitTesting'))); |
| 78 | + |
| 79 | + try { |
| 80 | + await installer.install(product.value, resource); |
| 81 | + } catch (ex) { |
| 82 | + moduleInstaller.verify(m => m.installModule(TypeMoq.It.isValue(moduleName), TypeMoq.It.isValue(resource)), TypeMoq.Times.once()); |
| 83 | + } |
| 84 | + }); |
| 85 | + test(`Ensure resource info is passed into the module installer (created using ProductInstaller) ${product.name} (${resource ? 'With a resource' : 'without a resource'})`, async () => { |
| 86 | + const moduleName = installer.translateProductToModuleName(product.value, ModuleNamePurpose.install); |
| 87 | + const logger = TypeMoq.Mock.ofType<ILogger>(); |
| 88 | + logger.setup(l => l.logError(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => new Error('UnitTesting')); |
| 89 | + serviceContainer.setup(c => c.get(TypeMoq.It.isValue(ILogger), TypeMoq.It.isAny())).returns(() => logger.object); |
| 90 | + |
| 91 | + moduleInstaller.setup(m => m.installModule(TypeMoq.It.isValue(moduleName), TypeMoq.It.isValue(resource))).returns(() => Promise.reject(new Error('UnitTesting'))); |
| 92 | + |
| 93 | + try { |
| 94 | + await productInstallerFactory.install(product.value, resource); |
| 95 | + } catch (ex) { |
| 96 | + moduleInstaller.verify(m => m.installModule(TypeMoq.It.isValue(moduleName), TypeMoq.It.isValue(resource)), TypeMoq.Times.once()); |
| 97 | + } |
| 98 | + }); |
| 99 | + } |
| 100 | + } |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments