22// Licensed under the MIT License.
33
44import { assert } from 'chai' ;
5- import { NotebookDocument , CancellationTokenSource , VariablesResult , Variable , EventEmitter } from 'vscode' ;
5+ import sinon from 'sinon' ;
6+ import {
7+ NotebookDocument ,
8+ CancellationTokenSource ,
9+ VariablesResult ,
10+ Variable ,
11+ EventEmitter ,
12+ ConfigurationScope ,
13+ WorkspaceConfiguration ,
14+ } from 'vscode' ;
615import * as TypeMoq from 'typemoq' ;
716import { IVariableDescription } from '../../client/repl/variables/types' ;
817import { VariablesProvider } from '../../client/repl/variables/variablesProvider' ;
918import { VariableRequester } from '../../client/repl/variables/variableRequester' ;
19+ import * as workspaceApis from '../../client/common/vscodeApis/workspaceApis' ;
1020
11- suite ( 'ReplVariablesProvider' , ( ) => {
21+ suite . only ( 'ReplVariablesProvider' , ( ) => {
1222 let provider : VariablesProvider ;
1323 let varRequester : TypeMoq . IMock < VariableRequester > ;
1424 let notebook : TypeMoq . IMock < NotebookDocument > ;
25+ let getConfigurationStub : sinon . SinonStub ;
26+ let configMock : TypeMoq . IMock < WorkspaceConfiguration > ;
27+ let enabled : boolean ;
1528 const executionEventEmitter = new EventEmitter < void > ( ) ;
1629 const cancellationToken = new CancellationTokenSource ( ) . token ;
1730
@@ -68,9 +81,23 @@ suite('ReplVariablesProvider', () => {
6881 }
6982
7083 setup ( ( ) => {
84+ enabled = true ;
7185 varRequester = TypeMoq . Mock . ofType < VariableRequester > ( ) ;
7286 notebook = TypeMoq . Mock . ofType < NotebookDocument > ( ) ;
7387 provider = new VariablesProvider ( varRequester . object , ( ) => notebook . object , executionEventEmitter . event ) ;
88+ configMock = TypeMoq . Mock . ofType < WorkspaceConfiguration > ( ) ;
89+ configMock . setup ( ( c ) => c . get < boolean > ( 'REPL.provideVariables' ) ) . returns ( ( ) => enabled ) ;
90+ getConfigurationStub = sinon . stub ( workspaceApis , 'getConfiguration' ) ;
91+ getConfigurationStub . callsFake ( ( section ?: string , _scope ?: ConfigurationScope | null ) => {
92+ if ( section === 'python' ) {
93+ return configMock . object ;
94+ }
95+ return undefined ;
96+ } ) ;
97+ } ) ;
98+
99+ teardown ( ( ) => {
100+ sinon . restore ( ) ;
74101 } ) ;
75102
76103 test ( 'provideVariables without parent should yield variables' , async ( ) => {
@@ -84,6 +111,38 @@ suite('ReplVariablesProvider', () => {
84111 assert . equal ( results [ 0 ] . variable . expression , 'myObject' ) ;
85112 } ) ;
86113
114+ test ( 'No variables are returned when variable provider is disabled' , async ( ) => {
115+ enabled = false ;
116+ setVariablesForParent ( undefined , [ objectVariable ] ) ;
117+
118+ const results = await provideVariables ( undefined ) ;
119+
120+ assert . isEmpty ( results ) ;
121+ } ) ;
122+
123+ test ( 'No change event from provider when disabled' , async ( ) => {
124+ enabled = false ;
125+ let eventFired = false ;
126+ provider . onDidChangeVariables ( ( ) => {
127+ eventFired = true ;
128+ } ) ;
129+
130+ executionEventEmitter . fire ( ) ;
131+
132+ assert . isFalse ( eventFired , 'event should not have fired' ) ;
133+ } ) ;
134+
135+ test ( 'Variables change event from provider should fire when execution happens' , async ( ) => {
136+ let eventFired = false ;
137+ provider . onDidChangeVariables ( ( ) => {
138+ eventFired = true ;
139+ } ) ;
140+
141+ executionEventEmitter . fire ( ) ;
142+
143+ assert . isTrue ( eventFired , 'event should have fired' ) ;
144+ } ) ;
145+
87146 test ( 'provideVariables with a parent should call get children correctly' , async ( ) => {
88147 const listVariableItems = [ 0 , 1 , 2 ] . map ( createListItem ) ;
89148 setVariablesForParent ( undefined , [ objectVariable ] ) ;
0 commit comments