Fix open notebook bug (#10930)

* Fix open notebook bug

* cleanup

* clean up spaces
This commit is contained in:
Charles Gagnon
2020-06-16 13:06:44 -07:00
committed by GitHub
parent 381a747b62
commit 94bc0d9559
9 changed files with 213 additions and 169 deletions

View File

@@ -4,15 +4,29 @@
*--------------------------------------------------------------------------------------------*/
import * as azdata from 'azdata';
import { newNotebook } from '../../common/notebookUtils';
import { NotebookUtils } from '../../common/notebookUtils';
import * as should from 'should';
import * as vscode from 'vscode';
import * as TypeMoq from 'typemoq';
import * as os from 'os';
import * as path from 'path';
import * as uuid from 'uuid';
import { promises as fs } from 'fs';
import { ApiWrapper } from '../../common/apiWrapper';
import { tryDeleteFile } from './testUtils';
describe('notebookUtils Tests', async function (): Promise<void> {
describe('newNotebook', async function (): Promise<void> {
describe('notebookUtils Tests', function (): void {
let notebookUtils: NotebookUtils;
let apiWrapperMock: TypeMoq.IMock<ApiWrapper>;
before(function (): void {
apiWrapperMock = TypeMoq.Mock.ofInstance(new ApiWrapper());
notebookUtils = new NotebookUtils(apiWrapperMock.object);
});
describe('newNotebook', function (): void {
it('Should open a new notebook successfully', async function (): Promise<void> {
should(azdata.nb.notebookDocuments.length).equal(0, 'There should be not any open Notebook documents');
await newNotebook(undefined);
await notebookUtils.newNotebook(undefined);
should(azdata.nb.notebookDocuments.length).equal(1, 'There should be exactly 1 open Notebook document');
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
should(azdata.nb.notebookDocuments.length).equal(0, 'There should be not any open Notebook documents');
@@ -20,12 +34,12 @@ describe('notebookUtils Tests', async function (): Promise<void> {
it('Opening an untitled editor after closing should re-use previous untitled name', async function (): Promise<void> {
should(azdata.nb.notebookDocuments.length).equal(0, 'There should be not any open Notebook documents');
await newNotebook(undefined);
await notebookUtils.newNotebook(undefined);
should(azdata.nb.notebookDocuments.length).equal(1, 'There should be exactly 1 open Notebook document');
should(azdata.nb.notebookDocuments[0].fileName).equal('Notebook-0', 'The first Untitled Notebook should have an index of 0');
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
should(azdata.nb.notebookDocuments.length).equal(0, 'There should be not any open Notebook documents');
await newNotebook(undefined);
await notebookUtils.newNotebook(undefined);
should(azdata.nb.notebookDocuments.length).equal(1, 'There should be exactly 1 open Notebook document after second opening');
should(azdata.nb.notebookDocuments[0].fileName).equal('Notebook-0', 'The first Untitled Notebook should have an index of 0 after closing first Untitled Notebook');
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
@@ -33,9 +47,9 @@ describe('notebookUtils Tests', async function (): Promise<void> {
it('Untitled Name index should increase', async function (): Promise<void> {
should(azdata.nb.notebookDocuments.length).equal(0, 'There should be not any open Notebook documents');
await newNotebook(undefined);
await notebookUtils.newNotebook(undefined);
should(azdata.nb.notebookDocuments.length).equal(1, 'There should be exactly 1 open Notebook document');
const secondNotebook = await newNotebook(undefined);
const secondNotebook = await notebookUtils.newNotebook(undefined);
should(azdata.nb.notebookDocuments.length).equal(2, 'There should be exactly 2 open Notebook documents');
should(secondNotebook.document.fileName).equal('Notebook-1', 'The second Untitled Notebook should have an index of 1');
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
@@ -43,4 +57,27 @@ describe('notebookUtils Tests', async function (): Promise<void> {
should(azdata.nb.notebookDocuments.length).equal(0, 'There should be not any open Notebook documents');
});
});
describe('openNotebook', function () {
it('opens a Notebook successfully', async function (): Promise<void> {
const notebookPath = path.join(os.tmpdir(), `OpenNotebookTest_${uuid.v4()}.ipynb`);
const notebookUri = vscode.Uri.file(notebookPath);
try {
await fs.writeFile(notebookPath, '');
apiWrapperMock.setup(x => x.showOpenDialog(TypeMoq.It.isAny())).returns(() => Promise.resolve([notebookUri]));
await notebookUtils.openNotebook();
should(azdata.nb.notebookDocuments.find(doc => doc.fileName === notebookUri.fsPath)).not.be.undefined();
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
} finally {
tryDeleteFile(notebookPath);
}
});
it('shows error if unexpected error is thrown', async function (): Promise<void> {
apiWrapperMock.setup(x => x.showOpenDialog(TypeMoq.It.isAny())).throws(new Error('Unexpected error'));
apiWrapperMock.setup(x => x.showErrorMessage(TypeMoq.It.isAny())).returns(() => Promise.resolve(''));
await notebookUtils.openNotebook();
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
});
});
});

View File

@@ -1,22 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as should from 'should';
import 'mocha';
import * as notebookUtils from '../../common/notebookUtils';
describe('Random Token', () => {
it('Should have default length and be hex only', async function (): Promise<void> {
let token = await notebookUtils.getRandomToken();
should(token).have.length(48);
let validChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
for (let i = 0; i < token.length; i++) {
let char = token.charAt(i);
should(validChars.indexOf(char)).be.greaterThan(-1);
}
});
});

View File

@@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { promises as fs } from 'fs';
export async function assertThrowsAsync(fn: () => Promise<any>, msg: string): Promise<void> {
let f = () => {
@@ -21,3 +22,11 @@ export async function assertThrowsAsync(fn: () => Promise<any>, msg: string): Pr
export async function sleep(ms: number): Promise<{}> {
return new Promise(resolve => setTimeout(resolve, ms));
}
export async function tryDeleteFile(path: string): Promise<void> {
try {
await fs.unlink(path);
} catch {
console.warn(`Could not delete file ${path}`);
}
}

View File

@@ -160,7 +160,7 @@ describe('Utils Tests', function () {
});
describe('isEditorTitleFree', () => {
afterEach( async () => {
afterEach(async () => {
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
});
@@ -318,4 +318,17 @@ describe('Utils Tests', function () {
}).throw();
});
});
describe('getRandomToken', function (): void {
it('Should have default length and be hex only', async function (): Promise<void> {
let token = await utils.getRandomToken();
should(token).have.length(48);
let validChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
for (let i = 0; i < token.length; i++) {
let char = token.charAt(i);
should(validChars.indexOf(char)).be.greaterThan(-1);
}
});
});
});