Skip to content

Commit 5a350a2

Browse files
added save test
1 parent 1e2540b commit 5a350a2

File tree

3 files changed

+117
-23
lines changed

3 files changed

+117
-23
lines changed

src/features/PowerShellNotebooks.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
2121
public label: string = 'PowerShell';
2222
public preloads?: vscode.Uri[];
2323

24-
public constructor() {
24+
public constructor(skipRegisteringCommands?: boolean) {
2525
// VS Code Notebook API uses this property for handling cell execution.
2626
this.kernel = this;
2727

28-
this.showNotebookModeCommand = vscode.commands.registerCommand(
29-
"PowerShell.ShowNotebookMode",
30-
PowerShellNotebooksFeature.showNotebookMode);
28+
if(!skipRegisteringCommands) {
29+
this.showNotebookModeCommand = vscode.commands.registerCommand(
30+
"PowerShell.ShowNotebookMode",
31+
PowerShellNotebooksFeature.showNotebookMode);
3132

32-
this.hideNotebookModeCommand = vscode.commands.registerCommand(
33-
"PowerShell.HideNotebookMode",
34-
PowerShellNotebooksFeature.hideNotebookMode);
33+
this.hideNotebookModeCommand = vscode.commands.registerCommand(
34+
"PowerShell.HideNotebookMode",
35+
PowerShellNotebooksFeature.hideNotebookMode);
36+
}
3537
}
3638

3739
public async openNotebook(uri: vscode.Uri, context: vscode.NotebookDocumentOpenContext): Promise<vscode.NotebookData> {

test/features/PowerShellNotebooks.test.ts

Lines changed: 102 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { PowerShellNotebooksFeature } from "../../src/features/PowerShellNoteboo
99
import { before } from "mocha";
1010
import os = require("os");
1111
import { readFileSync } from "fs";
12+
import { CommentType } from "../../src/settings";
1213

1314
const notebookDir = [
1415
__dirname,
@@ -40,7 +41,11 @@ suite("PowerShellNotebooks tests", () => {
4041
language: "powershell",
4142
source: readBackingFile(notebookOnlyCode),
4243
outputs: [],
43-
metadata: {}
44+
metadata: {
45+
custom: {
46+
commentType: CommentType.Disabled,
47+
}
48+
}
4449
}
4550
]);
4651

@@ -50,7 +55,11 @@ suite("PowerShellNotebooks tests", () => {
5055
language: "markdown",
5156
source: readBackingFile(notebookOnlyMarkdown),
5257
outputs: [],
53-
metadata: {}
58+
metadata: {
59+
custom: {
60+
commentType: CommentType.LineComment,
61+
}
62+
}
5463
}
5564
]);
5665

@@ -61,21 +70,33 @@ suite("PowerShellNotebooks tests", () => {
6170
language: "markdown",
6271
source: content.slice(0, 5).join(os.EOL),
6372
outputs: [],
64-
metadata: {}
73+
metadata: {
74+
custom: {
75+
commentType: CommentType.BlockComment,
76+
}
77+
}
6578
},
6679
{
6780
cellKind: vscode.CellKind.Code,
6881
language: "powershell",
6982
source: content.slice(5, 6).join(os.EOL),
7083
outputs: [],
71-
metadata: {}
84+
metadata: {
85+
custom: {
86+
commentType: CommentType.Disabled,
87+
}
88+
}
7289
},
7390
{
7491
cellKind: vscode.CellKind.Markdown,
7592
language: "markdown",
7693
source: content.slice(6, 11).join(os.EOL),
7794
outputs: [],
78-
metadata: {}
95+
metadata: {
96+
custom: {
97+
commentType: CommentType.BlockComment,
98+
}
99+
}
79100
},
80101
]);
81102

@@ -86,21 +107,33 @@ suite("PowerShellNotebooks tests", () => {
86107
language: "markdown",
87108
source: content.slice(0, 3).join(os.EOL),
88109
outputs: [],
89-
metadata: {}
110+
metadata: {
111+
custom: {
112+
commentType: CommentType.LineComment,
113+
}
114+
}
90115
},
91116
{
92117
cellKind: vscode.CellKind.Code,
93118
language: "powershell",
94119
source: content.slice(3, 4).join(os.EOL),
95120
outputs: [],
96-
metadata: {}
121+
metadata: {
122+
custom: {
123+
commentType: CommentType.Disabled,
124+
}
125+
}
97126
},
98127
{
99128
cellKind: vscode.CellKind.Markdown,
100129
language: "markdown",
101130
source: content.slice(4, 7).join(os.EOL),
102131
outputs: [],
103-
metadata: {}
132+
metadata: {
133+
custom: {
134+
commentType: CommentType.LineComment,
135+
}
136+
}
104137
},
105138
]);
106139

