workspace.test.ts 30.8 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';
8
import { createRandomFile, deleteFile, closeAllEditors, pathEquals, rndName, disposeAll, testFs, delay, withLogDisabled } from '../utils';
J
Johannes Rieken 已提交
9
import { join, posix, basename } from 'path';
10
import * as fs from 'fs';
B
Benjamin Pasero 已提交
11

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

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

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

31

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

	test('rootPath', () => {
38
		assert.ok(pathEquals(vscode.workspace.rootPath!, join(__dirname, '../../testWorkspace')));
39
		assert.throws(() => (vscode.workspace as any).rootPath = 'farboo');
E
Erich Gamma 已提交
40 41
	});

42 43 44 45
	test('workspaceFile', () => {
		assert.ok(!vscode.workspace.workspaceFile);
	});

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

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

78 79
	test('openTextDocument, untitled is dirty', async function () {
		return vscode.workspace.openTextDocument(vscode.workspace.workspaceFolders![0].uri.with({ scheme: 'untitled', path: posix.join(vscode.workspace.workspaceFolders![0].uri.path, 'newfile.txt') })).then(doc => {
80 81 82 83
			assert.equal(doc.uri.scheme, 'untitled');
			assert.ok(doc.isDirty);
		});
	});
84

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

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

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

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

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

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

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

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

132
					d0.dispose();
133
					fs.unlinkSync(join(vscode.workspace.rootPath || '', './newfile.txt'));
134 135
				});
			});
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

217 218 219 220 221 222
	test('eol, change via onWillSave', function () {
		if (vscode.env.uiKind === vscode.UIKind.Web) {
			// TODO@Jo Test seems to fail when running in web due to
			// onWillSaveTextDocument not getting called
			return this.skip();
		}
223

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

J
Johannes Rieken 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
		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 已提交
250

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

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

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

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

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

M
Matt Bierner 已提交
283
							disposeAll(disposables);
284

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

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	test('events: onDidSaveTextDocument fires even for non dirty file when saved', () => {
		return createRandomFile().then(file => {
			let disposables: vscode.Disposable[] = [];

			let onDidSaveTextDocument = false;
			disposables.push(vscode.workspace.onDidSaveTextDocument(e => {
				assert.ok(pathEquals(e.uri.fsPath, file.fsPath));
				onDidSaveTextDocument = true;
			}));

			return vscode.workspace.openTextDocument(file).then(doc => {
				return vscode.window.showTextDocument(doc).then(() => {
					return vscode.commands.executeCommand('workbench.action.files.save').then(() => {
						assert.ok(onDidSaveTextDocument);

						disposeAll(disposables);

						return deleteFile(file);
					});
				});
			});
		});
	});

317 318 319 320 321 322 323 324 325 326 327 328 329
	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 已提交
330
	test('registerTextDocumentContentProvider, simple', function () {
331

J
Johannes Rieken 已提交
332
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
333
			provideTextDocumentContent(uri) {
334 335 336 337
				return uri.toString();
			}
		});

J
Johannes Rieken 已提交
338 339
		const uri = vscode.Uri.parse('foo://testing/virtual.js');
		return vscode.workspace.openTextDocument(uri).then(doc => {
340 341 342 343 344 345 346
			assert.equal(doc.getText(), uri.toString());
			assert.equal(doc.isDirty, false);
			assert.equal(doc.uri.toString(), uri.toString());
			registration.dispose();
		});
	});

B
Benjamin Pasero 已提交
347
	test('registerTextDocumentContentProvider, constrains', function () {
348 349

		// built-in
B
Benjamin Pasero 已提交
350
		assert.throws(function () {
J
Johannes Rieken 已提交
351
			vscode.workspace.registerTextDocumentContentProvider('untitled', { provideTextDocumentContent() { return null; } });
352 353
		});
		// built-in
B
Benjamin Pasero 已提交
354
		assert.throws(function () {
J
Johannes Rieken 已提交
355
			vscode.workspace.registerTextDocumentContentProvider('file', { provideTextDocumentContent() { return null; } });
356 357
		});

358
		// missing scheme
J
Johannes Rieken 已提交
359
		return vscode.workspace.openTextDocument(vscode.Uri.parse('notThere://foo/far/boo/bar')).then(() => {
B
Benjamin Pasero 已提交
360
			assert.ok(false, 'expected failure');
361
		}, _err => {
362
			// expected
B
Benjamin Pasero 已提交
363
		});
364 365
	});

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

