Skip to content

Commit 8b65f32

Browse files
committed
made test
1 parent 910f68a commit 8b65f32

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
'use strict';
5+
6+
import { inject, injectable } from 'inversify';
7+
import { QuickPickItem, QuickPickOptions } from 'vscode';
8+
import { ICommandNameArgumentTypeMapping } from '../../common/application/commands';
9+
import { IApplicationShell, ICommandManager } from '../../common/application/types';
10+
import { IDisposable } from '../../common/types';
11+
import { Commands } from '../constants';
12+
import { ExportFormat, ExportManager, IExportManager } from '../export/exportManager';
13+
import { INotebookEditorProvider } from '../types';
14+
15+
interface IExportQuickPickItem extends QuickPickItem {
16+
handler(): void;
17+
}
18+
19+
@injectable()
20+
export class ExportCommands implements IDisposable {
21+
private readonly disposables: IDisposable[] = [];
22+
constructor(
23+
@inject(ICommandManager) private readonly commandManager: ICommandManager,
24+
@inject(IExportManager) private exportManager: ExportManager,
25+
@inject(INotebookEditorProvider) private notebookEditorProvider: INotebookEditorProvider,
26+
@inject(IApplicationShell) private readonly applicationShell: IApplicationShell
27+
) {}
28+
public register() {
29+
this.registerCommand(Commands.ExportAsPythonScript, () => this.export(ExportFormat.python));
30+
this.registerCommand(Commands.ExportToHTML, () => this.export(ExportFormat.html));
31+
this.registerCommand(Commands.ExportToPDF, () => this.export(ExportFormat.pdf));
32+
this.registerCommand(Commands.Export, this.export);
33+
}
34+
35+
public dispose() {
36+
this.disposables.forEach((d) => d.dispose());
37+
}
38+
39+
private registerCommand<
40+
E extends keyof ICommandNameArgumentTypeMapping,
41+
U extends ICommandNameArgumentTypeMapping[E]
42+
// tslint:disable-next-line: no-any
43+
>(command: E, callback: (...args: U) => any) {
44+
const disposable = this.commandManager.registerCommand(command, callback, this);
45+
this.disposables.push(disposable);
46+
}
47+
48+
private async export(exportMethod?: ExportFormat) {
49+
// get notebook provider
50+
const model = this.notebookEditorProvider.activeEditor?.model;
51+
if (!model) {
52+
throw Error('No active editor found.');
53+
}
54+
55+
if (exportMethod) {
56+
await this.exportManager.export(exportMethod, model);
57+
} else {
58+
const pickedItem = await this.showExportQuickPickMenu().then((item) => item);
59+
if (pickedItem !== undefined) {
60+
pickedItem.handler();
61+
}
62+
}
63+
}
64+
65+
private getExportQuickPickItems(): IExportQuickPickItem[] {
66+
return [
67+
{
68+
label: 'Python Script',
69+
picked: true,
70+
handler: () => this.commandManager.executeCommand(Commands.ExportAsPythonScript)
71+
}
72+
//{ label: 'HTML', picked: false, handler: () => this.commandManager.executeCommand(Commands.ExportToHTML) },
73+
//{ label: 'PDF', picked: false, handler: () => this.commandManager.executeCommand(Commands.ExportToPDF) }
74+
];
75+
}
76+
77+
private async showExportQuickPickMenu(): Promise<IExportQuickPickItem | undefined> {
78+
const items = this.getExportQuickPickItems();
79+
80+
const options: QuickPickOptions = {
81+
ignoreFocusOut: false,
82+
matchOnDescription: true,
83+
matchOnDetail: true,
84+
placeHolder: 'Export As...'
85+
};
86+
87+
return this.applicationShell.showQuickPick(items, options);
88+
}
89+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
'use strict';
5+
6+
import { anything, instance, mock, verify, when } from 'ts-mockito';
7+
import { Uri } from 'vscode';
8+
import { IDocumentManager } from '../../../client/common/application/types';
9+
import { IDisposable } from '../../../client/common/types';
10+
import { ExportFormat, IExportManager } from '../../../client/datascience/export/exportManager';
11+
import { ExportManagerFileOpener } from '../../../client/datascience/export/exportManagerFileOpener';
12+
import { ProgressReporter } from '../../../client/datascience/progress/progressReporter';
13+
import { INotebookModel } from '../../../client/datascience/types';
14+
15+
suite('Data Science - Export File Opener', () => {
16+
let fileOpener: ExportManagerFileOpener;
17+
let exporter: IExportManager;
18+
let documentManager: IDocumentManager;
19+
const model = instance(mock<INotebookModel>());
20+
setup(async () => {
21+
exporter = mock<IExportManager>();
22+
documentManager = mock<IDocumentManager>();
23+
const reporter = mock(ProgressReporter);
24+
// tslint:disable-next-line: no-any
25+
when(reporter.createProgressIndicator(anything())).thenReturn(instance(mock<IDisposable>()) as any);
26+
when(documentManager.openTextDocument(anything())).thenResolve();
27+
when(documentManager.showTextDocument(anything())).thenResolve();
28+
fileOpener = new ExportManagerFileOpener(instance(exporter), instance(documentManager), instance(reporter));
29+
});
30+
31+
test('No file is opened if nothing is exported', async () => {
32+
when(exporter.export(anything(), anything())).thenResolve();
33+
34+
await fileOpener.export(ExportFormat.python, model);
35+
36+
verify(documentManager.showTextDocument(anything())).never();
37+
});
38+
test('File is opened if exported', async () => {
39+
const uri = Uri.file('blah.python');
40+
when(exporter.export(anything(), anything())).thenResolve(uri);
41+
42+
await fileOpener.export(ExportFormat.python, model);
43+
44+
verify(documentManager.showTextDocument(anything())).once();
45+
});
46+
});

0 commit comments

Comments
 (0)