workspace.test.ts 21.3 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import * as assert from 'assert';
J
Johannes Rieken 已提交
9
import * as vscode from 'vscode';
10 11
import { createRandomFile, deleteFile, closeAllEditors, pathEquals, rndName } from '../utils';
import { join, basename, dirname } from 'path';
12
import * as fs from 'fs';
13
import * as os from 'os';
B
Benjamin Pasero 已提交
14

E
Erich Gamma 已提交
15 16
suite('workspace-namespace', () => {

J
Johannes Rieken 已提交
17
	teardown(closeAllEditors);
18

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
	test('MarkdownString', function () {
		let md = new vscode.MarkdownString();
		assert.equal(md.value, '');
		assert.equal(md.isTrusted, undefined);

		md = new vscode.MarkdownString('**bold**');
		assert.equal(md.value, '**bold**');

		md.appendText('**bold?**');
		assert.equal(md.value, '**bold**\\*\\*bold?\\*\\*');

		md.appendMarkdown('**bold**');
		assert.equal(md.value, '**bold**\\*\\*bold?\\*\\***bold**');
	});

34

E
Erich Gamma 已提交
35
	test('textDocuments', () => {
J
Johannes Rieken 已提交
36 37
		assert.ok(Array.isArray(vscode.workspace.textDocuments));
		assert.throws(() => (<any>vscode.workspace).textDocuments = null);
E
Erich Gamma 已提交
38 39 40
	});

	test('rootPath', () => {
J
Johannes Rieken 已提交
41
		if (vscode.workspace.rootPath) {
42
			assert.ok(pathEquals(vscode.workspace.rootPath, join(__dirname, '../../testWorkspace')));
43
		}
J
Johannes Rieken 已提交
44
		assert.throws(() => vscode.workspace.rootPath = 'farboo');
E
Erich Gamma 已提交
45 46
	});

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	test('workspaceFolders', () => {
		if (vscode.workspace.workspaceFolders) {
			assert.equal(vscode.workspace.workspaceFolders.length, 1);
			assert.ok(pathEquals(vscode.workspace.workspaceFolders[0].uri.fsPath, join(__dirname, '../../testWorkspace')));
		}
	});

	test('getWorkspaceFolder', () => {
		const folder = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(join(__dirname, '../../testWorkspace/far.js')));
		assert.ok(!!folder);

		if (folder) {
			assert.ok(pathEquals(folder.uri.fsPath, join(__dirname, '../../testWorkspace')));
		}
	});

J
Joao Moreno 已提交
63
	test('openTextDocument', () => {
J
Johannes Rieken 已提交
64 65
		let len = vscode.workspace.textDocuments.length;
		return vscode.workspace.openTextDocument(join(vscode.workspace.rootPath || '', './simple.txt')).then(doc => {
E
Erich Gamma 已提交
66
			assert.ok(doc);
J
Johannes Rieken 已提交
67
			assert.equal(vscode.workspace.textDocuments.length, len + 1);
E
Erich Gamma 已提交
68 69 70
		});
	});

71
	test('openTextDocument, illegal path', () => {
J
Johannes Rieken 已提交
72
		return vscode.workspace.openTextDocument('funkydonky.txt').then(doc => {
73
			throw new Error('missing error');
E
Erich Gamma 已提交
74
		}, err => {
75
			// good!
E
Erich Gamma 已提交
76 77 78
		});
	});

79 80 81 82 83 84
	test('openTextDocument, untitled is dirty', function () {
		return vscode.workspace.openTextDocument(vscode.Uri.parse('untitled:' + join(vscode.workspace.rootPath || '', './newfile.txt'))).then(doc => {
			assert.equal(doc.uri.scheme, 'untitled');
			assert.ok(doc.isDirty);
		});
	});
85

86
	test('openTextDocument, untitled with host', function () {
J
Johannes Rieken 已提交
87 88
		const uri = vscode.Uri.parse('untitled://localhost/c%24/Users/jrieken/code/samples/foobar.txt');
		return vscode.workspace.openTextDocument(uri).then(doc => {
89 90 91 92
			assert.equal(doc.uri.scheme, 'untitled');
		});
	});

B
Benjamin Pasero 已提交
93
	test('openTextDocument, untitled without path', function () {
J
Johannes Rieken 已提交
94
		return vscode.workspace.openTextDocument().then(doc => {
B
Benjamin Pasero 已提交
95 96 97 98 99 100
			assert.equal(doc.uri.scheme, 'untitled');
			assert.ok(doc.isDirty);
		});
	});

	test('openTextDocument, untitled without path but language ID', function () {
J
Johannes Rieken 已提交
101
		return vscode.workspace.openTextDocument({ language: 'xml' }).then(doc => {
B
Benjamin Pasero 已提交
102 103 104 105 106 107
			assert.equal(doc.uri.scheme, 'untitled');
			assert.equal(doc.languageId, 'xml');
			assert.ok(doc.isDirty);
		});
	});

108
	test('openTextDocument, untitled without path but language ID and content', function () {
J
Johannes Rieken 已提交
109
		return vscode.workspace.openTextDocument({ language: 'html', content: '<h1>Hello world!</h1>' }).then(doc => {
110 111 112 113 114 115 116
			assert.equal(doc.uri.scheme, 'untitled');
			assert.equal(doc.languageId, 'html');
			assert.ok(doc.isDirty);
			assert.equal(doc.getText(), '<h1>Hello world!</h1>');
		});
	});

117 118
	test('openTextDocument, untitled closes on save', function () {
		const path = join(vscode.workspace.rootPath || '', './newfile.txt');
B
Benjamin Pasero 已提交
119

120 121 122
		return vscode.workspace.openTextDocument(vscode.Uri.parse('untitled:' + path)).then(doc => {
			assert.equal(doc.uri.scheme, 'untitled');
			assert.ok(doc.isDirty);
123

124 125
			let closed: vscode.TextDocument;
			let d0 = vscode.workspace.onDidCloseTextDocument(e => closed = e);
126

127 128 129 130 131
			return vscode.window.showTextDocument(doc).then(() => {
				return doc.save().then(() => {
					assert.ok(closed === doc);
					assert.ok(!doc.isDirty);
					assert.ok(fs.existsSync(path));
132

133
					d0.dispose();
134

135 136 137
					return deleteFile(vscode.Uri.file(join(vscode.workspace.rootPath || '', './newfile.txt')));
				});
			});
138

139 140
		});
	});
141

B
Benjamin Pasero 已提交
142
	test('openTextDocument, uri scheme/auth/path', function () {
143

J
Johannes Rieken 已提交
144
		let registration = vscode.workspace.registerTextDocumentContentProvider('sc', {
145 146 147 148 149 150
			provideTextDocumentContent() {
				return 'SC';
			}
		});

		return Promise.all([
J
Johannes Rieken 已提交
151
			vscode.workspace.openTextDocument(vscode.Uri.parse('sc://auth')).then(doc => {
152 153 154
				assert.equal(doc.uri.authority, 'auth');
				assert.equal(doc.uri.path, '');
			}),
J
Johannes Rieken 已提交
155
			vscode.workspace.openTextDocument(vscode.Uri.parse('sc:///path')).then(doc => {
156 157 158
				assert.equal(doc.uri.authority, '');
				assert.equal(doc.uri.path, '/path');
			}),
J
Johannes Rieken 已提交
159
			vscode.workspace.openTextDocument(vscode.Uri.parse('sc://auth/path')).then(doc => {
160 161 162 163 164 165
				assert.equal(doc.uri.authority, 'auth');
				assert.equal(doc.uri.path, '/path');
			})
		]).then(() => {
			registration.dispose();
		});
B
Benjamin Pasero 已提交
166
	});
167

J
Johannes Rieken 已提交
168 169
	test('eol, read', () => {
		const a = createRandomFile('foo\nbar\nbar').then(file => {
J
Johannes Rieken 已提交
170 171
			return vscode.workspace.openTextDocument(file).then(doc => {
				assert.equal(doc.eol, vscode.EndOfLine.LF);
J
Johannes Rieken 已提交
172 173 174
			});
		});
		const b = createRandomFile('foo\nbar\nbar\r\nbaz').then(file => {
J
Johannes Rieken 已提交
175 176
			return vscode.workspace.openTextDocument(file).then(doc => {
				assert.equal(doc.eol, vscode.EndOfLine.LF);
J
Johannes Rieken 已提交
177 178 179
			});
		});
		const c = createRandomFile('foo\r\nbar\r\nbar').then(file => {
J
Johannes Rieken 已提交
180 181
			return vscode.workspace.openTextDocument(file).then(doc => {
				assert.equal(doc.eol, vscode.EndOfLine.CRLF);
J
Johannes Rieken 已提交
182 183 184 185 186
			});
		});
		return Promise.all([a, b, c]);
	});

J
Johannes Rieken 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	test('eol, change via editor', () => {
		return createRandomFile('foo\nbar\nbar').then(file => {
			return vscode.workspace.openTextDocument(file).then(doc => {
				assert.equal(doc.eol, vscode.EndOfLine.LF);
				return vscode.window.showTextDocument(doc).then(editor => {
					return editor.edit(builder => builder.setEndOfLine(vscode.EndOfLine.CRLF));

				}).then(value => {
					assert.ok(value);
					assert.ok(doc.isDirty);
					assert.equal(doc.eol, vscode.EndOfLine.CRLF);
				});
			});
		});
	});
J
Johannes Rieken 已提交
202

J
Johannes Rieken 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	test('eol, change via applyEdit', () => {
		return createRandomFile('foo\nbar\nbar').then(file => {
			return vscode.workspace.openTextDocument(file).then(doc => {
				assert.equal(doc.eol, vscode.EndOfLine.LF);

				const edit = new vscode.WorkspaceEdit();
				edit.set(file, [vscode.TextEdit.setEndOfLine(vscode.EndOfLine.CRLF)]);
				return vscode.workspace.applyEdit(edit).then(value => {
					assert.ok(value);
					assert.ok(doc.isDirty);
					assert.equal(doc.eol, vscode.EndOfLine.CRLF);
				});
			});
		});
	});
218

J
Johannes Rieken 已提交
219
	test('eol, change via onWillSave', () => {
220

J
Johannes Rieken 已提交
221 222 223 224 225
		let called = false;
		let sub = vscode.workspace.onWillSaveTextDocument(e => {
			called = true;
			e.waitUntil(Promise.resolve([vscode.TextEdit.setEndOfLine(vscode.EndOfLine.LF)]));
		});
226

J
Johannes Rieken 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
		return createRandomFile('foo\r\nbar\r\nbar').then(file => {
			return vscode.workspace.openTextDocument(file).then(doc => {
				assert.equal(doc.eol, vscode.EndOfLine.CRLF);
				const edit = new vscode.WorkspaceEdit();
				edit.set(file, [vscode.TextEdit.insert(new vscode.Position(0, 0), '-changes-')]);

				return vscode.workspace.applyEdit(edit).then(success => {
					assert.ok(success);
					return doc.save();

				}).then(success => {
					assert.ok(success);
					assert.ok(called);
					assert.ok(!doc.isDirty);
					assert.equal(doc.eol, vscode.EndOfLine.LF);
					sub.dispose();
				});
			});
		});
	});
J
Johannes Rieken 已提交
247

248 249
	test('events: onDidOpenTextDocument, onDidChangeTextDocument, onDidSaveTextDocument', () => {
		return createRandomFile().then(file => {
J
Johannes Rieken 已提交
250
			let disposables: vscode.Disposable[] = [];
251

B
Benjamin Pasero 已提交
252
			let onDidOpenTextDocument = false;
J
Johannes Rieken 已提交
253
			disposables.push(vscode.workspace.onDidOpenTextDocument(e => {
254
				assert.ok(pathEquals(e.uri.fsPath, file.fsPath));
B
Benjamin Pasero 已提交
255
				onDidOpenTextDocument = true;
256
			}));
B
Benjamin Pasero 已提交
257 258

			let onDidChangeTextDocument = false;
J
Johannes Rieken 已提交
259
			disposables.push(vscode.workspace.onDidChangeTextDocument(e => {
260
				assert.ok(pathEquals(e.document.uri.fsPath, file.fsPath));
B
Benjamin Pasero 已提交
261
				onDidChangeTextDocument = true;
262
			}));
B
Benjamin Pasero 已提交
263 264

			let onDidSaveTextDocument = false;
J
Johannes Rieken 已提交
265
			disposables.push(vscode.workspace.onDidSaveTextDocument(e => {
266
				assert.ok(pathEquals(e.uri.fsPath, file.fsPath));
B
Benjamin Pasero 已提交
267
				onDidSaveTextDocument = true;
268
			}));
B
Benjamin Pasero 已提交
269

J
Johannes Rieken 已提交
270 271
			return vscode.workspace.openTextDocument(file).then(doc => {
				return vscode.window.showTextDocument(doc).then((editor) => {
B
Benjamin Pasero 已提交
272
					return editor.edit((builder) => {
J
Johannes Rieken 已提交
273
						builder.insert(new vscode.Position(0, 0), 'Hello World');
B
Benjamin Pasero 已提交
274 275 276 277 278 279
					}).then(applied => {
						return doc.save().then(saved => {
							assert.ok(onDidOpenTextDocument);
							assert.ok(onDidChangeTextDocument);
							assert.ok(onDidSaveTextDocument);

280
							while (disposables.length) {
281 282 283 284
								const item = disposables.pop();
								if (item) {
									item.dispose();
								}
285 286
							}

B
Benjamin Pasero 已提交
287 288 289 290 291
							return deleteFile(file);
						});
					});
				});
			});
292
		});
B
Benjamin Pasero 已提交
293
	});
E
Erich Gamma 已提交
294

295 296 297 298 299 300 301 302 303 304 305 306 307
	test('openTextDocument, with selection', function () {
		return createRandomFile('foo\nbar\nbar').then(file => {
			return vscode.workspace.openTextDocument(file).then(doc => {
				return vscode.window.showTextDocument(doc, { selection: new vscode.Range(new vscode.Position(1, 1), new vscode.Position(1, 2)) }).then(editor => {
					assert.equal(editor.selection.start.line, 1);
					assert.equal(editor.selection.start.character, 1);
					assert.equal(editor.selection.end.line, 1);
					assert.equal(editor.selection.end.character, 2);
				});
			});
		});
	});

B
Benjamin Pasero 已提交
308
	test('registerTextDocumentContentProvider, simple', function () {
309

J
Johannes Rieken 已提交
310
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
311
			provideTextDocumentContent(uri) {
312 313 314 315
				return uri.toString();
			}
		});

J
Johannes Rieken 已提交
316 317
		const uri = vscode.Uri.parse('foo://testing/virtual.js');
		return vscode.workspace.openTextDocument(uri).then(doc => {
318 319 320 321 322 323 324
			assert.equal(doc.getText(), uri.toString());
			assert.equal(doc.isDirty, false);
			assert.equal(doc.uri.toString(), uri.toString());
			registration.dispose();
		});
	});