368
		// duplicate registration
J
Johannes Rieken 已提交
369
		let registration1 = vscode.workspace.registerTextDocumentContentProvider('foo', {
370
			provideTextDocumentContent(uri) {
371
				if (uri.authority === 'foo') {
B
Benjamin Pasero 已提交
372
					return '1';
373
				}
374
				return undefined;
375 376
			}
		});
J
Johannes Rieken 已提交
377
		let registration2 = vscode.workspace.registerTextDocumentContentProvider('foo', {
378 379
			provideTextDocumentContent(uri) {
				if (uri.authority === 'bar') {
B
Benjamin Pasero 已提交
380
					return '2';
381
				}
382
				return undefined;
383
			}
384 385
		});

386
		return Promise.all([
J
Johannes Rieken 已提交
387 388
			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'); })
389 390 391 392 393
		]).then(() => {
			registration1.dispose();
			registration2.dispose();
		});
	});
394

B
Benjamin Pasero 已提交
395
	test('registerTextDocumentContentProvider, evil provider', function () {
396 397

		// duplicate registration
J
Johannes Rieken 已提交
398
		let registration1 = vscode.workspace.registerTextDocumentContentProvider('foo', {
399
			provideTextDocumentContent(_uri) {
400 401 402
				return '1';
			}
		});
J
Johannes Rieken 已提交
403
		let registration2 = vscode.workspace.registerTextDocumentContentProvider('foo', {
404
			provideTextDocumentContent(_uri): string {
B
Benjamin Pasero 已提交
405
				throw new Error('fail');
406 407 408
			}
		});

J
Johannes Rieken 已提交
409
		return vscode.workspace.openTextDocument(vscode.Uri.parse('foo://foo/bla')).then(doc => {
410 411 412 413 414 415
			assert.equal(doc.getText(), '1');
			registration1.dispose();
			registration2.dispose();
		});
	});

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

J
Johannes Rieken 已提交
418
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
419
			provideTextDocumentContent(_uri) {
B
Benjamin Pasero 已提交
420
				return <any>123;
421 422
			}
		});
J
Johannes Rieken 已提交
423
		return vscode.workspace.openTextDocument(vscode.Uri.parse('foo://auth/path')).then(() => {
B
Benjamin Pasero 已提交
424
			assert.ok(false, 'expected failure');
425
		}, _err => {
426
			// expected
427 428
			registration.dispose();
		});
429 430
	});

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

J
Johannes Rieken 已提交
433
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
434
			provideTextDocumentContent(_uri) {
435 436 437 438
				return 'I am virtual';
			}
		});

J
Johannes Rieken 已提交
439 440
		return vscode.workspace.openTextDocument(vscode.Uri.parse('foo://something/path')).then(doc => {
			return vscode.window.showTextDocument(doc).then(editor => {
441 442 443 444

				assert.ok(editor.document === doc);
				assert.equal(editor.document.getText(), 'I am virtual');
				registration.dispose();
B
Benjamin Pasero 已提交
445
			});
446 447
		});
	});
448

B
Benjamin Pasero 已提交
449
	test('registerTextDocumentContentProvider, open/open document', function () {
450 451

		let callCount = 0;
J
Johannes Rieken 已提交
452
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
453
			provideTextDocumentContent(_uri) {
454 455 456 457 458
				callCount += 1;
				return 'I am virtual';
			}
		});

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

