11// Create test suite and test cases for the `replUtils` module
22import * as TypeMoq from 'typemoq' ;
3- import { Disposable } from 'vscode' ;
3+ import { commands , Disposable , Uri } from 'vscode' ;
44import * as sinon from 'sinon' ;
55import { expect } from 'chai' ;
66import { IInterpreterService } from '../../client/interpreter/contracts' ;
@@ -9,6 +9,7 @@ import { ICodeExecutionHelper } from '../../client/terminals/types';
99import * as replCommands from '../../client/repl/replCommands' ;
1010import * as replUtils from '../../client/repl/replUtils' ;
1111import * as nativeRepl from '../../client/repl/nativeRepl' ;
12+ import * as windowApis from '../../client/common/vscodeApis/windowApis' ;
1213import { Commands } from '../../client/common/constants' ;
1314import { PythonEnvironment } from '../../client/pythonEnvironments/info' ;
1415
@@ -203,3 +204,47 @@ suite('REPL - register native repl command', () => {
203204 sinon . assert . notCalled ( getNativeReplStub ) ;
204205 } ) ;
205206} ) ;
207+
208+ suite ( 'Native REPL getActiveInterpreter' , ( ) => {
209+ let interpreterService : TypeMoq . IMock < IInterpreterService > ;
210+ let executeCommandStub : sinon . SinonStub ;
211+ let getActiveResourceStub : sinon . SinonStub ;
212+
213+ setup ( ( ) => {
214+ interpreterService = TypeMoq . Mock . ofType < IInterpreterService > ( ) ;
215+ executeCommandStub = sinon . stub ( commands , 'executeCommand' ) . resolves ( undefined ) ;
216+ getActiveResourceStub = sinon . stub ( windowApis , 'getActiveResource' ) ;
217+ } ) ;
218+
219+ teardown ( ( ) => {
220+ sinon . restore ( ) ;
221+ } ) ;
222+
223+ test ( 'Uses active resource when uri is undefined' , async ( ) => {
224+ const resource = Uri . file ( '/workspace/app.py' ) ;
225+ const expected = ( { path : 'ps' } as unknown ) as PythonEnvironment ;
226+ getActiveResourceStub . returns ( resource ) ;
227+ interpreterService
228+ . setup ( ( i ) => i . getActiveInterpreter ( TypeMoq . It . isValue ( resource ) ) )
229+ . returns ( ( ) => Promise . resolve ( expected ) ) ;
230+
231+ const result = await replUtils . getActiveInterpreter ( undefined , interpreterService . object ) ;
232+
233+ expect ( result ) . to . equal ( expected ) ;
234+ interpreterService . verify ( ( i ) => i . getActiveInterpreter ( TypeMoq . It . isValue ( resource ) ) , TypeMoq . Times . once ( ) ) ;
235+ sinon . assert . notCalled ( executeCommandStub ) ;
236+ } ) ;
237+
238+ test ( 'Triggers environment selection using active resource when interpreter is missing' , async ( ) => {
239+ const resource = Uri . file ( '/workspace/app.py' ) ;
240+ getActiveResourceStub . returns ( resource ) ;
241+ interpreterService
242+ . setup ( ( i ) => i . getActiveInterpreter ( TypeMoq . It . isValue ( resource ) ) )
243+ . returns ( ( ) => Promise . resolve ( undefined ) ) ;
244+
245+ const result = await replUtils . getActiveInterpreter ( undefined , interpreterService . object ) ;
246+
247+ expect ( result ) . to . equal ( undefined ) ;
248+ sinon . assert . calledWith ( executeCommandStub , Commands . TriggerEnvironmentSelection , resource ) ;
249+ } ) ;
250+ } ) ;
0 commit comments