Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add some tests to verify round tripping
  • Loading branch information
rchiodo committed Sep 30, 2020
commit 8ca1b523f68347ce28280cc9d218366a5f72f9f3
8 changes: 8 additions & 0 deletions src/test/datascience/interactiveWindow.functional.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ import {
verifyLastCellInputState
} from './testHelpers';
import { ITestInteractiveWindowProvider } from './testInteractiveWindowProvider';
// tslint:disable-next-line: no-require-imports no-var-requires
const _escape = require('lodash/escape') as typeof import('lodash/escape'); // NOSONAR

// tslint:disable:max-func-body-length trailing-comma no-any no-multiline-string
suite('DataScience Interactive Window output tests', () => {
Expand Down Expand Up @@ -385,6 +387,9 @@ for _ in range(50):
time.sleep(0.1)
sys.stdout.write('\\r')`;

const exception = 'raise Exception("<html check>")';
addMockData(ioc, exception, `"<html check>"`, 'text/html', 'error');

addMockData(ioc, badPanda, `pandas has no attribute 'read'`, 'text/html', 'error');
addMockData(ioc, goodPanda, `<td>A table</td>`, 'text/html');
addMockData(ioc, matPlotLib, matPlotLibResults, 'text/html');
Expand All @@ -401,6 +406,9 @@ for _ in range(50):
return Promise.resolve({ result: result, haveMore: loops > 0 });
});

await addCode(ioc, exception, true);
verifyHtmlOnInteractiveCell(_escape(`<html check>`), CellPosition.Last);

await addCode(ioc, badPanda, true);
verifyHtmlOnInteractiveCell(`has no attribute 'read'`, CellPosition.Last);

Expand Down
128 changes: 128 additions & 0 deletions src/test/datascience/nativeEditor.functional.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { KeyPrefix } from '../../client/datascience/notebookStorage/nativeEditor
import {
ICell,
IDataScienceErrorHandler,
IDataScienceFileSystem,
IJupyterExecution,
INotebookEditor,
INotebookEditorProvider,
Expand Down Expand Up @@ -880,6 +881,133 @@ df.head()`;
verifyHtmlOnCell(ne.mount.wrapper, 'NativeCell', `3`, 2);
});

runMountedTest('Roundtrip with jupyter', async () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ace

// Write out a temporary file
const baseFile = `
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a='<1>'\\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b=2\\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c=3\\n",
"c"
]
}
],
"metadata": {
"file_extension": ".py",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
},
"mimetype": "text/x-python",
"name": "python",
"npconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": 3
},
"nbformat": 4,
"nbformat_minor": 2
}`;
addMockData(ioc, `a='<1>'\na`, `'<1>'`);
const dsfs = ioc.get<IDataScienceFileSystem>(IDataScienceFileSystem);
const tf = await dsfs.createTemporaryLocalFile('.ipynb');
try {
await dsfs.writeLocalFile(tf.filePath, baseFile);

// File should exist. Open and run all cells
const n = await openEditor(ioc, '', tf.filePath);
const threeCellsUpdated = n.mount.waitForMessage(InteractiveWindowMessages.ExecutionRendered, {
numberOfTimes: 3
});
n.editor.runAllCells();
await threeCellsUpdated;

// Save the file
const saveButton = findButton(n.mount.wrapper, NativeEditor, 8);
const saved = waitForMessage(ioc, InteractiveWindowMessages.NotebookClean);
saveButton!.simulate('click');
await saved;

// Read in the file contents. Should match the original
const savedContents = await dsfs.readLocalFile(tf.filePath);
assert.equal(savedContents, baseFile, 'File contents were changed by execution');
} finally {
tf.dispose();
}
});

runMountedTest('Startup and shutdown', async () => {
// Turn off raw kernel for this test as it's testing jupyterserver start / shutdown
ioc.setExperimentState(LocalZMQKernel.experiment, false);
Expand Down
6 changes: 2 additions & 4 deletions src/test/datascience/notebook.functional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { assert } from 'chai';
import { ChildProcess } from 'child_process';
import * as fs from 'fs-extra';
import { injectable } from 'inversify';
// tslint:disable-next-line: no-require-imports
import escape = require('lodash/escape');
import * as os from 'os';
import * as path from 'path';
import { SemVer } from 'semver';
Expand Down Expand Up @@ -1031,7 +1029,7 @@ a`,
mimeType: 'text/plain',
cellType: 'code',
result: `<a href=f>`,
verifyValue: (d) => assert.ok(d.includes(escape(`<a href=f>`)), 'XML not escaped')
verifyValue: (d) => assert.ok(d.includes(`<a href=f>`), 'Should not escape at the notebook level')
},
{
markdownRegEx: undefined,
Expand All @@ -1043,7 +1041,7 @@ df.head()`,
cellType: 'error',
// tslint:disable-next-line:quotemark
verifyValue: (d) =>
assert.ok((d as string).includes(escape("has no attribute 'read'")), 'Unexpected error result')
assert.ok((d as string).includes("has no attribute 'read'"), 'Unexpected error result')
},
{
markdownRegEx: undefined,
Expand Down
3 changes: 1 addition & 2 deletions src/test/datascience/testHelpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { min } from 'lodash';
import * as path from 'path';
import * as React from 'react';
import { Provider } from 'react-redux';
import { isString } from 'util';
import { CancellationToken } from 'vscode';

import { EXTENSION_ROOT_DIR } from '../../client/common/constants';
Expand Down Expand Up @@ -273,7 +272,7 @@ export function verifyHtmlOnCell(
output = targetCell!.find('div.markdown-cell-output');
}
const outputHtml = output.length > 0 ? output.html() : undefined;
if (html && isString(html)) {
if (html && typeof html === 'string') {
// Extract only the first 100 chars from the input string
const sliced = html.substr(0, min([html.length, 100]));
assert.ok(output.length > 0, 'No output cell found');
Expand Down