J
Johannes Rieken 已提交
461
		return Promise.all([vscode.workspace.openTextDocument(uri), vscode.workspace.openTextDocument(uri)]).then(docs => {
462 463
			let [first, second] = docs;
			assert.ok(first === second);
J
Johannes Rieken 已提交
464
			assert.ok(vscode.workspace.textDocuments.some(doc => doc.uri.toString() === uri.toString()));
465 466 467 468 469
			assert.equal(callCount, 1);
			registration.dispose();
		});
	});

470 471
	test('registerTextDocumentContentProvider, empty doc', function () {

J
Johannes Rieken 已提交
472
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
473
			provideTextDocumentContent(_uri) {
474 475 476 477
				return '';
			}
		});

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

J
Johannes Rieken 已提交
480
		return vscode.workspace.openTextDocument(uri).then(doc => {
481 482 483 484 485 486
			assert.equal(doc.getText(), '');
			assert.equal(doc.uri.toString(), uri.toString());
			registration.dispose();
		});
	});

J
Johannes Rieken 已提交
487
	test('registerTextDocumentContentProvider, change event', async function () {
488 489

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

J
Johannes Rieken 已提交
492
		let registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
493
			onDidChange: emitter.event,
494
			provideTextDocumentContent(_uri) {
495 496 497 498
				return 'call' + (callCount++);
			}
		});

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

J
Johannes Rieken 已提交
502 503
		assert.equal(callCount, 1);
		assert.equal(doc.getText(), 'call0');
504

J
Johannes Rieken 已提交
505
		return new Promise(resolve => {
506

J
Johannes Rieken 已提交
507 508 509 510
			let subscription = vscode.workspace.onDidChangeTextDocument(event => {
				assert.ok(event.document === doc);
				assert.equal(event.document.getText(), 'call1');
				subscription.dispose();
511
				registration.dispose();
J
Johannes Rieken 已提交
512
				resolve();
513
			});
J
Johannes Rieken 已提交
514 515

			emitter.fire(doc.uri);
516 517 518
		});
	});

B
Benjamin Pasero 已提交
519
	test('findFiles', () => {
R
Rob Lourens 已提交
520 521 522
		return vscode.workspace.findFiles('**/*.png').then((res) => {
			assert.equal(res.length, 2);
			assert.equal(basename(vscode.workspace.asRelativePath(res[0])), 'image.png');
R
Rob Lourens 已提交
523 524 525
		});
	});

526 527 528 529 530 531 532 533 534 535 536 537 538 539
	test('findFiles - null exclude', async () => {
		await vscode.workspace.findFiles('**/file.txt').then((res) => {
			// search.exclude folder is still searched, files.exclude folder is not
			assert.equal(res.length, 1);
			assert.equal(basename(vscode.workspace.asRelativePath(res[0])), 'file.txt');
		});

		await vscode.workspace.findFiles('**/file.txt', null).then((res) => {
			// search.exclude and files.exclude folders are both searched
			assert.equal(res.length, 2);
			assert.equal(basename(vscode.workspace.asRelativePath(res[0])), 'file.txt');
		});
	});

B
Benjamin Pasero 已提交
540
	test('findFiles - exclude', () => {
R
Rob Lourens 已提交
541 542 543
		return vscode.workspace.findFiles('**/*.png').then((res) => {
			assert.equal(res.length, 2);
			assert.equal(basename(vscode.workspace.asRelativePath(res[0])), 'image.png');
R
Rob Lourens 已提交
544 545 546
		});
	});

B
Benjamin Pasero 已提交
547
	test('findFiles, exclude', () => {
R
Rob Lourens 已提交
548
		return vscode.workspace.findFiles('**/*.png', '**/sub/**').then((res) => {
B
Benjamin Pasero 已提交
549
			assert.equal(res.length, 1);
550
			assert.equal(basename(vscode.workspace.asRelativePath(res[0])), 'image.png');
B
Benjamin Pasero 已提交
551
		});
B
Benjamin Pasero 已提交
552
	});
553

B
Benjamin Pasero 已提交
554
	test('findFiles, cancellation', () => {
555

556 557 558
		const source = new vscode.CancellationTokenSource();
		const token = source.token; // just to get an instance first
		source.cancel();
559

560 561 562 563
		return vscode.workspace.findFiles('*.js', null, 100, token).then((res) => {
			assert.deepEqual(res, []);
		});
	});
