|
| 1 | +// You can import and use all API from the 'vscode' module |
| 2 | +// as well as import your extension to test it |
| 3 | +import * as vscode from 'vscode'; |
| 4 | +import { LANGUAGE_ID, NOTEBOOK_TYPE, getContextUriFromCell, getPreferredPathFormatFromUri } from '../../utils'; |
| 5 | +import { expect } from 'chai'; |
| 6 | +const path = require('upath'); |
| 7 | +// import * as myExtension from '../../extension'; |
| 8 | + |
| 9 | +const getTestFileUri = (relativePath: string) => { |
| 10 | + const workspacePath = __dirname + '../../../..//src/test/suite/files/'; |
| 11 | + return vscode.Uri.file(path.join(workspacePath, relativePath)); |
| 12 | +}; |
| 13 | + |
| 14 | +const delay = (ms: number) => new Promise(res => setTimeout(res, ms)); |
| 15 | + |
| 16 | +describe('Path Type Tests', async () => { |
| 17 | + vscode.window.showInformationMessage('Start all tests.'); |
| 18 | + |
| 19 | + beforeEach(async () => { |
| 20 | + //close all open tabs |
| 21 | + await vscode.commands.executeCommand('workbench.action.closeAllEditors'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('getPreferredPathFormatFromUri: Use relative path for file context when useRelativePaths set to true (default)', async () => { |
| 25 | + const notebookUri = getTestFileUri('/notebooks/empty.jsonpath-notebook'); |
| 26 | + const notebook = await vscode.workspace.openNotebookDocument(notebookUri); |
| 27 | + await vscode.workspace.getConfiguration('jsonpath-notebook').update('useRelativePaths', true, true); |
| 28 | + await vscode.window.showNotebookDocument(notebook); |
| 29 | + |
| 30 | + const contextUri = getTestFileUri('/input/bookstore.json'); |
| 31 | + |
| 32 | + const expectedPath = '../input/bookstore.json'; |
| 33 | + const actualPath = getPreferredPathFormatFromUri(contextUri); |
| 34 | + |
| 35 | + expect(actualPath).to.equal(expectedPath); |
| 36 | + }); |
| 37 | + |
| 38 | + it('getPreferredPathFormatFromUri: Use absolute path for file context when useRelativePaths set to true but no notebook is open', async () => { |
| 39 | + const contextUri = getTestFileUri('/input/bookstore.json'); |
| 40 | + |
| 41 | + const expectedPath = contextUri.path; |
| 42 | + const actualPath = getPreferredPathFormatFromUri(contextUri); |
| 43 | + |
| 44 | + expect(actualPath).to.equal(expectedPath); |
| 45 | + }); |
| 46 | + |
| 47 | + it('getPreferredPathFormatFromUri: Use absolute path for file context when useRelativePaths set to true but is on different drive', async function () { |
| 48 | + if (process.platform !== 'win32') { this.skip(); } |
| 49 | + |
| 50 | + const notebookUri = getTestFileUri('/notebooks/empty.jsonpath-notebook'); |
| 51 | + const notebook = await vscode.workspace.openNotebookDocument(notebookUri); |
| 52 | + await vscode.window.showNotebookDocument(notebook); |
| 53 | + await vscode.workspace.getConfiguration('jsonpath-notebook').update('useRelativePaths', true, true); |
| 54 | + |
| 55 | + const contextUri = vscode.Uri.file('Z:\\input\\bookstore.json'); |
| 56 | + |
| 57 | + const expectedPath = contextUri.path; |
| 58 | + const actualPath = getPreferredPathFormatFromUri(contextUri); |
| 59 | + |
| 60 | + expect(actualPath).to.equal(expectedPath); |
| 61 | + }); |
| 62 | + |
| 63 | + it('getPreferredPathFormatFromUri: Use absolute path for file context when useRelativePaths set to false', async () => { |
| 64 | + const notebookUri = getTestFileUri('/notebooks/empty.jsonpath-notebook'); |
| 65 | + const notebook = await vscode.workspace.openNotebookDocument(notebookUri); |
| 66 | + await vscode.window.showNotebookDocument(notebook); |
| 67 | + await vscode.workspace.getConfiguration('jsonpath-notebook').update('useRelativePaths', false, true); |
| 68 | + |
| 69 | + const contextUri = getTestFileUri('/input/bookstore.json'); |
| 70 | + |
| 71 | + const expectedPath = contextUri.path; |
| 72 | + const actualPath = getPreferredPathFormatFromUri(contextUri); |
| 73 | + |
| 74 | + expect(actualPath).to.equal(expectedPath); |
| 75 | + }); |
| 76 | + |
| 77 | + it('getContextUriFromCell: Returns correct uri when metadata selectedFileUri is relative path', async () => { |
| 78 | + const notebookUri = getTestFileUri('/notebooks/empty.jsonpath-notebook'); |
| 79 | + const notebook = await vscode.workspace.openNotebookDocument(notebookUri); |
| 80 | + |
| 81 | + await vscode.window.showNotebookDocument(notebook); |
| 82 | + |
| 83 | + //add cell with metadata |
| 84 | + const edit = new vscode.WorkspaceEdit(); |
| 85 | + const nbEdit = vscode.NotebookEdit.insertCells(0, [{ |
| 86 | + kind: vscode.NotebookCellKind.Code, |
| 87 | + languageId: LANGUAGE_ID, |
| 88 | + value: '$..book[?(@.price<10)]', |
| 89 | + metadata: { |
| 90 | + "selectedFileUri": "../input/bookstore.json" |
| 91 | + } |
| 92 | + }]); |
| 93 | + edit.set(notebook.uri, [nbEdit]); |
| 94 | + await vscode.workspace.applyEdit(edit); |
| 95 | + |
| 96 | + const cell = notebook.cellAt(0); |
| 97 | + if (!cell) { return; } |
| 98 | + const expectedUri = getTestFileUri('/input/bookstore.json'); |
| 99 | + const actualUri = getContextUriFromCell(cell); |
| 100 | + expect(actualUri).to.deep.equal(expectedUri); |
| 101 | + }); |
| 102 | + |
| 103 | + it('getContextUriFromCell: Returns correct uri when metadata selectedFileUri is absolute', async () => { |
| 104 | + const notebookUri = getTestFileUri('/notebooks/empty.jsonpath-notebook'); |
| 105 | + const notebook = await vscode.workspace.openNotebookDocument(notebookUri); |
| 106 | + |
| 107 | + await vscode.window.showNotebookDocument(notebook); |
| 108 | + |
| 109 | + //add cell with metadata |
| 110 | + const edit = new vscode.WorkspaceEdit(); |
| 111 | + const nbEdit = vscode.NotebookEdit.insertCells(0, [{ |
| 112 | + kind: vscode.NotebookCellKind.Code, |
| 113 | + languageId: LANGUAGE_ID, |
| 114 | + value: '$..book[?(@.price<10)]', |
| 115 | + metadata: { |
| 116 | + "selectedFileUri": getTestFileUri('/input/bookstore.json').path |
| 117 | + } |
| 118 | + }]); |
| 119 | + edit.set(notebook.uri, [nbEdit]); |
| 120 | + await vscode.workspace.applyEdit(edit); |
| 121 | + |
| 122 | + const cell = notebook.cellAt(0); |
| 123 | + if (!cell) { return; } |
| 124 | + const expectedUri = getTestFileUri('/input/bookstore.json'); |
| 125 | + const actualUri = getContextUriFromCell(cell); |
| 126 | + expect(actualUri).to.deep.equal(expectedUri); |
| 127 | + }); |
| 128 | +}); |
0 commit comments