|
| 1 | +/*--------------------------------------------------------- |
| 2 | + * Copyright (C) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------*/ |
| 4 | + |
| 5 | +import * as assert from "assert"; |
| 6 | +import * as path from "path"; |
| 7 | +import * as vscode from "vscode"; |
| 8 | +import { PowerShellNotebooksFeature } from "../../src/features/PowerShellNotebooks"; |
| 9 | +import { before } from "mocha"; |
| 10 | +import os = require("os"); |
| 11 | +import { readFileSync } from "fs"; |
| 12 | + |
| 13 | +const notebookDir = [ |
| 14 | + __dirname, |
| 15 | + "..", |
| 16 | + "..", |
| 17 | + "..", |
| 18 | + "test", |
| 19 | + "features", |
| 20 | + "testNotebookFiles" |
| 21 | +]; |
| 22 | + |
| 23 | +const notebookOnlyCode = vscode.Uri.file( |
| 24 | + path.join(...notebookDir, "onlyCode.ps1")); |
| 25 | +const notebookOnlyMarkdown = vscode.Uri.file( |
| 26 | + path.join(...notebookDir,"onlyMarkdown.ps1")); |
| 27 | +const notebookSimpleBlockComments = vscode.Uri.file( |
| 28 | + path.join(...notebookDir,"simpleBlockComments.ps1")); |
| 29 | +const notebookSimpleLineComments = vscode.Uri.file( |
| 30 | + path.join(...notebookDir,"simpleLineComments.ps1")); |
| 31 | +const notebookSimpleMixedComments = vscode.Uri.file( |
| 32 | + path.join(...notebookDir,"simpleMixedComments.ps1")); |
| 33 | + |
| 34 | +const notebookTestData = new Map<vscode.Uri, vscode.NotebookCellData[]>(); |
| 35 | + |
| 36 | +suite("PowerShellNotebooks tests", () => { |
| 37 | + notebookTestData.set(notebookOnlyCode, [ |
| 38 | + { |
| 39 | + cellKind: vscode.CellKind.Code, |
| 40 | + language: "powershell", |
| 41 | + source: readBackingFile(notebookOnlyCode), |
| 42 | + outputs: [], |
| 43 | + metadata: {} |
| 44 | + } |
| 45 | + ]); |
| 46 | + |
| 47 | + notebookTestData.set(notebookOnlyMarkdown, [ |
| 48 | + { |
| 49 | + cellKind: vscode.CellKind.Markdown, |
| 50 | + language: "markdown", |
| 51 | + source: readBackingFile(notebookOnlyMarkdown), |
| 52 | + outputs: [], |
| 53 | + metadata: {} |
| 54 | + } |
| 55 | + ]); |
| 56 | + |
| 57 | + let content = readBackingFile(notebookSimpleBlockComments).split(os.EOL); |
| 58 | + notebookTestData.set(notebookSimpleBlockComments, [ |
| 59 | + { |
| 60 | + cellKind: vscode.CellKind.Markdown, |
| 61 | + language: "markdown", |
| 62 | + source: content.slice(0, 5).join(os.EOL), |
| 63 | + outputs: [], |
| 64 | + metadata: {} |
| 65 | + }, |
| 66 | + { |
| 67 | + cellKind: vscode.CellKind.Code, |
| 68 | + language: "powershell", |
| 69 | + source: content.slice(5, 6).join(os.EOL), |
| 70 | + outputs: [], |
| 71 | + metadata: {} |
| 72 | + }, |
| 73 | + { |
| 74 | + cellKind: vscode.CellKind.Markdown, |
| 75 | + language: "markdown", |
| 76 | + source: content.slice(6, 11).join(os.EOL), |
| 77 | + outputs: [], |
| 78 | + metadata: {} |
| 79 | + }, |
| 80 | + ]); |
| 81 | + |
| 82 | + content = readBackingFile(notebookSimpleLineComments).split(os.EOL); |
| 83 | + notebookTestData.set(notebookSimpleLineComments, [ |
| 84 | + { |
| 85 | + cellKind: vscode.CellKind.Markdown, |
| 86 | + language: "markdown", |
| 87 | + source: content.slice(0, 3).join(os.EOL), |
| 88 | + outputs: [], |
| 89 | + metadata: {} |
| 90 | + }, |
| 91 | + { |
| 92 | + cellKind: vscode.CellKind.Code, |
| 93 | + language: "powershell", |
| 94 | + source: content.slice(3, 4).join(os.EOL), |
| 95 | + outputs: [], |
| 96 | + metadata: {} |
| 97 | + }, |
| 98 | + { |
| 99 | + cellKind: vscode.CellKind.Markdown, |
| 100 | + language: "markdown", |
| 101 | + source: content.slice(4, 7).join(os.EOL), |
| 102 | + outputs: [], |
| 103 | + metadata: {} |
| 104 | + }, |
| 105 | + ]); |
| 106 | + |
| 107 | + content = readBackingFile(notebookSimpleMixedComments).split(os.EOL); |
| 108 | + notebookTestData.set(notebookSimpleMixedComments, [ |
| 109 | + { |
| 110 | + cellKind: vscode.CellKind.Markdown, |
| 111 | + language: "markdown", |
| 112 | + source: content.slice(0, 3).join(os.EOL), |
| 113 | + outputs: [], |
| 114 | + metadata: {} |
| 115 | + }, |
| 116 | + { |
| 117 | + cellKind: vscode.CellKind.Code, |
| 118 | + language: "powershell", |
| 119 | + source: content.slice(3, 4).join(os.EOL), |
| 120 | + outputs: [], |
| 121 | + metadata: {} |
| 122 | + }, |
| 123 | + { |
| 124 | + cellKind: vscode.CellKind.Markdown, |
| 125 | + language: "markdown", |
| 126 | + source: content.slice(4, 9).join(os.EOL), |
| 127 | + outputs: [], |
| 128 | + metadata: {} |
| 129 | + }, |
| 130 | + ]); |
| 131 | + |
| 132 | + const feature = new PowerShellNotebooksFeature(); |
| 133 | + |
| 134 | + for (const [uri, cells] of notebookTestData) { |
| 135 | + test(`Can open a notebook with expected cells - ${uri.fsPath}`, async () => { |
| 136 | + const notebookData = await feature.openNotebook(uri, {}); |
| 137 | + assert.deepStrictEqual(notebookData.cells.length, cells.length); |
| 138 | + }); |
| 139 | + } |
| 140 | +}); |
| 141 | + |
| 142 | +function readBackingFile(uri: vscode.Uri): string { |
| 143 | + return readFileSync(uri.fsPath).toString(); |
| 144 | +} |
0 commit comments