B
Benjamin Pasero 已提交
325
	test('registerTextDocumentContentProvider, constrains', function () {
326 327

		// built-in
B
Benjamin Pasero 已提交
328
		assert.throws(function () {
J
Johannes Rieken 已提交
329
			vscode.workspace.registerTextDocumentContentProvider('untitled', { provideTextDocumentContent() { return null; } });
330 331
		});
		// built-in
B
Benjamin Pasero 已提交
332
		assert.throws(function () {
J
Johannes Rieken 已提交
333
			vscode.workspace.registerTextDocumentContentProvider('file', { provideTextDocumentContent() { return null; } });
334 335
		});

336
		// missing scheme
J
Johannes Rieken 已提交
337
		return vscode.workspace.openTextDocument(vscode.Uri.parse('notThere://foo/far/boo/bar')).then(() => {
B
Benjamin Pasero 已提交
338
			assert.ok(false, 'expected failure');
339 340
		}, err => {
			// expected
B
Benjamin Pasero 已提交
341
		});
342 343
	});

B
Benjamin Pasero 已提交
344
	test('registerTextDocumentContentProvider, multiple', function () {
345

346
		// duplicate registration
J
Johannes Rieken 已提交
347
		let registration1 = vscode.workspace.registerTextDocumentContentProvider('foo', {
348
			provideTextDocumentContent(uri) {
349
				if (uri.authority === 'foo') {
B
Benjamin Pasero 已提交
350
					return '1';
351
				}
352 353
			}
		});
J
Johannes Rieken 已提交
354
		let registration2 = vscode.workspace.registerTextDocumentContentProvider('foo', {
355 356
			provideTextDocumentContent(uri) {
				if (uri.authority === 'bar') {
B
Benjamin Pasero 已提交
357
					return '2';
358 359
				}
			}
360 361
		});

362
		return Promise.all([
J
Johannes Rieken 已提交
363 364
			vscode.workspace.openTextDocument(vscode.Uri.parse('foo://foo/bla')).then(doc => { assert.equal(doc.getText(), '1'); }),
			vscode.workspace.openTextDocument(vscode.Uri.parse('foo://bar/bla')).then(doc => { assert.equal(doc.getText(), '2'); })
365 366 367 368 369
		]).then(() => {
			registration1.dispose();
			registration2.dispose();
		});
	});
