workspace.test.ts 24.2 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  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';
J
Johannes Rieken 已提交
7
import * as vscode from 'vscode';
M
Matt Bierner 已提交
8
import { createRandomFile, deleteFile, closeAllEditors, pathEquals, rndName, disposeAll } from '../utils';
J
Johannes Rieken 已提交
9
import { join, posix, basename } from 'path';
10
import * as fs from 'fs';
11
import * as os from 'os';
B
Benjamin Pasero 已提交
12

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

J
Johannes Rieken 已提交
15
	teardown(closeAllEditors);
16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
	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**');
	});

32

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

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

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	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 已提交
61
	test('openTextDocument', () => {
J
Johannes Rieken 已提交
62 63
		let len = vscode.workspace.textDocuments.length;
		return vscode.workspace.openTextDocument(join(vscode.workspace.rootPath || '', './simple.txt')).then(doc => {
E
Erich Gamma 已提交
64
			assert.ok(doc);
J
Johannes Rieken 已提交
65
			assert.equal(vscode.workspace.textDocuments.length, len + 1);
E
Erich Gamma 已提交
66 67 68
		});
	});

69
	test('openTextDocument, illegal path', () => {
70
		return vscode.workspace.openTextDocument('funkydonky.txt').then(_doc => {
71
			throw new Error('missing error');
72
		}, _err => {
73
			// good!
E
Erich Gamma 已提交
74 75 76
		});
	});

77 78 79 80 81 82
	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);
		});
	});
83

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

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

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

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

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

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

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

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

131
					d0.dispose();
132

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

137 138
		});
	});
139

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

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

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

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

J
Johannes Rieken 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	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 已提交
200

J
Johannes Rieken 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	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);
				});
			});
		});
	});
216

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

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

J
Johannes Rieken 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
		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 已提交
245

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

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

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

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

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

M
Matt Bierner 已提交
278
							disposeAll(disposables);
279

B
Benjamin Pasero 已提交
280 281 282 283 284
							return deleteFile(file);
						});
					});
				});
			});
285
		});
B
Benjamin Pasero 已提交
286
	});
E
Erich Gamma 已提交
287

288 289 290 291 292 293 294 295 296 297 298 299 300
	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 已提交
301
	test('registerTextDocumentContentProvider, simple', function () {
302

J
Johannes Rieken 已提交
303
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
304
			provideTextDocumentContent(uri) {
305 306 307 308
				return uri.toString();
			}
		});

J
Johannes Rieken 已提交
309 310
		const uri = vscode.Uri.parse('foo://testing/virtual.js');
		return vscode.workspace.openTextDocument(uri).then(doc => {
311 312 313 314 315 316 317
			assert.equal(doc.getText(), uri.toString());
			assert.equal(doc.isDirty, false);
			assert.equal(doc.uri.toString(), uri.toString());
			registration.dispose();
		});
	});

B
Benjamin Pasero 已提交
318
	test('registerTextDocumentContentProvider, constrains', function () {
319 320

		// built-in
B
Benjamin Pasero 已提交
321
		assert.throws(function () {
J
Johannes Rieken 已提交
322
			vscode.workspace.registerTextDocumentContentProvider('untitled', { provideTextDocumentContent() { return null; } });
323 324
		});
		// built-in
B
Benjamin Pasero 已提交
325
		assert.throws(function () {
J
Johannes Rieken 已提交
326
			vscode.workspace.registerTextDocumentContentProvider('file', { provideTextDocumentContent() { return null; } });
327 328
		});

329
		// missing scheme
J
Johannes Rieken 已提交
330
		return vscode.workspace.openTextDocument(vscode.Uri.parse('notThere://foo/far/boo/bar')).then(() => {
B
Benjamin Pasero 已提交
331
			assert.ok(false, 'expected failure');
332
		}, _err => {
333
			// expected
B
Benjamin Pasero 已提交
334
		});
335 336
	});

