File size: 23,059 Bytes
7a1ad33 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import * as fs from 'node:fs/promises';
import * as os from 'node:os';
import * as path from 'node:path';
import { FileDiscoveryService } from './fileDiscoveryService.js';
import { GEMINI_IGNORE_FILE_NAME } from '../config/constants.js';
describe('FileDiscoveryService', () => {
let testRootDir: string;
let projectRoot: string;
async function createTestFile(filePath: string, content = '') {
const fullPath = path.join(projectRoot, filePath);
await fs.mkdir(path.dirname(fullPath), { recursive: true });
await fs.writeFile(fullPath, content);
return fullPath;
}
beforeEach(async () => {
testRootDir = await fs.mkdtemp(
path.join(os.tmpdir(), 'file-discovery-test-'),
);
projectRoot = path.join(testRootDir, 'project');
await fs.mkdir(projectRoot, { recursive: true });
});
afterEach(async () => {
await fs.rm(testRootDir, { recursive: true, force: true });
});
describe('initialization', () => {
it('should initialize git ignore parser by default in a git repo', async () => {
await fs.mkdir(path.join(projectRoot, '.git'));
await createTestFile('.gitignore', 'node_modules/');
const service = new FileDiscoveryService(projectRoot);
// Let's check the effect of the parser instead of mocking it.
expect(service.shouldIgnoreFile('node_modules/foo.js')).toBe(true);
expect(service.shouldIgnoreFile('src/foo.js')).toBe(false);
});
it('should not load git repo patterns when not in a git repo', async () => {
// No .git directory
await createTestFile('.gitignore', 'node_modules/');
const service = new FileDiscoveryService(projectRoot);
// .gitignore is not loaded in non-git repos
expect(service.shouldIgnoreFile('node_modules/foo.js')).toBe(false);
});
it('should load .geminiignore patterns even when not in a git repo', async () => {
await createTestFile(GEMINI_IGNORE_FILE_NAME, 'secrets.txt');
const service = new FileDiscoveryService(projectRoot);
expect(service.shouldIgnoreFile('secrets.txt')).toBe(true);
expect(service.shouldIgnoreFile('src/index.js')).toBe(false);
});
it('should call applyFilterFilesOptions in constructor', () => {
const resolveSpy = vi.spyOn(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
FileDiscoveryService.prototype as any,
'applyFilterFilesOptions',
);
const options = { respectGitIgnore: false };
new FileDiscoveryService(projectRoot, options);
expect(resolveSpy).toHaveBeenCalledWith(options);
});
it('should correctly resolve options passed to constructor', () => {
const options = {
respectGitIgnore: false,
respectGeminiIgnore: false,
customIgnoreFilePaths: ['custom/.ignore'],
};
const service = new FileDiscoveryService(projectRoot, options);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const defaults = (service as any).defaultFilterFileOptions;
expect(defaults.respectGitIgnore).toBe(false);
expect(defaults.respectGeminiIgnore).toBe(false);
expect(defaults.customIgnoreFilePaths).toStrictEqual(['custom/.ignore']);
});
it('should use defaults when options are not provided', () => {
const service = new FileDiscoveryService(projectRoot);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const defaults = (service as any).defaultFilterFileOptions;
expect(defaults.respectGitIgnore).toBe(true);
expect(defaults.respectGeminiIgnore).toBe(true);
expect(defaults.customIgnoreFilePaths).toStrictEqual([]);
});
it('should partially override defaults', () => {
const service = new FileDiscoveryService(projectRoot, {
respectGitIgnore: false,
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const defaults = (service as any).defaultFilterFileOptions;
expect(defaults.respectGitIgnore).toBe(false);
expect(defaults.respectGeminiIgnore).toBe(true);
});
});
describe('filterFiles', () => {
beforeEach(async () => {
await fs.mkdir(path.join(projectRoot, '.git'));
await createTestFile('.gitignore', 'node_modules/\n.git/\ndist');
await createTestFile(GEMINI_IGNORE_FILE_NAME, 'logs/');
});
it('should filter out git-ignored and gemini-ignored files by default', () => {
const files = [
'src/index.ts',
'node_modules/package/index.js',
'README.md',
'.git/config',
'dist/bundle.js',
'logs/latest.log',
].map((f) => path.join(projectRoot, f));
const service = new FileDiscoveryService(projectRoot);
expect(service.filterFiles(files)).toEqual(
['src/index.ts', 'README.md'].map((f) => path.join(projectRoot, f)),
);
});
it('should not filter files when respectGitIgnore is false', () => {
const files = [
'src/index.ts',
'node_modules/package/index.js',
'.git/config',
'logs/latest.log',
].map((f) => path.join(projectRoot, f));
const service = new FileDiscoveryService(projectRoot);
const filtered = service.filterFiles(files, {
respectGitIgnore: false,
respectGeminiIgnore: true, // still respect this one
});
expect(filtered).toEqual(
['src/index.ts', 'node_modules/package/index.js', '.git/config'].map(
(f) => path.join(projectRoot, f),
),
);
});
it('should not filter files when respectGeminiIgnore is false', () => {
const files = [
'src/index.ts',
'node_modules/package/index.js',
'logs/latest.log',
].map((f) => path.join(projectRoot, f));
const service = new FileDiscoveryService(projectRoot);
const filtered = service.filterFiles(files, {
respectGitIgnore: true,
respectGeminiIgnore: false,
});
expect(filtered).toEqual(
['src/index.ts', 'logs/latest.log'].map((f) =>
path.join(projectRoot, f),
),
);
});
it('should handle empty file list', () => {
const service = new FileDiscoveryService(projectRoot);
expect(service.filterFiles([])).toEqual([]);
});
});
describe('filterFilesWithReport', () => {
beforeEach(async () => {
await fs.mkdir(path.join(projectRoot, '.git'));
await createTestFile('.gitignore', 'node_modules/');
await createTestFile(GEMINI_IGNORE_FILE_NAME, '*.log');
});
it('should return filtered paths and correct ignored count', () => {
const files = [
'src/index.ts',
'node_modules/package/index.js',
'debug.log',
'README.md',
].map((f) => path.join(projectRoot, f));
const service = new FileDiscoveryService(projectRoot);
const report = service.filterFilesWithReport(files);
expect(report.filteredPaths).toEqual(
['src/index.ts', 'README.md'].map((f) => path.join(projectRoot, f)),
);
expect(report.ignoredCount).toBe(2);
});
it('should handle no ignored files', () => {
const files = ['src/index.ts', 'README.md'].map((f) =>
path.join(projectRoot, f),
);
const service = new FileDiscoveryService(projectRoot);
const report = service.filterFilesWithReport(files);
expect(report.filteredPaths).toEqual(files);
expect(report.ignoredCount).toBe(0);
});
});
describe('shouldIgnoreFile & shouldIgnoreDirectory', () => {
beforeEach(async () => {
await fs.mkdir(path.join(projectRoot, '.git'));
await createTestFile('.gitignore', 'node_modules/');
await createTestFile(GEMINI_IGNORE_FILE_NAME, '*.log');
});
it('should return true for git-ignored files', () => {
const service = new FileDiscoveryService(projectRoot);
expect(
service.shouldIgnoreFile(
path.join(projectRoot, 'node_modules/package/index.js'),
),
).toBe(true);
});
it('should return true for git-ignored directories', () => {
const service = new FileDiscoveryService(projectRoot);
expect(
service.shouldIgnoreDirectory(path.join(projectRoot, 'node_modules')),
).toBe(true);
});
it('should return false for non-git-ignored files', () => {
const service = new FileDiscoveryService(projectRoot);
expect(
service.shouldIgnoreFile(path.join(projectRoot, 'src/index.ts')),
).toBe(false);
});
it('should return true for gemini-ignored files', () => {
const service = new FileDiscoveryService(projectRoot);
expect(
service.shouldIgnoreFile(path.join(projectRoot, 'debug.log')),
).toBe(true);
});
it('should return false for non-gemini-ignored files', () => {
const service = new FileDiscoveryService(projectRoot);
expect(
service.shouldIgnoreFile(path.join(projectRoot, 'src/index.ts')),
).toBe(false);
});
});
describe('edge cases', () => {
it('should handle relative project root paths', async () => {
await fs.mkdir(path.join(projectRoot, '.git'));
await createTestFile('.gitignore', 'ignored.txt');
const service = new FileDiscoveryService(
path.relative(process.cwd(), projectRoot),
);
expect(
service.shouldIgnoreFile(path.join(projectRoot, 'ignored.txt')),
).toBe(true);
expect(
service.shouldIgnoreFile(path.join(projectRoot, 'not-ignored.txt')),
).toBe(false);
});
it('should handle filterFiles with undefined options', async () => {
await fs.mkdir(path.join(projectRoot, '.git'));
await createTestFile('.gitignore', 'ignored.txt');
const service = new FileDiscoveryService(projectRoot);
const files = ['src/index.ts', 'ignored.txt'].map((f) =>
path.join(projectRoot, f),
);
expect(service.filterFiles(files, undefined)).toEqual([
path.join(projectRoot, 'src/index.ts'),
]);
});
});
describe('precedence (.geminiignore over .gitignore)', () => {
beforeEach(async () => {
await fs.mkdir(path.join(projectRoot, '.git'));
});
it('should un-ignore a file in .geminiignore that is ignored in .gitignore', async () => {
await createTestFile('.gitignore', '*.txt');
await createTestFile(GEMINI_IGNORE_FILE_NAME, '!important.txt');
const service = new FileDiscoveryService(projectRoot);
const files = ['file.txt', 'important.txt'].map((f) =>
path.join(projectRoot, f),
);
const filtered = service.filterFiles(files);
expect(filtered).toEqual([path.join(projectRoot, 'important.txt')]);
});
it('should un-ignore a directory in .geminiignore that is ignored in .gitignore', async () => {
await createTestFile('.gitignore', 'logs/');
await createTestFile(GEMINI_IGNORE_FILE_NAME, '!logs/');
const service = new FileDiscoveryService(projectRoot);
const files = ['logs/app.log', 'other/app.log'].map((f) =>
path.join(projectRoot, f),
);
const filtered = service.filterFiles(files);
expect(filtered).toEqual(files);
});
it('should extend ignore rules in .geminiignore', async () => {
await createTestFile('.gitignore', '*.log');
await createTestFile(GEMINI_IGNORE_FILE_NAME, 'temp/');
const service = new FileDiscoveryService(projectRoot);
const files = ['app.log', 'temp/file.txt'].map((f) =>
path.join(projectRoot, f),
);
const filtered = service.filterFiles(files);
expect(filtered).toEqual([]);
});
it('should use .gitignore rules if respectGeminiIgnore is false', async () => {
await createTestFile('.gitignore', '*.txt');
await createTestFile(GEMINI_IGNORE_FILE_NAME, '!important.txt');
const service = new FileDiscoveryService(projectRoot);
const files = ['file.txt', 'important.txt'].map((f) =>
path.join(projectRoot, f),
);
const filtered = service.filterFiles(files, {
respectGitIgnore: true,
respectGeminiIgnore: false,
});
expect(filtered).toEqual([]);
});
it('should use .geminiignore rules if respectGitIgnore is false', async () => {
await createTestFile('.gitignore', '*.txt');
await createTestFile(GEMINI_IGNORE_FILE_NAME, '!important.txt\ntemp/');
const service = new FileDiscoveryService(projectRoot);
const files = ['file.txt', 'important.txt', 'temp/file.js'].map((f) =>
path.join(projectRoot, f),
);
const filtered = service.filterFiles(files, {
respectGitIgnore: false,
respectGeminiIgnore: true,
});
// .gitignore is ignored, so *.txt is not applied.
// .geminiignore un-ignores important.txt (which wasn't ignored anyway)
// and ignores temp/
expect(filtered).toEqual(
['file.txt', 'important.txt'].map((f) => path.join(projectRoot, f)),
);
});
});
describe('custom ignore file', () => {
it('should respect patterns from a custom ignore file', async () => {
const customIgnoreName = '.customignore';
await createTestFile(customIgnoreName, '*.secret');
const service = new FileDiscoveryService(projectRoot, {
customIgnoreFilePaths: [customIgnoreName],
});
const files = ['file.txt', 'file.secret'].map((f) =>
path.join(projectRoot, f),
);
const filtered = service.filterFiles(files);
expect(filtered).toEqual([path.join(projectRoot, 'file.txt')]);
});
it('should prioritize custom ignore patterns over .geminiignore patterns in git repo', async () => {
await fs.mkdir(path.join(projectRoot, '.git'));
await createTestFile('.gitignore', 'node_modules/');
await createTestFile(GEMINI_IGNORE_FILE_NAME, '*.log');
const customIgnoreName = '.customignore';
// .geminiignore ignores *.log, custom un-ignores debug.log
await createTestFile(customIgnoreName, '!debug.log');
const service = new FileDiscoveryService(projectRoot, {
customIgnoreFilePaths: [customIgnoreName],
});
const files = ['debug.log', 'error.log'].map((f) =>
path.join(projectRoot, f),
);
const filtered = service.filterFiles(files);
expect(filtered).toEqual([path.join(projectRoot, 'debug.log')]);
});
it('should prioritize custom ignore patterns over .geminiignore patterns in non-git repo', async () => {
// No .git directory created
await createTestFile(GEMINI_IGNORE_FILE_NAME, 'secret.txt');
const customIgnoreName = '.customignore';
// .geminiignore ignores secret.txt, custom un-ignores it
await createTestFile(customIgnoreName, '!secret.txt');
const service = new FileDiscoveryService(projectRoot, {
customIgnoreFilePaths: [customIgnoreName],
});
const files = ['secret.txt'].map((f) => path.join(projectRoot, f));
const filtered = service.filterFiles(files);
expect(filtered).toEqual([path.join(projectRoot, 'secret.txt')]);
});
});
describe('getIgnoreFilePaths & getAllIgnoreFilePaths', () => {
beforeEach(async () => {
await fs.mkdir(path.join(projectRoot, '.git'));
await createTestFile('.gitignore', '*.log');
await createTestFile(GEMINI_IGNORE_FILE_NAME, '*.tmp');
await createTestFile('.customignore', '*.secret');
});
it('should return .geminiignore path by default', () => {
const service = new FileDiscoveryService(projectRoot);
const paths = service.getIgnoreFilePaths();
expect(paths).toEqual([path.join(projectRoot, GEMINI_IGNORE_FILE_NAME)]);
});
it('should not return .geminiignore path if respectGeminiIgnore is false', () => {
const service = new FileDiscoveryService(projectRoot, {
respectGeminiIgnore: false,
});
const paths = service.getIgnoreFilePaths();
expect(paths).toEqual([]);
});
it('should return custom ignore file paths', () => {
const service = new FileDiscoveryService(projectRoot, {
customIgnoreFilePaths: ['.customignore'],
});
const paths = service.getIgnoreFilePaths();
expect(paths).toContain(path.join(projectRoot, GEMINI_IGNORE_FILE_NAME));
expect(paths).toContain(path.join(projectRoot, '.customignore'));
});
it('should return all ignore paths including .gitignore', () => {
const service = new FileDiscoveryService(projectRoot);
const paths = service.getAllIgnoreFilePaths();
expect(paths).toContain(path.join(projectRoot, GEMINI_IGNORE_FILE_NAME));
expect(paths).toContain(path.join(projectRoot, '.gitignore'));
});
it('should not return .gitignore if respectGitIgnore is false', () => {
const service = new FileDiscoveryService(projectRoot, {
respectGitIgnore: false,
});
const paths = service.getAllIgnoreFilePaths();
expect(paths).toContain(path.join(projectRoot, GEMINI_IGNORE_FILE_NAME));
expect(paths).not.toContain(path.join(projectRoot, '.gitignore'));
});
it('should not return .gitignore if it does not exist', async () => {
await fs.rm(path.join(projectRoot, '.gitignore'));
const service = new FileDiscoveryService(projectRoot);
const paths = service.getAllIgnoreFilePaths();
expect(paths).not.toContain(path.join(projectRoot, '.gitignore'));
expect(paths).toContain(path.join(projectRoot, GEMINI_IGNORE_FILE_NAME));
});
it('should ensure .gitignore is the first file in the list', () => {
const service = new FileDiscoveryService(projectRoot);
const paths = service.getAllIgnoreFilePaths();
expect(paths[0]).toBe(path.join(projectRoot, '.gitignore'));
});
it('should exclude directories from getIgnoreFilePaths (#19868)', async () => {
// Create a directory that shares a name with a customIgnoreFilePaths entry
await fs.mkdir(path.join(projectRoot, 'node_modules'), {
recursive: true,
});
const service = new FileDiscoveryService(projectRoot, {
customIgnoreFilePaths: ['node_modules'],
});
const paths = service.getIgnoreFilePaths();
// node_modules/ is a directory, not a file — it should be excluded
expect(paths).not.toContain(path.join(projectRoot, 'node_modules'));
});
it('should exclude directories from getAllIgnoreFilePaths (#19868)', async () => {
await fs.mkdir(path.join(projectRoot, 'node_modules'), {
recursive: true,
});
const service = new FileDiscoveryService(projectRoot, {
customIgnoreFilePaths: ['node_modules'],
});
const paths = service.getAllIgnoreFilePaths();
expect(paths).not.toContain(path.join(projectRoot, 'node_modules'));
// .gitignore should still be present
expect(paths).toContain(path.join(projectRoot, '.gitignore'));
});
it('should not crash when customIgnoreFilePaths contains directory names (#19868)', async () => {
await fs.mkdir(path.join(projectRoot, 'node_modules'), {
recursive: true,
});
await fs.mkdir(path.join(projectRoot, 'temp'), { recursive: true });
// This is the exact user scenario from issue #19868
expect(() => {
new FileDiscoveryService(projectRoot, {
customIgnoreFilePaths: ['node_modules/', 'temp/', 'cache/'],
});
}).not.toThrow();
});
});
describe('getIgnoredPaths', () => {
beforeEach(async () => {
await fs.mkdir(path.join(projectRoot, '.git'));
});
it('should return all ignored paths that exist on disk', async () => {
await createTestFile(
'.gitignore',
'ignored-dir/\nignored-file.txt\n*.log',
);
await createTestFile('ignored-dir/inside.txt');
await createTestFile('ignored-file.txt');
await createTestFile('keep.log');
await createTestFile('src/index.ts');
await createTestFile(GEMINI_IGNORE_FILE_NAME, 'secrets/');
await createTestFile('secrets/passwords.txt');
const service = new FileDiscoveryService(projectRoot);
const ignoredPaths = await service.getIgnoredPaths();
const expectedPaths = [
path.join(projectRoot, '.git'),
path.join(projectRoot, 'ignored-dir'),
path.join(projectRoot, 'ignored-file.txt'),
path.join(projectRoot, 'keep.log'),
path.join(projectRoot, 'secrets'),
].sort();
expect(ignoredPaths.sort()).toEqual(expectedPaths);
});
it('should optimize by not traversing into ignored directories', async () => {
await createTestFile('.gitignore', 'ignored-dir/');
const ignoredDir = path.join(projectRoot, 'ignored-dir');
await fs.mkdir(ignoredDir);
await createTestFile('ignored-dir/large-file-1.txt');
const service = new FileDiscoveryService(projectRoot);
const ignoredPaths = await service.getIgnoredPaths();
expect(ignoredPaths.sort()).toEqual(
[path.join(projectRoot, '.git'), ignoredDir].sort(),
);
});
it('should handle un-ignore patterns correctly', async () => {
await createTestFile(
'.gitignore',
'ignored-dir/*\n!ignored-dir/keep.txt',
);
await createTestFile('ignored-dir/ignored.txt');
await createTestFile('ignored-dir/keep.txt');
const service = new FileDiscoveryService(projectRoot);
const ignoredPaths = await service.getIgnoredPaths();
expect(ignoredPaths).toContain(
path.join(projectRoot, 'ignored-dir/ignored.txt'),
);
expect(ignoredPaths).not.toContain(
path.join(projectRoot, 'ignored-dir/keep.txt'),
);
expect(ignoredPaths).not.toContain(path.join(projectRoot, 'ignored-dir'));
});
it('should respect FilterFilesOptions when provided', async () => {
await createTestFile('.gitignore', 'ignored-by-git.txt');
await createTestFile(GEMINI_IGNORE_FILE_NAME, 'ignored-by-gemini.txt');
await createTestFile('ignored-by-git.txt');
await createTestFile('ignored-by-gemini.txt');
const service = new FileDiscoveryService(projectRoot);
const onlyGemini = await service.getIgnoredPaths({
respectGitIgnore: false,
respectGeminiIgnore: true,
});
expect(onlyGemini).toContain(
path.join(projectRoot, 'ignored-by-gemini.txt'),
);
expect(onlyGemini).not.toContain(
path.join(projectRoot, 'ignored-by-git.txt'),
);
const onlyGit = await service.getIgnoredPaths({
respectGitIgnore: true,
respectGeminiIgnore: false,
});
expect(onlyGit).toContain(path.join(projectRoot, 'ignored-by-git.txt'));
expect(onlyGit).not.toContain(
path.join(projectRoot, 'ignored-by-gemini.txt'),
);
});
});
});
|