564

B
Benjamin Pasero 已提交
565
	test('findTextInFiles', async () => {
566 567 568
		const options: vscode.FindTextInFilesOptions = {
			include: '*.ts',
			previewOptions: {
R
Rob Lourens 已提交
569 570
				matchLines: 1,
				charsPerLine: 100
571 572 573
			}
		};

574
		const results: vscode.TextSearchResult[] = [];
575
		await vscode.workspace.findTextInFiles({ pattern: 'foo' }, options, result => {
576 577 578 579
			results.push(result);
		});

		assert.equal(results.length, 1);
R
Rob Lourens 已提交
580 581 582
		const match = <vscode.TextSearchMatch>results[0];
		assert(match.preview.text.indexOf('foo') >= 0);
		assert.equal(vscode.workspace.asRelativePath(match.uri), '10linefile.ts');
583 584
	});

B
Benjamin Pasero 已提交
585
	test('findTextInFiles, cancellation', async () => {
586 587 588 589 590 591 592 593 594
		const results: vscode.TextSearchResult[] = [];
		const cancellation = new vscode.CancellationTokenSource();
		cancellation.cancel();

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

B
Benjamin Pasero 已提交
595 596
	test('applyEdit', async () => {
		const doc = await vscode.workspace.openTextDocument(vscode.Uri.parse('untitled:' + join(vscode.workspace.rootPath || '', './new2.txt')));
597

B
Benjamin Pasero 已提交
598 599 600 601 602 603
		let edit = new vscode.WorkspaceEdit();
		edit.insert(doc.uri, new vscode.Position(0, 0), new Array(1000).join('Hello World'));

		let success = await vscode.workspace.applyEdit(edit);
		assert.equal(success, true);
		assert.equal(doc.isDirty, true);
604
	});
605

606
	test('applyEdit should fail when editing deleted resource', withLogDisabled(async () => {
607
		const resource = await createRandomFile();
608

609 610 611
		const edit = new vscode.WorkspaceEdit();
		edit.deleteFile(resource);
		edit.insert(resource, new vscode.Position(0, 0), '');
M
Matt Bierner 已提交
612

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

617
	test('applyEdit should fail when renaming deleted resource', withLogDisabled(async () => {
618 619 620 621 622
		const resource = await createRandomFile();

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

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

628
	test('applyEdit should fail when editing renamed from resource', withLogDisabled(async () => {
629
		const resource = await createRandomFile();
J
Johannes Rieken 已提交
630
		const newResource = vscode.Uri.file(resource.fsPath + '.1');
631 632 633 634 635 636
		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);
637
	}));
638

639
	test('applyEdit "edit A -> rename A to B -> edit B"', async () => {
B
Benjamin Pasero 已提交
640 641 642 643 644 645 646 647 648 649 650 651
		await testEditRenameEdit(oldUri => oldUri.with({ path: oldUri.path + 'NEW' }));
	});

	test('applyEdit "edit A -> rename A to B (different case)" -> edit B', async () => {
		await testEditRenameEdit(oldUri => oldUri.with({ path: oldUri.path.toUpperCase() }));
	});

	test('applyEdit "edit A -> rename A to B (same case)" -> edit B', async () => {
		await testEditRenameEdit(oldUri => oldUri);
	});

	async function testEditRenameEdit(newUriCreator: (oldUri: vscode.Uri) => vscode.Uri): Promise<void> {
652
		const oldUri = await createRandomFile();
B
Benjamin Pasero 已提交
653
		const newUri = newUriCreator(oldUri);
654 655 656 657
		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');
658

B
Benjamin Pasero 已提交
659
		assert.ok(await vscode.workspace.applyEdit(edit));
660

B
Benjamin Pasero 已提交
661 662
		let doc = await vscode.workspace.openTextDocument(newUri);
		assert.equal(doc.getText(), 'AFTERBEFORE');
B
Benjamin Pasero 已提交
663 664
		assert.equal(doc.isDirty, true);
	}
665 666

	function nameWithUnderscore(uri: vscode.Uri) {
J
Johannes Rieken 已提交
667
		return uri.with({ path: posix.join(posix.dirname(uri.path), `_${posix.basename(uri.path)}`) });
668 669
	}

670
	test('WorkspaceEdit: applying edits before and after rename duplicates resource #42633', withLogDisabled(async function () {
671 672 673 674 675 676 677 678 679 680
		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 已提交
681 682
		let doc = await vscode.workspace.openTextDocument(newUri);
		assert.equal(doc.getText(), 'BarHelloFoo');
683
	}));
684

685
	test('WorkspaceEdit: Problem recreating a renamed resource #42634', withLogDisabled(async function () {
686 687 688 689 690 691 692 693 694 695 696 697 698
		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 已提交
699 700
		let newDoc = await vscode.workspace.openTextDocument(newUri);
		assert.equal(newDoc.getText(), 'HelloFoo');
B
Benjamin Pasero 已提交
701 702
		let doc = await vscode.workspace.openTextDocument(docUri);
		assert.equal(doc.getText(), 'Bar');
703
	}));
704

705
	test('WorkspaceEdit api - after saving a deleted file, it still shows up as deleted. #42667', withLogDisabled(async function () {
706 707 708 709 710 711 712 713 714 715 716 717
		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);
		}
718
	}));
719 720 721

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

722 723
		let dir = vscode.Uri.parse(`${testFs.scheme}:/before-${rndName()}`);
		await testFs.createDirectory(dir);
724 725

		let docUri = await createRandomFile('', dir);
J
Johannes Rieken 已提交
726
		let docParent = docUri.with({ path: posix.dirname(docUri.path) });
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
		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 已提交
742
		let newUri = newParent.with({ path: posix.join(newParent.path, posix.basename(docUri.path)) });
743 744 745
		let doc = await vscode.workspace.openTextDocument(newUri);
		assert.ok(doc);

B
Benjamin Pasero 已提交
746
		assert.equal(doc.getText(), 'Hello');
747
	});
J
Johannes Rieken 已提交
748

749
	test('WorkspaceEdit: rename resource followed by edit does not work #42638', withLogDisabled(async function () {
J
Johannes Rieken 已提交
750 751 752 753 754 755 756 757 758 759 760
		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');
761
	}));
J
Johannes Rieken 已提交
762

763
	test('WorkspaceEdit: create & override', withLogDisabled(async function () {
J
Johannes Rieken 已提交
764 765 766 767 768 769 770 771 772

		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 已提交
773
		we.createFile(docUri, { overwrite: true });
J
Johannes Rieken 已提交
774
		assert.ok(await vscode.workspace.applyEdit(we));
B
Benjamin Pasero 已提交
775
		assert.equal((await vscode.workspace.openTextDocument(docUri)).getText(), '');
776
	}));
777

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

781 782 783 784
		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 已提交
785 786 787 788 789

		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(), '');
790
	}));