B
Benjamin Pasero 已提交
337
	test('registerTextDocumentContentProvider, multiple', function () {
338

339
		// duplicate registration
J
Johannes Rieken 已提交
340
		let registration1 = vscode.workspace.registerTextDocumentContentProvider('foo', {
341
			provideTextDocumentContent(uri) {
342
				if (uri.authority === 'foo') {
B
Benjamin Pasero 已提交
343
					return '1';
344
				}
345
				return undefined;
346 347
			}
		});
J
Johannes Rieken 已提交
348
		let registration2 = vscode.workspace.registerTextDocumentContentProvider('foo', {
349 350
			provideTextDocumentContent(uri) {
				if (uri.authority === 'bar') {
B
Benjamin Pasero 已提交
351
					return '2';
352
				}
353
				return undefined;
354
			}
355 356
		});

357
		return Promise.all([
J
Johannes Rieken 已提交
358 359
			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'); })
360 361 362 363 364
		]).then(() => {
			registration1.dispose();
			registration2.dispose();
		});
	});
365

B
Benjamin Pasero 已提交
366
	test('registerTextDocumentContentProvider, evil provider', function () {
367 368

		// duplicate registration
J
Johannes Rieken 已提交
369
		let registration1 = vscode.workspace.registerTextDocumentContentProvider('foo', {
370
			provideTextDocumentContent(_uri) {
371 372 373
				return '1';
			}
		});
J
Johannes Rieken 已提交
374
		let registration2 = vscode.workspace.registerTextDocumentContentProvider('foo', {
375
			provideTextDocumentContent(_uri): string {
B
Benjamin Pasero 已提交
376
				throw new Error('fail');
377 378 379
			}
		});

J
Johannes Rieken 已提交
380
		return vscode.workspace.openTextDocument(vscode.Uri.parse('foo://foo/bla')).then(doc => {
381 382 383 384 385 386
			assert.equal(doc.getText(), '1');
			registration1.dispose();
			registration2.dispose();
		});
	});

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

J
Johannes Rieken 已提交
389
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
390
			provideTextDocumentContent(_uri) {
B
Benjamin Pasero 已提交
391
				return <any>123;
392 393
			}
		});
J
Johannes Rieken 已提交
394
		return vscode.workspace.openTextDocument(vscode.Uri.parse('foo://auth/path')).then(() => {
B
Benjamin Pasero 已提交
395
			assert.ok(false, 'expected failure');
396
		}, _err => {
397
			// expected
398 399
			registration.dispose();
		});
400 401
	});

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

J
Johannes Rieken 已提交
404
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
405
			provideTextDocumentContent(_uri) {
406 407 408 409
				return 'I am virtual';
			}
		});

J
Johannes Rieken 已提交
410 411
		return vscode.workspace.openTextDocument(vscode.Uri.parse('foo://something/path')).then(doc => {
			return vscode.window.showTextDocument(doc).then(editor => {
412 413 414 415

				assert.ok(editor.document === doc);
				assert.equal(editor.document.getText(), 'I am virtual');
				registration.dispose();
B
Benjamin Pasero 已提交
416
			});
417 418
		});
	});
419

B
Benjamin Pasero 已提交
420
	test('registerTextDocumentContentProvider, open/open document', function () {
421 422

		let callCount = 0;
J
Johannes Rieken 已提交
423
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
424
			provideTextDocumentContent(_uri) {
425 426 427 428 429
				callCount += 1;
				return 'I am virtual';
			}
		});

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

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

441 442
	test('registerTextDocumentContentProvider, empty doc', function () {

J
Johannes Rieken 已提交
443
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
444
			provideTextDocumentContent(_uri) {
445 446 447 448
				return '';
			}
		});

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

J
Johannes Rieken 已提交
451
		return vscode.workspace.openTextDocument(uri).then(doc => {
452 453 454 455 456 457
			assert.equal(doc.getText(), '');
			assert.equal(doc.uri.toString(), uri.toString());
			registration.dispose();
		});
	});

J
Johannes Rieken 已提交
458
	test('registerTextDocumentContentProvider, change event', async function () {
459 460

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

J
Johannes Rieken 已提交
463
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
464
			onDidChange: emitter.event,
465
			provideTextDocumentContent(_uri) {
466 467 468 469
				return 'call' + (callCount++);
			}
		});

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

