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

'use strict';

import path = require('path');
import assert = require('assert');

B
Benjamin Pasero 已提交
11
import {join, normalize} from 'vs/base/common/paths';
E
Erich Gamma 已提交
12 13 14
import {LineMatch} from 'vs/platform/search/common/search';

import {FileWalker, Engine as FileSearchEngine} from 'vs/workbench/services/search/node/fileSearch';
15
import {IRawFileMatch} from 'vs/workbench/services/search/node/search';
E
Erich Gamma 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
import {Engine as TextSearchEngine} from 'vs/workbench/services/search/node/textSearch';

function count(lineMatches: LineMatch[]): number {
	let count = 0;
	if (lineMatches) {
		for (let i = 0; i < lineMatches.length; i++) {
			let line = lineMatches[i];
			let wordMatches = line.offsetAndLengths;
			count += wordMatches.length;
		}
	}

	return count;
}

31
function rootfolders() {
B
Benjamin Pasero 已提交
32 33 34
	return [path.normalize(require.toUrl('./fixtures'))];
}

E
Erich Gamma 已提交
35 36
suite('Search', () => {

B
Benjamin Pasero 已提交
37
	test('Files: *.js', function (done: () => void) {
E
Erich Gamma 已提交
38
		let engine = new FileSearchEngine({
39
			rootFolders: rootfolders(),
40
			filePattern: '*.js'
E
Erich Gamma 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54
		});

		let count = 0;
		engine.search((result) => {
			if (result) {
				count++;
			}
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(count, 4);
			done();
		});
	});

B
Benjamin Pasero 已提交
55
	test('Files: examples/com*', function (done: () => void) {
56
		let engine = new FileSearchEngine({
57
			rootFolders: rootfolders(),
B
Benjamin Pasero 已提交
58
			filePattern: normalize(join('examples', 'com*'), true)
59 60 61 62 63 64 65 66 67 68 69 70 71 72
		});

		let count = 0;
		engine.search((result) => {
			if (result) {
				count++;
			}
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(count, 1);
			done();
		});
	});

B
Benjamin Pasero 已提交
73
	test('Files: examples (fuzzy)', function (done: () => void) {
74
		let engine = new FileSearchEngine({
75
			rootFolders: rootfolders(),
76
			filePattern: 'xl'
77 78 79 80 81 82 83 84 85
		});

		let count = 0;
		engine.search((result) => {
			if (result) {
				count++;
			}
		}, () => { }, (error) => {
			assert.ok(!error);
B
Benjamin Pasero 已提交
86
			assert.equal(count, 6);
87 88 89 90
			done();
		});
	});

B
Benjamin Pasero 已提交
91
	test('Files: NPE (CamelCase)', function (done: () => void) {
E
Erich Gamma 已提交
92
		let engine = new FileSearchEngine({
93
			rootFolders: rootfolders(),
94
			filePattern: 'NullPE'
E
Erich Gamma 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108
		});

		let count = 0;
		engine.search((result) => {
			if (result) {
				count++;
			}
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(count, 1);
			done();
		});
	});

B
Benjamin Pasero 已提交
109
	test('Files: *.*', function (done: () => void) {
E
Erich Gamma 已提交
110
		let engine = new FileSearchEngine({
111
			rootFolders: rootfolders(),
112
			filePattern: '*.*'
E
Erich Gamma 已提交
113 114 115 116 117 118 119 120 121
		});

		let count = 0;
		engine.search((result) => {
			if (result) {
				count++;
			}
		}, () => { }, (error) => {
			assert.ok(!error);
122
			assert.equal(count, 12);
E
Erich Gamma 已提交
123 124 125 126
			done();
		});
	});

B
Benjamin Pasero 已提交
127
	test('Files: *.as', function (done: () => void) {
E
Erich Gamma 已提交
128
		let engine = new FileSearchEngine({
129
			rootFolders: rootfolders(),
130
			filePattern: '*.as'
E
Erich Gamma 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144
		});

		let count = 0;
		engine.search((result) => {
			if (result) {
				count++;
			}
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(count, 0);
			done();
		});
	});

B
Benjamin Pasero 已提交
145
	test('Files: *.* without derived', function (done: () => void) {
B
Benjamin Pasero 已提交
146
		let engine = new FileSearchEngine({
147
			rootFolders: rootfolders(),
B
Benjamin Pasero 已提交
148
			filePattern: 'site.*',
B
Benjamin Pasero 已提交
149
			excludePattern: { '**/*.css': { 'when': '$(basename).less' } }
B
Benjamin Pasero 已提交
150 151 152
		});

		let count = 0;
153
		let res: IRawFileMatch;
B
Benjamin Pasero 已提交
154 155 156 157 158 159 160 161
		engine.search((result) => {
			if (result) {
				count++;
			}
			res = result;
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(count, 1);
C
Christof Marti 已提交
162
			assert.strictEqual(path.basename(res.path), 'site.less');
B
Benjamin Pasero 已提交
163 164 165 166
			done();
		});
	});

B
Benjamin Pasero 已提交
167
	test('Files: *.* exclude folder without wildcard', function (done: () => void) {
B
Benjamin Pasero 已提交
168
		let engine = new FileSearchEngine({
169
			rootFolders: rootfolders(),
B
Benjamin Pasero 已提交
170
			filePattern: '*.*',
B
Benjamin Pasero 已提交
171
			excludePattern: { 'examples': true }
B
Benjamin Pasero 已提交
172 173 174 175 176 177 178 179 180
		});

		let count = 0;
		engine.search((result) => {
			if (result) {
				count++;
			}
		}, () => { }, (error) => {
			assert.ok(!error);
181
			assert.equal(count, 7);
B
Benjamin Pasero 已提交
182 183 184 185
			done();
		});
	});

B
Benjamin Pasero 已提交
186
	test('Files: *.* exclude folder with leading wildcard', function (done: () => void) {
B
Benjamin Pasero 已提交
187
		let engine = new FileSearchEngine({
188
			rootFolders: rootfolders(),
B
Benjamin Pasero 已提交
189
			filePattern: '*.*',
B
Benjamin Pasero 已提交
190
			excludePattern: { '**/examples': true }
B
Benjamin Pasero 已提交
191 192 193 194 195 196 197 198 199
		});

		let count = 0;
		engine.search((result) => {
			if (result) {
				count++;
			}
		}, () => { }, (error) => {
			assert.ok(!error);
200
			assert.equal(count, 7);
B
Benjamin Pasero 已提交
201 202 203 204
			done();
		});
	});

B
Benjamin Pasero 已提交
205
	test('Files: *.* exclude folder with trailing wildcard', function (done: () => void) {
B
Benjamin Pasero 已提交
206
		let engine = new FileSearchEngine({
207
			rootFolders: rootfolders(),
B
Benjamin Pasero 已提交
208
			filePattern: '*.*',
B
Benjamin Pasero 已提交
209
			excludePattern: { 'examples/**': true }
B
Benjamin Pasero 已提交
210 211 212 213 214 215 216 217 218
		});

		let count = 0;
		engine.search((result) => {
			if (result) {
				count++;
			}
		}, () => { }, (error) => {
			assert.ok(!error);
219 220 221 222 223
			assert.equal(count, 7);
			done();
		});
	});

B
Benjamin Pasero 已提交
224
	test('Files: *.* exclude with unicode', function (done: () => void) {
225
		let engine = new FileSearchEngine({
226
			rootFolders: rootfolders(),
227
			filePattern: '*.*',
B
Benjamin Pasero 已提交
228
			excludePattern: { '**/üm laut汉语': true }
229 230 231 232 233 234 235 236 237 238 239 240 241 242
		});

		let count = 0;
		engine.search((result) => {
			if (result) {
				count++;
			}
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(count, 11);
			done();
		});
	});

B
Benjamin Pasero 已提交
243
	test('Files: Unicode and Spaces', function (done: () => void) {
244
		let engine = new FileSearchEngine({
245
			rootFolders: rootfolders(),
246 247 248 249
			filePattern: '汉语'
		});

		let count = 0;
250
		let res: IRawFileMatch;
251 252 253 254 255 256 257 258
		engine.search((result) => {
			if (result) {
				count++;
			}
			res = result;
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(count, 1);
C
Christof Marti 已提交
259
			assert.equal(path.basename(res.path), '汉语.txt');
B
Benjamin Pasero 已提交
260 261 262 263
			done();
		});
	});

B
Benjamin Pasero 已提交
264
	test('Files: no results', function (done: () => void) {
265
		let engine = new FileSearchEngine({
266
			rootFolders: rootfolders(),
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
			filePattern: 'nofilematch'
		});

		let count = 0;
		engine.search((result) => {
			if (result) {
				count++;
			}
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(count, 0);
			done();
		});
	});

B
Benjamin Pasero 已提交
282
	test('Files: absolute path to file ignores excludes', function (done: () => void) {
283
		let engine = new FileSearchEngine({
284
			rootFolders: rootfolders(),
285
			filePattern: path.normalize(path.join(require.toUrl('./fixtures'), 'site.css')),
B
Benjamin Pasero 已提交
286
			excludePattern: { '**/*.css': true }
287 288 289
		});

		let count = 0;
290
		let res: IRawFileMatch;
291 292 293 294 295 296 297 298
		engine.search((result) => {
			if (result) {
				count++;
			}
			res = result;
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(count, 1);
C
Christof Marti 已提交
299
			assert.equal(path.basename(res.path), 'site.css');
300 301 302 303
			done();
		});
	});

C
Christof Marti 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
	test('Files: relative path matched once', function (done: () => void) {
		let engine = new FileSearchEngine({
			rootFolders: rootfolders(),
			filePattern: path.normalize(path.join('examples', 'company.js'))
		});

		let count = 0;
		let res: IRawFileMatch;
		engine.search((result) => {
			if (result) {
				count++;
			}
			res = result;
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(count, 1);
C
Christof Marti 已提交
320
			assert.equal(path.basename(res.path), 'company.js');
C
Christof Marti 已提交
321 322 323 324
			done();
		});
	});

B
Benjamin Pasero 已提交
325
	test('Files: relative path to file ignores excludes', function (done: () => void) {
326
		let engine = new FileSearchEngine({
327
			rootFolders: rootfolders(),
328
			filePattern: path.normalize(path.join('examples', 'company.js')),
B
Benjamin Pasero 已提交
329
			excludePattern: { '**/*.js': true }
330 331 332
		});

		let count = 0;
333
		let res: IRawFileMatch;
334 335 336 337 338 339 340 341
		engine.search((result) => {
			if (result) {
				count++;
			}
			res = result;
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(count, 1);
C
Christof Marti 已提交
342
			assert.equal(path.basename(res.path), 'company.js');
343 344 345 346
			done();
		});
	});

B
Benjamin Pasero 已提交
347
	test('Files: extraFiles only', function (done: () => void) {
348
		let engine = new FileSearchEngine({
349 350
			rootFolders: [],
			extraFiles: [
351 352 353 354 355 356 357 358
				path.normalize(path.join(require.toUrl('./fixtures'), 'site.css')),
				path.normalize(path.join(require.toUrl('./fixtures'), 'examples', 'company.js')),
				path.normalize(path.join(require.toUrl('./fixtures'), 'index.html'))
			],
			filePattern: '*.js'
		});

		let count = 0;
359
		let res: IRawFileMatch;
360 361 362 363 364 365 366 367
		engine.search((result) => {
			if (result) {
				count++;
			}
			res = result;
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(count, 1);
C
Christof Marti 已提交
368
			assert.equal(path.basename(res.path), 'company.js');
369 370 371 372
			done();
		});
	});

B
Benjamin Pasero 已提交
373
	test('Files: extraFiles only (with include)', function (done: () => void) {
374
		let engine = new FileSearchEngine({
375 376
			rootFolders: [],
			extraFiles: [
377 378 379 380 381 382 383 384 385
				path.normalize(path.join(require.toUrl('./fixtures'), 'site.css')),
				path.normalize(path.join(require.toUrl('./fixtures'), 'examples', 'company.js')),
				path.normalize(path.join(require.toUrl('./fixtures'), 'index.html'))
			],
			filePattern: '*.*',
			includePattern: { '**/*.css': true }
		});

		let count = 0;
386
		let res: IRawFileMatch;
387 388 389 390 391 392 393 394
		engine.search((result) => {
			if (result) {
				count++;
			}
			res = result;
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(count, 1);
C
Christof Marti 已提交
395
			assert.equal(path.basename(res.path), 'site.css');
396 397 398 399
			done();
		});
	});

B
Benjamin Pasero 已提交
400
	test('Files: extraFiles only (with exclude)', function (done: () => void) {
401
		let engine = new FileSearchEngine({
402 403
			rootFolders: [],
			extraFiles: [
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
				path.normalize(path.join(require.toUrl('./fixtures'), 'site.css')),
				path.normalize(path.join(require.toUrl('./fixtures'), 'examples', 'company.js')),
				path.normalize(path.join(require.toUrl('./fixtures'), 'index.html'))
			],
			filePattern: '*.*',
			excludePattern: { '**/*.css': true }
		});

		let count = 0;
		engine.search((result) => {
			if (result) {
				count++;
			}
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(count, 2);
			done();
		});
	});

B
Benjamin Pasero 已提交
424
	test('Text: GameOfLife', function (done: () => void) {
E
Erich Gamma 已提交
425 426
		let c = 0;
		let config = {
427
			rootFolders: rootfolders(),
428
			filePattern: '*.js',
E
Erich Gamma 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
			contentPattern: { pattern: 'GameOfLife', modifiers: 'i' }
		};

		let engine = new TextSearchEngine(config, new FileWalker(config));

		engine.search((result) => {
			if (result && result.lineMatches) {
				c += count(result.lineMatches);
			}
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(c, 4);
			done();
		});
	});

B
Benjamin Pasero 已提交
445
	test('Text: GameOfLife (RegExp)', function (done: () => void) {
E
Erich Gamma 已提交
446 447
		let c = 0;
		let config = {
448
			rootFolders: rootfolders(),
449
			filePattern: '*.js',
E
Erich Gamma 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
			contentPattern: { pattern: 'Game.?fL\\w?fe', isRegExp: true }
		};

		let engine = new TextSearchEngine(config, new FileWalker(config));

		engine.search((result) => {
			if (result && result.lineMatches) {
				c += count(result.lineMatches);
			}
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(c, 4);
			done();
		});
	});

B
Benjamin Pasero 已提交
466
	test('Text: GameOfLife (Word Match, Case Sensitive)', function (done: () => void) {
E
Erich Gamma 已提交
467 468
		let c = 0;
		let config = {
469
			rootFolders: rootfolders(),
470
			filePattern: '*.js',
E
Erich Gamma 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
			contentPattern: { pattern: 'GameOfLife', isWordMatch: true, isCaseSensitive: true }
		};

		let engine = new TextSearchEngine(config, new FileWalker(config));

		engine.search((result) => {
			if (result && result.lineMatches) {
				c += count(result.lineMatches);
			}
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(c, 4);
			done();
		});
	});

B
Benjamin Pasero 已提交
487
	test('Text: Helvetica (UTF 16)', function (done: () => void) {
E
Erich Gamma 已提交
488 489
		let c = 0;
		let config = {
490
			rootFolders: rootfolders(),
491
			filePattern: '*.css',
E
Erich Gamma 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
			contentPattern: { pattern: 'Helvetica', modifiers: 'i' }
		};

		let engine = new TextSearchEngine(config, new FileWalker(config));

		engine.search((result) => {
			if (result && result.lineMatches) {
				c += count(result.lineMatches);
			}
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(c, 2);
			done();
		});
	});

B
Benjamin Pasero 已提交
508
	test('Text: e', function (done: () => void) {
E
Erich Gamma 已提交
509 510
		let c = 0;
		let config = {
511
			rootFolders: rootfolders(),
512
			filePattern: '*.*',
E
Erich Gamma 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
			contentPattern: { pattern: 'e', modifiers: 'i' }
		};

		let engine = new TextSearchEngine(config, new FileWalker(config));

		engine.search((result) => {
			if (result && result.lineMatches) {
				c += count(result.lineMatches);
			}
		}, (result) => { }, (error) => {
			assert.ok(!error);
			assert.equal(c, 748);
			done();
		});
	});

B
Benjamin Pasero 已提交
529
	test('Text: e (with excludes)', function (done: () => void) {
E
Erich Gamma 已提交
530
		let c = 0;
531
		let config: any = {
532
			rootFolders: rootfolders(),
533
			filePattern: '*.*',
E
Erich Gamma 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
			contentPattern: { pattern: 'e', modifiers: 'i' },
			excludePattern: { '**/examples': true }
		};

		let engine = new TextSearchEngine(config, new FileWalker(config));

		engine.search((result) => {
			if (result && result.lineMatches) {
				c += count(result.lineMatches);
			}
		}, (result) => { }, (error) => {
			assert.ok(!error);
			assert.equal(c, 366);
			done();
		});
	});

B
Benjamin Pasero 已提交
551
	test('Text: e (with includes)', function (done: () => void) {
E
Erich Gamma 已提交
552
		let c = 0;
553
		let config: any = {
554
			rootFolders: rootfolders(),
555
			filePattern: '*.*',
E
Erich Gamma 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
			contentPattern: { pattern: 'e', modifiers: 'i' },
			includePattern: { '**/examples/**': true }
		};

		let engine = new TextSearchEngine(config, new FileWalker(config));

		engine.search((result) => {
			if (result && result.lineMatches) {
				c += count(result.lineMatches);
			}
		}, (result) => { }, (error) => {
			assert.ok(!error);
			assert.equal(c, 382);
			done();
		});
	});

B
Benjamin Pasero 已提交
573
	test('Text: e (with includes and exclude)', function (done: () => void) {
E
Erich Gamma 已提交
574
		let c = 0;
575
		let config: any = {
576
			rootFolders: rootfolders(),
577
			filePattern: '*.*',
E
Erich Gamma 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
			contentPattern: { pattern: 'e', modifiers: 'i' },
			includePattern: { '**/examples/**': true },
			excludePattern: { '**/examples/small.js': true }
		};

		let engine = new TextSearchEngine(config, new FileWalker(config));

		engine.search((result) => {
			if (result && result.lineMatches) {
				c += count(result.lineMatches);
			}
		}, (result) => { }, (error) => {
			assert.ok(!error);
			assert.equal(c, 361);
			done();
		});
	});

B
Benjamin Pasero 已提交
596
	test('Text: a (capped)', function (done: () => void) {
E
Erich Gamma 已提交
597 598
		let c = 0;
		let config = {
599
			rootFolders: rootfolders(),
600
			filePattern: '*.*',
E
Erich Gamma 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
			contentPattern: { pattern: 'a', modifiers: 'i' },
			maxResults: 520
		};

		let engine = new TextSearchEngine(config, new FileWalker(config));

		engine.search((result) => {
			if (result && result.lineMatches) {
				c += count(result.lineMatches);
			}
		}, (result) => { }, (error) => {
			assert.ok(!error);
			assert.equal(c, 520);
			done();
		});
	});

B
Benjamin Pasero 已提交
618
	test('Text: a (no results)', function (done: () => void) {
E
Erich Gamma 已提交
619 620
		let c = 0;
		let config = {
621
			rootFolders: rootfolders(),
622
			filePattern: '*.*',
E
Erich Gamma 已提交
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
			contentPattern: { pattern: 'ahsogehtdas', modifiers: 'i' }
		};

		let engine = new TextSearchEngine(config, new FileWalker(config));

		engine.search((result) => {
			if (result && result.lineMatches) {
				c += count(result.lineMatches);
			}
		}, (result) => { }, (error) => {
			assert.ok(!error);
			assert.equal(c, 0);
			done();
		});
	});
});