Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/2 Fixes/7567.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When exporting a notebook editor to python script don't use the temp file location for generating the export.
2 changes: 1 addition & 1 deletion src/client/datascience/interactive-ipynb/nativeEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ export class NativeEditor extends InteractiveBase implements INotebookEditor {
await this.fileSystem.writeFile(tempFile.filePath, this.generateNotebookContent(cells), { encoding: 'utf-8' });

// Import this file and show it
const contents = await this.importer.importFromFile(tempFile.filePath);
const contents = await this.importer.importFromFile(tempFile.filePath, this.file.fsPath);
if (contents) {
await this.viewDocument(contents);
}
Expand Down
7 changes: 4 additions & 3 deletions src/client/datascience/jupyter/jupyterImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,20 @@ export class JupyterImporter implements INotebookImporter {
this.templatePromise = this.createTemplateFile();
}

public async importFromFile(file: string): Promise<string> {
public async importFromFile(contentsFile: string, originalFile?: string): Promise<string> {
const template = await this.templatePromise;

// If the user has requested it, add a cd command to the imported file so that relative paths still work
const settings = this.configuration.getSettings();
let directoryChange: string | undefined;
if (settings.datascience.changeDirOnImportExport) {
directoryChange = await this.calculateDirectoryChange(file);
// If an original file is passed in, then use that for calculating the directory change as contents might be an invalid location
directoryChange = await this.calculateDirectoryChange(originalFile ? originalFile : contentsFile);
}

// Use the jupyter nbconvert functionality to turn the notebook into a python file
if (await this.jupyterExecution.isImportSupported()) {
let fileOutput: string = await this.jupyterExecution.importNotebook(file, template);
let fileOutput: string = await this.jupyterExecution.importNotebook(contentsFile, template);
if (fileOutput.includes('get_ipython()')) {
fileOutput = this.addIPythonImport(fileOutput);
}
Expand Down
2 changes: 1 addition & 1 deletion src/client/datascience/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export interface IJupyterKernelSpec extends IAsyncDisposable {

export const INotebookImporter = Symbol('INotebookImporter');
export interface INotebookImporter extends Disposable {
importFromFile(file: string): Promise<string>;
importFromFile(contentsFile: string, originalFile?: string): Promise<string>; // originalFile is the base file if file is a temp file / location
importCellsFromFile(file: string): Promise<ICell[]>;
importCells(json: string): Promise<ICell[]>;
}
Expand Down
66 changes: 53 additions & 13 deletions src/test/datascience/interactive-ipynb/nativeEditor.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import { JupyterExecutionFactory } from '../../../client/datascience/jupyter/jup
import { JupyterExporter } from '../../../client/datascience/jupyter/jupyterExporter';
import { JupyterImporter } from '../../../client/datascience/jupyter/jupyterImporter';
import { JupyterVariables } from '../../../client/datascience/jupyter/jupyterVariables';
import { StatusProvider } from '../../../client/datascience/statusProvider';
import { ThemeFinder } from '../../../client/datascience/themeFinder';
import {
ICodeCssGenerator,
Expand All @@ -49,7 +48,6 @@ import {
INotebookEditorProvider,
INotebookExporter,
INotebookImporter,
IStatusProvider,
IThemeFinder
} from '../../../client/datascience/types';
import { IInterpreterService } from '../../../client/interpreter/contracts';
Expand All @@ -58,6 +56,7 @@ import { createEmptyCell } from '../../../datascience-ui/interactive-common/main
import { waitForCondition } from '../../common';
import { noop } from '../../core';
import { MockMemento } from '../../mocks/mementos';
import { MockStatusProvider } from '../mockStatusProvider';

// tslint:disable: no-any chai-vague-errors no-unused-expression

Expand All @@ -66,7 +65,7 @@ suite('Data Science - Native Editor', () => {
let workspace: IWorkspaceService;
let configService: IConfigurationService;
let fileSystem: IFileSystem;
let doctManager: IDocumentManager;
let docManager: IDocumentManager;
let dsErrorHandler: IDataScienceErrorHandler;
let cmdManager: ICommandManager;
let liveShare: ILiveShareApi;
Expand All @@ -76,7 +75,7 @@ suite('Data Science - Native Editor', () => {
const disposables: Disposable[] = [];
let cssGenerator: ICodeCssGenerator;
let themeFinder: IThemeFinder;
let statusProvider: IStatusProvider;
let statusProvider: MockStatusProvider;
let executionProvider: IJupyterExecution;
let exportProvider: INotebookExporter;
let editorProvider: INotebookEditorProvider;
Expand Down Expand Up @@ -188,7 +187,7 @@ suite('Data Science - Native Editor', () => {
storageUpdateSpy = sinon.spy(storage, 'update');
configService = mock(ConfigurationService);
fileSystem = mock(FileSystem);
doctManager = mock(DocumentManager);
docManager = mock(DocumentManager);
dsErrorHandler = mock(DataScienceErrorHandler);
cmdManager = mock(CommandManager);
workspace = mock(WorkspaceService);
Expand All @@ -198,7 +197,7 @@ suite('Data Science - Native Editor', () => {
webPanelProvider = mock(WebPanelProvider);
cssGenerator = mock(CodeCssGenerator);
themeFinder = mock(ThemeFinder);
statusProvider = mock(StatusProvider);
statusProvider = new MockStatusProvider();
executionProvider = mock(JupyterExecutionFactory);
exportProvider = mock(JupyterExporter);
editorProvider = mock(NativeEditorProvider);
Expand All @@ -218,10 +217,11 @@ suite('Data Science - Native Editor', () => {
when(interpreterService.onDidChangeInterpreter).thenReturn(interprerterChangeEvent.event);

const editorChangeEvent = new EventEmitter<TextEditor | undefined>();
when(doctManager.onDidChangeActiveTextEditor).thenReturn(editorChangeEvent.event);
when(docManager.onDidChangeActiveTextEditor).thenReturn(editorChangeEvent.event);

const sessionChangedEvent = new EventEmitter<void>();
when(executionProvider.sessionChanged).thenReturn(sessionChangedEvent.event);

});

teardown(() => {
Expand All @@ -234,13 +234,13 @@ suite('Data Science - Native Editor', () => {
[],
instance(liveShare),
instance(applicationShell),
instance(doctManager),
instance(docManager),
instance(interpreterService),
instance(webPanelProvider),
disposables,
instance(cssGenerator),
instance(themeFinder),
instance(statusProvider),
statusProvider,
instance(executionProvider),
instance(fileSystem),
instance(configService),
Expand Down Expand Up @@ -418,7 +418,7 @@ suite('Data Science - Native Editor', () => {
expect(newEditor.cells).to.be.lengthOf(3);
});

test('Pyton version info will be updated in notebook when a cell has been executed', async () => {
test('Python version info will be updated in notebook when a cell has been executed', async () => {
const file = Uri.parse('file://foo.ipynb');

const editor = createEditor();
Expand All @@ -429,11 +429,11 @@ suite('Data Science - Native Editor', () => {
expect(contents.metadata!.language_info!.version).to.not.equal('10.11.12');

// When a cell is executed, then ensure we store the python version info in the notebook data.
const version: Version = {build: [], major: 10, minor: 11, patch: 12, prerelease: [], raw: '10.11.12'};
when(executionProvider.getUsableJupyterPython()).thenResolve(({version} as any));
const version: Version = { build: [], major: 10, minor: 11, patch: 12, prerelease: [], raw: '10.11.12' };
when(executionProvider.getUsableJupyterPython()).thenResolve(({ version } as any));

try {
editor.onMessage(InteractiveWindowMessages.SubmitNewCell, {code: 'hello', id: '1'});
editor.onMessage(InteractiveWindowMessages.SubmitNewCell, { code: 'hello', id: '1' });
} catch {
// Ignore errors related to running cells, assume that works.
noop();
Expand All @@ -453,4 +453,44 @@ suite('Data Science - Native Editor', () => {
contents = JSON.parse(editor.contents) as nbformat.INotebookContent;
expect(contents.metadata!.language_info!.version).to.equal('10.11.12');
});

test('Export to Python script file from notebook.', async () => {
// Temp file location needed for export
const tempFile = {
dispose: () => {
return undefined;
},
filePath: '/foo/bar.ipynb'
};
when(fileSystem.createTemporaryFile('.ipynb')).thenResolve(tempFile);

// Set up our importer to return file contents, check that we have the correct temp file location and
// original file location
const file = Uri.parse('file:///foo.ipynb');
when(importer.importFromFile('/foo/bar.ipynb', file.fsPath)).thenResolve('# File Contents');

// Just return empty objects here, we don't care about open or show function, just that they were called
when(docManager.openTextDocument({ language: 'python', content: '# File Contents' })).thenResolve({} as any);
when(docManager.showTextDocument(anything(), anything())).thenResolve({} as any);

const editor = createEditor();
await editor.load(baseFile, file);
expect(editor.contents).to.be.equal(baseFile);

// Make our call to actually export
editor.onMessage(InteractiveWindowMessages.Export, editor.cells);

await waitForCondition(async () => {
try {
// Wait until showTextDocument has been called, that's the signal that export is done
verify(docManager.showTextDocument(anything(), anything())).atLeast(1);
return true;
} catch {
return false;
}
}, 1_000, 'Timeout');

// Verify that we also opened our text document not exact match as verify doesn't seem to match that
verify(docManager.openTextDocument(anything())).once();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ suite('Data Science - Native Editor Provider', () => {
let workspace: IWorkspaceService;
let configService: IConfigurationService;
let fileSystem: IFileSystem;
let doctManager: IDocumentManager;
let docManager: IDocumentManager;
let dsErrorHandler: IDataScienceErrorHandler;
let cmdManager: ICommandManager;
let svcContainer: IServiceContainer;
Expand All @@ -39,7 +39,7 @@ suite('Data Science - Native Editor Provider', () => {
svcContainer = mock(ServiceContainer);
configService = mock(ConfigurationService);
fileSystem = mock(FileSystem);
doctManager = mock(DocumentManager);
docManager = mock(DocumentManager);
dsErrorHandler = mock(DataScienceErrorHandler);
cmdManager = mock(CommandManager);
workspace = mock(WorkspaceService);
Expand All @@ -53,7 +53,7 @@ suite('Data Science - Native Editor Provider', () => {
instance(workspace),
instance(configService),
instance(fileSystem),
instance(doctManager),
instance(docManager),
instance(cmdManager),
instance(dsErrorHandler)
);
Expand All @@ -74,8 +74,8 @@ suite('Data Science - Native Editor Provider', () => {
const eventEmitter = new EventEmitter<TextEditor>();
const editor = typemoq.Mock.ofType<INotebookEditor>();
when(configService.getSettings()).thenReturn({ datascience: { useNotebookEditor: true } } as any);
when(doctManager.onDidChangeActiveTextEditor).thenReturn(eventEmitter.event);
when(doctManager.visibleTextEditors).thenReturn([]);
when(docManager.onDidChangeActiveTextEditor).thenReturn(eventEmitter.event);
when(docManager.visibleTextEditors).thenReturn([]);
editor.setup(e => e.closed).returns(() => new EventEmitter<INotebookEditor>().event);
editor.setup(e => e.executed).returns(() => new EventEmitter<INotebookEditor>().event);
editor.setup(e => (e as any).then).returns(() => undefined);
Expand Down
20 changes: 3 additions & 17 deletions src/test/datascience/interactiveWindowCommandListener.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { anything, instance, mock, when } from 'ts-mockito';
import { Matcher } from 'ts-mockito/lib/matcher/type/Matcher';
import * as TypeMoq from 'typemoq';
import * as uuid from 'uuid/v4';
import { Disposable, EventEmitter, Uri } from 'vscode';
import { EventEmitter, Uri } from 'vscode';

import { ApplicationShell } from '../../client/common/application/applicationShell';
import { PythonSettings } from '../../client/common/configSettings';
Expand All @@ -27,11 +27,9 @@ import { JupyterExecutionFactory } from '../../client/datascience/jupyter/jupyte
import { JupyterExporter } from '../../client/datascience/jupyter/jupyterExporter';
import { JupyterImporter } from '../../client/datascience/jupyter/jupyterImporter';
import {
IInteractiveBase,
IInteractiveWindow,
INotebook,
INotebookServer,
IStatusProvider
INotebookServer
} from '../../client/datascience/types';
import { InterpreterService } from '../../client/interpreter/interpreterService';
import { KnownSearchPathsForInterpreters } from '../../client/interpreter/locators/services/KnownPathsService';
Expand All @@ -41,6 +39,7 @@ import { MockAutoSelectionService } from '../mocks/autoSelector';
import * as vscodeMocks from '../vscode-mock';
import { MockCommandManager } from './mockCommandManager';
import { MockDocumentManager } from './mockDocumentManager';
import { MockStatusProvider } from './mockStatusProvider';

// tslint:disable:no-any no-http-string no-multiline-string max-func-body-length

Expand All @@ -53,19 +52,6 @@ function createTypeMoq<T>(tag: string): TypeMoq.IMock<T> {
return result;
}

class MockStatusProvider implements IStatusProvider {
public set(_message: string, _inweb: boolean, _timeout?: number, _cancel?: () => void, _panel?: IInteractiveBase): Disposable {
return {
dispose: noop
};
}

public waitWithStatus<T>(promise: () => Promise<T>, _message: string, _inweb: boolean, _timeout?: number, _canceled?: () => void, _panel?: IInteractiveBase): Promise<T> {
return promise();
}

}

// tslint:disable:no-any no-http-string no-multiline-string max-func-body-length
suite('Interactive window command listener', async () => {
const interpreterService = mock(InterpreterService);
Expand Down
18 changes: 18 additions & 0 deletions src/test/datascience/mockStatusProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Disposable } from 'vscode';
import {
IInteractiveBase,
IStatusProvider
} from '../../client/datascience/types';
import { noop } from '../core';
export class MockStatusProvider implements IStatusProvider {
public set(_message: string, _inweb: boolean, _timeout?: number, _cancel?: () => void, _panel?: IInteractiveBase): Disposable {
return {
dispose: noop
};
}

public waitWithStatus<T>(promise: () => Promise<T>, _message: string, _inweb: boolean, _timeout?: number, _canceled?: () => void, _panel?: IInteractiveBase): Promise<T> {
return promise();
}

}