J
Johannes Rieken 已提交
473 474
		assert.equal(callCount, 1);
		assert.equal(doc.getText(), 'call0');
475

J
Johannes Rieken 已提交
476
		return new Promise(resolve => {
477

J
Johannes Rieken 已提交
478 479 480 481
			let subscription = vscode.workspace.onDidChangeTextDocument(event => {
				assert.ok(event.document === doc);
				assert.equal(event.document.getText(), 'call1');
				subscription.dispose();
482
				registration.dispose();
J
Johannes Rieken 已提交
483
				resolve();
484
			});
J
Johannes Rieken 已提交
485 486

			emitter.fire(doc.uri);
487 488 489
		});
	});

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

497
	test('findFiles, cancellation', () => {
498

499 500 501
		const source = new vscode.CancellationTokenSource();
		const token = source.token; // just to get an instance first
		source.cancel();
502

503 504 505 506
		return vscode.workspace.findFiles('*.js', null, 100, token).then((res) => {
			assert.deepEqual(res, []);
		});
	});
507

R
Rob Lourens 已提交
508
	test('findTextInFiles', async () => {
509 510 511
		const options: vscode.FindTextInFilesOptions = {
			include: '*.ts',
			previewOptions: {
R
Rob Lourens 已提交
512 513
				matchLines: 1,
				charsPerLine: 100
514 515 516
			}
		};

517
		const results: vscode.TextSearchResult[] = [];
518
		await vscode.workspace.findTextInFiles({ pattern: 'foo' }, options, result => {
519 520 521 522
			results.push(result);
		});

		assert.equal(results.length, 1);
R
Rob Lourens 已提交
523 524 525
		const match = <vscode.TextSearchMatch>results[0];
		assert(match.preview.text.indexOf('foo') >= 0);
		assert.equal(vscode.workspace.asRelativePath(match.uri), '10linefile.ts');
526 527
	});

528 529 530 531 532 533 534 535 536 537
	test('findTextInFiles, cancellation', async () => {
		const results: vscode.TextSearchResult[] = [];
		const cancellation = new vscode.CancellationTokenSource();
		cancellation.cancel();

		await vscode.workspace.findTextInFiles({ pattern: 'foo' }, result => {
			results.push(result);
		}, cancellation.token);
	});

538
	test('applyEdit', () => {
539

540 541 542 543 544 545
		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);
		});
	});
546

547 548
	test('applyEdit should fail when editing deleted resource', async () => {
		const resource = await createRandomFile();
549

550 551 552
		const edit = new vscode.WorkspaceEdit();
		edit.deleteFile(resource);
		edit.insert(resource, new vscode.Position(0, 0), '');
M
Matt Bierner 已提交
553

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

558 559 560 561 562 563
	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 已提交
564

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
		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);
	});

580 581 582 583 584 585 586
	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');
587

588 589
		let success = await vscode.workspace.applyEdit(edit);
		assert.equal(success, true);
590

B
Benjamin Pasero 已提交
591 592
		let doc = await vscode.workspace.openTextDocument(newUri);
		assert.equal(doc.getText(), 'AFTERBEFORE');
593
	});
594 595

	function nameWithUnderscore(uri: vscode.Uri) {
J
Johannes Rieken 已提交
596
		return uri.with({ path: posix.join(posix.dirname(uri.path), `_${posix.basename(uri.path)}`) });
597 598 599 600 601 602 603 604 605 606 607 608 609
	}

	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));
B
Benjamin Pasero 已提交
610 611
		let doc = await vscode.workspace.openTextDocument(newUri);
		assert.equal(doc.getText(), 'BarHelloFoo');
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
	});

	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));

B
Benjamin Pasero 已提交
628 629
		let newDoc = await vscode.workspace.openTextDocument(newUri);
		assert.equal(newDoc.getText(), 'HelloFoo');