@@ -111,34 +144,88 @@ suite("PowerShellNotebooks tests", () => {
111144
language: "markdown",
112145
source: content.slice(0, 3).join(os.EOL),
113146
outputs: [],
114-
metadata: {}
147+
metadata: {
148+
custom: {
149+
commentType: CommentType.LineComment,
150+
}
151+
}
115152
},
116153
{
117154
cellKind: vscode.CellKind.Code,
118155
language: "powershell",
119156
source: content.slice(3, 4).join(os.EOL),
120157
outputs: [],
121-
metadata: {}
158+
metadata: {
159+
custom: {
160+
commentType: CommentType.Disabled,
161+
}
162+
}
122163
},
123164
{
124165
cellKind: vscode.CellKind.Markdown,
125166
language: "markdown",
126167
source: content.slice(4, 9).join(os.EOL),
127168
outputs: [],
128-
metadata: {}
169+
metadata: {
170+
custom: {
171+
commentType: CommentType.BlockComment,
172+
}
173+
}
129174
},
130175
]);
131176

132-
const feature = new PowerShellNotebooksFeature();
177+
const feature = new PowerShellNotebooksFeature(true);
133178

134-
for (const [uri, cells] of notebookTestData) {
179+
for (const [uri, expectedCells] of notebookTestData) {
135180
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);
181+
const actualNotebookData = await feature.openNotebook(uri, {});
182+
compareCells(actualNotebookData.cells, expectedCells);
138183
});
139184
}
185+
186+
test("Can save a new notebook with expected cells and metadata", async () => {
187+
const uri = vscode.Uri.file(path.join(__dirname, "testFile.ps1"));
188+
try {
189+
await vscode.workspace.fs.delete(uri);
190+
} catch {
191+
// If the file doesn't exist that's fine.
192+
}
193+
194+
// Open an existing notebook ps1.
195+
await vscode.commands.executeCommand("vscode.openWith", notebookSimpleMixedComments, "PowerShellNotebookMode");
196+
197+
// Allow some time to pass to render the Notebook
198+
await sleep(5000);
199+
assert.strictEqual(
200+
vscode.notebook.activeNotebookEditor.document.uri.toString(),
201+
notebookSimpleMixedComments.toString());
202+
203+
// Save it as testFile.ps1 and reopen it using the feature.
204+
await feature.saveNotebookAs(uri, vscode.notebook.activeNotebookEditor.document, null);
205+
const newNotebook = await feature.openNotebook(uri, {});
206+
207+
// Compare that saving as a file results in the same cell data as the existing one.
208+
const expectedCells = notebookTestData.get(notebookSimpleMixedComments);
209+
compareCells(newNotebook.cells, expectedCells);
210+
}).timeout(20000);
140211
});
141212

142213
function readBackingFile(uri: vscode.Uri): string {
143214
return readFileSync(uri.fsPath).toString();
144215
}
216+
217+
function compareCells(actualCells: vscode.NotebookCellData[], expectedCells: vscode.NotebookCellData[]) : void {
218+
assert.deepStrictEqual(actualCells.length, expectedCells.length);
219+
220+
// Compare cell metadata
221+
for (let i = 0; i < actualCells.length; i++) {
222+
assert.deepStrictEqual(
223+
actualCells[i].metadata.custom,
224+
expectedCells[i].metadata.custom
225+
);
226+
}
227+
}
228+
229+
function sleep(ms: number) {
230+
return new Promise(resolve => setTimeout(resolve, ms));
231+
}

test/runTests.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ async function main() {
1717
const extensionTestsPath = path.resolve(__dirname, "./testRunner");
1818

1919
// Download VS Code, unzip it and run the integration test from the local directory.
20-
await runTests({ extensionDevelopmentPath, extensionTestsPath, launchArgs: ["."] });
20+
await runTests({
21+
extensionDevelopmentPath,
22+
extensionTestsPath,
23+
launchArgs: ["--disable-extensions", "--enable-proposed-api", "ms-vscode.powershell-preview", "."],
24+
version: 'insiders'
25+
});
2126
} catch (err) {
2227
// tslint:disable-next-line:no-console
2328
console.error(err);

0 commit comments

Comments
 (0)