mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 10:58:30 -05:00
Add more notebook extension tests (#11143)
* more tests * More tests * More tests * Add prompt tests
This commit is contained in:
@@ -6,7 +6,9 @@
|
|||||||
"**/integrationTest/**",
|
"**/integrationTest/**",
|
||||||
"**/node_modules/**",
|
"**/node_modules/**",
|
||||||
"**/test/**",
|
"**/test/**",
|
||||||
"extension.js"
|
"extension.js",
|
||||||
|
"common/apiWrapper.js",
|
||||||
|
"common/localizedConstants.js"
|
||||||
],
|
],
|
||||||
"reports": [
|
"reports": [
|
||||||
"cobertura",
|
"cobertura",
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import * as vscode from 'vscode';
|
|||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as fs from 'fs-extra';
|
import * as fs from 'fs-extra';
|
||||||
import * as constants from '../common/constants';
|
import * as constants from '../common/constants';
|
||||||
import { IPrompter, QuestionTypes, IQuestion } from '../prompts/question';
|
import { IPrompter, IQuestion, confirm } from '../prompts/question';
|
||||||
import CodeAdapter from '../prompts/adapter';
|
import CodeAdapter from '../prompts/adapter';
|
||||||
import { BookTreeItem, BookTreeItemType } from './bookTreeItem';
|
import { BookTreeItem, BookTreeItemType } from './bookTreeItem';
|
||||||
import { BookModel } from './bookModel';
|
import { BookModel } from './bookModel';
|
||||||
@@ -236,7 +236,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
|||||||
let notebookPath: string;
|
let notebookPath: string;
|
||||||
// If no uri is passed in, try to use the current active notebook editor
|
// If no uri is passed in, try to use the current active notebook editor
|
||||||
if (!uri) {
|
if (!uri) {
|
||||||
let openDocument = azdata.nb.activeNotebookEditor;
|
let openDocument = this._apiWrapper.getActiveNotebookEditor();
|
||||||
if (openDocument) {
|
if (openDocument) {
|
||||||
notebookPath = openDocument.document.uri.fsPath;
|
notebookPath = openDocument.document.uri.fsPath;
|
||||||
}
|
}
|
||||||
@@ -511,10 +511,10 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
|||||||
//Confirmation message dialog
|
//Confirmation message dialog
|
||||||
private async confirmReplace(): Promise<boolean> {
|
private async confirmReplace(): Promise<boolean> {
|
||||||
return await this.prompter.promptSingle<boolean>(<IQuestion>{
|
return await this.prompter.promptSingle<boolean>(<IQuestion>{
|
||||||
type: QuestionTypes.confirm,
|
type: confirm,
|
||||||
message: loc.confirmReplace,
|
message: loc.confirmReplace,
|
||||||
default: false
|
default: false
|
||||||
});
|
}, this._apiWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
getNavigation(uri: vscode.Uri): Thenable<azdata.nb.NavigationResult> {
|
getNavigation(uri: vscode.Uri): Thenable<azdata.nb.NavigationResult> {
|
||||||
|
|||||||
@@ -69,6 +69,14 @@ export class ApiWrapper {
|
|||||||
return azdata.nb.notebookDocuments;
|
return azdata.nb.notebookDocuments;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getActiveNotebookEditor(): azdata.nb.NotebookEditor {
|
||||||
|
return azdata.nb.activeNotebookEditor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public showNotebookDocument(uri: vscode.Uri, showOptions?: azdata.nb.NotebookShowOptions): Thenable<azdata.nb.NotebookEditor> {
|
||||||
|
return azdata.nb.showNotebookDocument(uri, showOptions);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the configuration for a extensionName
|
* Get the configuration for a extensionName
|
||||||
* @param extensionName The string name of the extension to get the configuration for
|
* @param extensionName The string name of the extension to get the configuration for
|
||||||
@@ -95,4 +103,8 @@ export class ApiWrapper {
|
|||||||
public createTreeView<T>(viewId: string, options: vscode.TreeViewOptions<T>): vscode.TreeView<T> {
|
public createTreeView<T>(viewId: string, options: vscode.TreeViewOptions<T>): vscode.TreeView<T> {
|
||||||
return vscode.window.createTreeView(viewId, options);
|
return vscode.window.createTreeView(viewId, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public showQuickPick(items: string[] | Thenable<string[]>, options?: vscode.QuickPickOptions, token?: vscode.CancellationToken): Thenable<string | undefined> {
|
||||||
|
return vscode.window.showQuickPick(items, options, token);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,19 +12,10 @@ import { NotebookUtils } from './notebookUtils';
|
|||||||
*/
|
*/
|
||||||
export class AppContext {
|
export class AppContext {
|
||||||
|
|
||||||
private serviceMap: Map<string, any> = new Map();
|
|
||||||
public readonly notebookUtils: NotebookUtils;
|
public readonly notebookUtils: NotebookUtils;
|
||||||
|
|
||||||
constructor(public readonly extensionContext: vscode.ExtensionContext, public readonly apiWrapper: ApiWrapper) {
|
constructor(public readonly extensionContext: vscode.ExtensionContext, public readonly apiWrapper: ApiWrapper) {
|
||||||
this.apiWrapper = apiWrapper || new ApiWrapper();
|
this.apiWrapper = apiWrapper || new ApiWrapper();
|
||||||
this.notebookUtils = new NotebookUtils(apiWrapper);
|
this.notebookUtils = new NotebookUtils(apiWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getService<T>(serviceName: string): T {
|
|
||||||
return this.serviceMap.get(serviceName) as T;
|
|
||||||
}
|
|
||||||
|
|
||||||
public registerService<T>(serviceName: string, service: T): void {
|
|
||||||
this.serviceMap.set(serviceName, service);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,46 +64,46 @@ export class NotebookUtils {
|
|||||||
|
|
||||||
public async runActiveCell(): Promise<void> {
|
public async runActiveCell(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
let notebook = azdata.nb.activeNotebookEditor;
|
let notebook = this._apiWrapper.getActiveNotebookEditor();
|
||||||
if (notebook) {
|
if (notebook) {
|
||||||
await notebook.runCell();
|
await notebook.runCell();
|
||||||
} else {
|
} else {
|
||||||
throw new Error(noNotebookVisible);
|
throw new Error(noNotebookVisible);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
vscode.window.showErrorMessage(getErrorMessage(err));
|
this._apiWrapper.showErrorMessage(getErrorMessage(err));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async clearActiveCellOutput(): Promise<void> {
|
public async clearActiveCellOutput(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
let notebook = azdata.nb.activeNotebookEditor;
|
let notebook = this._apiWrapper.getActiveNotebookEditor();
|
||||||
if (notebook) {
|
if (notebook) {
|
||||||
await notebook.clearOutput();
|
await notebook.clearOutput();
|
||||||
} else {
|
} else {
|
||||||
throw new Error(noNotebookVisible);
|
throw new Error(noNotebookVisible);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
vscode.window.showErrorMessage(getErrorMessage(err));
|
this._apiWrapper.showErrorMessage(getErrorMessage(err));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async runAllCells(startCell?: azdata.nb.NotebookCell, endCell?: azdata.nb.NotebookCell): Promise<void> {
|
public async runAllCells(startCell?: azdata.nb.NotebookCell, endCell?: azdata.nb.NotebookCell): Promise<void> {
|
||||||
try {
|
try {
|
||||||
let notebook = azdata.nb.activeNotebookEditor;
|
let notebook = this._apiWrapper.getActiveNotebookEditor();
|
||||||
if (notebook) {
|
if (notebook) {
|
||||||
await notebook.runAllCells(startCell, endCell);
|
await notebook.runAllCells(startCell, endCell);
|
||||||
} else {
|
} else {
|
||||||
throw new Error(noNotebookVisible);
|
throw new Error(noNotebookVisible);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
vscode.window.showErrorMessage(getErrorMessage(err));
|
this._apiWrapper.showErrorMessage(getErrorMessage(err));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async addCell(cellType: azdata.nb.CellType): Promise<void> {
|
public async addCell(cellType: azdata.nb.CellType): Promise<void> {
|
||||||
try {
|
try {
|
||||||
let notebook = azdata.nb.activeNotebookEditor;
|
let notebook = this._apiWrapper.getActiveNotebookEditor();
|
||||||
if (notebook) {
|
if (notebook) {
|
||||||
await notebook.edit((editBuilder: azdata.nb.NotebookEditorEdit) => {
|
await notebook.edit((editBuilder: azdata.nb.NotebookEditorEdit) => {
|
||||||
// TODO should prompt and handle cell placement
|
// TODO should prompt and handle cell placement
|
||||||
@@ -116,7 +116,7 @@ export class NotebookUtils {
|
|||||||
throw new Error(noNotebookVisible);
|
throw new Error(noNotebookVisible);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
vscode.window.showErrorMessage(getErrorMessage(err));
|
this._apiWrapper.showErrorMessage(getErrorMessage(err));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +126,7 @@ export class NotebookUtils {
|
|||||||
let title = this.findNextUntitledEditorName();
|
let title = this.findNextUntitledEditorName();
|
||||||
let untitledUri = vscode.Uri.parse(`untitled:${title}`);
|
let untitledUri = vscode.Uri.parse(`untitled:${title}`);
|
||||||
|
|
||||||
let editor = await azdata.nb.showNotebookDocument(untitledUri, {
|
let editor = await this._apiWrapper.showNotebookDocument(untitledUri, {
|
||||||
connectionProfile: oeContext ? oeContext.connectionProfile : undefined,
|
connectionProfile: oeContext ? oeContext.connectionProfile : undefined,
|
||||||
providerId: JUPYTER_NOTEBOOK_PROVIDER,
|
providerId: JUPYTER_NOTEBOOK_PROVIDER,
|
||||||
preview: false,
|
preview: false,
|
||||||
@@ -142,8 +142,7 @@ export class NotebookUtils {
|
|||||||
if (hdfsPath.length > 0) {
|
if (hdfsPath.length > 0) {
|
||||||
let analyzeCommand = '#' + msgSampleCodeDataFrame + os.EOL + 'df = (spark.read.option("inferSchema", "true")'
|
let analyzeCommand = '#' + msgSampleCodeDataFrame + os.EOL + 'df = (spark.read.option("inferSchema", "true")'
|
||||||
+ os.EOL + '.option("header", "true")' + os.EOL + '.csv("{0}"))' + os.EOL + 'df.show(10)';
|
+ os.EOL + '.option("header", "true")' + os.EOL + '.csv("{0}"))' + os.EOL + 'df.show(10)';
|
||||||
|
await editor.edit(editBuilder => {
|
||||||
editor.edit(editBuilder => {
|
|
||||||
editBuilder.insertCell({
|
editBuilder.insertCell({
|
||||||
cell_type: 'code',
|
cell_type: 'code',
|
||||||
source: analyzeCommand.replace('{0}', hdfsPath)
|
source: analyzeCommand.replace('{0}', hdfsPath)
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { JupyterServerInstallation, PythonPkgDetails } from '../../jupyter/jupyt
|
|||||||
import * as utils from '../../common/utils';
|
import * as utils from '../../common/utils';
|
||||||
import { ManagePackagesDialog } from './managePackagesDialog';
|
import { ManagePackagesDialog } from './managePackagesDialog';
|
||||||
import CodeAdapter from '../../prompts/adapter';
|
import CodeAdapter from '../../prompts/adapter';
|
||||||
import { QuestionTypes, IQuestion } from '../../prompts/question';
|
import { IQuestion, confirm } from '../../prompts/question';
|
||||||
|
|
||||||
const localize = nls.loadMessageBundle();
|
const localize = nls.loadMessageBundle();
|
||||||
|
|
||||||
@@ -222,10 +222,10 @@ export class InstalledPackagesTab {
|
|||||||
|
|
||||||
this.uninstallPackageButton.updateProperties({ enabled: false });
|
this.uninstallPackageButton.updateProperties({ enabled: false });
|
||||||
let doUninstall = await this.prompter.promptSingle<boolean>(<IQuestion>{
|
let doUninstall = await this.prompter.promptSingle<boolean>(<IQuestion>{
|
||||||
type: QuestionTypes.confirm,
|
type: confirm,
|
||||||
message: localize('managePackages.confirmUninstall', "Are you sure you want to uninstall the specified packages?"),
|
message: localize('managePackages.confirmUninstall', "Are you sure you want to uninstall the specified packages?"),
|
||||||
default: false
|
default: false
|
||||||
});
|
}, this.jupyterInstallation?.apiWrapper);
|
||||||
|
|
||||||
if (doUninstall) {
|
if (doUninstall) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import * as localizedConstants from '../common/localizedConstants';
|
|||||||
import { JupyterServerInstallation } from './jupyterServerInstallation';
|
import { JupyterServerInstallation } from './jupyterServerInstallation';
|
||||||
import { IServerInstance } from './common';
|
import { IServerInstance } from './common';
|
||||||
import * as utils from '../common/utils';
|
import * as utils from '../common/utils';
|
||||||
import { IPrompter, QuestionTypes, IQuestion } from '../prompts/question';
|
import { IPrompter, IQuestion, confirm } from '../prompts/question';
|
||||||
|
|
||||||
import { AppContext } from '../common/appContext';
|
import { AppContext } from '../common/appContext';
|
||||||
import { ApiWrapper } from '../common/apiWrapper';
|
import { ApiWrapper } from '../common/apiWrapper';
|
||||||
@@ -42,6 +42,7 @@ export class JupyterController implements vscode.Disposable {
|
|||||||
|
|
||||||
private outputChannel: vscode.OutputChannel;
|
private outputChannel: vscode.OutputChannel;
|
||||||
private prompter: IPrompter;
|
private prompter: IPrompter;
|
||||||
|
private _notebookProvider: JupyterNotebookProvider;
|
||||||
|
|
||||||
constructor(private appContext: AppContext) {
|
constructor(private appContext: AppContext) {
|
||||||
this.prompter = new CodeAdapter();
|
this.prompter = new CodeAdapter();
|
||||||
@@ -56,6 +57,10 @@ export class JupyterController implements vscode.Disposable {
|
|||||||
return this.appContext && this.appContext.extensionContext;
|
return this.appContext && this.appContext.extensionContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public get notebookProvider(): JupyterNotebookProvider {
|
||||||
|
return this._notebookProvider;
|
||||||
|
}
|
||||||
|
|
||||||
public dispose(): void {
|
public dispose(): void {
|
||||||
this.deactivate();
|
this.deactivate();
|
||||||
}
|
}
|
||||||
@@ -89,23 +94,22 @@ export class JupyterController implements vscode.Disposable {
|
|||||||
let supportedFileFilter: vscode.DocumentFilter[] = [
|
let supportedFileFilter: vscode.DocumentFilter[] = [
|
||||||
{ scheme: 'untitled', language: '*' }
|
{ scheme: 'untitled', language: '*' }
|
||||||
];
|
];
|
||||||
let notebookProvider = this.registerNotebookProvider();
|
this.registerNotebookProvider();
|
||||||
this.extensionContext.subscriptions.push(this.apiWrapper.registerCompletionItemProvider(supportedFileFilter, new NotebookCompletionItemProvider(notebookProvider)));
|
this.extensionContext.subscriptions.push(this.apiWrapper.registerCompletionItemProvider(supportedFileFilter, new NotebookCompletionItemProvider(this._notebookProvider)));
|
||||||
|
|
||||||
this.registerDefaultPackageManageProviders();
|
this.registerDefaultPackageManageProviders();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private registerNotebookProvider(): JupyterNotebookProvider {
|
private registerNotebookProvider(): void {
|
||||||
let notebookProvider = new JupyterNotebookProvider((documentUri: vscode.Uri) => new LocalJupyterServerManager({
|
this._notebookProvider = new JupyterNotebookProvider((documentUri: vscode.Uri) => new LocalJupyterServerManager({
|
||||||
documentPath: documentUri.fsPath,
|
documentPath: documentUri.fsPath,
|
||||||
jupyterInstallation: this._jupyterInstallation,
|
jupyterInstallation: this._jupyterInstallation,
|
||||||
extensionContext: this.extensionContext,
|
extensionContext: this.extensionContext,
|
||||||
apiWrapper: this.apiWrapper,
|
apiWrapper: this.apiWrapper,
|
||||||
factory: this._serverInstanceFactory
|
factory: this._serverInstanceFactory
|
||||||
}));
|
}));
|
||||||
azdata.nb.registerNotebookProvider(notebookProvider);
|
azdata.nb.registerNotebookProvider(this._notebookProvider);
|
||||||
return notebookProvider;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private saveProfileAndCreateNotebook(profile: azdata.IConnectionProfile): Promise<void> {
|
private saveProfileAndCreateNotebook(profile: azdata.IConnectionProfile): Promise<void> {
|
||||||
@@ -198,10 +202,10 @@ export class JupyterController implements vscode.Disposable {
|
|||||||
//Confirmation message dialog
|
//Confirmation message dialog
|
||||||
private async confirmReinstall(): Promise<boolean> {
|
private async confirmReinstall(): Promise<boolean> {
|
||||||
return await this.prompter.promptSingle<boolean>(<IQuestion>{
|
return await this.prompter.promptSingle<boolean>(<IQuestion>{
|
||||||
type: QuestionTypes.confirm,
|
type: confirm,
|
||||||
message: localize('confirmReinstall', "Are you sure you want to reinstall?"),
|
message: localize('confirmReinstall', "Are you sure you want to reinstall?"),
|
||||||
default: true
|
default: true
|
||||||
});
|
}, this.apiWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async doManagePackages(options?: ManagePackageDialogOptions): Promise<void> {
|
public async doManagePackages(options?: ManagePackageDialogOptions): Promise<void> {
|
||||||
|
|||||||
@@ -31,8 +31,12 @@ export class JupyterNotebookProvider implements nb.NotebookProvider {
|
|||||||
return Promise.resolve(this.doGetNotebookManager(notebookUri));
|
return Promise.resolve(this.doGetNotebookManager(notebookUri));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public get notebookManagerCount(): number {
|
||||||
|
return this.managerTracker.size;
|
||||||
|
}
|
||||||
|
|
||||||
private doGetNotebookManager(notebookUri: vscode.Uri): nb.NotebookManager {
|
private doGetNotebookManager(notebookUri: vscode.Uri): nb.NotebookManager {
|
||||||
let baseFolder = this.transformToBaseFolder(notebookUri.fsPath.toString());
|
let baseFolder = this.transformToBaseFolder(notebookUri?.fsPath?.toString());
|
||||||
let manager = this.managerTracker.get(baseFolder);
|
let manager = this.managerTracker.get(baseFolder);
|
||||||
if (!manager) {
|
if (!manager) {
|
||||||
let baseFolderUri = vscode.Uri.file(baseFolder);
|
let baseFolderUri = vscode.Uri.file(baseFolder);
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import * as utils from '../common/utils';
|
|||||||
import { OutputChannel, ConfigurationTarget, window } from 'vscode';
|
import { OutputChannel, ConfigurationTarget, window } from 'vscode';
|
||||||
import { Deferred } from '../common/promise';
|
import { Deferred } from '../common/promise';
|
||||||
import { ConfigurePythonWizard } from '../dialog/configurePython/configurePythonWizard';
|
import { ConfigurePythonWizard } from '../dialog/configurePython/configurePythonWizard';
|
||||||
import { IPrompter, IQuestion, QuestionTypes } from '../prompts/question';
|
import { IPrompter, IQuestion, confirm } from '../prompts/question';
|
||||||
import CodeAdapter from '../prompts/adapter';
|
import CodeAdapter from '../prompts/adapter';
|
||||||
import { ConfigurePythonDialog } from '../dialog/configurePython/configurePythonDialog';
|
import { ConfigurePythonDialog } from '../dialog/configurePython/configurePythonDialog';
|
||||||
|
|
||||||
@@ -587,10 +587,10 @@ export class JupyterServerInstallation implements IJupyterServerInstallation {
|
|||||||
let doUpgrade: boolean;
|
let doUpgrade: boolean;
|
||||||
if (promptForUpgrade) {
|
if (promptForUpgrade) {
|
||||||
doUpgrade = await this._prompter.promptSingle<boolean>(<IQuestion>{
|
doUpgrade = await this._prompter.promptSingle<boolean>(<IQuestion>{
|
||||||
type: QuestionTypes.confirm,
|
type: confirm,
|
||||||
message: localize('confirmPackageUpgrade', "Some required python packages need to be installed. Would you like to install them now?"),
|
message: localize('confirmPackageUpgrade', "Some required python packages need to be installed. Would you like to install them now?"),
|
||||||
default: true
|
default: true
|
||||||
});
|
}, this.apiWrapper);
|
||||||
if (!doUpgrade) {
|
if (!doUpgrade) {
|
||||||
throw new Error(localize('configurePython.packageInstallDeclined', "Package installation was declined."));
|
throw new Error(localize('configurePython.packageInstallDeclined', "Package installation was declined."));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,32 +1,17 @@
|
|||||||
// This code is originally from https://github.com/DonJayamanne/bowerVSCode
|
// This code is originally from https://github.com/DonJayamanne/bowerVSCode
|
||||||
// License: https://github.com/DonJayamanne/bowerVSCode/blob/master/LICENSE
|
// License: https://github.com/DonJayamanne/bowerVSCode/blob/master/LICENSE
|
||||||
|
|
||||||
import { window } from 'vscode';
|
|
||||||
import PromptFactory from './factory';
|
import PromptFactory from './factory';
|
||||||
import EscapeException from './escapeException';
|
import EscapeException from './escapeException';
|
||||||
import { IQuestion, IPrompter, IPromptCallback } from './question';
|
import { IQuestion, IPrompter } from './question';
|
||||||
|
import { ApiWrapper } from '../common/apiWrapper';
|
||||||
|
|
||||||
// Supports simple pattern for prompting for user input and acting on this
|
// Supports simple pattern for prompting for user input and acting on this
|
||||||
export default class CodeAdapter implements IPrompter {
|
export default class CodeAdapter implements IPrompter {
|
||||||
|
|
||||||
// TODO define question interface
|
public promptSingle<T>(question: IQuestion, apiWrapper?: ApiWrapper): Promise<T> {
|
||||||
private fixQuestion(question: any): any {
|
|
||||||
if (question.type === 'checkbox' && Array.isArray(question.choices)) {
|
|
||||||
// For some reason when there's a choice of checkboxes, they aren't formatted properly
|
|
||||||
// Not sure where the issue is
|
|
||||||
question.choices = question.choices.map((item: any) => {
|
|
||||||
if (typeof (item) === 'string') {
|
|
||||||
return { checked: false, name: item, value: item };
|
|
||||||
} else {
|
|
||||||
return item;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public promptSingle<T>(question: IQuestion, ignoreFocusOut?: boolean): Promise<T> {
|
|
||||||
let questions: IQuestion[] = [question];
|
let questions: IQuestion[] = [question];
|
||||||
return this.prompt(questions, ignoreFocusOut).then((answers: { [key: string]: T }) => {
|
return this.prompt(questions, apiWrapper).then((answers: { [key: string]: T }) => {
|
||||||
if (answers) {
|
if (answers) {
|
||||||
let response: T = answers[question.name];
|
let response: T = answers[question.name];
|
||||||
return response || undefined;
|
return response || undefined;
|
||||||
@@ -35,15 +20,13 @@ export default class CodeAdapter implements IPrompter {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public prompt<T>(questions: IQuestion[], ignoreFocusOut?: boolean): Promise<{ [key: string]: T }> {
|
public prompt<T>(questions: IQuestion[], apiWrapper = new ApiWrapper()): Promise<{ [key: string]: T }> {
|
||||||
let answers: { [key: string]: T } = {};
|
let answers: { [key: string]: T } = {};
|
||||||
|
|
||||||
// Collapse multiple questions into a set of prompt steps
|
// Collapse multiple questions into a set of prompt steps
|
||||||
let promptResult: Promise<{ [key: string]: T }> = questions.reduce((promise: Promise<{ [key: string]: T }>, question: IQuestion) => {
|
let promptResult: Promise<{ [key: string]: T }> = questions.reduce((promise: Promise<{ [key: string]: T }>, question: IQuestion) => {
|
||||||
this.fixQuestion(question);
|
|
||||||
|
|
||||||
return promise.then(() => {
|
return promise.then(() => {
|
||||||
return PromptFactory.createPrompt(question, ignoreFocusOut);
|
return PromptFactory.createPrompt(question);
|
||||||
}).then(prompt => {
|
}).then(prompt => {
|
||||||
// Original Code: uses jQuery patterns. Keeping for reference
|
// Original Code: uses jQuery patterns. Keeping for reference
|
||||||
// if (!question.when || question.when(answers) === true) {
|
// if (!question.when || question.when(answers) === true) {
|
||||||
@@ -53,7 +36,7 @@ export default class CodeAdapter implements IPrompter {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
if (!question.shouldPrompt || question.shouldPrompt(answers) === true) {
|
if (!question.shouldPrompt || question.shouldPrompt(answers) === true) {
|
||||||
return prompt.render().then((result: any) => {
|
return prompt.render(apiWrapper).then((result: any) => {
|
||||||
answers[question.name] = result;
|
answers[question.name] = result;
|
||||||
|
|
||||||
if (question.onAnswered) {
|
if (question.onAnswered) {
|
||||||
@@ -71,17 +54,7 @@ export default class CodeAdapter implements IPrompter {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
window.showErrorMessage(err.message);
|
apiWrapper.showErrorMessage(err.message);
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper to make it possible to prompt using callback pattern. Generally Promise is a preferred flow
|
|
||||||
public promptCallback(questions: IQuestion[], callback: IPromptCallback): void {
|
|
||||||
// Collapse multiple questions into a set of prompt steps
|
|
||||||
this.prompt(questions).then(answers => {
|
|
||||||
if (callback) {
|
|
||||||
callback(answers);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,18 +3,18 @@
|
|||||||
// This code is originally from https://github.com/DonJayamanne/bowerVSCode
|
// This code is originally from https://github.com/DonJayamanne/bowerVSCode
|
||||||
// License: https://github.com/DonJayamanne/bowerVSCode/blob/master/LICENSE
|
// License: https://github.com/DonJayamanne/bowerVSCode/blob/master/LICENSE
|
||||||
|
|
||||||
import { window } from 'vscode';
|
|
||||||
import Prompt from './prompt';
|
import Prompt from './prompt';
|
||||||
import LocalizedConstants = require('../common/localizedConstants');
|
import LocalizedConstants = require('../common/localizedConstants');
|
||||||
import EscapeException from './escapeException';
|
import EscapeException from './escapeException';
|
||||||
|
import { ApiWrapper } from '../common/apiWrapper';
|
||||||
|
|
||||||
export default class ConfirmPrompt extends Prompt {
|
export default class ConfirmPrompt extends Prompt {
|
||||||
|
|
||||||
constructor(question: any, ignoreFocusOut?: boolean) {
|
constructor(question: any) {
|
||||||
super(question, ignoreFocusOut);
|
super(question);
|
||||||
}
|
}
|
||||||
|
|
||||||
public render(): any {
|
public render(apiWrapper: ApiWrapper): any {
|
||||||
let choices: { [id: string]: boolean } = {};
|
let choices: { [id: string]: boolean } = {};
|
||||||
choices[LocalizedConstants.msgYes] = true;
|
choices[LocalizedConstants.msgYes] = true;
|
||||||
choices[LocalizedConstants.msgNo] = false;
|
choices[LocalizedConstants.msgNo] = false;
|
||||||
@@ -22,7 +22,7 @@ export default class ConfirmPrompt extends Prompt {
|
|||||||
let options = this.defaultQuickPickOptions;
|
let options = this.defaultQuickPickOptions;
|
||||||
options.placeHolder = this._question.message;
|
options.placeHolder = this._question.message;
|
||||||
|
|
||||||
return window.showQuickPick(Object.keys(choices), options)
|
return apiWrapper.showQuickPick(Object.keys(choices), options)
|
||||||
.then(result => {
|
.then(result => {
|
||||||
if (result === undefined) {
|
if (result === undefined) {
|
||||||
throw new EscapeException();
|
throw new EscapeException();
|
||||||
|
|||||||
@@ -8,10 +8,10 @@ import ConfirmPrompt from './confirm';
|
|||||||
|
|
||||||
export default class PromptFactory {
|
export default class PromptFactory {
|
||||||
|
|
||||||
public static createPrompt(question: any, ignoreFocusOut?: boolean): Prompt {
|
public static createPrompt(question: any): Prompt {
|
||||||
switch (question.type) {
|
switch (question.type) {
|
||||||
case 'confirm':
|
case 'confirm':
|
||||||
return new ConfirmPrompt(question, ignoreFocusOut);
|
return new ConfirmPrompt(question);
|
||||||
default:
|
default:
|
||||||
throw new Error(`Could not find a prompt for question type ${question.type}`);
|
throw new Error(`Could not find a prompt for question type ${question.type}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
// License: https://github.com/DonJayamanne/bowerVSCode/blob/master/LICENSE
|
// License: https://github.com/DonJayamanne/bowerVSCode/blob/master/LICENSE
|
||||||
|
|
||||||
import { InputBoxOptions, QuickPickOptions } from 'vscode';
|
import { InputBoxOptions, QuickPickOptions } from 'vscode';
|
||||||
|
import { ApiWrapper } from '../common/apiWrapper';
|
||||||
|
|
||||||
abstract class Prompt {
|
abstract class Prompt {
|
||||||
|
|
||||||
@@ -15,7 +16,7 @@ abstract class Prompt {
|
|||||||
this._ignoreFocusOut = ignoreFocusOut ? ignoreFocusOut : false;
|
this._ignoreFocusOut = ignoreFocusOut ? ignoreFocusOut : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract render(): any;
|
public abstract render(apiWrapper: ApiWrapper): any;
|
||||||
|
|
||||||
protected get defaultQuickPickOptions(): QuickPickOptions {
|
protected get defaultQuickPickOptions(): QuickPickOptions {
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -4,15 +4,9 @@
|
|||||||
*--------------------------------------------------------------------------------------------*/
|
*--------------------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
import vscode = require('vscode');
|
import vscode = require('vscode');
|
||||||
|
import { ApiWrapper } from '../common/apiWrapper';
|
||||||
|
|
||||||
export class QuestionTypes {
|
export const confirm = 'confirm';
|
||||||
public static get input(): string { return 'input'; }
|
|
||||||
public static get password(): string { return 'password'; }
|
|
||||||
public static get list(): string { return 'list'; }
|
|
||||||
public static get confirm(): string { return 'confirm'; }
|
|
||||||
public static get checkbox(): string { return 'checkbox'; }
|
|
||||||
public static get expand(): string { return 'expand'; }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Question interface to clarify how to use the prompt feature
|
// Question interface to clarify how to use the prompt feature
|
||||||
// based on Bower Question format: https://github.com/bower/bower/blob/89069784bb46bfd6639b4a75e98a0d7399a8c2cb/packages/bower-logger/README.md
|
// based on Bower Question format: https://github.com/bower/bower/blob/89069784bb46bfd6639b4a75e98a0d7399a8c2cb/packages/bower-logger/README.md
|
||||||
@@ -54,15 +48,14 @@ export interface IQuestionHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface IPrompter {
|
export interface IPrompter {
|
||||||
promptSingle<T>(question: IQuestion, ignoreFocusOut?: boolean): Promise<T>;
|
promptSingle<T>(question: IQuestion, apiWrapper?: ApiWrapper): Promise<T>;
|
||||||
/**
|
/**
|
||||||
* Prompts for multiple questions
|
* Prompts for multiple questions
|
||||||
*
|
*
|
||||||
* @returns Map of question IDs to results, or undefined if
|
* @returns Map of question IDs to results, or undefined if
|
||||||
* the user canceled the question session
|
* the user canceled the question session
|
||||||
*/
|
*/
|
||||||
prompt<T>(questions: IQuestion[], ignoreFocusOut?: boolean): Promise<{ [questionId: string]: any }>;
|
prompt<T>(questions: IQuestion[], apiWrapper?: ApiWrapper): Promise<{ [questionId: string]: any }>;
|
||||||
promptCallback(questions: IQuestion[], callback: IPromptCallback): void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IPromptCallback {
|
export interface IPromptCallback {
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import * as path from 'path';
|
|||||||
import * as querystring from 'querystring';
|
import * as querystring from 'querystring';
|
||||||
import * as nls from 'vscode-nls';
|
import * as nls from 'vscode-nls';
|
||||||
const localize = nls.loadMessageBundle();
|
const localize = nls.loadMessageBundle();
|
||||||
import { IQuestion, QuestionTypes } from '../prompts/question';
|
import { IQuestion, confirm } from '../prompts/question';
|
||||||
import CodeAdapter from '../prompts/adapter';
|
import CodeAdapter from '../prompts/adapter';
|
||||||
import { getErrorMessage, isEditorTitleFree } from '../common/utils';
|
import { getErrorMessage, isEditorTitleFree } from '../common/utils';
|
||||||
|
|
||||||
@@ -61,7 +61,7 @@ export class NotebookUriHandler implements vscode.UriHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let doOpen = await this.prompter.promptSingle<boolean>(<IQuestion>{
|
let doOpen = await this.prompter.promptSingle<boolean>(<IQuestion>{
|
||||||
type: QuestionTypes.confirm,
|
type: confirm,
|
||||||
message: localize('notebook.confirmOpen', "Download and open '{0}'?", url),
|
message: localize('notebook.confirmOpen', "Download and open '{0}'?", url),
|
||||||
default: true
|
default: true
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -14,15 +14,21 @@ import * as uuid from 'uuid';
|
|||||||
import { promises as fs } from 'fs';
|
import { promises as fs } from 'fs';
|
||||||
import { ApiWrapper } from '../../common/apiWrapper';
|
import { ApiWrapper } from '../../common/apiWrapper';
|
||||||
import { tryDeleteFile } from './testUtils';
|
import { tryDeleteFile } from './testUtils';
|
||||||
|
import { CellTypes } from '../../contracts/content';
|
||||||
|
|
||||||
describe('notebookUtils Tests', function (): void {
|
describe('notebookUtils Tests', function (): void {
|
||||||
let notebookUtils: NotebookUtils;
|
let notebookUtils: NotebookUtils;
|
||||||
let apiWrapperMock: TypeMoq.IMock<ApiWrapper>;
|
let apiWrapperMock: TypeMoq.IMock<ApiWrapper>;
|
||||||
before(function (): void {
|
|
||||||
|
beforeEach(function (): void {
|
||||||
apiWrapperMock = TypeMoq.Mock.ofInstance(new ApiWrapper());
|
apiWrapperMock = TypeMoq.Mock.ofInstance(new ApiWrapper());
|
||||||
notebookUtils = new NotebookUtils(apiWrapperMock.object);
|
notebookUtils = new NotebookUtils(apiWrapperMock.object);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.afterAll(async function (): Promise<void> {
|
||||||
|
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
|
||||||
|
});
|
||||||
|
|
||||||
describe('newNotebook', function (): void {
|
describe('newNotebook', function (): void {
|
||||||
it('Should open a new notebook successfully', async function (): Promise<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');
|
should(azdata.nb.notebookDocuments.length).equal(0, 'There should be not any open Notebook documents');
|
||||||
@@ -80,4 +86,117 @@ describe('notebookUtils Tests', function (): void {
|
|||||||
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
|
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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());
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
await notebookUtils.runActiveCell();
|
||||||
|
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
|
||||||
|
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);
|
||||||
|
await notebookUtils.clearActiveCellOutput();
|
||||||
|
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.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);
|
||||||
|
await notebookUtils.clearActiveCellOutput();
|
||||||
|
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
|
||||||
|
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);
|
||||||
|
await notebookUtils.runAllCells();
|
||||||
|
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.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);
|
||||||
|
await notebookUtils.runAllCells();
|
||||||
|
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
|
||||||
|
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);
|
||||||
|
await notebookUtils.addCell('code');
|
||||||
|
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
|
||||||
|
});
|
||||||
|
|
||||||
|
it('shows error when no notebook visible for markdown cell', async function (): Promise<void> {
|
||||||
|
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => undefined);
|
||||||
|
await notebookUtils.addCell('markdown');
|
||||||
|
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.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);
|
||||||
|
await notebookUtils.addCell('code');
|
||||||
|
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
|
||||||
|
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);
|
||||||
|
await notebookUtils.addCell('markdown');
|
||||||
|
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
|
||||||
|
should(notebookEditor.document.cells.length).equal(1);
|
||||||
|
should(notebookEditor.document.cells[0].contents.cell_type).equal(CellTypes.Markdown);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
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));
|
||||||
|
const oeContext: azdata.ObjectExplorerContext = {
|
||||||
|
connectionProfile: undefined,
|
||||||
|
isConnectionNode: true,
|
||||||
|
nodeInfo: {
|
||||||
|
nodePath: 'path/HDFS/path2',
|
||||||
|
errorMessage: undefined,
|
||||||
|
isLeaf: false,
|
||||||
|
label: 'fakeLabel',
|
||||||
|
metadata: undefined,
|
||||||
|
nodeStatus: undefined,
|
||||||
|
nodeSubType: undefined,
|
||||||
|
nodeType: undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await notebookUtils.analyzeNotebook(oeContext);
|
||||||
|
should(notebookEditor.document.cells.length).equal(1, 'One cell should exist');
|
||||||
|
should(notebookEditor.document.cells[0].contents.cell_type).equal(CellTypes.Code, 'Cell was created with incorrect type');
|
||||||
|
});
|
||||||
|
|
||||||
|
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));
|
||||||
|
await notebookUtils.analyzeNotebook();
|
||||||
|
should(notebookEditor.document.cells.length).equal(0, 'No cells should exist');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
50
extensions/notebook/src/test/common/prompt.test.ts
Normal file
50
extensions/notebook/src/test/common/prompt.test.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
/*---------------------------------------------------------------------------------------------
|
||||||
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||||
|
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||||
|
*--------------------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
import * as TypeMoq from 'typemoq';
|
||||||
|
|
||||||
|
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>;
|
||||||
|
|
||||||
|
|
||||||
|
before(function () {
|
||||||
|
prompter = new CodeAdapter();
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
mockApiWrapper = TypeMoq.Mock.ofType<ApiWrapper>();
|
||||||
|
mockApiWrapper.setup(x => x.showErrorMessage(TypeMoq.It.isAny()));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should not find prompt for invalid question type', async function (): Promise<void> {
|
||||||
|
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());
|
||||||
|
});
|
||||||
|
|
||||||
|
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'));
|
||||||
|
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());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||||
*--------------------------------------------------------------------------------------------*/
|
*--------------------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
import * as azdata from 'azdata';
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import * as should from 'should';
|
import * as should from 'should';
|
||||||
import * as TypeMoq from 'typemoq';
|
import * as TypeMoq from 'typemoq';
|
||||||
@@ -12,28 +13,34 @@ import { AppContext } from '../../common/appContext';
|
|||||||
import { JupyterController } from '../../jupyter/jupyterController';
|
import { JupyterController } from '../../jupyter/jupyterController';
|
||||||
import { LocalPipPackageManageProvider } from '../../jupyter/localPipPackageManageProvider';
|
import { LocalPipPackageManageProvider } from '../../jupyter/localPipPackageManageProvider';
|
||||||
import { MockExtensionContext } from '../common/stubs';
|
import { MockExtensionContext } from '../common/stubs';
|
||||||
|
import { NotebookUtils } from '../../common/notebookUtils';
|
||||||
|
|
||||||
describe('JupyterController tests', function () {
|
describe('Jupyter Controller', function () {
|
||||||
let mockExtensionContext: vscode.ExtensionContext;
|
let mockExtensionContext: vscode.ExtensionContext;
|
||||||
let appContext: AppContext;
|
let appContext: AppContext;
|
||||||
let controller: JupyterController;
|
let controller: JupyterController;
|
||||||
let mockApiWrapper: TypeMoq.IMock<ApiWrapper>;
|
let mockApiWrapper: TypeMoq.IMock<ApiWrapper>;
|
||||||
|
let connection: azdata.connection.ConnectionProfile;
|
||||||
|
|
||||||
this.beforeAll(() => {
|
this.beforeAll(() => {
|
||||||
mockExtensionContext = new MockExtensionContext();
|
mockExtensionContext = new MockExtensionContext();
|
||||||
mockApiWrapper = TypeMoq.Mock.ofType<ApiWrapper>();
|
connection = new azdata.connection.ConnectionProfile();
|
||||||
appContext = new AppContext(mockExtensionContext, mockApiWrapper.object);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.beforeEach(() => {
|
this.beforeEach(() => {
|
||||||
|
mockApiWrapper = TypeMoq.Mock.ofType<ApiWrapper>();
|
||||||
|
mockApiWrapper.setup(x => x.getCurrentConnection()).returns(() => { return Promise.resolve(connection); });
|
||||||
|
appContext = new AppContext(mockExtensionContext, mockApiWrapper.object);
|
||||||
controller = new JupyterController(appContext);
|
controller = new JupyterController(appContext);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should activate new JupyterController successfully', async () => {
|
it('should activate new JupyterController successfully', async () => {
|
||||||
should(controller.extensionContext).deepEqual(appContext.extensionContext, 'Extension context should be passed through');
|
should(controller.extensionContext).deepEqual(appContext.extensionContext, 'Extension context should be passed through');
|
||||||
|
should(controller.jupyterInstallation).equal(undefined, 'JupyterInstallation should be undefined before controller activation');
|
||||||
await should(controller.activate()).not.be.rejected();
|
await should(controller.activate()).not.be.rejected();
|
||||||
// On activation, local pip and local conda package providers should exist
|
// On activation, local pip and local conda package providers should exist
|
||||||
should(controller.packageManageProviders.size).equal(2, 'Local pip and conda package providers should be default providers');
|
should(controller.packageManageProviders.size).equal(2, 'Local pip and conda package providers should be default providers');
|
||||||
|
should(controller.jupyterInstallation.extensionPath).equal(appContext.extensionContext.extensionPath, 'JupyterInstallation extension path should match appContext extension path');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create new packageManageProvider successfully', async () => {
|
it('should create new packageManageProvider successfully', async () => {
|
||||||
@@ -50,4 +57,43 @@ describe('JupyterController tests', function () {
|
|||||||
should.throws(() => controller.registerPackageManager('provider1', mockProvider.object));
|
should.throws(() => controller.registerPackageManager('provider1', mockProvider.object));
|
||||||
should(controller.packageManageProviders.size).equal(1, 'Package manage providers should still equal 1');
|
should(controller.packageManageProviders.size).equal(1, 'Package manage providers should still equal 1');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should should get defaultConnection() successfully', async () => {
|
||||||
|
let defaultConnection = await controller.getDefaultConnection();
|
||||||
|
should(defaultConnection).deepEqual(connection, 'getDefaultConnection() did not return expected result');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should show error message for doManagePackages before activation', async () => {
|
||||||
|
await controller.doManagePackages();
|
||||||
|
mockApiWrapper.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not show error message for doManagePackages after activation', async () => {
|
||||||
|
await controller.activate();
|
||||||
|
await controller.doManagePackages();
|
||||||
|
mockApiWrapper.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Returns expected values from notebook provider', async () => {
|
||||||
|
await controller.activate();
|
||||||
|
should(controller.notebookProvider.standardKernels).deepEqual([], 'Notebook provider standard kernels should return empty array');
|
||||||
|
should(controller.notebookProvider.providerId).equal('jupyter', 'Notebook provider should be jupyter');
|
||||||
|
should(controller.notebookProvider.getNotebookManager(undefined)).be.rejected();
|
||||||
|
should(controller.notebookProvider.notebookManagerCount).equal(0);
|
||||||
|
controller.notebookProvider.handleNotebookClosed(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Returns notebook manager for real notebook editor', async () => {
|
||||||
|
await controller.activate();
|
||||||
|
let notebookUtils = new NotebookUtils(mockApiWrapper.object);
|
||||||
|
const notebookEditor = await notebookUtils.newNotebook(undefined);
|
||||||
|
let notebookManager = await controller.notebookProvider.getNotebookManager(notebookEditor.document.uri);
|
||||||
|
should(controller.notebookProvider.notebookManagerCount).equal(1);
|
||||||
|
|
||||||
|
// Session manager should not be immediately ready
|
||||||
|
should(notebookManager.sessionManager.isReady).equal(false);
|
||||||
|
// Session manager should not immediately have specs
|
||||||
|
should(notebookManager.sessionManager.specs).equal(undefined);
|
||||||
|
controller.notebookProvider.handleNotebookClosed(notebookEditor.document.uri);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user