B
Benjamin Pasero 已提交
630 631
		let doc = await vscode.workspace.openTextDocument(docUri);
		assert.equal(doc.getText(), 'Bar');
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
	});

	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);
J
Johannes Rieken 已提交
657
		let docParent = docUri.with({ path: posix.dirname(docUri.path) });
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
		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);
		}

J
Johannes Rieken 已提交
673
		let newUri = newParent.with({ path: posix.join(newParent.path, posix.basename(docUri.path)) });
674 675 676
		let doc = await vscode.workspace.openTextDocument(newUri);
		assert.ok(doc);

B
Benjamin Pasero 已提交
677
		assert.equal(doc.getText(), 'Hello');
678
	});
J
Johannes Rieken 已提交
679 680 681 682 683 684 685 686 687 688 689 690 691 692

	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');
	});
J
Johannes Rieken 已提交
693 694 695 696 697 698 699 700 701 702 703

	test('WorkspaceEdit: create & override', async function () {

		let docUri = await createRandomFile('before');

		let we = new vscode.WorkspaceEdit();
		we.createFile(docUri);
		assert.ok(!await vscode.workspace.applyEdit(we));
		assert.equal((await vscode.workspace.openTextDocument(docUri)).getText(), 'before');

		we = new vscode.WorkspaceEdit();
J
Johannes Rieken 已提交
704
		we.createFile(docUri, { overwrite: true });
J
Johannes Rieken 已提交
705
		assert.ok(await vscode.workspace.applyEdit(we));
B
Benjamin Pasero 已提交
706
		assert.equal((await vscode.workspace.openTextDocument(docUri)).getText(), '');
J
Johannes Rieken 已提交
707
	});
708 709 710

	test('WorkspaceEdit: create & ignoreIfExists', async function () {
		let docUri = await createRandomFile('before');
J
Johannes Rieken 已提交
711

712 713 714 715
		let we = new vscode.WorkspaceEdit();
		we.createFile(docUri, { ignoreIfExists: true });
		assert.ok(await vscode.workspace.applyEdit(we));
		assert.equal((await vscode.workspace.openTextDocument(docUri)).getText(), 'before');
J
Johannes Rieken 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757

		we = new vscode.WorkspaceEdit();
		we.createFile(docUri, { overwrite: true, ignoreIfExists: true });
		assert.ok(await vscode.workspace.applyEdit(we));
		assert.equal((await vscode.workspace.openTextDocument(docUri)).getText(), '');
	});

	test('WorkspaceEdit: rename & ignoreIfExists', async function () {
		let aUri = await createRandomFile('aaa');
		let bUri = await createRandomFile('bbb');

		let we = new vscode.WorkspaceEdit();
		we.renameFile(aUri, bUri);
		assert.ok(!await vscode.workspace.applyEdit(we));

		we = new vscode.WorkspaceEdit();
		we.renameFile(aUri, bUri, { ignoreIfExists: true });
		assert.ok(await vscode.workspace.applyEdit(we));

		we = new vscode.WorkspaceEdit();
		we.renameFile(aUri, bUri, { overwrite: false, ignoreIfExists: true });
		assert.ok(!await vscode.workspace.applyEdit(we));

		we = new vscode.WorkspaceEdit();
		we.renameFile(aUri, bUri, { overwrite: true, ignoreIfExists: true });
		assert.ok(await vscode.workspace.applyEdit(we));
	});

	test('WorkspaceEdit: delete & ignoreIfNotExists', async function () {

		let docUri = await createRandomFile();
		let we = new vscode.WorkspaceEdit();
		we.deleteFile(docUri, { ignoreIfNotExists: false });
		assert.ok(await vscode.workspace.applyEdit(we));

		we = new vscode.WorkspaceEdit();
		we.deleteFile(docUri, { ignoreIfNotExists: false });
		assert.ok(!await vscode.workspace.applyEdit(we));

		we = new vscode.WorkspaceEdit();
		we.deleteFile(docUri, { ignoreIfNotExists: true });
		assert.ok(await vscode.workspace.applyEdit(we));
758
	});
759
});