|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { assert } from 'chai'; |
| 7 | +import * as path from 'path'; |
| 8 | +import * as sinon from 'sinon'; |
| 9 | +import { commands, ConfigurationTarget, Uri } from 'vscode'; |
| 10 | +import { IWorkspaceService } from '../../client/common/application/types'; |
| 11 | +import { IFileSystem } from '../../client/common/platform/types'; |
| 12 | +import { IPythonToolExecutionService } from '../../client/common/process/types'; |
| 13 | +import { IDisposable, Product } from '../../client/common/types'; |
| 14 | +import { INotebookEditorProvider } from '../../client/datascience/types'; |
| 15 | +import { IExtensionTestApi } from '../common'; |
| 16 | +import { |
| 17 | + canRunTests, |
| 18 | + closeNotebooksAndCleanUpAfterTests, |
| 19 | + createTemporaryNotebook, |
| 20 | + disposeAllDisposables, |
| 21 | + swallowSavingOfNotebooks |
| 22 | +} from '../datascience/notebook/helper'; |
| 23 | +import { EXTENSION_ROOT_DIR_FOR_TESTS, initialize, initializeTest } from '../initialize'; |
| 24 | + |
| 25 | +// tslint:disable: no-any no-invalid-this |
| 26 | +suite('Formatting - Notebooks', () => { |
| 27 | + let api: IExtensionTestApi; |
| 28 | + suiteSetup(async function () { |
| 29 | + api = await initialize(); |
| 30 | + if (!(await canRunTests())) { |
| 31 | + return this.skip(); |
| 32 | + } |
| 33 | + }); |
| 34 | + suiteTeardown(closeNotebooksAndCleanUpAfterTests); |
| 35 | + ['yapf', 'black', 'autopep8'].forEach((formatter) => { |
| 36 | + suite(formatter, () => { |
| 37 | + const disposables: IDisposable[] = []; |
| 38 | + let testIPynb: Uri; |
| 39 | + let executionService: IPythonToolExecutionService; |
| 40 | + let editorProvider: INotebookEditorProvider; |
| 41 | + let fs: IFileSystem; |
| 42 | + const product: Product = |
| 43 | + formatter === 'yapf' ? Product.yapf : formatter === 'black' ? Product.black : Product.autopep8; |
| 44 | + suiteSetup(async () => { |
| 45 | + const workspaceService = api.serviceContainer.get<IWorkspaceService>(IWorkspaceService); |
| 46 | + const config = workspaceService.getConfiguration( |
| 47 | + 'python.formatting', |
| 48 | + workspaceService.workspaceFolders![0].uri |
| 49 | + ); |
| 50 | + await config.update('provider', formatter, ConfigurationTarget.Workspace); |
| 51 | + }); |
| 52 | + setup(async () => { |
| 53 | + sinon.restore(); |
| 54 | + await initializeTest(); |
| 55 | + await swallowSavingOfNotebooks(); |
| 56 | + |
| 57 | + // Don't use same file (due to dirty handling, we might save in dirty.) |
| 58 | + // Cuz we won't save to file, hence extension will backup in dirty file and when u re-open it will open from dirty. |
| 59 | + const templateIPynb = path.join( |
| 60 | + EXTENSION_ROOT_DIR_FOR_TESTS, |
| 61 | + 'src', |
| 62 | + 'test', |
| 63 | + 'datascience', |
| 64 | + 'test.ipynb' |
| 65 | + ); |
| 66 | + testIPynb = Uri.file(await createTemporaryNotebook(templateIPynb, disposables)); |
| 67 | + executionService = api.serviceContainer.get<IPythonToolExecutionService>(IPythonToolExecutionService); |
| 68 | + editorProvider = api.serviceContainer.get<INotebookEditorProvider>(INotebookEditorProvider); |
| 69 | + fs = api.serviceContainer.get<IFileSystem>(IFileSystem); |
| 70 | + }); |
| 71 | + teardown(closeNotebooksAndCleanUpAfterTests); |
| 72 | + suiteTeardown(() => disposeAllDisposables(disposables)); |
| 73 | + test('Formatted with temporary file when formatting existing saved notebooks (without changes)', async () => { |
| 74 | + // Open a new notebook & add a cell |
| 75 | + await editorProvider.open(testIPynb); |
| 76 | + |
| 77 | + // Check if a temp file is created. |
| 78 | + const spiedExecution = sinon.spy(executionService, 'exec'); |
| 79 | + const spiedWriteFile = sinon.spy(fs, 'writeFile'); |
| 80 | + disposables.push({ dispose: () => spiedWriteFile.restore() }); |
| 81 | + disposables.push({ dispose: () => spiedExecution.restore() }); |
| 82 | + |
| 83 | + // Format the cell |
| 84 | + await commands.executeCommand('notebook.formatCell'); |
| 85 | + |
| 86 | + // Verify a temp file was created, having a file starting with thenb file name. |
| 87 | + assert.isOk( |
| 88 | + spiedWriteFile |
| 89 | + .getCalls() |
| 90 | + .some( |
| 91 | + (call) => |
| 92 | + call.args[0].includes(path.basename(testIPynb.fsPath)) && |
| 93 | + call.args[0].includes('.ipynb') |
| 94 | + ), |
| 95 | + 'Temp file not created' |
| 96 | + ); |
| 97 | + // Verify we tried to format. |
| 98 | + assert.isOk( |
| 99 | + spiedExecution.getCalls().some((call) => call.args[0].product === product), |
| 100 | + 'Not formatted' |
| 101 | + ); |
| 102 | + }); |
| 103 | + test('Formatted with temporary file when formatting untitled notebooks', async () => { |
| 104 | + // Open a new notebook & add a cell |
| 105 | + await editorProvider.createNew(); |
| 106 | + |
| 107 | + // Check if a temp file is created. |
| 108 | + const spiedExecution = sinon.spy(executionService, 'exec'); |
| 109 | + const spiedWriteFile = sinon.spy(fs, 'writeFile'); |
| 110 | + disposables.push({ dispose: () => spiedWriteFile.restore() }); |
| 111 | + disposables.push({ dispose: () => spiedExecution.restore() }); |
| 112 | + |
| 113 | + // Format the cell |
| 114 | + await commands.executeCommand('notebook.formatCell'); |
| 115 | + |
| 116 | + // Verify a temp file was created, having a file starting with thenb file name. |
| 117 | + assert.isOk( |
| 118 | + spiedWriteFile.getCalls().some((call) => call.args[0].includes('.ipynb')), |
| 119 | + 'Temp file not created' |
| 120 | + ); |
| 121 | + // Verify we tried to format. |
| 122 | + assert.isOk( |
| 123 | + spiedExecution.getCalls().some((call) => call.args[0].product === product), |
| 124 | + 'Not formatted' |
| 125 | + ); |
| 126 | + }); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments