search.test.ts 14.1 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11
/*---------------------------------------------------------------------------------------------
 *  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');

import uri from 'vs/base/common/uri';
B
Benjamin Pasero 已提交
12
import {join, normalize} from 'vs/base/common/paths';
E
Erich Gamma 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
import {LineMatch} from 'vs/platform/search/common/search';

import {FileWalker, Engine as FileSearchEngine} from 'vs/workbench/services/search/node/fileSearch';
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 37 38
suite('Search', () => {

	test('Files: *.js', function(done: () => void) {
		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();
		});
	});

55 56
	test('Files: examples/com*', function(done: () => void) {
		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();
		});
	});

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

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

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

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

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

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

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

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

B
Benjamin Pasero 已提交
146 147
	test('Files: *.* without derived', function(done: () => void) {
		let engine = new FileSearchEngine({
148
			rootFolders: rootfolders(),
B
Benjamin Pasero 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
			filePattern: 'site.*',
			excludePattern: { "**/*.css": { "when": "$(basename).less" } }
		});

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

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

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

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

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

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

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

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

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

244 245
	test('Files: Unicode and Spaces', function(done: () => void) {
		let engine = new FileSearchEngine({
246
			rootFolders: rootfolders(),
247 248 249 250 251 252 253 254 255 256 257 258 259 260
			filePattern: '汉语'
		});

		let count = 0;
		let res;
		engine.search((result) => {
			if (result) {
				count++;
			}
			res = result;
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(count, 1);
			assert.equal(path.basename(res.path), '汉语.txt');
B
Benjamin Pasero 已提交
261 262 263 264
			done();
		});
	});

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

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

	test('Files: absolute path to file ignores excludes', function(done: () => void) {
		let engine = new FileSearchEngine({
285
			rootFolders: rootfolders(),
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
			filePattern: path.normalize(path.join(require.toUrl('./fixtures'), 'site.css')),
			excludePattern: { "**/*.css": true }
		});

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

	test('Files: relative path to file ignores excludes', function(done: () => void) {
		let engine = new FileSearchEngine({
307
			rootFolders: rootfolders(),
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
			filePattern: path.normalize(path.join('examples', 'company.js')),
			excludePattern: { "**/*.js": true }
		});

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

327
	test('Files: extraFiles only', function(done: () => void) {
328
		let engine = new FileSearchEngine({
329 330
			rootFolders: [],
			extraFiles: [
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
				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;
		let res;
		engine.search((result) => {
			if (result) {
				count++;
			}
			res = result;
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(count, 1);
			assert.equal(path.basename(res.path), 'company.js');
			done();
		});
	});

353
	test('Files: extraFiles only (with include)', function(done: () => void) {
354
		let engine = new FileSearchEngine({
355 356
			rootFolders: [],
			extraFiles: [
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
				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;
		let res;
		engine.search((result) => {
			if (result) {
				count++;
			}
			res = result;
		}, () => { }, (error) => {
			assert.ok(!error);
			assert.equal(count, 1);
			assert.equal(path.basename(res.path), 'site.css');
			done();
		});
	});

380
	test('Files: extraFiles only (with exclude)', function(done: () => void) {
381
		let engine = new FileSearchEngine({
382 383
			rootFolders: [],
			extraFiles: [
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
				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();
		});
	});

E
Erich Gamma 已提交
404 405 406
	test('Text: GameOfLife', function(done: () => void) {
		let c = 0;
		let config = {
407
			rootFolders: rootfolders(),
408
			filePattern: '*.js',
E
Erich Gamma 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
			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();
		});
	});

	test('Text: GameOfLife (RegExp)', function(done: () => void) {
		let c = 0;
		let config = {
428
			rootFolders: rootfolders(),
429
			filePattern: '*.js',
E
Erich Gamma 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
			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();
		});
	});

	test('Text: GameOfLife (Word Match, Case Sensitive)', function(done: () => void) {
		let c = 0;
		let config = {
449
			rootFolders: rootfolders(),
450
			filePattern: '*.js',
E
Erich Gamma 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
			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();
		});
	});

	test('Text: Helvetica (UTF 16)', function(done: () => void) {
		let c = 0;
		let config = {
470
			rootFolders: rootfolders(),
471
			filePattern: '*.css',
E
Erich Gamma 已提交
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
			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();
		});
	});

	test('Text: e', function(done: () => void) {
		let c = 0;
		let config = {
491
			rootFolders: rootfolders(),
492
			filePattern: '*.*',
E
Erich Gamma 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
			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();
		});
	});

	test('Text: e (with excludes)', function(done: () => void) {
		let c = 0;
511
		let config: any = {
512
			rootFolders: rootfolders(),
513
			filePattern: '*.*',
E
Erich Gamma 已提交
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
			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();
		});
	});

	test('Text: e (with includes)', function(done: () => void) {
		let c = 0;
533
		let config: any = {
534
			rootFolders: rootfolders(),
535
			filePattern: '*.*',
E
Erich Gamma 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
			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();
		});
	});

	test('Text: e (with includes and exclude)', function(done: () => void) {
		let c = 0;
555
		let config: any = {
556
			rootFolders: rootfolders(),
557
			filePattern: '*.*',
E
Erich Gamma 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
			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();
		});
	});

	test('Text: a (capped)', function(done: () => void) {
		let c = 0;
		let config = {
579
			rootFolders: rootfolders(),
580
			filePattern: '*.*',
E
Erich Gamma 已提交
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
			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();
		});
	});

	test('Text: a (no results)', function(done: () => void) {
		let c = 0;
		let config = {
601
			rootFolders: rootfolders(),
602
			filePattern: '*.*',
E
Erich Gamma 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
			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();
		});
	});
});