Skip to content

Commit 435876b

Browse files
committed
unit tests
1 parent 3559536 commit 435876b

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

src/client/repl/variables/variablesProvider.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class VariablesProvider implements NotebookVariableProvider {
3838
const notebook = this.getNotebookDocument();
3939
if (notebook) {
4040
this.executionCount += 1;
41-
if (isEnabled()) {
41+
if (isEnabled(notebook.uri)) {
4242
this._onDidChangeVariables.fire(notebook);
4343
}
4444
}
@@ -52,7 +52,12 @@ export class VariablesProvider implements NotebookVariableProvider {
5252
token: CancellationToken,
5353
): AsyncIterable<VariablesResult> {
5454
const notebookDocument = this.getNotebookDocument();
55-
if (!isEnabled() || token.isCancellationRequested || !notebookDocument || notebookDocument !== notebook) {
55+
if (
56+
!isEnabled(notebook.uri) ||
57+
token.isCancellationRequested ||
58+
!notebookDocument ||
59+
notebookDocument !== notebook
60+
) {
5661
return;
5762
}
5863

src/test/repl/variableProvider.test.ts

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,29 @@
22
// Licensed under the MIT License.
33

44
import { 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';
615
import * as TypeMoq from 'typemoq';
716
import { IVariableDescription } from '../../client/repl/variables/types';
817
import { VariablesProvider } from '../../client/repl/variables/variablesProvider';
918
import { 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

Comments
 (0)