J
Johannes Rieken 已提交
791

792
	test('WorkspaceEdit: rename & ignoreIfExists', withLogDisabled(async function () {
J
Johannes Rieken 已提交
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
		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));
811
	}));
J
Johannes Rieken 已提交
812

813
	test('WorkspaceEdit: delete & ignoreIfNotExists', withLogDisabled(async function () {
J
Johannes Rieken 已提交
814 815 816 817 818 819 820 821 822 823 824 825 826

		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));
827
	}));
J
Johannes Rieken 已提交
828

B
Benjamin Pasero 已提交
829
	test('WorkspaceEdit: insert & rename multiple', async function () {
J
Johannes Rieken 已提交
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852

		let [f1, f2, f3] = await Promise.all([createRandomFile(), createRandomFile(), createRandomFile()]);

		let we = new vscode.WorkspaceEdit();
		we.insert(f1, new vscode.Position(0, 0), 'f1');
		we.insert(f2, new vscode.Position(0, 0), 'f2');
		we.insert(f3, new vscode.Position(0, 0), 'f3');

		let f1_ = nameWithUnderscore(f1);
		we.renameFile(f1, f1_);

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

		assert.equal((await vscode.workspace.openTextDocument(f3)).getText(), 'f3');
		assert.equal((await vscode.workspace.openTextDocument(f2)).getText(), 'f2');
		assert.equal((await vscode.workspace.openTextDocument(f1_)).getText(), 'f1');
		try {
			await vscode.workspace.fs.stat(f1);
			assert.ok(false);
		} catch {
			assert.ok(true);
		}
	});
