Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose currently selected interpreter path using API #11295

Merged
merged 6 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
1 change: 1 addition & 0 deletions news/1 Enhancements/11294.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Expose currently selected interpreter path using API.
21 changes: 19 additions & 2 deletions src/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { isTestExecution } from './common/constants';
import { DebugAdapterNewPtvsd } from './common/experimentGroups';
import { traceError } from './common/logger';
import { IExperimentsManager } from './common/types';
import { IConfigurationService, IExperimentsManager, Resource } from './common/types';
import { getDebugpyLauncherArgs, getPtvsdLauncherScriptArgs } from './debugger/extension/adapter/remoteLaunchers';
import { IServiceContainer, IServiceManager } from './ioc/types';

Expand Down Expand Up @@ -34,15 +34,27 @@ export interface IExtensionApi {
*/
getRemoteLauncherCommand(host: string, port: number, waitUntilDebuggerAttaches: boolean): Promise<string[]>;
};
/**
* Return internal settings within the extension which are stored in VSCode storage
*/
settings: {
/**
* Return the currently selected interpreter path.
* @param {Resource} [resource]
karrtikr marked this conversation as resolved.
Show resolved Hide resolved
* @returns {string}
*/
getInterpreterPath(resource?: Resource): string;
};
}

export function buildApi(
// tslint:disable-next-line:no-any
ready: Promise<any>,
serviceManager: IServiceManager,
serviceContainer: IServiceContainer
) {
): IExtensionApi {
const experimentsManager = serviceContainer.get<IExperimentsManager>(IExperimentsManager);
const configurationService = serviceContainer.get<IConfigurationService>(IConfigurationService);
const api = {
// 'ready' will propagate the exception, but we must log it here first.
ready: ready.catch((ex) => {
Expand Down Expand Up @@ -71,6 +83,11 @@ export function buildApi(
waitUntilDebuggerAttaches
});
}
},
settings: {
getInterpreterPath(resource?: Resource) {
return configurationService.getSettings(resource).pythonPath;
}
}
};

Expand Down
24 changes: 22 additions & 2 deletions src/test/api.functional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
import { expect } from 'chai';
import * as path from 'path';
import { anyString, instance, mock, when } from 'ts-mockito';
import { Uri } from 'vscode';
import { buildApi } from '../client/api';
import { ConfigurationService } from '../client/common/configuration/service';
import { EXTENSION_ROOT_DIR } from '../client/common/constants';
import { ExperimentsManager } from '../client/common/experiments';
import { IExperimentsManager } from '../client/common/types';
import { IConfigurationService, IExperimentsManager } from '../client/common/types';
import { ServiceContainer } from '../client/ioc/container';
import { ServiceManager } from '../client/ioc/serviceManager';
import { IServiceContainer, IServiceManager } from '../client/ioc/types';

suite('Extension API - Debugger', () => {
suite('Extension API', () => {
const expectedLauncherPath = `${EXTENSION_ROOT_DIR.fileToCommandArgument()}/pythonFiles/ptvsd_launcher.py`;
const ptvsdPath = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'lib', 'python', 'debugpy', 'no_wheels', 'debugpy');
const ptvsdHost = 'somehost';
Expand All @@ -25,15 +27,33 @@ suite('Extension API - Debugger', () => {
let serviceContainer: IServiceContainer;
let serviceManager: IServiceManager;
let experimentsManager: IExperimentsManager;
let configurationService: IConfigurationService;

setup(() => {
serviceContainer = mock(ServiceContainer);
serviceManager = mock(ServiceManager);
experimentsManager = mock(ExperimentsManager);
configurationService = mock(ConfigurationService);

when(serviceContainer.get<IConfigurationService>(IConfigurationService)).thenReturn(
instance(configurationService)
);
when(serviceContainer.get<IExperimentsManager>(IExperimentsManager)).thenReturn(instance(experimentsManager));
});

test('Test interpreter path settings API', async () => {
const resource = Uri.parse('a');
when(configurationService.getSettings(resource)).thenReturn({ pythonPath: 'settingValue' } as any);

const interpreterPath = buildApi(
Promise.resolve(),
instance(serviceManager),
instance(serviceContainer)
).settings.getInterpreterPath(resource);

expect(interpreterPath).to.equal('settingValue');
});

test('Test debug launcher args (no-wait and not in experiment)', async () => {
const waitForAttach = false;
when(experimentsManager.inExperiment(anyString())).thenReturn(false);
Expand Down