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

Add copy of Python Extension API service for functional tests #33

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 0 additions & 4 deletions src/client/api/pythonApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,4 @@ export class InterpreterService implements IInterpreterService {
public getInterpreterDetails(pythonPath: string, resource?: Uri): Promise<undefined | PythonEnvironment> {
return this.api.getApi().then((api) => api.getInterpreterDetails(pythonPath, resource));
}

public initialize(): void {
// Noop.
}
}
1 change: 0 additions & 1 deletion src/client/interpreter/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ export interface IInterpreterService {
getInterpreters(resource?: Uri): Promise<PythonEnvironment[]>;
getActiveInterpreter(resource?: Uri): Promise<PythonEnvironment | undefined>;
getInterpreterDetails(pythonPath: string, resource?: Uri): Promise<undefined | PythonEnvironment>;
initialize(): void;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only used in tests.

}
19 changes: 18 additions & 1 deletion src/test/datascience/dataScienceIocContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,17 @@ import {
} from '../../client/datascience/types';
import { ProtocolParser } from '../../client/debugger/extension/helpers/protocolParser';
import { IProtocolParser } from '../../client/debugger/extension/types';
import { IEnvironmentActivationService } from '../../client/interpreter/activation/types';
import { IInterpreterSelector } from '../../client/interpreter/configuration/types';
import { IInterpreterService } from '../../client/interpreter/contracts';
import { IWindowsStoreInterpreter } from '../../client/interpreter/locators/types';
import { PythonEnvironment } from '../../client/pythonEnvironments/info';
import { CodeExecutionHelper } from '../../client/terminals/codeExecution/helper';
import { ICodeExecutionHelper } from '../../client/terminals/types';
import { EnvironmentActivationService } from '../interpreters/envActivation';
import { InterpreterService } from '../interpreters/interpreterService';
import { InterpreterSelector } from '../interpreters/selector';
import { WindowsStoreInterpreter } from '../interpreters/winStoreInterpreter';
import { MockOutputChannel } from '../mockClasses';
import { UnitTestIocContainer } from '../testing/serviceRegistry';
import { MockCommandManager } from './mockCommandManager';
Expand Down Expand Up @@ -905,6 +912,16 @@ export class DataScienceIocContainer extends UnitTestIocContainer {
this.serviceManager.addSingletonInstance<KernelService>(KernelService, instance(this.kernelServiceMock));
} else {
this.serviceManager.addSingleton<IInstaller>(IInstaller, ProductInstaller);
this.serviceManager.addSingleton<IInterpreterService>(IInterpreterService, InterpreterService);
this.serviceManager.addSingleton<IInterpreterSelector>(IInterpreterSelector, InterpreterSelector);
this.serviceManager.addSingleton<IWindowsStoreInterpreter>(
IWindowsStoreInterpreter,
WindowsStoreInterpreter
);
this.serviceManager.addSingleton<IEnvironmentActivationService>(
IEnvironmentActivationService,
EnvironmentActivationService
);
this.serviceManager.addSingleton<KernelService>(KernelService, KernelService);
this.serviceManager.addSingleton<IProcessServiceFactory>(IProcessServiceFactory, ProcessServiceFactory);
this.serviceManager.addSingleton<IPythonExecutionFactory>(IPythonExecutionFactory, PythonExecutionFactory);
Expand Down Expand Up @@ -975,7 +992,7 @@ export class DataScienceIocContainer extends UnitTestIocContainer {
when(this.applicationShell.onDidChangeWindowState).thenReturn(eventCallback);
when(this.applicationShell.withProgress(anything(), anything())).thenCall((_o, c) => c());

const interpreterManager = this.serviceContainer.get<IInterpreterService>(IInterpreterService);
const interpreterManager = this.serviceContainer.get<InterpreterService>(IInterpreterService);
interpreterManager.initialize();

if (this.mockJupyter) {
Expand Down
21 changes: 0 additions & 21 deletions src/test/datascience/notebook.functional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,12 +560,6 @@ suite('DataScience notebook tests', () => {
// Rewire our data we use to search for processes
@injectable()
class EmptyInterpreterService implements IInterpreterService {
public get hasInterpreters(): Promise<boolean> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant code

return Promise.resolve(true);
}
public onDidChangeInterpreterConfiguration(): Disposable {
return { dispose: noop };
}
public onDidChangeInterpreter(
_listener: (e: void) => any,
_thisArgs?: any,
Expand All @@ -577,27 +571,12 @@ suite('DataScience notebook tests', () => {
public getInterpreters(_resource?: Uri): Promise<PythonEnvironment[]> {
return Promise.resolve([]);
}
public autoSetInterpreter(): Promise<void> {
throw new Error('Method not implemented');
}
public getActiveInterpreter(_resource?: Uri): Promise<PythonEnvironment | undefined> {
return Promise.resolve(undefined);
}
public getInterpreterDetails(_pythonPath: string, _resoure?: Uri): Promise<PythonEnvironment> {
throw new Error('Method not implemented');
}
public refresh(_resource: Uri): Promise<void> {
throw new Error('Method not implemented');
}
public initialize(): void {
throw new Error('Method not implemented');
}
public getDisplayName(_interpreter: Partial<PythonEnvironment>): Promise<string> {
throw new Error('Method not implemented');
}
public shouldAutoSetInterpreter(): Promise<boolean> {
throw new Error('Method not implemented');
}
}
ioc.serviceManager.rebind<IInterpreterService>(IInterpreterService, EmptyInterpreterService);
await createNotebook(undefined, undefined, true);
Expand Down
19 changes: 19 additions & 0 deletions src/test/interpreters/envActivation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { injectable } from 'inversify';
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { getActivatedEnvVariables } from '.';
import { Resource } from '../../client/common/types';
import { IEnvironmentActivationService } from '../../client/interpreter/activation/types';
import { PythonEnvironment } from '../../client/pythonEnvironments/info';

@injectable()
export class EnvironmentActivationService implements IEnvironmentActivationService {
public getActivatedEnvironmentVariables(
_resource: Resource,
interpreter?: PythonEnvironment,
_allowExceptions?: boolean
): Promise<NodeJS.ProcessEnv | undefined> {
return getActivatedEnvVariables(interpreter?.path || process.env.CI_PYTHON_PATH || 'python');
}
}
95 changes: 76 additions & 19 deletions src/test/interpreters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,95 @@
// Licensed under the MIT License.

import * as path from 'path';
import '../../client/common/extensions';
import { traceError } from '../../client/common/logger';
import { BufferDecoder } from '../../client/common/process/decoder';
import { PythonEnvInfo } from '../../client/common/process/internal/scripts';
import { ProcessService } from '../../client/common/process/proc';
import { PythonEnvironment } from '../../client/pythonEnvironments/info';
import { getOSType, OSType } from '../common';
import { EXTENSION_ROOT_DIR_FOR_TESTS } from '../constants';
import { isCondaEnvironment } from './condaLocator';
import { getCondaEnvironment, getCondaFile, isCondaAvailable } from './condaService';
import { parsePythonVersion } from './pythonVersion';

const SCRIPTS_DIR = path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'pythonFiles');
const defaultShells = {
[OSType.Windows]: 'cmd',
[OSType.OSX]: 'bash',
[OSType.Linux]: 'bash',
[OSType.Unknown]: undefined
};

const defaultShell = defaultShells[getOSType()];

const interpreterInfoCache = new Map<string, Promise<PythonEnvironment | undefined>>();
export async function getInterpreterInfo(pythonPath: string): Promise<PythonEnvironment | undefined> {
const cli = await getPythonCli(pythonPath);
const processService = new ProcessService(new BufferDecoder());
const argv = [...cli, path.join(SCRIPTS_DIR, 'interpreterInfo.py')];
// Concat these together to make a set of quoted strings
const quoted = argv.reduce((p, c) => (p ? `${p} "${c}"` : `"${c.replace('\\', '\\\\')}"`), '');
if (interpreterInfoCache.has(pythonPath)) {
return interpreterInfoCache.get(pythonPath);
}

const promise = (async () => {
try {
const cli = await getPythonCli(pythonPath);
const processService = new ProcessService(new BufferDecoder());
const argv = [...cli, path.join(SCRIPTS_DIR, 'interpreterInfo.py').fileToCommandArgument()];
const cmd = argv.reduce((p, c) => (p ? `${p} "${c}"` : `"${c.replace('\\', '/')}"`), '');
const result = await processService.shellExec(cmd, {
timeout: 1_500,
env: process.env,
shell: defaultShell
});
if (result.stderr && result.stderr.length) {
traceError(`Failed to parse interpreter information for ${argv} stderr: ${result.stderr}`);
return;
}
const json: PythonEnvInfo = JSON.parse(result.stdout.trim());
const rawVersion = `${json.versionInfo.slice(0, 3).join('.')}-${json.versionInfo[3]}`;
return {
path: pythonPath,
version: parsePythonVersion(rawVersion),
sysVersion: json.sysVersion,
sysPrefix: json.sysPrefix
};
} catch (ex) {
traceError('Failed to get Activated env Variables');
return undefined;
}
})();
interpreterInfoCache.set(pythonPath, promise);
return promise;
}

const result = await processService.shellExec(quoted, { timeout: 1_500 });
if (result.stderr) {
traceError(`Failed to parse interpreter information for ${argv} stderr: ${result.stderr}`);
return;
const envVariables = new Map<string, Promise<NodeJS.ProcessEnv | undefined>>();
export async function getActivatedEnvVariables(pythonPath: string): Promise<NodeJS.ProcessEnv | undefined> {
if (envVariables.has(pythonPath)) {
return envVariables.get(pythonPath);
}
const json: PythonEnvInfo = JSON.parse(result.stdout.trim());
const rawVersion = `${json.versionInfo.slice(0, 3).join('.')}-${json.versionInfo[3]}`;
return {
path: pythonPath,
version: parsePythonVersion(rawVersion),
sysVersion: json.sysVersion,
sysPrefix: json.sysPrefix
};
const promise = (async () => {
const cli = await getPythonCli(pythonPath);
const processService = new ProcessService(new BufferDecoder());
const argv = [...cli, path.join(SCRIPTS_DIR, 'printEnvVariables.py')];
const cmd = argv.reduce((p, c) => (p ? `${p} "${c}"` : `"${c.replace('\\', '/')}"`), '');
const result = await processService.shellExec(cmd, {
timeout: 1_500,
maxBuffer: 1000 * 1000,
throwOnStdErr: false,
env: process.env,
shell: defaultShell
});
if (result.stderr && result.stderr.length) {
traceError(`Failed to parse interpreter information for ${argv} stderr: ${result.stderr}`);
return;
}
try {
return JSON.parse(result.stdout.trim());
} catch (ex) {
traceError(`Failed to parse interpreter information for ${argv}`, ex);
}
})();
envVariables.set(pythonPath, promise);
return promise;
}

async function getPythonCli(pythonPath: string) {
Expand All @@ -55,11 +112,11 @@ async function getPythonCli(pythonPath: string) {
}

const condaFile = await getCondaFile();
return [condaFile, ...runArgs, 'python'];
return [condaFile.fileToCommandArgument(), ...runArgs, 'python'];
} catch {
// Noop.
}
traceError('Using Conda Interpreter, but no conda');
}
return [pythonPath];
return [pythonPath.fileToCommandArgument()];
}
18 changes: 15 additions & 3 deletions src/test/interpreters/interpreterService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { injectable } from 'inversify';
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

Expand All @@ -6,12 +7,23 @@ import { getInterpreterInfo } from '.';
import { IInterpreterService } from '../../client/interpreter/contracts';
import { PythonEnvironment } from '../../client/pythonEnvironments/info';

@injectable()
export class InterpreterService implements IInterpreterService {
public readonly onDidChangeInterpreter = new EventEmitter<void>().event;

public async getInterpreters(resource?: Uri): Promise<PythonEnvironment[]> {
const active = await this.getActiveInterpreter(resource);
return active ? [active] : [];
public async getInterpreters(_resource?: Uri): Promise<PythonEnvironment[]> {
const [active, globalInterpreter] = await Promise.all([
getInterpreterInfo(process.env.CI_PYTHON_PATH as string),
getInterpreterInfo('python')
]);
const interpreters: PythonEnvironment[] = [];
if (active) {
interpreters.push(active);
}
if (globalInterpreter) {
interpreters.push(globalInterpreter);
}
return interpreters;
}

public async getActiveInterpreter(_resource?: Uri): Promise<PythonEnvironment | undefined> {
Expand Down
23 changes: 23 additions & 0 deletions src/test/interpreters/selector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { inject, injectable } from 'inversify';
import { Resource } from '../../client/common/types';
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { IInterpreterQuickPickItem, IInterpreterSelector } from '../../client/interpreter/configuration/types';
import { IInterpreterService } from '../../client/interpreter/contracts';

@injectable()
export class InterpreterSelector implements IInterpreterSelector {
constructor(@inject(IInterpreterService) private readonly interpreterService: IInterpreterService) {}

public async getSuggestions(resource: Resource): Promise<IInterpreterQuickPickItem[]> {
const interpreters = await this.interpreterService.getInterpreters(resource);
return interpreters.map((item) => ({
label: item.displayName || item.path,
description: item.displayName || item.path,
detail: item.displayName || item.path,
path: item.path,
interpreter: item
}));
}
}
24 changes: 24 additions & 0 deletions src/test/interpreters/winStoreInterpreter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { injectable } from 'inversify';
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { IWindowsStoreInterpreter } from '../../client/interpreter/locators/types';

@injectable()
export class WindowsStoreInterpreter implements IWindowsStoreInterpreter {
/**
* Whether this is a Windows Store/App Interpreter.
*
* @param {string} pythonPath
* @returns {boolean}
* @memberof WindowsStoreInterpreter
*/
public async isWindowsStoreInterpreter(pythonPath: string): Promise<boolean> {
const pythonPathToCompare = pythonPath.toUpperCase().replace(/\//g, '\\');
return (
pythonPathToCompare.includes('\\Microsoft\\WindowsApps\\'.toUpperCase()) ||
pythonPathToCompare.includes('\\Program Files\\WindowsApps\\'.toUpperCase()) ||
pythonPathToCompare.includes('\\Microsoft\\WindowsApps\\PythonSoftwareFoundation'.toUpperCase())
);
}
}