|
| 1 | +// Licensed under the MIT License. |
| 2 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + |
| 4 | +// tslint:disable: no-var-requires no-require-imports no-invalid-this no-any |
| 5 | + |
| 6 | +import * as path from 'path'; |
| 7 | +import * as sinon from 'sinon'; |
| 8 | +import { Position, Range, Uri, window } from 'vscode'; |
| 9 | +import { IVSCodeNotebook } from '../../../client/common/application/types'; |
| 10 | +import { IDisposable } from '../../../client/common/types'; |
| 11 | +import { ICell, INotebookEditorProvider, INotebookModel } from '../../../client/datascience/types'; |
| 12 | +import { splitMultilineString } from '../../../datascience-ui/common'; |
| 13 | +import { IExtensionTestApi, waitForCondition } from '../../common'; |
| 14 | +import { EXTENSION_ROOT_DIR_FOR_TESTS } from '../../constants'; |
| 15 | +import { initialize } from '../../initialize'; |
| 16 | +import { |
| 17 | + canRunTests, |
| 18 | + closeNotebooksAndCleanUpAfterTests, |
| 19 | + createTemporaryNotebook, |
| 20 | + deleteAllCellsAndWait, |
| 21 | + insertPythonCellAndWait, |
| 22 | + swallowSavingOfNotebooks |
| 23 | +} from './helper'; |
| 24 | + |
| 25 | +suite('DataScience - VSCode Notebook (Cell Edit Syncing)', function () { |
| 26 | + this.timeout(10_000); |
| 27 | + |
| 28 | + const templateIPynb = path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'src', 'test', 'datascience', 'test.ipynb'); |
| 29 | + let testIPynb: Uri; |
| 30 | + let api: IExtensionTestApi; |
| 31 | + let editorProvider: INotebookEditorProvider; |
| 32 | + let vscNotebook: IVSCodeNotebook; |
| 33 | + const disposables: IDisposable[] = []; |
| 34 | + suiteSetup(async function () { |
| 35 | + this.timeout(10_000); |
| 36 | + api = await initialize(); |
| 37 | + if (!(await canRunTests())) { |
| 38 | + return this.skip(); |
| 39 | + } |
| 40 | + editorProvider = api.serviceContainer.get<INotebookEditorProvider>(INotebookEditorProvider); |
| 41 | + vscNotebook = api.serviceContainer.get<IVSCodeNotebook>(IVSCodeNotebook); |
| 42 | + }); |
| 43 | + suiteTeardown(() => closeNotebooksAndCleanUpAfterTests(disposables)); |
| 44 | + [true, false].forEach((isUntitled) => { |
| 45 | + suite(isUntitled ? 'Untitled Notebook' : 'Existing Notebook', () => { |
| 46 | + let model: INotebookModel; |
| 47 | + setup(async () => { |
| 48 | + sinon.restore(); |
| 49 | + await swallowSavingOfNotebooks(); |
| 50 | + |
| 51 | + // Don't use same file (due to dirty handling, we might save in dirty.) |
| 52 | + // Cuz we won't save to file, hence extension will backup in dirty file and when u re-open it will open from dirty. |
| 53 | + testIPynb = Uri.file(await createTemporaryNotebook(templateIPynb, disposables)); |
| 54 | + |
| 55 | + // Reset for tests, do this every time, as things can change due to config changes etc. |
| 56 | + const editor = isUntitled ? await editorProvider.createNew() : await editorProvider.open(testIPynb); |
| 57 | + model = editor.model!; |
| 58 | + await deleteAllCellsAndWait(); |
| 59 | + }); |
| 60 | + teardown(() => closeNotebooksAndCleanUpAfterTests(disposables)); |
| 61 | + |
| 62 | + async function assertTextInCell(cell: ICell, text: string) { |
| 63 | + await waitForCondition( |
| 64 | + async () => (cell.data.source as string[]).join('') === splitMultilineString(text).join(''), |
| 65 | + 1_000, |
| 66 | + `Source; is not ${text}` |
| 67 | + ); |
| 68 | + } |
| 69 | + test('Insert and edit cell', async () => { |
| 70 | + await insertPythonCellAndWait('HELLO'); |
| 71 | + const doc = vscNotebook.activeNotebookEditor?.document; |
| 72 | + const cellEditor1 = window.visibleTextEditors.find( |
| 73 | + (item) => doc?.cells.length && item.document.uri.toString() === doc?.cells[0].uri.toString() |
| 74 | + ); |
| 75 | + await assertTextInCell(model.cells[0], 'HELLO'); |
| 76 | + |
| 77 | + // Edit cell. |
| 78 | + await new Promise((resolve) => |
| 79 | + cellEditor1?.edit((editor) => { |
| 80 | + editor.insert(new Position(0, 5), ' WORLD'); |
| 81 | + resolve(); |
| 82 | + }) |
| 83 | + ); |
| 84 | + |
| 85 | + await assertTextInCell(model.cells[0], 'HELLO WORLD'); |
| 86 | + |
| 87 | + //Clear cell text. |
| 88 | + await new Promise((resolve) => |
| 89 | + cellEditor1?.edit((editor) => { |
| 90 | + editor.delete(new Range(0, 0, 0, 'HELLO WORLD'.length)); |
| 91 | + resolve(); |
| 92 | + }) |
| 93 | + ); |
| 94 | + |
| 95 | + await assertTextInCell(model.cells[0], ''); |
| 96 | + }); |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
0 commit comments