Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify the buildApi() call in activateUnsafe().
  • Loading branch information
ericsnowcurrently committed Mar 9, 2020
commit 957818040d0eb4a84b0e8bcf552def55d0cc1924
20 changes: 17 additions & 3 deletions src/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

'use strict';

import { isTestExecution } from './common/constants';
import { DebugAdapterNewPtvsd } from './common/experimentGroups';
import { traceError } from './common/logger';
import { IExperimentsManager } from './common/types';
import { RemoteDebuggerExternalLauncherScriptProvider } from './debugger/debugAdapter/DebugClients/launcherProvider';
import { IDebugAdapterDescriptorFactory } from './debugger/extension/types';
import { IServiceContainer, IServiceManager } from './ioc/types';

/*
* Do not introduce any breaking changes to this API.
Expand Down Expand Up @@ -38,10 +40,13 @@ export interface IExtensionApi {
export function buildApi(
// tslint:disable-next-line:no-any
ready: Promise<any>,
experimentsManager: IExperimentsManager,
debugFactory: IDebugAdapterDescriptorFactory
serviceManager: IServiceManager,
serviceContainer: IServiceContainer
) {
return {
const experimentsManager = serviceContainer.get<IExperimentsManager>(IExperimentsManager);
const debugFactory = serviceContainer.get<IDebugAdapterDescriptorFactory>(IDebugAdapterDescriptorFactory);

const api = {
// 'ready' will propagate the exception, but we must log it here first.
ready: ready.catch(ex => {
traceError('Failure during activation.', ex);
Expand Down Expand Up @@ -69,4 +74,13 @@ export function buildApi(
}
}
};

// In test environment return the DI Container.
if (isTestExecution()) {
Comment thread
kimadeline marked this conversation as resolved.
// tslint:disable:no-any
(api as any).serviceContainer = serviceContainer;
(api as any).serviceManager = serviceManager;
// tslint:enable:no-any
}
return api;
}
22 changes: 3 additions & 19 deletions src/client/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import { registerTypes as appRegisterTypes } from './application/serviceRegistry
import { IApplicationDiagnostics } from './application/types';
import { DebugService } from './common/application/debugService';
import { IApplicationShell, ICommandManager, IWorkspaceService } from './common/application/types';
import { Commands, isTestExecution, PYTHON, PYTHON_LANGUAGE, STANDARD_OUTPUT_CHANNEL } from './common/constants';
import { Commands, PYTHON, PYTHON_LANGUAGE, STANDARD_OUTPUT_CHANNEL } from './common/constants';
import { registerTypes as installerRegisterTypes } from './common/installer/serviceRegistry';
import { traceError } from './common/logger';
import { registerTypes as platformRegisterTypes } from './common/platform/serviceRegistry';
Expand Down Expand Up @@ -76,11 +76,7 @@ import { DebuggerTypeName } from './debugger/constants';
import { DebugSessionEventDispatcher } from './debugger/extension/hooks/eventHandlerDispatcher';
import { IDebugSessionEventHandlers } from './debugger/extension/hooks/types';
import { registerTypes as debugConfigurationRegisterTypes } from './debugger/extension/serviceRegistry';
import {
IDebugAdapterDescriptorFactory,
IDebugConfigurationService,
IDebuggerBanner
} from './debugger/extension/types';
import { IDebugConfigurationService, IDebuggerBanner } from './debugger/extension/types';
import { registerTypes as formattersRegisterTypes } from './formatters/serviceRegistry';
import { IInterpreterSelector } from './interpreter/configuration/types';
import {
Expand Down Expand Up @@ -210,19 +206,7 @@ async function activateUnsafe(context: ExtensionContext): Promise<IExtensionApi>
// It will run in the background.
.ignoreErrors();

const api = buildApi(
Promise.all([activationDeferred.promise, activationPromise]),
serviceContainer.get<IExperimentsManager>(IExperimentsManager),
serviceContainer.get<IDebugAdapterDescriptorFactory>(IDebugAdapterDescriptorFactory)
);
// In test environment return the DI Container.
if (isTestExecution()) {
// tslint:disable:no-any
(api as any).serviceContainer = serviceContainer;
(api as any).serviceManager = serviceManager;
// tslint:enable:no-any
}
return api;
return buildApi(activationPromise, serviceManager, serviceContainer);
}

export function deactivate(): Thenable<void> {
Expand Down
30 changes: 22 additions & 8 deletions src/test/extension.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import { ExperimentsManager } from '../client/common/experiments';
import { IExperimentsManager } from '../client/common/types';
import { DebugAdapterDescriptorFactory } from '../client/debugger/extension/adapter/factory';
import { IDebugAdapterDescriptorFactory } from '../client/debugger/extension/types';
import { ServiceContainer } from '../client/ioc/container';
import { ServiceManager } from '../client/ioc/serviceManager';
import { IServiceContainer, IServiceManager } from '../client/ioc/types';

// tslint:disable-next-line: max-func-body-length
suite('Extension API Debugger', () => {
Expand All @@ -26,12 +29,23 @@ suite('Extension API Debugger', () => {
const ptvsdHost = 'somehost';
const ptvsdPort = 12345;

let serviceManager: IServiceManager;
let serviceContainer: IServiceContainer;
let experimentsManager: IExperimentsManager;
let debugAdapterFactory: IDebugAdapterDescriptorFactory;

setup(() => {
serviceManager = mock(ServiceManager);
serviceContainer = mock(ServiceContainer);
experimentsManager = mock(ExperimentsManager);
debugAdapterFactory = mock(DebugAdapterDescriptorFactory);

when(serviceManager.get(IExperimentsManager))
// Return the mock.
.thenReturn(Promise.resolve(instance(experimentsManager)));
when(serviceManager.get(IDebugAdapterDescriptorFactory))
// Return the mock.
.thenReturn(Promise.resolve(instance(debugAdapterFactory)));
});

function mockInExperiment(host: string, port: number, wait: boolean) {
Expand Down Expand Up @@ -85,8 +99,8 @@ suite('Extension API Debugger', () => {

const args = await buildApi(
Promise.resolve(),
instance(experimentsManager),
instance(debugAdapterFactory)
instance(serviceManager),
instance(serviceContainer)
).debug.getRemoteLauncherCommand(ptvsdHost, ptvsdPort, waitForAttach);
const expectedArgs = [expectedLauncherPath, '--default', '--host', ptvsdHost, '--port', ptvsdPort.toString()];

Expand All @@ -99,8 +113,8 @@ suite('Extension API Debugger', () => {

const args = await buildApi(
Promise.resolve(),
instance(experimentsManager),
instance(debugAdapterFactory)
instance(serviceManager),
instance(serviceContainer)
).debug.getRemoteLauncherCommand(ptvsdHost, ptvsdPort, waitForAttach);
const expectedArgs = [ptvsdPath, '--host', ptvsdHost, '--port', ptvsdPort.toString()];

Expand All @@ -113,8 +127,8 @@ suite('Extension API Debugger', () => {

const args = await buildApi(
Promise.resolve(),
instance(experimentsManager),
instance(debugAdapterFactory)
instance(serviceManager),
instance(serviceContainer)
).debug.getRemoteLauncherCommand(ptvsdHost, ptvsdPort, waitForAttach);
const expectedArgs = [
expectedLauncherPath,
Expand All @@ -135,8 +149,8 @@ suite('Extension API Debugger', () => {

const args = await buildApi(
Promise.resolve(),
instance(experimentsManager),
instance(debugAdapterFactory)
instance(serviceManager),
instance(serviceContainer)
).debug.getRemoteLauncherCommand(ptvsdHost, ptvsdPort, waitForAttach);
const expectedArgs = [ptvsdPath, '--host', ptvsdHost, '--port', ptvsdPort.toString(), '--wait'];

Expand Down