370

B
Benjamin Pasero 已提交
371
	test('registerTextDocumentContentProvider, evil provider', function () {
372 373

		// duplicate registration
J
Johannes Rieken 已提交
374
		let registration1 = vscode.workspace.registerTextDocumentContentProvider('foo', {
375 376 377 378
			provideTextDocumentContent(uri) {
				return '1';
			}
		});
J
Johannes Rieken 已提交
379
		let registration2 = vscode.workspace.registerTextDocumentContentProvider('foo', {
380
			provideTextDocumentContent(uri): string {
B
Benjamin Pasero 已提交
381
				throw new Error('fail');
382 383 384
			}
		});

J
Johannes Rieken 已提交
385
		return vscode.workspace.openTextDocument(vscode.Uri.parse('foo://foo/bla')).then(doc => {
386 387 388 389 390 391
			assert.equal(doc.getText(), '1');
			registration1.dispose();
			registration2.dispose();
		});
	});

B
Benjamin Pasero 已提交
392
	test('registerTextDocumentContentProvider, invalid text', function () {
393

J
Johannes Rieken 已提交
394
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
395
			provideTextDocumentContent(uri) {
B
Benjamin Pasero 已提交
396
				return <any>123;
397 398
			}
		});
J
Johannes Rieken 已提交
399
		return vscode.workspace.openTextDocument(vscode.Uri.parse('foo://auth/path')).then(() => {
B
Benjamin Pasero 已提交
400
			assert.ok(false, 'expected failure');
401 402
		}, err => {
			// expected
403 404
			registration.dispose();
		});
