Merge branch 'master' into joh/celldocs

This commit is contained in:
Johannes Rieken
2020-08-10 18:24:01 +02:00
666 changed files with 18801 additions and 11371 deletions

View File

@@ -6,7 +6,7 @@
import 'mocha';
import * as assert from 'assert';
import * as vscode from 'vscode';
import { join } from 'path';
import { createRandomFile } from './utils';
export function timeoutAsync(n: number): Promise<void> {
return new Promise(resolve => {
@@ -56,6 +56,42 @@ async function splitEditor() {
await once;
}
async function saveFileAndCloseAll(resource: vscode.Uri) {
const documentClosed = new Promise((resolve, _reject) => {
const d = vscode.notebook.onDidCloseNotebookDocument(e => {
if (e.uri.toString() === resource.toString()) {
d.dispose();
resolve();
}
});
});
await vscode.commands.executeCommand('workbench.action.files.save');
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
await documentClosed;
}
async function saveAllFilesAndCloseAll(resource: vscode.Uri) {
const documentClosed = new Promise((resolve, _reject) => {
const d = vscode.notebook.onDidCloseNotebookDocument(e => {
if (e.uri.toString() === resource.toString()) {
d.dispose();
resolve();
}
});
});
await vscode.commands.executeCommand('workbench.action.files.saveAll');
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
await documentClosed;
}
function assertInitalState() {
// no-op unless we figure out why some documents are opened after the editor is closed
// assert.equal(vscode.notebook.activeNotebookEditor, undefined);
// assert.equal(vscode.notebook.notebookDocuments.length, 0);
// assert.equal(vscode.notebook.visibleNotebookEditors.length, 0);
}
suite('Notebook API tests', () => {
// test.only('crash', async function () {
// for (let i = 0; i < 200; i++) {
@@ -83,7 +119,9 @@ suite('Notebook API tests', () => {
// });
test('document open/close event', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
const firstDocumentOpen = getEventOncePromise(vscode.notebook.onDidOpenNotebookDocument);
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
await firstDocumentOpen;
@@ -134,7 +172,9 @@ suite('Notebook API tests', () => {
});
test('shared document in notebook editors', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
let counter = 0;
const disposables: vscode.Disposable[] = [];
disposables.push(vscode.notebook.onDidOpenNotebookDocument(() => {
@@ -155,7 +195,9 @@ suite('Notebook API tests', () => {
});
test('editor open/close event', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
const firstEditorOpen = getEventOncePromise(vscode.notebook.onDidChangeVisibleNotebookEditors);
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
await firstEditorOpen;
@@ -166,7 +208,9 @@ suite('Notebook API tests', () => {
});
test('editor open/close event 2', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
let count = 0;
const disposables: vscode.Disposable[] = [];
disposables.push(vscode.notebook.onDidChangeVisibleNotebookEditors(() => {
@@ -184,7 +228,9 @@ suite('Notebook API tests', () => {
});
test('editor editing event 2', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
const cellsChangeEvent = getEventOncePromise<vscode.NotebookCellsChangeEvent>(vscode.notebook.onDidChangeNotebookCells);
@@ -256,7 +302,8 @@ suite('Notebook API tests', () => {
});
test('editor move cell event', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
await vscode.commands.executeCommand('notebook.cell.insertCodeCellAbove');
@@ -297,7 +344,8 @@ suite('Notebook API tests', () => {
});
test('notebook editor active/visible', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
const firstEditor = vscode.notebook.activeNotebookEditor;
assert.equal(firstEditor?.active, true);
@@ -332,7 +380,8 @@ suite('Notebook API tests', () => {
});
test('notebook active editor change', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
const firstEditorOpen = getEventOncePromise(vscode.notebook.onDidChangeActiveNotebookEditor);
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
await firstEditorOpen;
@@ -341,12 +390,12 @@ suite('Notebook API tests', () => {
await vscode.commands.executeCommand('workbench.action.splitEditor');
await firstEditorDeactivate;
await vscode.commands.executeCommand('workbench.action.files.save');
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
await saveFileAndCloseAll(resource);
});
test('edit API', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
const cellsChangeEvent = getEventOncePromise<vscode.NotebookCellsChangeEvent>(vscode.notebook.onDidChangeNotebookCells);
@@ -361,12 +410,12 @@ suite('Notebook API tests', () => {
assert.deepEqual(cellChangeEventRet.changes[0].deletedCount, 0);
assert.equal(cellChangeEventRet.changes[0].items[0], vscode.notebook.activeNotebookEditor!.document.cells[1]);
await vscode.commands.executeCommand('workbench.action.files.save');
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
await saveFileAndCloseAll(resource);
});
test('initialzation should not emit cell change events.', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
let count = 0;
const disposables: vscode.Disposable[] = [];
@@ -378,14 +427,15 @@ suite('Notebook API tests', () => {
assert.equal(count, 0);
disposables.forEach(d => d.dispose());
await vscode.commands.executeCommand('workbench.action.files.save');
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
await saveFileAndCloseAll(resource);
});
});
suite('notebook workflow', () => {
test('notebook open', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first');
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.document.getText(), 'test');
@@ -406,7 +456,8 @@ suite('notebook workflow', () => {
});
test('notebook cell actions', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first');
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.document.getText(), 'test');
@@ -479,7 +530,8 @@ suite('notebook workflow', () => {
});
test('notebook join cells', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first');
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.document.getText(), 'test');
@@ -487,7 +539,9 @@ suite('notebook workflow', () => {
await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.document.getText(), '');
await vscode.commands.executeCommand('default:type', { text: 'var abc = 0;' });
const edit = new vscode.WorkspaceEdit();
edit.insert(vscode.notebook.activeNotebookEditor!.selection!.uri, new vscode.Position(0, 0), 'var abc = 0;');
await vscode.workspace.applyEdit(edit);
const cellsChangeEvent = getEventOncePromise<vscode.NotebookCellsChangeEvent>(vscode.notebook.onDidChangeNotebookCells);
await vscode.commands.executeCommand('notebook.cell.joinAbove');
@@ -500,7 +554,8 @@ suite('notebook workflow', () => {
});
test('move cells will not recreate cells in ExtHost', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
await vscode.commands.executeCommand('notebook.cell.insertCodeCellAbove');
@@ -513,15 +568,14 @@ suite('notebook workflow', () => {
const newActiveCell = vscode.notebook.activeNotebookEditor!.selection;
assert.deepEqual(activeCell, newActiveCell);
await vscode.commands.executeCommand('workbench.action.files.saveAll');
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
await saveFileAndCloseAll(resource);
// TODO@rebornix, there are still some events order issue.
// assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.indexOf(newActiveCell!), 2);
});
// test.only('document metadata is respected', async function () {
// const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
// const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
// await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
// assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first');
@@ -545,7 +599,8 @@ suite('notebook workflow', () => {
// });
test('cell runnable metadata is respected', async () => {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first');
const editor = vscode.notebook.activeNotebookEditor!;
@@ -566,7 +621,8 @@ suite('notebook workflow', () => {
});
test('document runnable metadata is respected', async () => {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first');
const editor = vscode.notebook.activeNotebookEditor!;
@@ -588,7 +644,8 @@ suite('notebook workflow', () => {
suite('notebook dirty state', () => {
test('notebook open', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first');
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.document.getText(), 'test');
@@ -604,25 +661,22 @@ suite('notebook dirty state', () => {
assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.length, 3);
assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.indexOf(activeCell!), 1);
await vscode.commands.executeCommand('default:type', { text: 'var abc = 0;' });
await vscode.commands.executeCommand('workbench.action.files.newUntitledFile');
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
const edit = new vscode.WorkspaceEdit();
edit.insert(activeCell!.uri, new vscode.Position(0, 0), 'var abc = 0;');
await vscode.workspace.applyEdit(edit);
assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true);
assert.equal(vscode.notebook.activeNotebookEditor?.selection !== undefined, true);
assert.deepEqual(vscode.notebook.activeNotebookEditor?.document.cells[1], vscode.notebook.activeNotebookEditor?.selection);
assert.equal(vscode.notebook.activeNotebookEditor?.selection?.document.getText(), 'var abc = 0;');
await vscode.commands.executeCommand('workbench.action.files.save');
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
await saveFileAndCloseAll(resource);
});
});
suite('notebook undo redo', () => {
test('notebook open', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first');
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.document.getText(), 'test');
@@ -640,14 +694,16 @@ suite('notebook undo redo', () => {
// modify the second cell, delete it
await vscode.commands.executeCommand('default:type', { text: 'var abc = 0;' });
const edit = new vscode.WorkspaceEdit();
edit.insert(vscode.notebook.activeNotebookEditor!.selection!.uri, new vscode.Position(0, 0), 'var abc = 0;');
await vscode.workspace.applyEdit(edit);
await vscode.commands.executeCommand('notebook.cell.delete');
assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.length, 2);
assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.indexOf(vscode.notebook.activeNotebookEditor!.selection!), 1);
// undo should bring back the deleted cell, and revert to previous content and selection
await vscode.commands.executeCommand('notebook.undo');
await vscode.commands.executeCommand('undo');
assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.length, 3);
assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.indexOf(vscode.notebook.activeNotebookEditor!.selection!), 1);
assert.equal(vscode.notebook.activeNotebookEditor?.selection?.document.getText(), 'var abc = 0;');
@@ -658,12 +714,12 @@ suite('notebook undo redo', () => {
// assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.indexOf(vscode.notebook.activeNotebookEditor!.selection!), 1);
// assert.equal(vscode.notebook.activeNotebookEditor?.selection?.document.getText(), 'test');
await vscode.commands.executeCommand('workbench.action.files.save');
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
await saveFileAndCloseAll(resource);
});
test.skip('execute and then undo redo', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
const cellsChangeEvent = getEventOncePromise<vscode.NotebookCellsChangeEvent>(vscode.notebook.onDidChangeNotebookCells);
@@ -713,7 +769,7 @@ suite('notebook undo redo', () => {
assert.equal(cellOutputsAddedRet.cells[0].outputs.length, 1);
const cellOutputClear = getEventOncePromise<vscode.NotebookCellOutputsChangeEvent>(vscode.notebook.onDidChangeCellOutputs);
await vscode.commands.executeCommand('notebook.undo');
await vscode.commands.executeCommand('undo');
const cellOutputsCleardRet = await cellOutputClear;
assert.deepEqual(cellOutputsCleardRet, {
document: vscode.notebook.activeNotebookEditor!.document,
@@ -721,15 +777,14 @@ suite('notebook undo redo', () => {
});
assert.equal(cellOutputsAddedRet.cells[0].outputs.length, 0);
await vscode.commands.executeCommand('workbench.action.files.save');
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
await saveFileAndCloseAll(resource);
});
});
suite('notebook working copy', () => {
// test('notebook revert on close', async function () {
// const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
// const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
// await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
// await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
// assert.equal(vscode.notebook.activeNotebookEditor!.selection?.document.getText(), '');
@@ -750,7 +805,7 @@ suite('notebook working copy', () => {
// });
// test('notebook revert', async function () {
// const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
// const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
// await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
// await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
// assert.equal(vscode.notebook.activeNotebookEditor!.selection?.document.getText(), '');
@@ -770,15 +825,18 @@ suite('notebook working copy', () => {
// });
test('multiple tabs: dirty + clean', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.document.getText(), '');
await vscode.commands.executeCommand('notebook.cell.insertCodeCellAbove');
await vscode.commands.executeCommand('default:type', { text: 'var abc = 0;' });
const edit = new vscode.WorkspaceEdit();
edit.insert(vscode.notebook.activeNotebookEditor!.selection!.uri, new vscode.Position(0, 0), 'var abc = 0;');
await vscode.workspace.applyEdit(edit);
const secondResource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './second.vsctestnb'));
const secondResource = await createRandomFile('', undefined, 'second', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', secondResource, 'notebookCoreTest');
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
@@ -789,20 +847,22 @@ suite('notebook working copy', () => {
assert.deepEqual(vscode.notebook.activeNotebookEditor?.document.cells.length, 3);
assert.equal(vscode.notebook.activeNotebookEditor?.selection?.document.getText(), 'var abc = 0;');
await vscode.commands.executeCommand('workbench.action.files.save');
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
await saveFileAndCloseAll(resource);
});
test('multiple tabs: two dirty tabs and switching', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.document.getText(), '');
await vscode.commands.executeCommand('notebook.cell.insertCodeCellAbove');
await vscode.commands.executeCommand('default:type', { text: 'var abc = 0;' });
const edit = new vscode.WorkspaceEdit();
edit.insert(vscode.notebook.activeNotebookEditor!.selection!.uri, new vscode.Position(0, 0), 'var abc = 0;');
await vscode.workspace.applyEdit(edit);
const secondResource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './second.vsctestnb'));
const secondResource = await createRandomFile('', undefined, 'second', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', secondResource, 'notebookCoreTest');
await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.document.getText(), '');
@@ -823,13 +883,15 @@ suite('notebook working copy', () => {
assert.deepEqual(vscode.notebook.activeNotebookEditor?.document.cells.length, 2);
assert.equal(vscode.notebook.activeNotebookEditor?.selection?.document.getText(), '');
await vscode.commands.executeCommand('workbench.action.files.saveAll');
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
await saveAllFilesAndCloseAll(secondResource);
// await vscode.commands.executeCommand('workbench.action.files.saveAll');
// await vscode.commands.executeCommand('workbench.action.closeAllEditors');
});
test('multiple tabs: different editors with same document', async function () {
assertInitalState();
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
const firstNotebookEditor = vscode.notebook.activeNotebookEditor;
assert.equal(firstNotebookEditor !== undefined, true, 'notebook first');
@@ -846,28 +908,31 @@ suite('notebook working copy', () => {
assert.equal(firstNotebookEditor?.document, secondNotebookEditor?.document, 'split notebook editors share the same document');
assert.notEqual(firstNotebookEditor?.asWebviewUri(vscode.Uri.file('./hello.png')), secondNotebookEditor?.asWebviewUri(vscode.Uri.file('./hello.png')));
await vscode.commands.executeCommand('workbench.action.files.saveAll');
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
await saveAllFilesAndCloseAll(resource);
// await vscode.commands.executeCommand('workbench.action.files.saveAll');
// await vscode.commands.executeCommand('workbench.action.closeAllEditors');
});
});
suite('metadata', () => {
test('custom metadata should be supported', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first');
assert.equal(vscode.notebook.activeNotebookEditor!.document.metadata.custom!['testMetadata'] as boolean, false);
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.metadata.custom!['testCellMetadata'] as number, 123);
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.language, 'typescript');
await vscode.commands.executeCommand('workbench.action.files.saveAll');
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
await saveFileAndCloseAll(resource);
});
// TODO@rebornix skip as it crashes the process all the time
test.skip('custom metadata should be supported', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
test.skip('custom metadata should be supported 2', async function () {
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first');
assert.equal(vscode.notebook.activeNotebookEditor!.document.metadata.custom!['testMetadata'] as boolean, false);
@@ -880,27 +945,29 @@ suite('metadata', () => {
// assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.indexOf(activeCell!), 1);
// assert.equal(activeCell?.metadata.custom!['testCellMetadata'] as number, 123);
await vscode.commands.executeCommand('workbench.action.files.saveAll');
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
await saveFileAndCloseAll(resource);
});
});
suite('regression', () => {
test('microsoft/vscode-github-issue-notebooks#26. Insert template cell in the new empty document', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './empty.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'empty', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first');
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.document.getText(), '');
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.language, 'typescript');
await vscode.commands.executeCommand('workbench.action.files.saveAll');
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
await saveFileAndCloseAll(resource);
});
test('#97830, #97764. Support switch to other editor types', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './empty.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'empty', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
await vscode.commands.executeCommand('default:type', { text: 'var abc = 0;' });
const edit = new vscode.WorkspaceEdit();
edit.insert(vscode.notebook.activeNotebookEditor!.selection!.uri, new vscode.Position(0, 0), 'var abc = 0;');
await vscode.workspace.applyEdit(edit);
assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first');
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.document.getText(), 'var abc = 0;');
@@ -909,33 +976,37 @@ suite('regression', () => {
await vscode.commands.executeCommand('vscode.openWith', resource, 'default');
assert.equal(vscode.window.activeTextEditor?.document.uri.path, resource.path);
await vscode.commands.executeCommand('workbench.action.revertAndCloseActiveEditor');
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
});
// open text editor, pin, and then open a notebook
test('#96105 - dirty editors', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './empty.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'empty', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'default');
await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
await vscode.commands.executeCommand('default:type', { text: 'var abc = 0;' });
const edit = new vscode.WorkspaceEdit();
edit.insert(resource, new vscode.Position(0, 0), 'var abc = 0;');
await vscode.workspace.applyEdit(edit);
// now it's dirty, open the resource with notebook editor should open a new one
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
assert.notEqual(vscode.notebook.activeNotebookEditor, undefined, 'notebook first');
assert.notEqual(vscode.window.activeTextEditor, undefined);
await vscode.commands.executeCommand('workbench.action.revertAndCloseActiveEditor');
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
});
test('#102411 - untitled notebook creation failed', async function () {
assertInitalState();
await vscode.commands.executeCommand('workbench.action.files.newUntitledFile', { viewType: 'notebookCoreTest' });
assert.notEqual(vscode.notebook.activeNotebookEditor, undefined, 'untitled notebook editor is not undefined');
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
});
test('#102423 - copy/paste shares the same text buffer', async function () {
const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
let activeCell = vscode.notebook.activeNotebookEditor!.selection;
@@ -947,10 +1018,14 @@ suite('regression', () => {
assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.indexOf(activeCell!), 1);
assert.equal(activeCell?.document.getText(), 'test');
await vscode.commands.executeCommand('default:type', { text: 'var abc = 0;' });
const edit = new vscode.WorkspaceEdit();
edit.insert(vscode.notebook.activeNotebookEditor!.selection!.uri, new vscode.Position(0, 0), 'var abc = 0;');
await vscode.workspace.applyEdit(edit);
assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.length, 2);
assert.notEqual(vscode.notebook.activeNotebookEditor!.document.cells[0].document.getText(), vscode.notebook.activeNotebookEditor!.document.cells[1].document.getText());
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
});
});
@@ -961,11 +1036,11 @@ suite('webview', () => {
// return;
// }
// const resource = vscode.Uri.file(join(vscode.workspace.rootPath || '', './first.vsctestnb'));
// const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
// await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
// assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first');
// const uri = vscode.notebook.activeNotebookEditor!.asWebviewUri(vscode.Uri.file('./hello.png'));
// assert.equal(uri.scheme, 'vscode-resource');
// assert.equal(uri.scheme, 'vscode-webview-resource');
// await vscode.commands.executeCommand('workbench.action.closeAllEditors');
// });

View File

@@ -67,7 +67,7 @@ export function smokeTestActivate(context: vscode.ExtensionContext): any {
}
}));
context.subscriptions.push(vscode.notebook.registerNotebookKernel('notebookSmokeTest', ['*.vsctestnb'], {
context.subscriptions.push(vscode.notebook.registerNotebookKernel('notebookSmokeTest', ['*.smoke-nb'], {
label: 'notebookSmokeTest',
executeAllCells: async (_document: vscode.NotebookDocument) => {
for (let i = 0; i < _document.cells.length; i++) {
@@ -79,7 +79,8 @@ export function smokeTestActivate(context: vscode.ExtensionContext): any {
}];
}
},
executeCell: async (_document: vscode.NotebookDocument, _cell: vscode.NotebookCell | undefined, _token: vscode.CancellationToken) => {
cancelAllCellsExecution: async () => { },
executeCell: async (_document: vscode.NotebookDocument, _cell: vscode.NotebookCell | undefined) => {
if (!_cell) {
_cell = _document.cells[0];
}
@@ -92,6 +93,7 @@ export function smokeTestActivate(context: vscode.ExtensionContext): any {
}];
return;
},
cancelCellExecution: async () => { }
}));
context.subscriptions.push(vscode.commands.registerCommand('vscode-notebook-tests.debugAction', async (cell: vscode.NotebookCell) => {

View File

@@ -15,7 +15,7 @@ export function activate(context: vscode.ExtensionContext): any {
context.subscriptions.push(vscode.notebook.registerNotebookContentProvider('notebookCoreTest', {
onDidChangeNotebook: _onDidChangeNotebook.event,
openNotebook: async (_resource: vscode.Uri) => {
if (_resource.path.endsWith('empty.vsctestnb')) {
if (/.*empty\-.*\.vsctestnb$/.test(_resource.path)) {
return {
languages: ['typescript'],
metadata: {},
@@ -62,8 +62,8 @@ export function activate(context: vscode.ExtensionContext): any {
context.subscriptions.push(vscode.notebook.registerNotebookKernel('notebookKernelTest', ['*.vsctestnb'], {
label: 'Notebook Test Kernel',
executeAllCells: async (_document: vscode.NotebookDocument, _token: vscode.CancellationToken) => {
let cell = _document.cells[0];
executeAllCells: async (_document: vscode.NotebookDocument) => {
const cell = _document.cells[0];
cell.outputs = [{
outputKind: vscode.CellOutputKind.Rich,
@@ -73,7 +73,8 @@ export function activate(context: vscode.ExtensionContext): any {
}];
return;
},
executeCell: async (document: vscode.NotebookDocument, cell: vscode.NotebookCell | undefined, _token: vscode.CancellationToken) => {
cancelAllCellsExecution: async (_document: vscode.NotebookDocument) => { },
executeCell: async (document: vscode.NotebookDocument, cell: vscode.NotebookCell | undefined) => {
if (!cell) {
cell = document.cells[0];
}
@@ -113,7 +114,8 @@ export function activate(context: vscode.ExtensionContext): any {
}
});
return;
}
},
cancelCellExecution: async (_document: vscode.NotebookDocument, _cell: vscode.NotebookCell) => { }
}));
const preloadUri = vscode.Uri.file(path.resolve(__dirname, '../src/customRenderer.js'));

View File

@@ -0,0 +1,261 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as path from 'path';
import * as vscode from 'vscode';
class File implements vscode.FileStat {
type: vscode.FileType;
ctime: number;
mtime: number;
size: number;
name: string;
data?: Uint8Array;
constructor(name: string) {
this.type = vscode.FileType.File;
this.ctime = Date.now();
this.mtime = Date.now();
this.size = 0;
this.name = name;
}
}
class Directory implements vscode.FileStat {
type: vscode.FileType;
ctime: number;
mtime: number;
size: number;
name: string;
entries: Map<string, File | Directory>;
constructor(name: string) {
this.type = vscode.FileType.Directory;
this.ctime = Date.now();
this.mtime = Date.now();
this.size = 0;
this.name = name;
this.entries = new Map();
}
}
export type Entry = File | Directory;
export class TestFS implements vscode.FileSystemProvider {
constructor(
readonly scheme: string,
readonly isCaseSensitive: boolean
) { }
readonly root = new Directory('');
// --- manage file metadata
stat(uri: vscode.Uri): vscode.FileStat {
return this._lookup(uri, false);
}
readDirectory(uri: vscode.Uri): [string, vscode.FileType][] {
const entry = this._lookupAsDirectory(uri, false);
const result: [string, vscode.FileType][] = [];
for (const [name, child] of entry.entries) {
result.push([name, child.type]);
}
return result;
}
// --- manage file contents
readFile(uri: vscode.Uri): Uint8Array {
const data = this._lookupAsFile(uri, false).data;
if (data) {
return data;
}
throw vscode.FileSystemError.FileNotFound();
}
writeFile(uri: vscode.Uri, content: Uint8Array, options: { create: boolean, overwrite: boolean }): void {
const basename = path.posix.basename(uri.path);
const parent = this._lookupParentDirectory(uri);
let entry = parent.entries.get(basename);
if (entry instanceof Directory) {
throw vscode.FileSystemError.FileIsADirectory(uri);
}
if (!entry && !options.create) {
throw vscode.FileSystemError.FileNotFound(uri);
}
if (entry && options.create && !options.overwrite) {
throw vscode.FileSystemError.FileExists(uri);
}
if (!entry) {
entry = new File(basename);
parent.entries.set(basename, entry);
this._fireSoon({ type: vscode.FileChangeType.Created, uri });
}
entry.mtime = Date.now();
entry.size = content.byteLength;
entry.data = content;
this._fireSoon({ type: vscode.FileChangeType.Changed, uri });
}
// --- manage files/folders
rename(oldUri: vscode.Uri, newUri: vscode.Uri, options: { overwrite: boolean }): void {
if (!options.overwrite && this._lookup(newUri, true)) {
throw vscode.FileSystemError.FileExists(newUri);
}
const entry = this._lookup(oldUri, false);
const oldParent = this._lookupParentDirectory(oldUri);
const newParent = this._lookupParentDirectory(newUri);
const newName = path.posix.basename(newUri.path);
oldParent.entries.delete(entry.name);
entry.name = newName;
newParent.entries.set(newName, entry);
this._fireSoon(
{ type: vscode.FileChangeType.Deleted, uri: oldUri },
{ type: vscode.FileChangeType.Created, uri: newUri }
);
}
delete(uri: vscode.Uri): void {
const dirname = uri.with({ path: path.posix.dirname(uri.path) });
const basename = path.posix.basename(uri.path);
const parent = this._lookupAsDirectory(dirname, false);
if (!parent.entries.has(basename)) {
throw vscode.FileSystemError.FileNotFound(uri);
}
parent.entries.delete(basename);
parent.mtime = Date.now();
parent.size -= 1;
this._fireSoon({ type: vscode.FileChangeType.Changed, uri: dirname }, { uri, type: vscode.FileChangeType.Deleted });
}
createDirectory(uri: vscode.Uri): void {
const basename = path.posix.basename(uri.path);
const dirname = uri.with({ path: path.posix.dirname(uri.path) });
const parent = this._lookupAsDirectory(dirname, false);
const entry = new Directory(basename);
parent.entries.set(entry.name, entry);
parent.mtime = Date.now();
parent.size += 1;
this._fireSoon({ type: vscode.FileChangeType.Changed, uri: dirname }, { type: vscode.FileChangeType.Created, uri });
}
// --- lookup
private _lookup(uri: vscode.Uri, silent: false): Entry;
private _lookup(uri: vscode.Uri, silent: boolean): Entry | undefined;
private _lookup(uri: vscode.Uri, silent: boolean): Entry | undefined {
const parts = uri.path.split('/');
let entry: Entry = this.root;
for (const part of parts) {
const partLow = part.toLowerCase();
if (!part) {
continue;
}
let child: Entry | undefined;
if (entry instanceof Directory) {
if (this.isCaseSensitive) {
child = entry.entries.get(part);
} else {
for (const [key, value] of entry.entries) {
if (key.toLowerCase() === partLow) {
child = value;
break;
}
}
}
}
if (!child) {
if (!silent) {
throw vscode.FileSystemError.FileNotFound(uri);
} else {
return undefined;
}
}
entry = child;
}
return entry;
}
private _lookupAsDirectory(uri: vscode.Uri, silent: boolean): Directory {
const entry = this._lookup(uri, silent);
if (entry instanceof Directory) {
return entry;
}
throw vscode.FileSystemError.FileNotADirectory(uri);
}
private _lookupAsFile(uri: vscode.Uri, silent: boolean): File {
const entry = this._lookup(uri, silent);
if (entry instanceof File) {
return entry;
}
throw vscode.FileSystemError.FileIsADirectory(uri);
}
private _lookupParentDirectory(uri: vscode.Uri): Directory {
const dirname = uri.with({ path: path.posix.dirname(uri.path) });
return this._lookupAsDirectory(dirname, false);
}
// --- manage file events
private _emitter = new vscode.EventEmitter<vscode.FileChangeEvent[]>();
private _bufferedEvents: vscode.FileChangeEvent[] = [];
private _fireSoonHandle?: NodeJS.Timer;
readonly onDidChangeFile: vscode.Event<vscode.FileChangeEvent[]> = this._emitter.event;
watch(_resource: vscode.Uri): vscode.Disposable {
// ignore, fires for all changes...
return new vscode.Disposable(() => { });
}
private _fireSoon(...events: vscode.FileChangeEvent[]): void {
this._bufferedEvents.push(...events);
if (this._fireSoonHandle) {
clearTimeout(this._fireSoonHandle);
}
this._fireSoonHandle = setTimeout(() => {
this._emitter.fire(this._bufferedEvents);
this._bufferedEvents.length = 0;
}, 5);
}
}
export function rndName() {
return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10);
}
export const testFs = new TestFS('fake-fs', true);
vscode.workspace.registerFileSystemProvider(testFs.scheme, testFs, { isCaseSensitive: testFs.isCaseSensitive });
export async function createRandomFile(contents = '', dir: vscode.Uri | undefined = undefined, prefix = '', ext = ''): Promise<vscode.Uri> {
let fakeFile: vscode.Uri;
if (dir) {
fakeFile = dir.with({ path: dir.path + '/' + rndName() + ext });
} else {
fakeFile = vscode.Uri.parse(`${testFs.scheme}:/${prefix}-${rndName() + ext}`);
}
await testFs.writeFile(fakeFile, Buffer.from(contents), { create: true, overwrite: true });
return fakeFile;
}