-
Notifications
You must be signed in to change notification settings - Fork 29.7k
/
customEditor.test.ts
314 lines (246 loc) · 10.6 KB
/
customEditor.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
import { Testing } from '../customTextEditor';
import { closeAllEditors, delay, disposeAll, randomFilePath } from './utils';
assert.ok(vscode.workspace.rootPath);
const testWorkspaceRoot = vscode.Uri.file(path.join(vscode.workspace.rootPath!, 'customEditors'));
const commands = Object.freeze({
open: 'vscode.open',
openWith: 'vscode.openWith',
save: 'workbench.action.files.save',
undo: 'undo',
});
async function writeRandomFile(options: { ext: string; contents: string; }): Promise<vscode.Uri> {
const fakeFile = randomFilePath({ root: testWorkspaceRoot, ext: options.ext });
await fs.promises.writeFile(fakeFile.fsPath, Buffer.from(options.contents));
return fakeFile;
}
const disposables: vscode.Disposable[] = [];
function _register<T extends vscode.Disposable>(disposable: T) {
disposables.push(disposable);
return disposable;
}
class CustomEditorUpdateListener {
public static create() {
return _register(new CustomEditorUpdateListener());
}
private readonly commandSubscription: vscode.Disposable;
private readonly unconsumedResponses: Array<Testing.CustomEditorContentChangeEvent> = [];
private readonly callbackQueue: Array<(data: Testing.CustomEditorContentChangeEvent) => void> = [];
private constructor() {
this.commandSubscription = vscode.commands.registerCommand(Testing.abcEditorContentChangeCommand, (data: Testing.CustomEditorContentChangeEvent) => {
if (this.callbackQueue.length) {
const callback = this.callbackQueue.shift();
assert.ok(callback);
callback!(data);
} else {
this.unconsumedResponses.push(data);
}
});
}
dispose() {
this.commandSubscription.dispose();
}
async nextResponse(): Promise<Testing.CustomEditorContentChangeEvent> {
if (this.unconsumedResponses.length) {
return this.unconsumedResponses.shift()!;
}
return new Promise(resolve => {
this.callbackQueue.push(resolve);
});
}
}
suite('CustomEditor tests', () => {
setup(async () => {
await closeAllEditors();
await resetTestWorkspace();
});
teardown(async () => {
await closeAllEditors();
disposeAll(disposables);
await resetTestWorkspace();
});
test('Should load basic content from disk', async () => {
const startingContent = `load, init`;
const testDocument = await writeRandomFile({ ext: '.abc', contents: startingContent });
const listener = CustomEditorUpdateListener.create();
await vscode.commands.executeCommand(commands.open, testDocument);
const { content } = await listener.nextResponse();
assert.equal(content, startingContent);
});
test('Should support basic edits', async () => {
const startingContent = `basic edit, init`;
const testDocument = await writeRandomFile({ ext: '.abc', contents: startingContent });
const listener = CustomEditorUpdateListener.create();
await vscode.commands.executeCommand(commands.open, testDocument);
await listener.nextResponse();
const newContent = `basic edit test`;
await vscode.commands.executeCommand(Testing.abcEditorTypeCommand, newContent);
const { content } = await listener.nextResponse();
assert.equal(content, newContent);
});
test('Should support single undo', async () => {
const startingContent = `single undo, init`;
const testDocument = await writeRandomFile({ ext: '.abc', contents: startingContent });
const listener = CustomEditorUpdateListener.create();
await vscode.commands.executeCommand(commands.open, testDocument);
await listener.nextResponse();
const newContent = `undo test`;
{
await vscode.commands.executeCommand(Testing.abcEditorTypeCommand, newContent);
const { content } = await listener.nextResponse();
assert.equal(content, newContent);
}
await delay(100);
{
await vscode.commands.executeCommand(commands.undo);
const { content } = await listener.nextResponse();
assert.equal(content, startingContent);
}
});
test('Should support multiple undo', async () => {
const startingContent = `multiple undo, init`;
const testDocument = await writeRandomFile({ ext: '.abc', contents: startingContent });
const listener = CustomEditorUpdateListener.create();
await vscode.commands.executeCommand(commands.open, testDocument);
await listener.nextResponse();
const count = 10;
// Make edits
for (let i = 0; i < count; ++i) {
await vscode.commands.executeCommand(Testing.abcEditorTypeCommand, `${i}`);
const { content } = await listener.nextResponse();
assert.equal(`${i}`, content);
}
// Then undo them in order
for (let i = count - 1; i; --i) {
await delay(100);
await vscode.commands.executeCommand(commands.undo);
const { content } = await listener.nextResponse();
assert.equal(`${i - 1}`, content);
}
{
await delay(100);
await vscode.commands.executeCommand(commands.undo);
const { content } = await listener.nextResponse();
assert.equal(content, startingContent);
}
});
test('Should update custom editor on file move', async () => {
const startingContent = `file move, init`;
const testDocument = await writeRandomFile({ ext: '.abc', contents: startingContent });
const listener = CustomEditorUpdateListener.create();
await vscode.commands.executeCommand(commands.open, testDocument);
await listener.nextResponse();
const newFileName = vscode.Uri.file(path.join(testWorkspaceRoot.fsPath, 'y.abc'));
const edit = new vscode.WorkspaceEdit();
edit.renameFile(testDocument, newFileName);
await vscode.workspace.applyEdit(edit);
const response = (await listener.nextResponse());
assert.equal(response.content, startingContent);
assert.equal(response.source.toString(), newFileName.toString());
});
test('Should support saving custom editors', async () => {
const startingContent = `save, init`;
const testDocument = await writeRandomFile({ ext: '.abc', contents: startingContent });
const listener = CustomEditorUpdateListener.create();
await vscode.commands.executeCommand(commands.open, testDocument);
await listener.nextResponse();
const newContent = `save, new`;
{
await vscode.commands.executeCommand(Testing.abcEditorTypeCommand, newContent);
const { content } = await listener.nextResponse();
assert.equal(content, newContent);
}
{
await vscode.commands.executeCommand(commands.save);
const fileContent = (await fs.promises.readFile(testDocument.fsPath)).toString();
assert.equal(fileContent, newContent);
}
});
test('Should undo after saving custom editor', async () => {
const startingContent = `undo after save, init`;
const testDocument = await writeRandomFile({ ext: '.abc', contents: startingContent });
const listener = CustomEditorUpdateListener.create();
await vscode.commands.executeCommand(commands.open, testDocument);
await listener.nextResponse();
const newContent = `undo after save, new`;
{
await vscode.commands.executeCommand(Testing.abcEditorTypeCommand, newContent);
const { content } = await listener.nextResponse();
assert.equal(content, newContent);
}
{
await vscode.commands.executeCommand(commands.save);
const fileContent = (await fs.promises.readFile(testDocument.fsPath)).toString();
assert.equal(fileContent, newContent);
}
await delay(100);
{
await vscode.commands.executeCommand(commands.undo);
const { content } = await listener.nextResponse();
assert.equal(content, startingContent);
}
});
test.skip('Should support untitled custom editors', async () => {
const listener = CustomEditorUpdateListener.create();
const untitledFile = randomFilePath({ root: testWorkspaceRoot, ext: '.abc' }).with({ scheme: 'untitled' });
await vscode.commands.executeCommand(commands.open, untitledFile);
assert.equal((await listener.nextResponse()).content, '');
await vscode.commands.executeCommand(Testing.abcEditorTypeCommand, `123`);
assert.equal((await listener.nextResponse()).content, '123');
await vscode.commands.executeCommand(commands.save);
const content = await fs.promises.readFile(untitledFile.fsPath);
assert.equal(content.toString(), '123');
});
test.skip('When switching away from a non-default custom editors and then back, we should continue using the non-default editor', async () => {
const startingContent = `switch, init`;
const testDocument = await writeRandomFile({ ext: '.abc', contents: startingContent });
const listener = CustomEditorUpdateListener.create();
{
await vscode.commands.executeCommand(commands.open, testDocument, { preview: false });
const { content } = await listener.nextResponse();
assert.strictEqual(content, startingContent.toString());
assert.ok(!vscode.window.activeTextEditor);
}
// Switch to non-default editor
await vscode.commands.executeCommand(commands.openWith, testDocument, 'default', { preview: false });
assert.strictEqual(vscode.window.activeTextEditor!?.document.uri.toString(), testDocument.toString());
// Then open a new document (hiding existing one)
const otherFile = vscode.Uri.file(path.join(testWorkspaceRoot.fsPath, 'other.json'));
await vscode.commands.executeCommand(commands.open, otherFile);
assert.strictEqual(vscode.window.activeTextEditor!?.document.uri.toString(), otherFile.toString());
// And then back
await vscode.commands.executeCommand('workbench.action.navigateBack');
await vscode.commands.executeCommand('workbench.action.navigateBack');
// Make sure we have the file on as text
assert.ok(vscode.window.activeTextEditor);
assert.strictEqual(vscode.window.activeTextEditor!?.document.uri.toString(), testDocument.toString());
});
test('Should release the text document when the editor is closed', async () => {
const startingContent = `release document init,`;
const testDocument = await writeRandomFile({ ext: '.abc', contents: startingContent });
const listener = CustomEditorUpdateListener.create();
await vscode.commands.executeCommand(commands.open, testDocument);
await listener.nextResponse();
const doc = vscode.workspace.textDocuments.find(x => x.uri.toString() === testDocument.toString());
assert.ok(doc);
assert.ok(!doc!.isClosed);
await closeAllEditors();
await delay(100);
assert.ok(doc!.isClosed);
});
});
async function resetTestWorkspace() {
try {
await vscode.workspace.fs.delete(testWorkspaceRoot, { recursive: true });
} catch {
// ok if file doesn't exist
}
await vscode.workspace.fs.createDirectory(testWorkspaceRoot);
}