Remove ApiWrapper from notebook extension (#11224)

* Remove ApiWrapper in notebook extension

* delete file

* Remove copyrights
This commit is contained in:
Charles Gagnon
2020-07-08 11:06:32 -07:00
committed by GitHub
parent 449a46d7fc
commit b3a01fcf77
38 changed files with 359 additions and 445 deletions

View File

@@ -8,25 +8,24 @@ import { NotebookUtils } from '../../common/notebookUtils';
import * as should from 'should';
import * as vscode from 'vscode';
import * as TypeMoq from 'typemoq';
import * as sinon from 'sinon';
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';
import { CellTypes } from '../../contracts/content';
describe('notebookUtils Tests', function (): void {
let notebookUtils: NotebookUtils;
let apiWrapperMock: TypeMoq.IMock<ApiWrapper>;
let notebookUtils: NotebookUtils = new NotebookUtils();
let showErrorMessageSpy: sinon.SinonSpy;
this.beforeAll(async function(): Promise<void> {
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
beforeEach(function(): void {
showErrorMessageSpy = sinon.spy(vscode.window, 'showErrorMessage');
});
beforeEach(function (): void {
apiWrapperMock = TypeMoq.Mock.ofInstance(new ApiWrapper());
notebookUtils = new NotebookUtils(apiWrapperMock.object);
afterEach(function(): void {
sinon.restore();
});
this.afterAll(async function (): Promise<void> {
@@ -74,7 +73,7 @@ describe('notebookUtils Tests', function (): void {
const notebookUri = vscode.Uri.file(notebookPath);
try {
await fs.writeFile(notebookPath, '');
apiWrapperMock.setup(x => x.showOpenDialog(TypeMoq.It.isAny())).returns(() => Promise.resolve([notebookUri]));
sinon.stub(vscode.window, 'showOpenDialog').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');
@@ -84,88 +83,86 @@ describe('notebookUtils Tests', function (): void {
});
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(''));
sinon.stub(vscode.window, 'showOpenDialog').throws(new Error('Unexpected error'));
await notebookUtils.openNotebook();
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
should(showErrorMessageSpy.calledOnce).be.true('showErrorMessage should have been called');
});
});
describe('runActiveCell', function () {
it('shows error when no notebook visible', async function (): Promise<void> {
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => undefined);
await notebookUtils.runActiveCell();
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
should(showErrorMessageSpy.calledOnce).be.true('showErrorMessage should have been called');
});
it('does not show error when notebook visible', async function (): Promise<void> {
let mockNotebookEditor = TypeMoq.Mock.ofType<azdata.nb.NotebookEditor>();
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => mockNotebookEditor.object);
sinon.replaceGetter(azdata.nb, 'activeNotebookEditor', () => mockNotebookEditor.object);
await notebookUtils.runActiveCell();
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
should(showErrorMessageSpy.notCalled).be.true('showErrorMessage should not have been called');
mockNotebookEditor.verify(x => x.runCell(TypeMoq.It.isAny()), TypeMoq.Times.once());
});
});
describe('clearActiveCellOutput', function () {
it('shows error when no notebook visible', async function (): Promise<void> {
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => undefined);
sinon.replaceGetter(azdata.nb, 'activeNotebookEditor', () => undefined);
await notebookUtils.clearActiveCellOutput();
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
});
should(showErrorMessageSpy.calledOnce).be.true('showErrorMessage should be called exactly once');
});
it('does not show error when notebook visible', async function (): Promise<void> {
let mockNotebookEditor = TypeMoq.Mock.ofType<azdata.nb.NotebookEditor>();
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => mockNotebookEditor.object);
sinon.replaceGetter(azdata.nb, 'activeNotebookEditor', () => mockNotebookEditor.object);
await notebookUtils.clearActiveCellOutput();
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
should(showErrorMessageSpy.notCalled).be.true('showErrorMessage should not have been called');
mockNotebookEditor.verify(x => x.clearOutput(TypeMoq.It.isAny()), TypeMoq.Times.once());
});
});
describe('runAllCells', function () {
it('shows error when no notebook visible', async function (): Promise<void> {
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => undefined);
sinon.replaceGetter(azdata.nb, 'activeNotebookEditor', () => undefined);
await notebookUtils.runAllCells();
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
should(showErrorMessageSpy.calledOnce).be.true('showErrorMessage should be called exactly once');
});
it('does not show error when notebook visible', async function (): Promise<void> {
let mockNotebookEditor = TypeMoq.Mock.ofType<azdata.nb.NotebookEditor>();
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => mockNotebookEditor.object);
sinon.replaceGetter(azdata.nb, 'activeNotebookEditor', () => mockNotebookEditor.object);
await notebookUtils.runAllCells();
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
should(showErrorMessageSpy.notCalled).be.true('showErrorMessage should not have been called');
mockNotebookEditor.verify(x => x.runAllCells(TypeMoq.It.isAny(), TypeMoq.It.isAny()), TypeMoq.Times.once());
});
});
describe('addCell', function () {
it('shows error when no notebook visible for code cell', async function (): Promise<void> {
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => undefined);
sinon.replaceGetter(azdata.nb, 'activeNotebookEditor', () => undefined);
await notebookUtils.addCell('code');
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
should(showErrorMessageSpy.calledOnce).be.true('showErrorMessage should be called exactly once');
});
it('shows error when no notebook visible for markdown cell', async function (): Promise<void> {
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => undefined);
sinon.replaceGetter(azdata.nb, 'activeNotebookEditor', () => undefined);
await notebookUtils.addCell('markdown');
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
should(showErrorMessageSpy.calledOnce).be.true('showErrorMessage should be called exactly once');
});
it('does not show error when notebook visible for code cell', async function (): Promise<void> {
const notebookEditor = await notebookUtils.newNotebook(undefined);
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => notebookEditor);
sinon.replaceGetter(azdata.nb, 'activeNotebookEditor', () => notebookEditor);
await notebookUtils.addCell('code');
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
should(showErrorMessageSpy.notCalled).be.true('showErrorMessage should never be called');
should(notebookEditor.document.cells.length).equal(1);
should(notebookEditor.document.cells[0].contents.cell_type).equal(CellTypes.Code);
});
it('does not show error when notebook visible for markdown cell', async function (): Promise<void> {
const notebookEditor = await notebookUtils.newNotebook(undefined);
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => notebookEditor);
sinon.replaceGetter(azdata.nb, 'activeNotebookEditor', () => notebookEditor);
await notebookUtils.addCell('markdown');
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
should(showErrorMessageSpy.notCalled).be.true('showErrorMessage should never be called');
should(notebookEditor.document.cells.length).equal(1);
should(notebookEditor.document.cells[0].contents.cell_type).equal(CellTypes.Markdown);
});
@@ -174,8 +171,8 @@ describe('notebookUtils Tests', function (): void {
describe('analyzeNotebook', function () {
it('creates cell when oeContext exists', async function (): Promise<void> {
const notebookEditor = await notebookUtils.newNotebook(undefined);
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => notebookEditor);
apiWrapperMock.setup(x => x.showNotebookDocument(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve(notebookEditor));
sinon.replaceGetter(azdata.nb, 'activeNotebookEditor', () => notebookEditor);
sinon.stub(azdata.nb, 'showNotebookDocument').returns(Promise.resolve(notebookEditor));
const oeContext: azdata.ObjectExplorerContext = {
connectionProfile: undefined,
isConnectionNode: true,
@@ -197,8 +194,8 @@ describe('notebookUtils Tests', function (): void {
it('does not create new cell when oeContext does not exist', async function (): Promise<void> {
const notebookEditor = await notebookUtils.newNotebook(undefined);
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => notebookEditor);
apiWrapperMock.setup(x => x.showNotebookDocument(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve(notebookEditor));
sinon.replaceGetter(azdata.nb, 'activeNotebookEditor', () => notebookEditor);
sinon.stub(azdata.nb, 'showNotebookDocument').returns(Promise.resolve(notebookEditor));
await notebookUtils.analyzeNotebook();
should(notebookEditor.document.cells.length).equal(0, 'No cells should exist');
});

View File

@@ -3,47 +3,51 @@
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as TypeMoq from 'typemoq';
import * as should from 'should';
import * as sinon from 'sinon';
import * as vscode from 'vscode';
import { IPrompter, confirm, IQuestion } from '../../prompts/question';
import CodeAdapter from '../../prompts/adapter';
import { ApiWrapper } from '../../common/apiWrapper';
describe('Prompt', () => {
let prompter: IPrompter;
let mockApiWrapper: TypeMoq.IMock<ApiWrapper>;
let showErrorMessageSpy: sinon.SinonSpy;
before(function () {
prompter = new CodeAdapter();
});
beforeEach(function () {
mockApiWrapper = TypeMoq.Mock.ofType<ApiWrapper>();
mockApiWrapper.setup(x => x.showErrorMessage(TypeMoq.It.isAny()));
beforeEach(function(): void {
showErrorMessageSpy = sinon.spy(vscode.window, 'showErrorMessage');
});
afterEach(function () {
sinon.restore();
});
it('Should not find prompt for invalid question type', async function (): Promise<void> {
const showQuickPickSpy = sinon.spy(vscode.window, 'showQuickPick');
await prompter.promptSingle<boolean>(<IQuestion>{
type: 'invalidType',
message: 'sample message',
default: false
}, mockApiWrapper.object);
mockApiWrapper.verify(s => s.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
mockApiWrapper.verify(s => s.showQuickPick(TypeMoq.It.isAny(), TypeMoq.It.isAny()), TypeMoq.Times.never());
});
should(showErrorMessageSpy.calledOnce).be.true('showErrorMessage should be called exactly once');
should(showQuickPickSpy.notCalled).be.true('showQuickPick should never have been called');
});
it('Should find prompt for confirm type', async function (): Promise<void> {
mockApiWrapper.setup(x => x.showQuickPick(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve('Yes'));
const quickPickSpy = sinon.stub(vscode.window, 'showQuickPick').returns(Promise.resolve('Yes') as any);
await prompter.promptSingle<boolean>(<IQuestion>{
type: confirm,
message: 'sample message',
default: false
}, mockApiWrapper.object);
mockApiWrapper.verify(s => s.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
mockApiWrapper.verify(s => s.showQuickPick(TypeMoq.It.isAny(), TypeMoq.It.isAny()), TypeMoq.Times.once());
});
should(showErrorMessageSpy.notCalled).be.true('showErrorMessage should never be called');
should(quickPickSpy.calledOnce).be.true('showQuickPick should have been called once');
});
});