405 406
	});

B
Benjamin Pasero 已提交
407
	test('registerTextDocumentContentProvider, show virtual document', function () {
408

J
Johannes Rieken 已提交
409
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
410
			provideTextDocumentContent(uri) {
411 412 413 414
				return 'I am virtual';
			}
		});

J
Johannes Rieken 已提交
415 416
		return vscode.workspace.openTextDocument(vscode.Uri.parse('foo://something/path')).then(doc => {
			return vscode.window.showTextDocument(doc).then(editor => {
417 418 419 420

				assert.ok(editor.document === doc);
				assert.equal(editor.document.getText(), 'I am virtual');
				registration.dispose();
B
Benjamin Pasero 已提交
421
			});
422 423
		});
	});
424

B
Benjamin Pasero 已提交
425
	test('registerTextDocumentContentProvider, open/open document', function () {
426 427

		let callCount = 0;
J
Johannes Rieken 已提交
428
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
429 430 431 432 433 434
			provideTextDocumentContent(uri) {
				callCount += 1;
				return 'I am virtual';
			}
		});

J
Johannes Rieken 已提交
435
		const uri = vscode.Uri.parse('foo://testing/path');
436

J
Johannes Rieken 已提交
437
		return Promise.all([vscode.workspace.openTextDocument(uri), vscode.workspace.openTextDocument(uri)]).then(docs => {
438 439
			let [first, second] = docs;
			assert.ok(first === second);
J
Johannes Rieken 已提交
440
			assert.ok(vscode.workspace.textDocuments.some(doc => doc.uri.toString() === uri.toString()));
441 442 443 444 445
			assert.equal(callCount, 1);
			registration.dispose();
		});
	});

