Skip to content

Commit

Permalink
Fix typemoq on ts 5
Browse files Browse the repository at this point in the history
  • Loading branch information
rchiodo committed Aug 22, 2024
1 parent cb6b40f commit 23b161f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
45 changes: 23 additions & 22 deletions src/test/interpreters/interpreterService.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { ServiceManager } from '../../client/ioc/serviceManager';
import { PYTHON_PATH } from '../common';
import { MockAutoSelectionService } from '../mocks/autoSelector';
import * as proposedApi from '../../client/environmentApi';
import { createTypeMoq } from './mocks';

/* eslint-disable @typescript-eslint/no-explicit-any */

Expand Down Expand Up @@ -67,23 +68,23 @@ suite('Interpreters service', () => {
serviceManager = new ServiceManager(cont);
serviceContainer = new ServiceContainer(cont);

interpreterPathService = TypeMoq.Mock.ofType<IInterpreterPathService>();
updater = TypeMoq.Mock.ofType<IPythonPathUpdaterServiceManager>();
pyenvs = TypeMoq.Mock.ofType<IComponentAdapter>();
helper = TypeMoq.Mock.ofType<IInterpreterHelper>();
workspace = TypeMoq.Mock.ofType<IWorkspaceService>();
config = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
fileSystem = TypeMoq.Mock.ofType<IFileSystem>();
interpreterDisplay = TypeMoq.Mock.ofType<IInterpreterDisplay>();
persistentStateFactory = TypeMoq.Mock.ofType<IPersistentStateFactory>();
pythonExecutionFactory = TypeMoq.Mock.ofType<IPythonExecutionFactory>();
pythonExecutionService = TypeMoq.Mock.ofType<IPythonExecutionService>();
configService = TypeMoq.Mock.ofType<IConfigurationService>();
installer = TypeMoq.Mock.ofType<IInstaller>();
appShell = TypeMoq.Mock.ofType<IApplicationShell>();
experiments = TypeMoq.Mock.ofType<IExperimentService>();

pythonSettings = TypeMoq.Mock.ofType<IPythonSettings>();
interpreterPathService = createTypeMoq<IInterpreterPathService>();
updater = createTypeMoq<IPythonPathUpdaterServiceManager>();
pyenvs = createTypeMoq<IComponentAdapter>();
helper = createTypeMoq<IInterpreterHelper>();
workspace = createTypeMoq<IWorkspaceService>();
config = createTypeMoq<WorkspaceConfiguration>();
fileSystem = createTypeMoq<IFileSystem>();
interpreterDisplay = createTypeMoq<IInterpreterDisplay>();
persistentStateFactory = createTypeMoq<IPersistentStateFactory>();
pythonExecutionFactory = createTypeMoq<IPythonExecutionFactory>();
pythonExecutionService = createTypeMoq<IPythonExecutionService>();
configService = createTypeMoq<IConfigurationService>();
installer = createTypeMoq<IInstaller>();
appShell = createTypeMoq<IApplicationShell>();
experiments = createTypeMoq<IExperimentService>();

pythonSettings = createTypeMoq<IPythonSettings>();
pythonSettings.setup((s) => s.pythonPath).returns(() => PYTHON_PATH);
configService.setup((c) => c.getSettings(TypeMoq.It.isAny())).returns(() => pythonSettings.object);

Expand Down Expand Up @@ -166,7 +167,7 @@ suite('Interpreters service', () => {

test('Changes to active document should invoke interpreter.refresh method', async () => {
const service = new InterpreterService(serviceContainer, pyenvs.object);
const documentManager = TypeMoq.Mock.ofType<IDocumentManager>();
const documentManager = createTypeMoq<IDocumentManager>();

workspace.setup((w) => w.workspaceFolders).returns(() => [{ uri: '' }] as any);
let activeTextEditorChangeHandler: (e: TextEditor | undefined) => any | undefined;
Expand All @@ -179,9 +180,9 @@ suite('Interpreters service', () => {
serviceManager.addSingletonInstance(IDocumentManager, documentManager.object);

service.initialize();
const textEditor = TypeMoq.Mock.ofType<TextEditor>();
const textEditor = createTypeMoq<TextEditor>();
const uri = Uri.file(path.join('usr', 'file.py'));
const document = TypeMoq.Mock.ofType<TextDocument>();
const document = createTypeMoq<TextDocument>();
textEditor.setup((t) => t.document).returns(() => document.object);
document.setup((d) => d.uri).returns(() => uri);
activeTextEditorChangeHandler!(textEditor.object);
Expand All @@ -191,7 +192,7 @@ suite('Interpreters service', () => {

test('If there is no active document then interpreter.refresh should not be invoked', async () => {
const service = new InterpreterService(serviceContainer, pyenvs.object);
const documentManager = TypeMoq.Mock.ofType<IDocumentManager>();
const documentManager = createTypeMoq<IDocumentManager>();

workspace.setup((w) => w.workspaceFolders).returns(() => [{ uri: '' }] as any);
let activeTextEditorChangeHandler: (e?: TextEditor | undefined) => any | undefined;
Expand All @@ -211,7 +212,7 @@ suite('Interpreters service', () => {

test('Register the correct handler', async () => {
const service = new InterpreterService(serviceContainer, pyenvs.object);
const documentManager = TypeMoq.Mock.ofType<IDocumentManager>();
const documentManager = createTypeMoq<IDocumentManager>();

workspace.setup((w) => w.workspaceFolders).returns(() => [{ uri: '' }] as any);
let interpreterPathServiceHandler: (e: InterpreterConfigurationScope) => any | undefined = () => 0;
Expand Down
12 changes: 12 additions & 0 deletions src/test/interpreters/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IRegistry, RegistryHive } from '../../client/common/platform/types';
import { IPersistentState } from '../../client/common/types';
import { Architecture } from '../../client/common/utils/platform';
import { MockMemento } from '../mocks/mementos';
import * as TypeMoq from 'typemoq';

@injectable()
export class MockRegistry implements IRegistry {
Expand Down Expand Up @@ -56,3 +57,14 @@ export class MockState implements IPersistentState<any> {
this.data = data;
}
}

export function createTypeMoq<T>(tag?: string): TypeMoq.IMock<T> {
// Use typemoqs for those things that are resolved as promises. mockito doesn't allow nesting of mocks. ES6 Proxy class
// is the problem. We still need to make it thenable though. See this issue: https://github.com/florinn/typemoq/issues/67
const result = TypeMoq.Mock.ofType<T>();
if (tag !== undefined) {
(result as any).tag = tag;
}
result.setup((x: any) => x.then).returns(() => undefined);
return result;
}

0 comments on commit 23b161f

Please sign in to comment.