|
| 1 | +import {randomUUID} from "crypto"; |
| 2 | +import os from "os"; |
| 3 | +import path from "path"; |
| 4 | +import {ConfigurationTarget, Uri, workspace} from "vscode"; |
| 5 | +import type {WorkspaceConfiguration} from "vscode"; |
| 6 | +import {instance, mock, reset, spy, verify, when} from "ts-mockito"; |
| 7 | +import {ConnectionManager} from "../../configuration/ConnectionManager"; |
| 8 | +import {WorkspaceFolderManager} from "../../vscode-objs/WorkspaceFolderManager"; |
| 9 | +import {setDbnbCellLimits} from "./DatabricksNbCellLimits"; |
| 10 | + |
| 11 | +describe("Databricks notebook cell markers", () => { |
| 12 | + let workspaceSpy: typeof workspace; |
| 13 | + let databricksConfig: WorkspaceConfiguration; |
| 14 | + let jupyterConfig: WorkspaceConfiguration; |
| 15 | + let workspaceFolderManager: WorkspaceFolderManager; |
| 16 | + let connectionManager: ConnectionManager; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + workspaceSpy = spy(workspace); |
| 20 | + databricksConfig = mock<WorkspaceConfiguration>(); |
| 21 | + jupyterConfig = mock<WorkspaceConfiguration>(); |
| 22 | + when(workspaceSpy.getConfiguration("databricks")).thenReturn( |
| 23 | + instance(databricksConfig) |
| 24 | + ); |
| 25 | + when(workspaceSpy.getConfiguration("jupyter")).thenReturn( |
| 26 | + instance(jupyterConfig) |
| 27 | + ); |
| 28 | + when( |
| 29 | + jupyterConfig.get<string>("interactiveWindow.cellMarker.codeRegex") |
| 30 | + ).thenReturn("^# custom"); |
| 31 | + workspaceFolderManager = mock(WorkspaceFolderManager); |
| 32 | + when(workspaceFolderManager.activeProjectUri).thenReturn( |
| 33 | + Uri.file(path.join(os.tmpdir(), randomUUID())) |
| 34 | + ); |
| 35 | + connectionManager = mock(ConnectionManager); |
| 36 | + when(connectionManager.waitForConnect()).thenResolve(); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + reset(workspaceSpy); |
| 41 | + }); |
| 42 | + |
| 43 | + it("leaves Jupyter settings untouched when automatic configuration is disabled", async () => { |
| 44 | + when( |
| 45 | + databricksConfig.get<boolean>( |
| 46 | + "notebooks.configureJupyterCellMarkers" |
| 47 | + ) |
| 48 | + ).thenReturn(false); |
| 49 | + |
| 50 | + await setDbnbCellLimits( |
| 51 | + instance(workspaceFolderManager), |
| 52 | + instance(connectionManager) |
| 53 | + ); |
| 54 | + |
| 55 | + verify(workspaceSpy.getConfiguration("jupyter")).never(); |
| 56 | + }); |
| 57 | + |
| 58 | + for (const enabled of [undefined, true]) { |
| 59 | + it(`preserves custom markers when automatic configuration is ${ |
| 60 | + enabled === undefined ? "unset" : "enabled" |
| 61 | + }`, async () => { |
| 62 | + when( |
| 63 | + databricksConfig.get<boolean>( |
| 64 | + "notebooks.configureJupyterCellMarkers" |
| 65 | + ) |
| 66 | + ).thenReturn(enabled); |
| 67 | + |
| 68 | + await setDbnbCellLimits( |
| 69 | + instance(workspaceFolderManager), |
| 70 | + instance(connectionManager) |
| 71 | + ); |
| 72 | + |
| 73 | + verify( |
| 74 | + jupyterConfig.update( |
| 75 | + "interactiveWindow.cellMarker.codeRegex", |
| 76 | + "^# COMMAND ----------|^# Databricks notebook source|^# custom", |
| 77 | + ConfigurationTarget.Workspace |
| 78 | + ) |
| 79 | + ).once(); |
| 80 | + verify( |
| 81 | + jupyterConfig.update( |
| 82 | + "interactiveWindow.cellMarker.default", |
| 83 | + "# COMMAND ----------", |
| 84 | + ConfigurationTarget.Workspace |
| 85 | + ) |
| 86 | + ).once(); |
| 87 | + }); |
| 88 | + } |
| 89 | +}); |
0 commit comments