446 447
	test('registerTextDocumentContentProvider, empty doc', function () {

J
Johannes Rieken 已提交
448
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
449 450 451 452 453
			provideTextDocumentContent(uri) {
				return '';
			}
		});

J
Johannes Rieken 已提交
454
		const uri = vscode.Uri.parse('foo:doc/empty');
455

J
Johannes Rieken 已提交
456
		return vscode.workspace.openTextDocument(uri).then(doc => {
457 458 459 460 461 462
			assert.equal(doc.getText(), '');
			assert.equal(doc.uri.toString(), uri.toString());
			registration.dispose();
		});
	});

J
Johannes Rieken 已提交
463
	test('registerTextDocumentContentProvider, change event', async function () {
464 465

		let callCount = 0;
J
Johannes Rieken 已提交
466
		let emitter = new vscode.EventEmitter<vscode.Uri>();
467

J
Johannes Rieken 已提交
468
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
469
			onDidChange: emitter.event,
470 471 472 473 474
			provideTextDocumentContent(uri) {
				return 'call' + (callCount++);
			}
		});

J
Johannes Rieken 已提交
475
		const uri = vscode.Uri.parse('foo://testing/path3');
J
Johannes Rieken 已提交
476
		const doc = await vscode.workspace.openTextDocument(uri);
