Skip to content

Commit 2b7cbaa

Browse files
authored
Variable explorer and mime test fixes for tests (microsoft#7740)
* Change variable explorer tests to wait for variable explorer complete * Fix native mime test * Review feedback * Try to make 2.7 tests pass
1 parent 311d91d commit 2b7cbaa

5 files changed

Lines changed: 37 additions & 30 deletions

File tree

pythonFiles/datascience/getJupyterVariableValue.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ def __call__(self, obj):
8484
return ''.join((x.encode('utf-8') if isinstance(x, unicode) else x) for x in self._repr(obj, 0))
8585
else:
8686
return ''.join(self._repr(obj, 0))
87-
except Exception:
87+
except Exception as e:
8888
try:
89-
return 'An exception was raised: %r' % sys.exc_info()[1]
89+
return 'An exception was raised: ' + str(e)
9090
except Exception:
9191
return 'An exception was raised'
9292

@@ -373,7 +373,7 @@ def _bytes_as_unicode_if_possible(self, obj_repr):
373373
# locale.getpreferredencoding() and 'utf-8). If no encoding can decode
374374
# the input, we return the original bytes.
375375
try_encodings = []
376-
encoding = self.sys_stdout_encoding or getattr(sys.stdout, 'encoding', '')
376+
encoding = self.sys_stdout_encoding or getattr(VC_sys.stdout, 'encoding', '')
377377
if encoding:
378378
try_encodings.append(encoding.lower())
379379

src/client/datascience/interactive-common/interactiveWindowTypes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export namespace InteractiveWindowMessages {
6969
export const NotebookClean = 'clean';
7070
export const SaveAll = 'save_all';
7171
export const NativeCommand = 'native_command';
72+
export const VariablesComplete = 'variables_complete';
7273

7374
}
7475

@@ -301,4 +302,5 @@ export class IInteractiveWindowMapping {
301302
public [InteractiveWindowMessages.NotebookClean]: never | undefined;
302303
public [InteractiveWindowMessages.SaveAll]: ISaveAll;
303304
public [InteractiveWindowMessages.NativeCommand]: INativeCommand;
305+
public [InteractiveWindowMessages.VariablesComplete]: never | undefined;
304306
}

src/datascience-ui/interactive-common/mainStateController.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,8 @@ export class MainStateController implements IMessageHandler {
636636
}
637637

638638
public renderUpdate(newState: {}) {
639+
const oldCount = this.state.pendingVariableCount;
640+
639641
// This method should be called during the render stage of anything
640642
// using this state Controller. That's because after shouldComponentUpdate
641643
// render is next and at this point the state has been set.
@@ -648,6 +650,11 @@ export class MainStateController implements IMessageHandler {
648650
if ('cellVMs' in newState) {
649651
this.sendInfo();
650652
}
653+
654+
// If the new state includes pendingVariableCount and it's gone to zero, send a message
655+
if (this.state.pendingVariableCount === 0 && oldCount !== 0) {
656+
setTimeout(() => this.sendMessage(InteractiveWindowMessages.VariablesComplete), 1);
657+
}
651658
}
652659

653660
public getState(): IMainState {

src/test/datascience/nativeEditor.functional.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ for _ in range(50):
127127
await addCell(wrapper, matPlotLib, true, 5);
128128
verifyHtmlOnCell(wrapper, 'NativeCell', matPlotLibResults, CellPosition.Last);
129129

130-
await addCell(wrapper, spinningCursor, true, 3 + (ioc.mockJupyter ? (cursors.length * 3) : 0));
130+
await addCell(wrapper, spinningCursor, true, 3 + (ioc.mockJupyter ? (cursors.length * 3) : 50));
131131
verifyHtmlOnCell(wrapper, 'NativeCell', '<div>', CellPosition.Last);
132132
}, () => { return ioc; });
133133

src/test/datascience/variableexplorer.functional.test.tsx

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { parse } from 'node-html-parser';
88
import * as React from 'react';
99
import { Disposable } from 'vscode';
1010

11+
import { InteractiveWindowMessages } from '../../client/datascience/interactive-common/interactiveWindowTypes';
1112
import { IJupyterVariable } from '../../client/datascience/types';
1213
import { InteractivePanel } from '../../datascience-ui/history-react/interactivePanel';
1314
import { VariableExplorer } from '../../datascience-ui/interactive-common/variableExplorer';
@@ -16,7 +17,7 @@ import { DataScienceIocContainer } from './dataScienceIocContainer';
1617
import { addCode } from './interactiveWindowTestHelpers';
1718
import { addCell, createNewEditor } from './nativeEditorTestHelpers';
1819
import { waitForUpdate } from './reactHelpers';
19-
import { runDoubleTest } from './testHelpers';
20+
import { runDoubleTest, waitForMessage } from './testHelpers';
2021

2122
// tslint:disable:max-func-body-length trailing-comma no-any no-multiline-string
2223
suite('DataScience Interactive Window variable explorer tests', () => {
@@ -60,10 +61,17 @@ suite('DataScience Interactive Window variable explorer tests', () => {
6061
// asyncDump();
6162
//});
6263

63-
async function addCodeImpartial(wrapper: ReactWrapper<any, Readonly<{}>, React.Component>, code: string, expectedRenderCount: number = 4, expectError: boolean = false): Promise<ReactWrapper<any, Readonly<{}>, React.Component>> {
64+
async function waitForVariablesUpdated(): Promise<void> {
65+
return waitForMessage(ioc, InteractiveWindowMessages.VariablesComplete);
66+
}
67+
68+
async function addCodeImpartial(wrapper: ReactWrapper<any, Readonly<{}>, React.Component>, code: string, waitForVariables: boolean = true, expectedRenderCount: number = 4, expectError: boolean = false): Promise<ReactWrapper<any, Readonly<{}>, React.Component>> {
69+
const variablesUpdated = waitForVariables ? waitForVariablesUpdated() : Promise.resolve();
6470
const nodes = wrapper.find('InteractivePanel');
6571
if (nodes.length > 0) {
66-
return addCode(ioc, wrapper, code, expectedRenderCount, expectError);
72+
const result = await addCode(ioc, wrapper, code, expectedRenderCount, expectError);
73+
await variablesUpdated;
74+
return result;
6775
} else {
6876
// For the native editor case, we need to create an editor before hand.
6977
if (!createdNotebook) {
@@ -72,6 +80,7 @@ suite('DataScience Interactive Window variable explorer tests', () => {
7280
expectedRenderCount += 1;
7381
}
7482
await addCell(wrapper, code, true, expectedRenderCount);
83+
await variablesUpdated;
7584
return wrapper;
7685
}
7786
}
@@ -85,8 +94,7 @@ value = 'hello world'`;
8594
openVariableExplorer(wrapper);
8695

8796
await addCodeImpartial(wrapper, 'a=1\na');
88-
await addCodeImpartial(wrapper, basicCode, 4);
89-
await waitForUpdate(wrapper, VariableExplorer, 3);
97+
await addCodeImpartial(wrapper, basicCode, true, 4);
9098

9199
// We should show a string and show an int, the modules should be hidden
92100
let targetVariables: IJupyterVariable[] = [
@@ -100,8 +108,7 @@ value = 'hello world'`;
100108
ioc.getSettings().datascience.variableExplorerExclude = `${ioc.getSettings().datascience.variableExplorerExclude};str`;
101109

102110
// Add another string and check our vars, strings should be hidden
103-
await addCodeImpartial(wrapper, basicCode2, 4);
104-
await waitForUpdate(wrapper, VariableExplorer, 2);
111+
await addCodeImpartial(wrapper, basicCode2, true, 4);
105112

106113
targetVariables = [
107114
{name: 'a', value: '1', supportsDataExplorer: false, type: 'int', size: 54, shape: '', count: 0, truncated: false}
@@ -116,7 +123,6 @@ value = 'hello world'`;
116123
openVariableExplorer(wrapper);
117124

118125
await addCodeImpartial(wrapper, 'a=1\na');
119-
await waitForUpdate(wrapper, VariableExplorer, 2);
120126

121127
// Check that we have just the 'a' variable
122128
let targetVariables: IJupyterVariable[] = [
@@ -125,8 +131,7 @@ value = 'hello world'`;
125131
verifyVariables(wrapper, targetVariables);
126132

127133
// Add another variable and check it
128-
await addCodeImpartial(wrapper, basicCode, 4);
129-
await waitForUpdate(wrapper, VariableExplorer, 3);
134+
await addCodeImpartial(wrapper, basicCode, true, 4);
130135

131136
targetVariables = [
132137
{name: 'a', value: '1', supportsDataExplorer: false, type: 'int', size: 54, shape: '', count: 0, truncated: false},
@@ -136,8 +141,7 @@ value = 'hello world'`;
136141
verifyVariables(wrapper, targetVariables);
137142

138143
// Add a second variable and check it
139-
await addCodeImpartial(wrapper, basicCode2, 4);
140-
await waitForUpdate(wrapper, VariableExplorer, 4);
144+
await addCodeImpartial(wrapper, basicCode2, true, 4);
141145

142146
targetVariables = [
143147
{name: 'a', value: '1', supportsDataExplorer: false, type: 'int', size: 54, shape: '', count: 0, truncated: false},
@@ -155,7 +159,7 @@ value = 'hello world'`;
155159
openVariableExplorer(wrapper);
156160

157161
await addCodeImpartial(wrapper, 'a=1\na');
158-
await addCodeImpartial(wrapper, basicCode, 4);
162+
await addCodeImpartial(wrapper, basicCode, false, 4);
159163

160164
// Here we are only going to wait for two renders instead of the needed three
161165
// a should have the value updated, but value should still be loading
@@ -187,11 +191,7 @@ myDict = {'a': 1}`;
187191
openVariableExplorer(wrapper);
188192

189193
await addCodeImpartial(wrapper, 'a=1\na');
190-
await addCodeImpartial(wrapper, basicCode, 4);
191-
192-
// Verify that we actually update the variable explorer
193-
// Count here is our main render + a render for each variable row as they come in
194-
await waitForUpdate(wrapper, VariableExplorer, 5);
194+
await addCodeImpartial(wrapper, basicCode, true, 4);
195195

196196
const targetVariables: IJupyterVariable[] = [
197197
{name: 'a', value: '1', supportsDataExplorer: false, type: 'int', size: 54, shape: '', count: 0, truncated: false},
@@ -219,11 +219,7 @@ myTuple = 1,2,3,4,5,6,7,8,9
219219
openVariableExplorer(wrapper);
220220

221221
await addCodeImpartial(wrapper, 'a=1\na');
222-
await addCodeImpartial(wrapper, basicCode, 4);
223-
224-
// Verify that we actually update the variable explorer
225-
// Count here is our main render + a render for each variable row as they come in
226-
await waitForUpdate(wrapper, VariableExplorer, 9);
222+
await addCodeImpartial(wrapper, basicCode, true, 4);
227223

228224
const targetVariables: IJupyterVariable[] = [
229225
{name: 'a', value: '1', supportsDataExplorer: false, type: 'int', size: 54, shape: '', count: 0, truncated: false},
@@ -255,9 +251,7 @@ strc = 'c'`;
255251
openVariableExplorer(wrapper);
256252

257253
await addCodeImpartial(wrapper, 'a=1\na');
258-
await addCodeImpartial(wrapper, basicCode, 4);
259-
260-
await waitForUpdate(wrapper, VariableExplorer, 7);
254+
await addCodeImpartial(wrapper, basicCode, true, 4);
261255

262256
let targetVariables: IJupyterVariable[] = [
263257
{name: 'a', value: '1', supportsDataExplorer: false, type: 'int', size: 54, shape: '', count: 0, truncated: false},
@@ -318,6 +312,10 @@ function sortVariableExplorer(wrapper: ReactWrapper<any, Readonly<{}>, React.Com
318312

319313
// Verify a set of rows versus a set of expected variables
320314
function verifyVariables(wrapper: ReactWrapper<any, Readonly<{}>, React.Component>, targetVariables: IJupyterVariable[]) {
315+
// Force an update so we render whatever the current state is
316+
wrapper.update();
317+
318+
// Then search for results.
321319
const foundRows = wrapper.find('div.react-grid-Row');
322320

323321
expect(foundRows.length).to.be.equal(targetVariables.length, 'Different number of variable explorer rows and target variables');

0 commit comments

Comments
 (0)