|
| 1 | +// Create test suite and test cases for the `replUtils` module |
| 2 | +import * as TypeMoq from 'typemoq'; |
| 3 | +import { Disposable } from 'vscode'; |
| 4 | +import * as sinon from 'sinon'; |
| 5 | +import { expect } from 'chai'; |
| 6 | +import { IInterpreterService } from '../../client/interpreter/contracts'; |
| 7 | +import { ICommandManager } from '../../client/common/application/types'; |
| 8 | +import { ICodeExecutionHelper } from '../../client/terminals/types'; |
| 9 | +import * as replCommands from '../../client/repl/replCommands'; |
| 10 | +import * as replUtils from '../../client/repl/replUtils'; |
| 11 | +import * as nativeRepl from '../../client/repl/nativeRepl'; |
| 12 | +import { Commands } from '../../client/common/constants'; |
| 13 | +import { PythonEnvironment } from '../../client/pythonEnvironments/info'; |
| 14 | + |
| 15 | +suite('REPL - register native repl command', () => { |
| 16 | + let interpreterService: TypeMoq.IMock<IInterpreterService>; |
| 17 | + let commandManager: TypeMoq.IMock<ICommandManager>; |
| 18 | + let executionHelper: TypeMoq.IMock<ICodeExecutionHelper>; |
| 19 | + let getSendToNativeREPLSettingStub: sinon.SinonStub; |
| 20 | + // @ts-ignore: TS6133 |
| 21 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 22 | + let registerCommandSpy: sinon.SinonSpy; |
| 23 | + let executeInTerminalStub: sinon.SinonStub; |
| 24 | + let getNativeReplStub: sinon.SinonStub; |
| 25 | + let disposable: TypeMoq.IMock<Disposable>; |
| 26 | + let disposableArray: Disposable[] = []; |
| 27 | + setup(() => { |
| 28 | + interpreterService = TypeMoq.Mock.ofType<IInterpreterService>(); |
| 29 | + commandManager = TypeMoq.Mock.ofType<ICommandManager>(); |
| 30 | + executionHelper = TypeMoq.Mock.ofType<ICodeExecutionHelper>(); |
| 31 | + commandManager |
| 32 | + .setup((cm) => cm.registerCommand(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny())) |
| 33 | + .returns(() => TypeMoq.Mock.ofType<Disposable>().object); |
| 34 | + |
| 35 | + getSendToNativeREPLSettingStub = sinon.stub(replUtils, 'getSendToNativeREPLSetting'); |
| 36 | + getSendToNativeREPLSettingStub.returns(false); |
| 37 | + executeInTerminalStub = sinon.stub(replUtils, 'executeInTerminal'); |
| 38 | + executeInTerminalStub.returns(Promise.resolve()); |
| 39 | + registerCommandSpy = sinon.spy(commandManager.object, 'registerCommand'); |
| 40 | + disposable = TypeMoq.Mock.ofType<Disposable>(); |
| 41 | + disposableArray = [disposable.object]; |
| 42 | + }); |
| 43 | + |
| 44 | + teardown(() => { |
| 45 | + sinon.restore(); |
| 46 | + disposableArray.forEach((d) => { |
| 47 | + if (d) { |
| 48 | + d.dispose(); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + disposableArray = []; |
| 53 | + }); |
| 54 | + |
| 55 | + test('Ensure repl command is registered', async () => { |
| 56 | + interpreterService |
| 57 | + .setup((i) => i.getActiveInterpreter(TypeMoq.It.isAny())) |
| 58 | + .returns(() => Promise.resolve(({ path: 'ps' } as unknown) as PythonEnvironment)); |
| 59 | + |
| 60 | + await replCommands.registerReplCommands( |
| 61 | + disposableArray, |
| 62 | + interpreterService.object, |
| 63 | + executionHelper.object, |
| 64 | + commandManager.object, |
| 65 | + ); |
| 66 | + |
| 67 | + commandManager.verify( |
| 68 | + (c) => c.registerCommand(TypeMoq.It.isAny(), TypeMoq.It.isAny()), |
| 69 | + TypeMoq.Times.atLeastOnce(), |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + test('Ensure getSendToNativeREPLSetting is called', async () => { |
| 74 | + interpreterService |
| 75 | + .setup((i) => i.getActiveInterpreter(TypeMoq.It.isAny())) |
| 76 | + .returns(() => Promise.resolve(({ path: 'ps' } as unknown) as PythonEnvironment)); |
| 77 | + |
| 78 | + let commandHandler: undefined | (() => Promise<void>); |
| 79 | + commandManager |
| 80 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 81 | + .setup((c) => c.registerCommand as any) |
| 82 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 83 | + .returns(() => (command: string, callback: (...args: any[]) => any, _thisArg?: any) => { |
| 84 | + if (command === Commands.Exec_In_REPL) { |
| 85 | + commandHandler = callback; |
| 86 | + } |
| 87 | + // eslint-disable-next-line no-void |
| 88 | + return { dispose: () => void 0 }; |
| 89 | + }); |
| 90 | + replCommands.registerReplCommands( |
| 91 | + disposableArray, |
| 92 | + interpreterService.object, |
| 93 | + executionHelper.object, |
| 94 | + commandManager.object, |
| 95 | + ); |
| 96 | + |
| 97 | + expect(commandHandler).not.to.be.an('undefined', 'Command handler not initialized'); |
| 98 | + |
| 99 | + await commandHandler!(); |
| 100 | + |
| 101 | + sinon.assert.calledOnce(getSendToNativeREPLSettingStub); |
| 102 | + }); |
| 103 | + |
| 104 | + test('Ensure executeInTerminal is called when getSendToNativeREPLSetting returns false', async () => { |
| 105 | + interpreterService |
| 106 | + .setup((i) => i.getActiveInterpreter(TypeMoq.It.isAny())) |
| 107 | + .returns(() => Promise.resolve(({ path: 'ps' } as unknown) as PythonEnvironment)); |
| 108 | + getSendToNativeREPLSettingStub.returns(false); |
| 109 | + |
| 110 | + let commandHandler: undefined | (() => Promise<void>); |
| 111 | + commandManager |
| 112 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 113 | + .setup((c) => c.registerCommand as any) |
| 114 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 115 | + .returns(() => (command: string, callback: (...args: any[]) => any, _thisArg?: any) => { |
| 116 | + if (command === Commands.Exec_In_REPL) { |
| 117 | + commandHandler = callback; |
| 118 | + } |
| 119 | + // eslint-disable-next-line no-void |
| 120 | + return { dispose: () => void 0 }; |
| 121 | + }); |
| 122 | + replCommands.registerReplCommands( |
| 123 | + disposableArray, |
| 124 | + interpreterService.object, |
| 125 | + executionHelper.object, |
| 126 | + commandManager.object, |
| 127 | + ); |
| 128 | + |
| 129 | + expect(commandHandler).not.to.be.an('undefined', 'Command handler not initialized'); |
| 130 | + |
| 131 | + await commandHandler!(); |
| 132 | + |
| 133 | + sinon.assert.calledOnce(executeInTerminalStub); |
| 134 | + }); |
| 135 | + |
| 136 | + test('Ensure we call getNativeREPL() when interpreter exist', async () => { |
| 137 | + interpreterService |
| 138 | + .setup((i) => i.getActiveInterpreter(TypeMoq.It.isAny())) |
| 139 | + .returns(() => Promise.resolve(({ path: 'ps' } as unknown) as PythonEnvironment)); |
| 140 | + getSendToNativeREPLSettingStub.returns(true); |
| 141 | + getNativeReplStub = sinon.stub(nativeRepl, 'getNativeRepl'); |
| 142 | + |
| 143 | + let commandHandler: undefined | ((uri: string) => Promise<void>); |
| 144 | + commandManager |
| 145 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 146 | + .setup((c) => c.registerCommand as any) |
| 147 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 148 | + .returns(() => (command: string, callback: (...args: any[]) => any, _thisArg?: any) => { |
| 149 | + if (command === Commands.Exec_In_REPL) { |
| 150 | + commandHandler = callback; |
| 151 | + } |
| 152 | + // eslint-disable-next-line no-void |
| 153 | + return { dispose: () => void 0 }; |
| 154 | + }); |
| 155 | + replCommands.registerReplCommands( |
| 156 | + disposableArray, |
| 157 | + interpreterService.object, |
| 158 | + executionHelper.object, |
| 159 | + commandManager.object, |
| 160 | + ); |
| 161 | + |
| 162 | + expect(commandHandler).not.to.be.an('undefined', 'Command handler not initialized'); |
| 163 | + |
| 164 | + await commandHandler!('uri'); |
| 165 | + sinon.assert.calledOnce(getNativeReplStub); |
| 166 | + }); |
| 167 | + |
| 168 | + test('Ensure we do not call getNativeREPL() when interpreter does not exist', async () => { |
| 169 | + getNativeReplStub = sinon.stub(nativeRepl, 'getNativeRepl'); |
| 170 | + getSendToNativeREPLSettingStub.returns(true); |
| 171 | + |
| 172 | + interpreterService |
| 173 | + .setup((i) => i.getActiveInterpreter(TypeMoq.It.isAny())) |
| 174 | + .returns(() => Promise.resolve(undefined)); |
| 175 | + |
| 176 | + let commandHandler: undefined | ((uri: string) => Promise<void>); |
| 177 | + commandManager |
| 178 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 179 | + .setup((c) => c.registerCommand as any) |
| 180 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 181 | + .returns(() => (command: string, callback: (...args: any[]) => any, _thisArg?: any) => { |
| 182 | + if (command === Commands.Exec_In_REPL) { |
| 183 | + commandHandler = callback; |
| 184 | + } |
| 185 | + // eslint-disable-next-line no-void |
| 186 | + return { dispose: () => void 0 }; |
| 187 | + }); |
| 188 | + interpreterService |
| 189 | + .setup((i) => i.getActiveInterpreter(TypeMoq.It.isAny())) |
| 190 | + .returns(() => Promise.resolve(undefined)); |
| 191 | + |
| 192 | + replCommands.registerReplCommands( |
| 193 | + disposableArray, |
| 194 | + interpreterService.object, |
| 195 | + executionHelper.object, |
| 196 | + commandManager.object, |
| 197 | + ); |
| 198 | + |
| 199 | + expect(commandHandler).not.to.be.an('undefined', 'Command handler not initialized'); |
| 200 | + |
| 201 | + await commandHandler!('uri'); |
| 202 | + sinon.assert.notCalled(getNativeReplStub); |
| 203 | + }); |
| 204 | +}); |
0 commit comments