477

J
Johannes Rieken 已提交
478 479
		assert.equal(callCount, 1);
		assert.equal(doc.getText(), 'call0');
480

J
Johannes Rieken 已提交
481
		return new Promise(resolve => {
482

J
Johannes Rieken 已提交
483 484 485 486
			let subscription = vscode.workspace.onDidChangeTextDocument(event => {
				assert.ok(event.document === doc);
				assert.equal(event.document.getText(), 'call1');
				subscription.dispose();
487
				registration.dispose();
J
Johannes Rieken 已提交
488
				resolve();
489
			});
J
Johannes Rieken 已提交
490 491

			emitter.fire(doc.uri);
492 493 494
		});
	});

495
	test('findFiles', () => {
J
Johannes Rieken 已提交
496
		return vscode.workspace.findFiles('*.js').then((res) => {
B
Benjamin Pasero 已提交
497
			assert.equal(res.length, 1);
J
Johannes Rieken 已提交
498
			assert.equal(basename(vscode.workspace.asRelativePath(res[0])), 'far.js');
B
Benjamin Pasero 已提交
499
		});
B
Benjamin Pasero 已提交
500
	});
501

B
Benjamin Pasero 已提交
502 503
	// TODO@Joh this test fails randomly
	// test('findFiles, cancellation', () => {
504

B
Benjamin Pasero 已提交
505 506 507
	// 	const source = new CancellationTokenSource();
	// 	const token = source.token; // just to get an instance first
	// 	source.cancel();
508

J
Johannes Rieken 已提交
509
	// 	return vscode.workspace.findFiles('*.js', null, 100, token).then((res) => {
B
Benjamin Pasero 已提交
510 511 512
	// 		assert.equal(res, void 0);
	// 	});
	// });
513

514
	test('applyEdit', () => {
515

516 517 518 519 520 521
		return vscode.workspace.openTextDocument(vscode.Uri.parse('untitled:' + join(vscode.workspace.rootPath || '', './new2.txt'))).then(doc => {
			let edit = new vscode.WorkspaceEdit();
			edit.insert(doc.uri, new vscode.Position(0, 0), new Array(1000).join('Hello World'));
			return vscode.workspace.applyEdit(edit);
		});
	});
522

523 524
	test('applyEdit should fail when editing deleted resource', async () => {
		const resource = await createRandomFile();
525

526 527 528
		const edit = new vscode.WorkspaceEdit();
		edit.deleteFile(resource);
		edit.insert(resource, new vscode.Position(0, 0), '');
M
Matt Bierner 已提交
529

530 531 532
		let success = await vscode.workspace.applyEdit(edit);
		assert.equal(success, false);
	});
533

534 535 536 537 538 539
	test('applyEdit should fail when renaming deleted resource', async () => {
		const resource = await createRandomFile();

		const edit = new vscode.WorkspaceEdit();
		edit.deleteFile(resource);
		edit.renameFile(resource, resource);
M
Matt Bierner 已提交
540

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
		let success = await vscode.workspace.applyEdit(edit);
		assert.equal(success, false);
	});

	test('applyEdit should fail when editing renamed from resource', async () => {
		const resource = await createRandomFile();
		const newResource = vscode.Uri.parse(resource.fsPath + '.1');
		const edit = new vscode.WorkspaceEdit();
		edit.renameFile(resource, newResource);
		edit.insert(resource, new vscode.Position(0, 0), '');

		let success = await vscode.workspace.applyEdit(edit);
		assert.equal(success, false);
	});