B
Benjamin Pasero 已提交
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904

	test('workspace.applyEdit drops the TextEdit if there is a RenameFile later #77735 (with opened editor)', async function () {
		await test77735(true);
	});

	test('workspace.applyEdit drops the TextEdit if there is a RenameFile later #77735 (without opened editor)', async function () {
		await test77735(false);
	});

	async function test77735(withOpenedEditor: boolean): Promise<void> {
		const docUriOriginal = await createRandomFile();
		const docUriMoved = docUriOriginal.with({ path: `${docUriOriginal.path}.moved` });

		if (withOpenedEditor) {
			const document = await vscode.workspace.openTextDocument(docUriOriginal);
			await vscode.window.showTextDocument(document);
		} else {
			await vscode.commands.executeCommand('workbench.action.closeAllEditors');
		}

		for (let i = 0; i < 4; i++) {
			let we = new vscode.WorkspaceEdit();
			let oldUri: vscode.Uri;
			let newUri: vscode.Uri;
			let expected: string;

			if (i % 2 === 0) {
				oldUri = docUriOriginal;
				newUri = docUriMoved;
				we.insert(oldUri, new vscode.Position(0, 0), 'Hello');
				expected = 'Hello';
			} else {
				oldUri = docUriMoved;
				newUri = docUriOriginal;
				we.delete(oldUri, new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 5)));
				expected = '';
			}

			we.renameFile(oldUri, newUri);
			assert.ok(await vscode.workspace.applyEdit(we));

			const document = await vscode.workspace.openTextDocument(newUri);
			assert.equal(document.isDirty, true);

			await document.save();
			assert.equal(document.isDirty, false);

			assert.equal(document.getText(), expected);

			await delay(10);
		}
	}
J
Johannes Rieken 已提交
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923

	test('The api workspace.applyEdit failed for some case of mixing resourceChange and textEdit #80688', async function () {
		const file1 = await createRandomFile();
		const file2 = await createRandomFile();
		let we = new vscode.WorkspaceEdit();
		we.insert(file1, new vscode.Position(0, 0), 'import1;');

		const file2Name = basename(file2.fsPath);
		const file2NewUri = vscode.Uri.parse(file2.toString().replace(file2Name, `new/${file2Name}`));
		we.renameFile(file2, file2NewUri);

		we.insert(file1, new vscode.Position(0, 0), 'import2;');
		await vscode.workspace.applyEdit(we);

		const document = await vscode.workspace.openTextDocument(file1);
		// const expected = 'import1;import2;';
		const expected2 = 'import2;import1;';
		assert.equal(document.getText(), expected2);
	});
J
Johannes Rieken 已提交
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942

	test('The api workspace.applyEdit failed for some case of mixing resourceChange and textEdit #80688', async function () {
		const file1 = await createRandomFile();
		const file2 = await createRandomFile();
		let we = new vscode.WorkspaceEdit();
		we.insert(file1, new vscode.Position(0, 0), 'import1;');
		we.insert(file1, new vscode.Position(0, 0), 'import2;');

		const file2Name = basename(file2.fsPath);
		const file2NewUri = vscode.Uri.parse(file2.toString().replace(file2Name, `new/${file2Name}`));
		we.renameFile(file2, file2NewUri);

		await vscode.workspace.applyEdit(we);

		const document = await vscode.workspace.openTextDocument(file1);
		const expected = 'import1;import2;';
		// const expected2 = 'import2;import1;';
		assert.equal(document.getText(), expected);
	});
943
});