@@ -9,6 +9,7 @@ import { PowerShellNotebooksFeature } from "../../src/features/PowerShellNoteboo
9
9
import { before } from "mocha" ;
10
10
import os = require( "os" ) ;
11
11
import { readFileSync } from "fs" ;
12
+ import { CommentType } from "../../src/settings" ;
12
13
13
14
const notebookDir = [
14
15
__dirname ,
@@ -40,7 +41,11 @@ suite("PowerShellNotebooks tests", () => {
40
41
language : "powershell" ,
41
42
source : readBackingFile ( notebookOnlyCode ) ,
42
43
outputs : [ ] ,
43
- metadata : { }
44
+ metadata : {
45
+ custom : {
46
+ commentType : CommentType . Disabled ,
47
+ }
48
+ }
44
49
}
45
50
] ) ;
46
51
@@ -50,7 +55,11 @@ suite("PowerShellNotebooks tests", () => {
50
55
language : "markdown" ,
51
56
source : readBackingFile ( notebookOnlyMarkdown ) ,
52
57
outputs : [ ] ,
53
- metadata : { }
58
+ metadata : {
59
+ custom : {
60
+ commentType : CommentType . LineComment ,
61
+ }
62
+ }
54
63
}
55
64
] ) ;
56
65
@@ -61,21 +70,33 @@ suite("PowerShellNotebooks tests", () => {
61
70
language : "markdown" ,
62
71
source : content . slice ( 0 , 5 ) . join ( os . EOL ) ,
63
72
outputs : [ ] ,
64
- metadata : { }
73
+ metadata : {
74
+ custom : {
75
+ commentType : CommentType . BlockComment ,
76
+ }
77
+ }
65
78
} ,
66
79
{
67
80
cellKind : vscode . CellKind . Code ,
68
81
language : "powershell" ,
69
82
source : content . slice ( 5 , 6 ) . join ( os . EOL ) ,
70
83
outputs : [ ] ,
71
- metadata : { }
84
+ metadata : {
85
+ custom : {
86
+ commentType : CommentType . Disabled ,
87
+ }
88
+ }
72
89
} ,
73
90
{
74
91
cellKind : vscode . CellKind . Markdown ,
75
92
language : "markdown" ,
76
93
source : content . slice ( 6 , 11 ) . join ( os . EOL ) ,
77
94
outputs : [ ] ,
78
- metadata : { }
95
+ metadata : {
96
+ custom : {
97
+ commentType : CommentType . BlockComment ,
98
+ }
99
+ }
79
100
} ,
80
101
] ) ;
81
102
@@ -86,21 +107,33 @@ suite("PowerShellNotebooks tests", () => {
86
107
language : "markdown" ,
87
108
source : content . slice ( 0 , 3 ) . join ( os . EOL ) ,
88
109
outputs : [ ] ,
89
- metadata : { }
110
+ metadata : {
111
+ custom : {
112
+ commentType : CommentType . LineComment ,
113
+ }
114
+ }
90
115
} ,
91
116
{
92
117
cellKind : vscode . CellKind . Code ,
93
118
language : "powershell" ,
94
119
source : content . slice ( 3 , 4 ) . join ( os . EOL ) ,
95
120
outputs : [ ] ,
96
- metadata : { }
121
+ metadata : {
122
+ custom : {
123
+ commentType : CommentType . Disabled ,
124
+ }
125
+ }
97
126
} ,
98
127
{
99
128
cellKind : vscode . CellKind . Markdown ,
100
129
language : "markdown" ,
101
130
source : content . slice ( 4 , 7 ) . join ( os . EOL ) ,
102
131
outputs : [ ] ,
103
- metadata : { }
132
+ metadata : {
133
+ custom : {
134
+ commentType : CommentType . LineComment ,
135
+ }
136
+ }
104
137
} ,
105
138
] ) ;
106
139
@@ -111,34 +144,88 @@ suite("PowerShellNotebooks tests", () => {
111
144
language : "markdown" ,
112
145
source : content . slice ( 0 , 3 ) . join ( os . EOL ) ,
113
146
outputs : [ ] ,
114
- metadata : { }
147
+ metadata : {
148
+ custom : {
149
+ commentType : CommentType . LineComment ,
150
+ }
151
+ }
115
152
} ,
116
153
{
117
154
cellKind : vscode . CellKind . Code ,
118
155
language : "powershell" ,
119
156
source : content . slice ( 3 , 4 ) . join ( os . EOL ) ,
120
157
outputs : [ ] ,
121
- metadata : { }
158
+ metadata : {
159
+ custom : {
160
+ commentType : CommentType . Disabled ,
161
+ }
162
+ }
122
163
} ,
123
164
{
124
165
cellKind : vscode . CellKind . Markdown ,
125
166
language : "markdown" ,
126
167
source : content . slice ( 4 , 9 ) . join ( os . EOL ) ,
127
168
outputs : [ ] ,
128
- metadata : { }
169
+ metadata : {
170
+ custom : {
171
+ commentType : CommentType . BlockComment ,
172
+ }
173
+ }
129
174
} ,
130
175
] ) ;
131
176
132
- const feature = new PowerShellNotebooksFeature ( ) ;
177
+ const feature = new PowerShellNotebooksFeature ( true ) ;
133
178
134
- for ( const [ uri , cells ] of notebookTestData ) {
179
+ for ( const [ uri , expectedCells ] of notebookTestData ) {
135
180
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 ) ;
138
183
} ) ;
139
184
}
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 ) ;
140
211
} ) ;
141
212
142
213
function readBackingFile ( uri : vscode . Uri ) : string {
143
214
return readFileSync ( uri . fsPath ) . toString ( ) ;
144
215
}
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