@@ -9,6 +9,7 @@ import { PowerShellNotebooksFeature } from "../../src/features/PowerShellNoteboo
99import { before } from "mocha" ;
1010import os = require( "os" ) ;
1111import { readFileSync } from "fs" ;
12+ import { CommentType } from "../../src/settings" ;
1213
1314const 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
142213function 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+ }
0 commit comments