workspace.test.ts 26.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';
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', () => {
39
		assert.ok(pathEquals(vscode.workspace.rootPath!, join(__dirname, '../../testWorkspace')));
40
		assert.throws(() => (vscode.workspace as any).rootPath = 'farboo');
E
Erich Gamma 已提交
41 42
	});

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

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', () => {
72
		return vscode.workspace.openTextDocument('funkydonky.txt').then(_doc => {
73
			throw new Error('missing error');
74
		}, _err => {
75
			// good!
E
Erich Gamma 已提交
76 77 78
		});
	});

79
	test('openTextDocument, untitled is dirty', function () {
M
Martin Aeschlimann 已提交
80
		return vscode.workspace.openTextDocument(vscode.Uri.parse('untitled:' + join(vscode.workspace.workspaceFolders![0].uri.toString() || '', './newfile.txt'))).then(doc => {
81 82 83 84
			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');
274 275
					}).then(_applied => {
						return doc.save().then(_saved => {
B
Benjamin Pasero 已提交
276 277 278 279
							assert.ok(onDidOpenTextDocument);
							assert.ok(onDidChangeTextDocument);
							assert.ok(onDidSaveTextDocument);

M
Matt Bierner 已提交
280
							disposeAll(disposables);
281

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

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
	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);
					});
				});
			});
		});
	});

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

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

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

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

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

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

B
Benjamin Pasero 已提交
363
	test('registerTextDocumentContentProvider, multiple', function () {
364

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

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

B
Benjamin Pasero 已提交
392
	test('registerTextDocumentContentProvider, evil provider', function () {
393 394

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

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

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

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

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

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

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

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

B
Benjamin Pasero 已提交
446
	test('registerTextDocumentContentProvider, open/open document', function () {
447 448

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

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

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

467 468
	test('registerTextDocumentContentProvider, empty doc', function () {

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

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

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

J
Johannes Rieken 已提交
484
	test('registerTextDocumentContentProvider, change event', async function () {
485 486

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

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

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

J
Johannes Rieken 已提交
499 500
		assert.equal(callCount, 1);
		assert.equal(doc.getText(), 'call0');
501

J
Johannes Rieken 已提交
502
		return new Promise(resolve => {
503

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

			emitter.fire(doc.uri);
513 514 515
		});
	});

B
Benjamin Pasero 已提交
516
	test('findFiles', () => {
R
Rob Lourens 已提交
517 518 519
		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 已提交
520 521 522
		});
	});

523 524 525 526 527 528 529 530 531 532 533 534 535 536
	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 已提交
537
	test('findFiles - exclude', () => {
R
Rob Lourens 已提交
538 539 540
		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 已提交
541 542 543
		});
	});

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

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

553 554 555
		const source = new vscode.CancellationTokenSource();
		const token = source.token; // just to get an instance first
		source.cancel();
556

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

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

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

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

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

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

592
	test('applyEdit', () => {
593

594 595 596 597 598 599
		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);
		});
	});
600

601 602
	test('applyEdit should fail when editing deleted resource', async () => {
		const resource = await createRandomFile();
603

604 605 606
		const edit = new vscode.WorkspaceEdit();
		edit.deleteFile(resource);
		edit.insert(resource, new vscode.Position(0, 0), '');
M
Matt Bierner 已提交
607

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

612 613 614 615 616 617
	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 已提交
618

619 620 621 622 623 624
		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();
J
Johannes Rieken 已提交
625
		const newResource = vscode.Uri.file(resource.fsPath + '.1');
626 627 628 629 630 631 632 633
		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);
	});

634 635 636 637 638 639 640
	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');
641

642 643
		let success = await vscode.workspace.applyEdit(edit);
		assert.equal(success, true);
644

B
Benjamin Pasero 已提交
645 646
		let doc = await vscode.workspace.openTextDocument(newUri);
		assert.equal(doc.getText(), 'AFTERBEFORE');
647
	});
648 649

	function nameWithUnderscore(uri: vscode.Uri) {
J
Johannes Rieken 已提交
650
		return uri.with({ path: posix.join(posix.dirname(uri.path), `_${posix.basename(uri.path)}`) });
651 652 653 654 655 656 657 658 659 660 661 662 663
	}

	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 已提交
664 665
		let doc = await vscode.workspace.openTextDocument(newUri);
		assert.equal(doc.getText(), 'BarHelloFoo');
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
	});

	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 已提交
682 683
		let newDoc = await vscode.workspace.openTextDocument(newUri);
		assert.equal(newDoc.getText(), 'HelloFoo');
B
Benjamin Pasero 已提交
684 685
		let doc = await vscode.workspace.openTextDocument(docUri);
		assert.equal(doc.getText(), 'Bar');
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
	});

	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 已提交
711
		let docParent = docUri.with({ path: posix.dirname(docUri.path) });
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
		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 已提交
727
		let newUri = newParent.with({ path: posix.join(newParent.path, posix.basename(docUri.path)) });
728 729 730
		let doc = await vscode.workspace.openTextDocument(newUri);
		assert.ok(doc);

B
Benjamin Pasero 已提交
731
		assert.equal(doc.getText(), 'Hello');
732
	});
J
Johannes Rieken 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746

	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 已提交
747 748 749 750 751 752 753 754 755 756 757

	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 已提交
758
		we.createFile(docUri, { overwrite: true });
J
Johannes Rieken 已提交
759
		assert.ok(await vscode.workspace.applyEdit(we));
B
Benjamin Pasero 已提交
760
		assert.equal((await vscode.workspace.openTextDocument(docUri)).getText(), '');
J
Johannes Rieken 已提交
761
	});
762 763 764

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

766 767 768 769
		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 已提交
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811

		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));
812
	});
J
Johannes Rieken 已提交
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837

	test('The api workspace.applyEdit drops the TextEdit if there is a RenameFile later #77735', async function () {

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