556 557 558 559 560 561 562
	test('applyEdit "edit A -> rename A to B -> edit B"', async () => {
		const oldUri = await createRandomFile();
		const newUri = oldUri.with({ path: oldUri.path + 'NEW' });
		const edit = new vscode.WorkspaceEdit();
		edit.insert(oldUri, new vscode.Position(0, 0), 'BEFORE');
		edit.renameFile(oldUri, newUri);
		edit.insert(newUri, new vscode.Position(0, 0), 'AFTER');
563

564 565
		let success = await vscode.workspace.applyEdit(edit);
		assert.equal(success, true);
566

567
		// todo@ben https://github.com/Microsoft/vscode/issues/52212
568 569 570
		// let doc = await vscode.workspace.openTextDocument(newUri);
		// assert.equal(doc.getText(), 'AFTERBEFORE');
	});
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

	function nameWithUnderscore(uri: vscode.Uri) {
		return uri.with({ path: join(dirname(uri.path), `_${basename(uri.path)}`) });
	}

	test('WorkspaceEdit: applying edits before and after rename duplicates resource #42633', async function () {
		let docUri = await createRandomFile();
		let newUri = nameWithUnderscore(docUri);

		let we = new vscode.WorkspaceEdit();
		we.insert(docUri, new vscode.Position(0, 0), 'Hello');
		we.insert(docUri, new vscode.Position(0, 0), 'Foo');
		we.renameFile(docUri, newUri);
		we.insert(newUri, new vscode.Position(0, 0), 'Bar');

		assert.ok(await vscode.workspace.applyEdit(we));
		// todo@ben https://github.com/Microsoft/vscode/issues/52212
		// let doc = await vscode.workspace.openTextDocument(newUri);
J
Johannes Rieken 已提交
589
		// assert.equal(doc.getText(), 'BarHelloFoo');
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
	});

	test('WorkspaceEdit: Problem recreating a renamed resource #42634', async function () {
		let docUri = await createRandomFile();
		let newUri = nameWithUnderscore(docUri);

		let we = new vscode.WorkspaceEdit();
		we.insert(docUri, new vscode.Position(0, 0), 'Hello');
		we.insert(docUri, new vscode.Position(0, 0), 'Foo');
		we.renameFile(docUri, newUri);

		we.createFile(docUri);
		we.insert(docUri, new vscode.Position(0, 0), 'Bar');

		assert.ok(await vscode.workspace.applyEdit(we));

		// todo@ben https://github.com/Microsoft/vscode/issues/52212
		// let newDoc = await vscode.workspace.openTextDocument(newUri);
J
Johannes Rieken 已提交
608
		// assert.equal(newDoc.getText(), 'HelloFoo');
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
		// let doc = await vscode.workspace.openTextDocument(docUri);
		// assert.equal(doc.getText(), 'Bar');
	});

	test('WorkspaceEdit api - after saving a deleted file, it still shows up as deleted. #42667', async function () {
		let docUri = await createRandomFile();
		let we = new vscode.WorkspaceEdit();
		we.deleteFile(docUri);
		we.insert(docUri, new vscode.Position(0, 0), 'InsertText');

		assert.ok(!(await vscode.workspace.applyEdit(we)));
		try {
			await vscode.workspace.openTextDocument(docUri);
			assert.ok(false);
		} catch (e) {
			assert.ok(true);
		}
	});

	test('WorkspaceEdit: edit and rename parent folder duplicates resource #42641', async function () {

		let dir = join(os.tmpdir(), 'before-' + rndName());
		if (!fs.existsSync(dir)) {
			fs.mkdirSync(dir);
		}

		let docUri = await createRandomFile('', dir);
		let docParent = docUri.with({ path: dirname(docUri.path) });
		let newParent = nameWithUnderscore(docParent);

		let we = new vscode.WorkspaceEdit();
		we.insert(docUri, new vscode.Position(0, 0), 'Hello');
		we.renameFile(docParent, newParent);

		assert.ok(await vscode.workspace.applyEdit(we));

		try {
			await vscode.workspace.openTextDocument(docUri);
			assert.ok(false);
		} catch (e) {
			assert.ok(true);
		}

		let newUri = newParent.with({ path: join(newParent.path, basename(docUri.path)) });
		let doc = await vscode.workspace.openTextDocument(newUri);
		assert.ok(doc);

		// todo@ben https://github.com/Microsoft/vscode/issues/52212
		// assert.equal(doc.getText(), 'Hello');
	});
J
Johannes Rieken 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672

	test('WorkspaceEdit: rename resource followed by edit does not work #42638', async function () {
		let docUri = await createRandomFile();
		let newUri = nameWithUnderscore(docUri);

		let we = new vscode.WorkspaceEdit();
		we.renameFile(docUri, newUri);
		we.insert(newUri, new vscode.Position(0, 0), 'Hello');

		assert.ok(await vscode.workspace.applyEdit(we));

		let doc = await vscode.workspace.openTextDocument(newUri);
		assert.equal(doc.getText(), 'Hello');
	});
673
});